BenjaminSchaaf / sbnf

A BNF-style language for writing sublime-syntax files
MIT License
58 stars 6 forks source link

Regex equivalence branch points don't work in slightly different scenario #24

Closed nuchi closed 2 years ago

nuchi commented 3 years ago

The following (adapted from the unit tests in src/compiler/mod.rs, scopes changed to be visible in screenshots) works:

main = (a | b)*;
a{variable.function} = 'c'{ac} 'a';
b{variable.parameter} = 'c'{bc} 'b';

image

But the following slight modification doesn't:

main = (`x` (a | b))*;
a{variable.function} = 'c'{ac} 'a';
b{variable.parameter} = 'c'{bc} 'b';

image

Interestingly, in the generated code, increasing the number of pops after the a and b checks seems to fix this but I don't know why.

generated code ```diff %YAML 1.2 --- # http://www.sublimetext.com/docs/syntax.html version: 2 name: test file_extensions: - test scope: source.test contexts: a|0|main@1: - meta_include_prototype: false - match: 'c' scope: variable.function.test ac.test push: main|1|main@1 b|0|main@1: - meta_include_prototype: false - match: 'c' scope: variable.parameter.test bc.test push: main|2|main@1 # Include context for branch point main@1 include!main@1: - match: '(?=c)' branch_point: main@1 branch: - a|0|main@1 - b|0|main@1 # Rule: main main: - match: 'x' push: main|0 - match: '\S' scope: invalid.illegal.test # Rule: main main|0: - include: include!main@1 - match: '\S' scope: invalid.illegal.test pop: true # Rule: main # For branch point 'main@1' main|1|main@1: - match: 'a' scope: variable.function.test - pop: true + pop: 3 - match: '\S' fail: main@1 # Rule: main # For branch point 'main@1' main|2|main@1: - match: 'b' scope: variable.parameter.test - pop: true + pop: 3 - match: '\S' scope: invalid.illegal.test pop: true ```

image

Again, hoping that this is a misunderstanding on my part and would love to find a workaround if so!

BenjaminSchaaf commented 3 years ago

That's certainly a bug!

nuchi commented 3 years ago

Would you accept a contribution to fix this if I manage it? Not sure if I'd be able to, but also I didn't see anything in the repo about whether you accept contributions.

BenjaminSchaaf commented 3 years ago

Of course, all contributions are welcome.

nuchi commented 2 years ago

I tried working through your code in order to understand how it works, and found it too difficult to wrap my head around, without understanding the underlying theory. So I went and learned a bit about parser construction.

And I, uh, went and started implementing my own syntax constructor. Thought you might be interested in taking a look. https://github.com/nuchi/sublime-from-cfg

nuchi commented 2 years ago

FYI for anyone reading along, I'm now self-hosting sbnf.sbnf and feature-complete. It's written in python. ~I haven't published to pypi yet but it's installable with pip install 'sublime-from-cfg @ git+ssh://git@github.com/nuchi/sublime-from-cfg#egg=sublime-from-cfg'~ It's installable with pip install sublime-from-cfg and then sublime-from-cfg /my/syntax/file.sbnf once it's in your PATH.