Extend binaryExpr to guards as well

pull/204/head
Jonathan Arnett 2022-01-15 16:56:44 +07:00
parent 7a9759c44c
commit b0a23fe88f
5 changed files with 13532 additions and 13465 deletions

@ -606,10 +606,10 @@ fn trial(x, y, z) {
(identifier)))) (identifier))))
guard: (case_clause_guard guard: (case_clause_guard
(binary_expression (binary_expression
(tuple_access left: (tuple_access
tuple: (identifier) tuple: (identifier)
index: (integer)) index: (integer))
(integer))) right: (integer)))
value: (record value: (record
name: (type_identifier))) name: (type_identifier)))
(case_clause (case_clause
@ -620,18 +620,18 @@ fn trial(x, y, z) {
(identifier)))) (identifier))))
guard: (case_clause_guard guard: (case_clause_guard
(binary_expression (binary_expression
(binary_expression left: (binary_expression
(binary_expression left: (binary_expression
(tuple_access left: (tuple_access
tuple: (identifier) tuple: (identifier)
index: (integer)) index: (integer))
(integer)) right: (integer))
(integer)) right: (integer))
(binary_expression right: (binary_expression
(tuple_access left: (tuple_access
tuple: (identifier) tuple: (identifier)
index: (integer)) index: (integer))
(integer)))) right: (integer))))
value: (record value: (record
name: (type_identifier))))) name: (type_identifier)))))
(case (case

@ -254,28 +254,28 @@ module.exports = grammar({
_expression: ($) => choice($._expression_unit, $.binary_expression), _expression: ($) => choice($._expression_unit, $.binary_expression),
binary_expression: ($) => binary_expression: ($) =>
choice( choice(
binaryExpr($, prec.left, 1, "||"), binaryExpr(prec.left, 1, "||", $._expression),
binaryExpr($, prec.left, 2, "&&"), binaryExpr(prec.left, 2, "&&", $._expression),
binaryExpr($, prec.left, 3, "=="), binaryExpr(prec.left, 3, "==", $._expression),
binaryExpr($, prec.left, 3, "!="), binaryExpr(prec.left, 3, "!=", $._expression),
binaryExpr($, prec.left, 4, "<"), binaryExpr(prec.left, 4, "<", $._expression),
binaryExpr($, prec.left, 4, "<="), binaryExpr(prec.left, 4, "<=", $._expression),
binaryExpr($, prec.left, 4, "<."), binaryExpr(prec.left, 4, "<.", $._expression),
binaryExpr($, prec.left, 4, "<=."), binaryExpr(prec.left, 4, "<=.", $._expression),
binaryExpr($, prec.left, 4, ">"), binaryExpr(prec.left, 4, ">", $._expression),
binaryExpr($, prec.left, 4, ">="), binaryExpr(prec.left, 4, ">=", $._expression),
binaryExpr($, prec.left, 4, ">."), binaryExpr(prec.left, 4, ">.", $._expression),
binaryExpr($, prec.left, 4, ">=."), binaryExpr(prec.left, 4, ">=.", $._expression),
binaryExpr($, prec.left, 5, "|>"), binaryExpr(prec.left, 5, "|>", $._expression),
binaryExpr($, prec.left, 6, "+"), binaryExpr(prec.left, 6, "+", $._expression),
binaryExpr($, prec.left, 6, "+."), binaryExpr(prec.left, 6, "+.", $._expression),
binaryExpr($, prec.left, 6, "-"), binaryExpr(prec.left, 6, "-", $._expression),
binaryExpr($, prec.left, 6, "-."), binaryExpr(prec.left, 6, "-.", $._expression),
binaryExpr($, prec.left, 7, "*"), binaryExpr(prec.left, 7, "*", $._expression),
binaryExpr($, prec.left, 7, "*."), binaryExpr(prec.left, 7, "*.", $._expression),
binaryExpr($, prec.left, 7, "/"), binaryExpr(prec.left, 7, "/", $._expression),
binaryExpr($, prec.left, 7, "/."), binaryExpr(prec.left, 7, "/.", $._expression),
binaryExpr($, prec.left, 7, "%") binaryExpr(prec.left, 7, "%", $._expression)
), ),
// The way that this function is written in the Gleam parser is essentially // The way that this function is written in the Gleam parser is essentially
// incompatible with tree-sitter. It first parses some base expression, // incompatible with tree-sitter. It first parses some base expression,
@ -373,102 +373,18 @@ module.exports = grammar({
), ),
_case_clause_guard_binary_expression: ($) => _case_clause_guard_binary_expression: ($) =>
choice( choice(
prec.left( binaryExpr(prec.left, 1, "||", $._case_clause_guard_expression),
1, binaryExpr(prec.left, 2, "&&", $._case_clause_guard_expression),
seq( binaryExpr(prec.left, 3, "==", $._case_clause_guard_expression),
$._case_clause_guard_expression, binaryExpr(prec.left, 3, "!=", $._case_clause_guard_expression),
"||", binaryExpr(prec.left, 4, "<", $._case_clause_guard_expression),
$._case_clause_guard_expression binaryExpr(prec.left, 4, "<=", $._case_clause_guard_expression),
) binaryExpr(prec.left, 4, "<.", $._case_clause_guard_expression),
), binaryExpr(prec.left, 4, "<=.", $._case_clause_guard_expression),
prec.left( binaryExpr(prec.left, 4, ">", $._case_clause_guard_expression),
2, binaryExpr(prec.left, 4, ">=", $._case_clause_guard_expression),
seq( binaryExpr(prec.left, 4, ">.", $._case_clause_guard_expression),
$._case_clause_guard_expression, binaryExpr(prec.left, 4, ">=.", $._case_clause_guard_expression)
"&&",
$._case_clause_guard_expression
)
),
prec.left(
3,
seq(
$._case_clause_guard_expression,
"==",
$._case_clause_guard_expression
)
),
prec.left(
3,
seq(
$._case_clause_guard_expression,
"!=",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
"<",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
"<=",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
"<.",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
"<=.",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
">",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
">=",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
">.",
$._case_clause_guard_expression
)
),
prec.left(
4,
seq(
$._case_clause_guard_expression,
">=.",
$._case_clause_guard_expression
)
)
), ),
_case_clause_guard_unit: ($) => _case_clause_guard_unit: ($) =>
choice( choice(
@ -822,13 +738,9 @@ function series_of(rule, separator) {
// A binary expression with a left-hand side, infix operator, and then right-hand-side // A binary expression with a left-hand side, infix operator, and then right-hand-side
// https://github.com/elixir-lang/tree-sitter-elixir/blob/de20391afe5cb03ef1e8a8e43167e7b58cc52869/grammar.js#L850-L859 // https://github.com/elixir-lang/tree-sitter-elixir/blob/de20391afe5cb03ef1e8a8e43167e7b58cc52869/grammar.js#L850-L859
function binaryExpr($, assoc, precedence, operator) { function binaryExpr(assoc, precedence, operator, expr) {
return assoc( return assoc(
precedence, precedence,
seq( seq(field("left", expr), field("operator", operator), field("right", expr))
field("left", $._expression),
field("operator", operator),
field("right", $._expression)
)
); );
} }

288
src/grammar.json vendored

@ -3307,16 +3307,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "||" "name": "operator",
"content": {
"type": "STRING",
"value": "||"
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3328,16 +3340,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "&&" "name": "operator",
"content": {
"type": "STRING",
"value": "&&"
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3349,16 +3373,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "==" "name": "operator",
"content": {
"type": "STRING",
"value": "=="
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3370,16 +3406,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "!=" "name": "operator",
"content": {
"type": "STRING",
"value": "!="
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3391,16 +3439,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "<" "name": "operator",
"content": {
"type": "STRING",
"value": "<"
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3412,16 +3472,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "<=" "name": "operator",
"content": {
"type": "STRING",
"value": "<="
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3433,16 +3505,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "<." "name": "operator",
"content": {
"type": "STRING",
"value": "<."
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3454,16 +3538,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": "<=." "name": "operator",
"content": {
"type": "STRING",
"value": "<=."
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3475,16 +3571,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": ">" "name": "operator",
"content": {
"type": "STRING",
"value": ">"
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3496,16 +3604,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": ">=" "name": "operator",
"content": {
"type": "STRING",
"value": ">="
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3517,16 +3637,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": ">." "name": "operator",
"content": {
"type": "STRING",
"value": ">."
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }
@ -3538,16 +3670,28 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "left",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
}, },
{ {
"type": "STRING", "type": "FIELD",
"value": ">=." "name": "operator",
"content": {
"type": "STRING",
"value": ">=."
}
}, },
{ {
"type": "SYMBOL", "type": "FIELD",
"name": "_case_clause_guard_expression" "name": "right",
"content": {
"type": "SYMBOL",
"name": "_case_clause_guard_expression"
}
} }
] ]
} }

@ -347,8 +347,8 @@
"named": true, "named": true,
"fields": { "fields": {
"left": { "left": {
"multiple": false, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "anonymous_function", "type": "anonymous_function",
@ -425,12 +425,20 @@
{ {
"type": "tuple_access", "type": "tuple_access",
"named": true "named": true
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
} }
] ]
}, },
"operator": { "operator": {
"multiple": false, "multiple": false,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "!=", "type": "!=",
@ -523,8 +531,8 @@
] ]
}, },
"right": { "right": {
"multiple": false, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "anonymous_function", "type": "anonymous_function",
@ -601,55 +609,17 @@
{ {
"type": "tuple_access", "type": "tuple_access",
"named": true "named": true
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
} }
] ]
} }
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "binary_expression",
"named": true
},
{
"type": "bit_string",
"named": true
},
{
"type": "float",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer",
"named": true
},
{
"type": "list",
"named": true
},
{
"type": "record",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "tuple",
"named": true
},
{
"type": "tuple_access",
"named": true
}
]
} }
}, },
{ {

26457
src/parser.c vendored

File diff suppressed because it is too large Load Diff