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

.mark() with a filename #82

Open
tsani opened this issue Jan 15, 2025 · 1 comment
Open

.mark() with a filename #82

tsani opened this issue Jan 15, 2025 · 1 comment

Comments

@tsani
Copy link

tsani commented Jan 15, 2025

I'm parsing a simple programming language with parsy. During parsing, I equip syntax tree nodes with their position in the file using .mark(), but this doesn't capture the filename. Then I encounter an issue when raising errors during runtime, as the reported error location doesn't mention a filename.

A workaround I can see but find extremely inelegant would be to traverse the whole syntax tree after parsing a file to equip all the nodes' locations with the right filename.

Would it make sense to augment .parse() to take an optional filename to thread through the parser? This would allow the filename to be mentioned in ParseErrors, but also via new combinator like .mark_file() for my use case.

@spookylukey
Copy link
Member

I think this does make sense and would be a good addition.

The optional filename should probably be named 'source' and be documented something like "a string representing the source document e.g. a filename". This allows for other things e.g. URLs.

I haven't thought this through, but perhaps if the source is present, then mark() should automatically add it as a 4th element in the tuple.

Or, we could have mark_source() which would have the new behaviour and leave mark() as it is. This would allow us to return something more useful like a namedtuple so that there are named fields. In this case mark() would be soft deprecated (i.e. no warnings, just docs that suggest using mark_source() instead.

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