私はルビーとパースレットから始めたばかりなので、これは他の人には明らかかもしれません(うまくいけば)。
消費せずに区切り文字(^)まで全ての単語を取得したい
次のルールは機能します (ただし、区切り文字を消費します)。{:wrd=>"otherthings"@0, :delim=>"^"@11}
require 'parslet'
class Mini < Parslet::Parser
rule(:word) { match('[a-zA-Z]').repeat}
rule(:delimeter) { str('^') }
rule(:othercontent) { word.as(:wrd) >> delimeter.as(:delim) }
root(:othercontent)
end
puts Mini.new.parse("otherthings^")
「プレゼント?」を使おうとしていたのですが、
require 'parslet'
class Mini < Parslet::Parser
rule(:word) { match('[a-zA-Z]').repeat}
rule(:delimeter) { str('^') }
rule(:othercontent) { word.as(:wrd) >> delimeter.present? }
root(:othercontent)
end
puts Mini.new.parse("otherthings^")
しかし、これは例外をスローします:
Failed to match sequence (wrd:WORD &DELIMETER) at line 1 char 12. (Parslet::ParseFailed)
後の段階で、区切り記号の右側にある単語を調べて、より複雑な文法を構築する必要があるため、区切り記号を使用したくありません。
パースレット 1.5.0 を使用しています。
ご協力いただきありがとうございます!