Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/andy-cpp/syntaxes/andy-cpp.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
{
"comment": "other keywords",
"name": "keyword.other.andy-cpp",
"match": "\\b(in)\\b"
"match": "\\b(in|as)\\b"
}
]
},
Expand Down
29 changes: 23 additions & 6 deletions ext/tree-sitter-andy-cpp/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*
* assignment < comma/sequence < or < and < not < range
* < comparison/in < spaceship < shift < | < ~ < &
* < + - ++ <> < * / \ % %% < ^ (right assoc) < unary ! - ~
* < postfix call/index/member
* < + - ++ <> < * / \ % %% < ^ (right assoc) < as cast
* < unary ! - ~ < postfix call/index/member
*/

/* eslint-disable arrow-parens */
Expand All @@ -33,8 +33,9 @@ const PREC = {
term: 13,
factor: 14,
exponent: 15,
unary: 16,
call: 17,
cast: 16,
unary: 17,
call: 18,
};

/** @param {RuleOrLiteral} rule */
Expand All @@ -59,6 +60,10 @@ module.exports = grammar({
conflicts: $ => [
[$._pattern, $._expression],
[$.if_expression, $.if_guard],
// `x as Int < 2` — a `<` after a cast type may open a type argument list
// or be a less-than operator. Both branches are explored; the dynamic
// precedence on `generic_type` picks the cast when either parse works.
[$._type, $.generic_type],
],

rules: {
Expand Down Expand Up @@ -151,13 +156,18 @@ module.exports = grammar({

type_identifier: $ => /[A-Za-z_][A-Za-z0-9_]*/,

generic_type: $ => seq(
// A `<` after a type name is ambiguous in a cast: it may open a type
// argument list or be a less-than operator. The conflict declared above
// makes the parser try both; this dynamic precedence prefers the type
// argument list whenever it parses, mirroring the recursive-descent
// parser in `ndc_parser/src/parser.rs`.
generic_type: $ => prec.dynamic(1, seq(
field('name', $.type_identifier),
'<',
commaSep($._type),
optional(','),
'>',
),
)),

tuple_type: $ => seq(
'(',
Expand Down Expand Up @@ -186,6 +196,7 @@ module.exports = grammar({
$._literal,
$.unary_expression,
$.not_expression,
$.cast_expression,
$.binary_expression,
$.range_expression,
$.assignment,
Expand Down Expand Up @@ -240,6 +251,12 @@ module.exports = grammar({
field('right', $._expression_or_sequence),
)),

cast_expression: $ => prec.left(PREC.cast, seq(
field('value', $._expression),
'as',
field('type', $._type),
)),

unary_expression: $ => prec(PREC.unary, seq(
field('operator', choice('!', '-', '~')),
field('operand', $._expression),
Expand Down
5 changes: 4 additions & 1 deletion ext/tree-sitter-andy-cpp/queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
(continue_expression)
] @keyword.control

"in" @keyword.operator
[
"in"
"as"
] @keyword.operator

[
"and"
Expand Down
175 changes: 108 additions & 67 deletions ext/tree-sitter-andy-cpp/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -520,70 +520,74 @@
"value": "[A-Za-z_][A-Za-z0-9_]*"
},
"generic_type": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "type_identifier"
}
},
{
"type": "STRING",
"value": "<"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_type"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_type"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
"type": "PREC_DYNAMIC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "type_identifier"
}
]
},
{
"type": "STRING",
"value": ">"
}
]
},
{
"type": "STRING",
"value": "<"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_type"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_type"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ">"
}
]
}
},
"tuple_type": {
"type": "SEQ",
Expand Down Expand Up @@ -704,6 +708,10 @@
"type": "SYMBOL",
"name": "not_expression"
},
{
"type": "SYMBOL",
"name": "cast_expression"
},
{
"type": "SYMBOL",
"name": "binary_expression"
Expand Down Expand Up @@ -1030,9 +1038,38 @@
]
}
},
"cast_expression": {
"type": "PREC_LEFT",
"value": 16,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "as"
},
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "_type"
}
}
]
}
},
"unary_expression": {
"type": "PREC",
"value": 16,
"value": 17,
"content": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -1878,7 +1915,7 @@
},
"call": {
"type": "PREC",
"value": 17,
"value": 18,
"content": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -1961,7 +1998,7 @@
},
"index": {
"type": "PREC",
"value": 17,
"value": 18,
"content": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -1994,7 +2031,7 @@
},
"member_expression": {
"type": "PREC_RIGHT",
"value": 17,
"value": 18,
"content": {
"type": "SEQ",
"members": [
Expand Down Expand Up @@ -2820,6 +2857,10 @@
[
"if_expression",
"if_guard"
],
[
"_type",
"generic_type"
]
],
"precedences": [],
Expand Down
Loading
Loading