Skip to content

Commit

Permalink
binary_expression: Explicit operator rules
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Sep 4, 2024
1 parent 824cf1a commit 5b1df51
Show file tree
Hide file tree
Showing 4 changed files with 3,780 additions and 3,148 deletions.
26 changes: 13 additions & 13 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ const PREC = {
or: 10,
}

const power_operators = ["**"]
const multiplicative_operators = ["*", "/", "%"]
const additive_operators = ["+", "-"]
const comparative_operators = ["==", "!=", "<", "<=", ">", ">="]
const and_operators = ["&&"]
const or_operators = ["||"]

// https://stackoverflow.com/questions/62661663/is-there-a-standard-treesitter-construct-for-parsing-an-arbitrary-length-list

function comma_sep(rule) {
Expand Down Expand Up @@ -134,16 +127,23 @@ module.exports = grammar({

unary_expression: $ => choice(seq("-", $.expression), seq("!", $.expression)),

power_operator: _ => choice("**"),
multiplicative_operator: _ => choice("*", "/", "%"),
additive_operator: _ => choice("+", "-"),
comparative_operator: _ => choice("==", "!=", "<", "<=", ">", ">="),
and_operator: _ => choice("&&"),
or_operator: _ => choice("||"),

binary_expression: $ => {
// Stolen from tree-sitter-go.

const table = [
[PREC.power, choice(...power_operators)],
[PREC.multiplicative, choice(...multiplicative_operators)],
[PREC.additive, choice(...additive_operators)],
[PREC.comparative, choice(...comparative_operators)],
[PREC.and, choice(...and_operators)],
[PREC.or, choice(...or_operators)],
[PREC.power, $.power_operator],
[PREC.multiplicative, $.multiplicative_operator],
[PREC.additive, $.additive_operator],
[PREC.comparative, $.comparative_operator],
[PREC.and, $.and_operator],
[PREC.or, $.or_operator],
]

return choice(
Expand Down
172 changes: 98 additions & 74 deletions src/grammar.json

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

86 changes: 42 additions & 44 deletions src/node-types.json

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

Loading

0 comments on commit 5b1df51

Please sign in to comment.