siefkenj / latex-parser

Generate an AST and beautify LaTeX code
MIT License
67 stars 4 forks source link

Modify latex parser grammar #27

Closed lun-lun-byte closed 2 years ago

lun-lun-byte commented 2 years ago

Hi, Siefkenj. I have a question to make a little modification on this grammar parser. for example, we know \frac{m}{n}, it is a correct style in latex. But in my own language, it's [FS][m][n]. This core elements is similar, but some symbal is different.

Would you like give me some suggestion on this problem? Thanks

siefkenj commented 2 years ago

Is the rest of your language the same as LaTeX?

Here's a hack that you could do...: hardcode int he grammar that [FS] is a macro with a blank escape character. Then tell the parser that [FS] accepts two optional arguments. Since optional arguments parse [...] automatically, you should get this operation for free.

lun-lun-byte commented 2 years ago

The rest of language is similar with LaTex, almost similar. So where can I modify this grammar in this project? Where can I find this rules and modify them. Thank you very much.

siefkenj commented 2 years ago

All the code is now in https://github.com/siefkenj/unified-latex/

You can look here

https://github.com/siefkenj/unified-latex/blob/2498a5c58a7f5def43e70de1c2a0cf4d7a127c4d/packages/unified-latex-util-pegjs/grammars/latex.pegjs#L108

if you want to define a special macro. Add something like

/ "[FS]" { return {type: "macro", content: "[FS]", escapeToken: ""} }

to the end of that and [FS] should be interpreted as a macro. Then you'll want to add something like https://github.com/siefkenj/unified-latex/blob/2498a5c58a7f5def43e70de1c2a0cf4d7a127c4d/packages/unified-latex-ctan/package/latex2e/provides.ts#L177 to either that file or create a new ctan-style package.

lun-lun-byte commented 2 years ago

I got it. Thanks. But the token in my grammar is [] not {}, where can I modify this symbol? And for this style grammar, for exmaple, [FS(]...[FS)], there is a close () in []. How can I add this? Thanks

siefkenj commented 2 years ago

I'm not exactly sure what you're asking, but you should read through the PEG.js documentation and play around with making your own grammar.

Good luck!