haxiomic / haxe-glsl-parser

GLSL ES 1.0 parser in Haxe
MIT License
26 stars 7 forks source link

[Question]Custom grammar for DSL #7

Open rraallvv opened 7 years ago

rraallvv commented 7 years ago

Given the grammar for a domain specific language, could haxe-glsl-parser be used to generate the corresponding AST?

What about other languages with syntax similar to GLSL, could haxe-glsl-parser be used to generate the AST for languages like C/C++ given the corresponding grammar files?

Regards.

haxiomic commented 7 years ago

Hey @rraallvv,

Yes! Although some work is required.

The repository includes a parser generator that takes a grammar in the lemon grammar format and produces haxe files for your parser: Actions.hx, Parser.hx and Tables.hx.

These haxe classes can be used to produce an AST from a series of tokens, (you'll need to develop the lexer yourself unless the language shares the same tokens as GLSL).

You can see how to use the parser generator by reading tools/parser-generator/run.sh.

Here's a breakdown of what's going on in run.sh:

Convert Grammar This step is optional, it converts the GLSL grammar from the specification into the lemon grammar format, but if you write your grammar in the lemon format you don't need to do this. This converts GLES-100_v4.txt to GLES-100_v4.lemon

Build Lemon This project uses a modified version lemon parser generator to generate the parsing tables and parsing cases - lemon produces C-based parsers, ideally we'd modify lemon to spit out haxe instead of C, but it was quicker to have lemon produce some JSON describing the parser configuration and then assemble the haxe separately. The build lemon step just compiles the lemon binary if the source has changed.

Run Lemon to Generate Parser Tables Execute lemon, passing in the .lemon grammar to generate a JSON file containing the parser tables

Build and Execute Parser Generator Rebuild the tool that assembles the parser from the JSON file (source is src/Main.hx) and then execute it

Copy the Generated Parser into the Main Project optional

Rebuild Demo optional, just for easy testing

It'd be useful to read parser-generator/templates/Actions.hx to get a handle on how to write the AST node generation code, which gets placed in Actions.reduce() as well as reading through the lemon parser docs for syntax and theory

Let me know if you've got any questions