EoinDavey / tsPEG

PEG Parser Generator for TypeScript
Mozilla Public License 2.0
195 stars 7 forks source link

Comments in grammar files #2

Closed SparkFountain closed 3 years ago

SparkFountain commented 4 years ago

I like the simplicity of tsPEG very much. What would be helpful for me to develop bigger grammars and to not lose track is the possibility to add comments in .gram files.

Could you please add some kind of comment syntax (e. g. # or //), at least for single lines or even multiple lines, which will simply be ignored by the parser generator? Would appreciate that very much :)

EoinDavey commented 4 years ago

Thanks for the suggestion! Should have been added long ago. Added in commit 700dc8. Will update issue when released

EoinDavey commented 4 years ago

Released v.2.0.0 with this + many other changes

SparkFountain commented 3 years ago

Unfortunately, this still does not seem to work in tsPEG 2.1.0.

Version Check:

$ tspeg --version
2.1.0

Grammar:

// this is a test comment
SUM  := head=FAC tail={ op='\+|-' sm=FAC }*
FAC  := head=ATOM tail={ op='\*|/' sm=ATOM }*
ATOM := _ val=INT _ | _ '\(' val=SUM '\)' _
INT  := val='[0-9]+'
_    := '\s*'

Error Message:

SyntaxErr {
  pos: { overallPos: 0, line: 1, offset: 0 },
  exprules: [ 'GRAM' ],
  expmatches: [ '---', '[a-zA-Z_]+' ]
}
EoinDavey commented 3 years ago

Will investigate

EoinDavey commented 3 years ago

Works on my machine. Can I see the command you ran to get this error?

<21:16>eoin@eoin-pc:/tmp/tspegtst$ cat tst.peg 
// this is a test comment
SUM  := head=FAC tail={ op='\+|-' sm=FAC }*
FAC  := head=ATOM tail={ op='\*|/' sm=ATOM }*
ATOM := _ val=INT _ | _ '\(' val=SUM '\)' _
INT  := val='[0-9]+'
_    := '\s*'
<21:16>eoin@eoin-pc:/tmp/tspegtst$ tspeg --version
2.1.0
<21:16>eoin@eoin-pc:/tmp/tspegtst$ tspeg tst.peg out.ts && echo "no errors"
no errors
<21:16>eoin@eoin-pc:/tmp/tspegtst$ 
SparkFountain commented 3 years ago

Okay, probably it's because of the file ending. My grammar file was named "calculator.gram". After changing it to "calculator.peg" it works without any errors.

Thank you for your fast response and help @EoinDavey !

EoinDavey commented 3 years ago

Glad I could help, but I don't think that the file extension should affect this.

<13:34>eoin@eoin-XPS:/tmp/tspegtst$ cat tst.gram 
// this is a test comment
SUM  := head=FAC tail={ op='\+|-' sm=FAC }*
FAC  := head=ATOM tail={ op='\*|/' sm=ATOM }*
ATOM := _ val=INT _ | _ '\(' val=SUM '\)' _
INT  := val='[0-9]+'
_    := '\s*'
<13:34>eoin@eoin-XPS:/tmp/tspegtst$ tspeg --version
2.1.0
<13:34>eoin@eoin-XPS:/tmp/tspegtst$ tspeg tst.gram out.ts
<13:35>eoin@eoin-XPS:/tmp/tspegtst$ 

My suspicion is that you may have a local version of tsPEG that is out of date (i.e. the version that is installed in the .node_modules directory) different from the global version (the version installed with npm i -g tspeg). What do you get if you run npx tspeg --version?

SparkFountain commented 3 years ago

I found out there seems to be an issue with the file itself. When I created a new file in my IDE (VS Code) and copy-pasted the whole calculator grammar, tsPEG did not compile at all.

Conclusion: There must not be any file header, otherwise tsPEG parses that content and throws an error.

@EoinDavey Is it possible for you to ignore file headers and only read the actual file content?

EoinDavey commented 3 years ago

I don't understand what you are saying about file headers, there should be no file header in a plain source code text file.

To aid in solving this problem could you provide the following information:

  1. What operating system are you running on (if Windows, are you using WSL or plain Windows).
  2. Attach the exact grammar file you are passing to tsPEG.
  3. The exact command you are executing to generate the output file (e.g. tspeg calculator.peg out.ts or similar).
  4. The output generated by tsPEG when you execute the command in 3.

Thanks

SparkFountain commented 3 years ago
  1. Windows 10 (Build 19042)
  2. TL;DR
  3. tspeg my-grammar.peg parser.ts
  4. nothing (empty command line)

You are right, there should not be any file header in an empty file. However, VS Code seems to write some file header automatically when I create a new file with ending .peg or .gram. Of course, I can create my files with touch instead, but it would be easier for developers if they could use their preferred IDE's and tsPEG would just ignore (erroneous) file headers anyway.

EoinDavey commented 3 years ago

I am not aware of any issue that would cause VSCode to add headers to files as you describe, there is also no way to "ignore file headers". Attaching the my-grammar.peg file or another file that is not compiling for you is the only way forward I see here