kschiess / parslet

A small PEG based parser library. See the Hacking page in the Wiki as well.
kschiess.github.com/parslet
MIT License
809 stars 95 forks source link

Alternation seems have problem when one alternative contains another. #194

Closed orbit-dna closed 6 years ago

orbit-dna commented 6 years ago

Alternation seems have problem when one alternative contains another.

version: 1.8.2.

this code:

parser_1 = (Parslet::Atoms::Str.new('a') | Parslet::Atoms::Str.new('aa')) >> Parslet::Atoms::Str.new(' ')
parser_2 = (Parslet::Atoms::Str.new('aa') | Parslet::Atoms::Str.new('a')) >> Parslet::Atoms::Str.new(' ')
parser_1.parse('aa ')  # raise Parslet::ParseFailed
parser_2.parse('aa ')  # works

parser_1 not work while parser_2 works as expected.

Is this a bug ? or should I avoid using parslet in this way ?

kschiess commented 6 years ago

When constructing PEG parsers, alternation is left to right. You need to put the longer unique prefix first. Your parser_1 succeeds on 'a' and then sees extra input at the end of the document - the second 'a'. This is not a bug, but the way this formalism works.