An attempt at parsing Cairo's code (written in Rust) with tree-sitter, to produce a grammar.js
for Cairo.
WARNING: This project is not working yet, due to some ambiguities in the grammar file.
Make sure you have tree-sitter, nodejs
installed.
cd cairo-syntax-parser/
cargo run > ../grammar.js
cd ../
tree-sitter generate
-
The whole syntax for Cairo is defined here.
-
There is also a lexer there,
-
And more files related to operators precedence in the same project.
As Cairo is defined with some Rust code, the idea is to programmatically parse some Rust using tree-sitter, in order to output a working grammar.js
for Cairo.
This code first parses lexer.rs
and operators.rs
, then parses cairo_spec.rs
and generate the grammar.
This is done by: cairo_syntax_parser/lexer_file_parser.rs
.
It tries its best to automatically generate JS code from the lexer.rs
file. In some cases, some hand-made JS code is written instead.
This module has a main handle parse_lexer(file: &str)
which returns two hashmaps for later use.
This is done by: cairo_syntax_parser/operators_parser.rs
.
It produces two hashmaps unary_precedence, post_precedence
, both of type HashMap<u32, Vec<String>>
. Each maps one level of precedence k
to a list of those operators who have precedence equal to k
.
This module has a main handle parse_operators(file: &str)
which returns two hashmaps for later use.
This is done by cairo_syntax_parser/cairo_spec_parser.rs
.
There is first a preprocessing phase to handle add_option
method calls, and to remove structs that would match the empty string.
Then the grammar is generated with a call to iterate_nodes_aggregator
, which uses the hashmaps from before.
This module has a main handle parse_cairo_spec(...)
which returns a single String
containing all the grammar rules.