Skip to content

Commit

Permalink
Merge pull request #8 from starkware-libs/lucas/constrained
Browse files Browse the repository at this point in the history
Update grammar to support constrained associated types, const functions and fix closure trailing comma
  • Loading branch information
0xLucqs authored Jan 27, 2025
2 parents 9ee027f + a774d47 commit 4c6a256
Show file tree
Hide file tree
Showing 10 changed files with 41,876 additions and 37,274 deletions.
32 changes: 18 additions & 14 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ module.exports = grammar({
// Helper for function definition
function: ($) =>
seq(
optional('const'),
'fn',
field('name', $.identifier),
field('type_parameters', optional($.type_parameters)),
Expand Down Expand Up @@ -471,21 +472,24 @@ module.exports = grammar({
seq('const', field('name', $.identifier), ':', field('type', $._type)),

// for example in impl ArraySerde<T, +Serde<T>, impl TDrop: Drop<T>> of Serde<Array<T>> {} it would be
// `+Serde<T>` and `impl TDrop: Drop<T>`
// `+Serde<T>` and `impl TDrop: Drop<T>`. Can also have a constrained associated type like that `+Iterator<I>[Item: T]`
constrained_type_parameter: ($) =>
choice(
seq(
field('left', seq('impl', $._type_identifier, ':')),
field('bound', $._type),
),
seq(
choice('+', '-'),
choice(
$.generic_type,
$._type_identifier,
$.generic_type_with_turbofish,
seq(
choice(
seq(
field('left', seq('impl', $._type_identifier, ':')),
field('bound', $._type),
),
seq(
choice('+', '-'),
choice(
$.generic_type,
$._type_identifier,
$.generic_type_with_turbofish,
),
),
),
optional(seq('[', repeat(seq($._type_identifier, ':', $._type)), ']')),
),
// in let a: Array<T>; it would be `Array<T>`
generic_type: ($) =>
Expand Down Expand Up @@ -891,7 +895,7 @@ module.exports = grammar({
break_expression: ($) => prec.left(seq('break', optional($.expression))),

// continue
continue_expression: ($) => prec.left(seq('continue')),
continue_expression: ($) => prec.left('continue'),

// a[i]
index_expression: ($) =>
Expand Down Expand Up @@ -1089,7 +1093,7 @@ module.exports = grammar({
sepBy(',', choice(
$._pattern,
$.parameter,
)),
)), optional(','),
'|',
),

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"queries/*",
"src/**"
],
"author": "LucasLvy",
"author": "0xLucqs",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/starkware-libs/tree-sitter-cairo/issues"
Expand Down Expand Up @@ -56,12 +56,6 @@
"parse": "tree-sitter parse",
"test": "tree-sitter test"
},
"tree-sitter": [
{
"scope": "source.cairo",
"injection-regex": "^cairo$"
}
],
"eslintConfig": {
"env": {
"commonjs": true,
Expand Down Expand Up @@ -104,4 +98,4 @@
]
}
}
}
}
221 changes: 170 additions & 51 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4c6a256

Please sign in to comment.