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

Question: how to match on end of string? #36

Closed
nmorse opened this issue Nov 18, 2018 · 4 comments
Closed

Question: how to match on end of string? #36

nmorse opened this issue Nov 18, 2018 · 4 comments

Comments

@nmorse
Copy link

nmorse commented Nov 18, 2018

How can I match on the end of input (or end of input string)?

in .peg file I have this end_of_string <- [$]+, but it does not seem to match the 'end of string'.

Is there a better way? (sorry to make an issue, I did look for the answer in the docs and on stackoverflow, first)

@jcoglan
Copy link
Owner

jcoglan commented Aug 3, 2020

There isn't an explicit operator for matching the end of the string -- Canopy parsers implicitly match the entire input. If you give the parser some input and the parser doesn't reach the end of the input by following the grammar rules, then it will fail with an error.

@nmorse
Copy link
Author

nmorse commented Aug 5, 2020

@jcoglan Thanks for the feedback. My work-around has been to append a space before parsing, that triggers a different rule and alleviates my need to identify an end_of_input.

@nmorse nmorse closed this as completed Aug 5, 2020
@jcoglan
Copy link
Owner

jcoglan commented Aug 6, 2020

Oh, I see what you mean. There is a way to match only if an expression appears at the end of the input, which is to use a negative look-ahead to check there is no character after the expression. So, expr !. matches expr only at the end of the input.

@jcoglan
Copy link
Owner

jcoglan commented Aug 6, 2020

(Sorry it took me so long to reply in the first place.)

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