Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot parse "rest of line" as a token without matching the entire line #7

Open
whweaver opened this issue Jan 22, 2021 · 1 comment

Comments

@whweaver
Copy link

whweaver commented Jan 22, 2021

I have a grammar in which I have lines like this:

    key: value

I need to extract the indent, key, and value from here. The indent is the number of spaces divided by 4, the key is any string that does not include ":", and the value is everything from the end of the whitespace following ":" until the end of the line, including any subsequent colons. My initial attempt at this was to make the following rules:

rule(indent: /(    )*/).as { |str| str.length / 4 }
rule(key: /[^:\s][^:]*/)
rule(":" => /:\s*/)
rule(value: /\S.*/)
rule(:line) do |r|
  r[:indent, :key, ":", :value]
end

The problem is that the :value rule matches the entire "key: value" sequence, making it the longest match, so whittle throws an error when it finds a :value after the :indent instead of a :key. Does whittle have a way to represent this kind of grammar?

@booch
Copy link

booch commented Jun 9, 2021

I think you want to use a rule that matches the end-of-line explicitly. Like the example at https://github.com/d11wtq/whittle#skipping-whitespace-and-comments, so you'd have:

rule(value: /\S.*$/m)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants