tree-sitter-tiger/grammar.js

94 lines
2 KiB
JavaScript
Raw Normal View History

2022-06-01 19:34:31 +02:00
function sepBy1(sep, rule) {
return seq(rule, repeat(seq(sep, rule)))
}
function sepBy(sep, rule) {
return optional(sepBy1(sep, rule))
}
const PREC = {
multiplicative: 5,
additive: 4,
comparative: 3,
and: 2,
or: 1,
};
module.exports = grammar({
name: "tiger",
rules: {
2022-06-01 19:33:42 +02:00
source_file: ($) => choice(
$._expr,
),
_expr: ($) => choice(
"nil",
$.integer_literal,
$.string_literal,
2022-06-01 19:34:31 +02:00
$.unary_expression,
$.binary_expression,
$.sequence_expression,
2022-06-01 19:33:42 +02:00
),
integer_literal: (_) => /[0-9]+/,
string_literal: ($) => seq(
'"',
repeat(choice($.escape_sequence, /[^"\\]+/)),
'"',
),
escape_sequence: (_) => token.immediate(
seq(
"\\",
choice(
// Special escapes
choice("a", "b", "f", "n", "r", "t", "v"),
// Octal
/[0-3][0-7]{2}/,
// Hexadecimal
seq("x", /[0-9a-fA-F]{2}/),
// Escaped characters
choice("\\", '"'),
)
)
),
2022-06-01 19:34:31 +02:00
unary_expression: ($) => seq(
field("operator", alias("-", $.operator)),
field("expression", $._expr),
),
binary_expression: ($) => {
const table = [
[PREC.multiplicative, prec.left, choice("*", "/")],
[PREC.additive, prec.left, choice("+", "-")],
2022-06-01 19:34:31 +02:00
// FIXME: comparisons should be non-associative
// See https://github.com/tree-sitter/tree-sitter/issues/761
[PREC.comparative, prec.left, choice(">=", "<=", "=", "<>", "<", ">")],
[PREC.and, prec.left, "&"],
[PREC.or, prec.left, "|"],
2022-06-01 19:34:31 +02:00
];
return choice(
...table.map(
([priority, assoc, operator]) => assoc(
priority,
seq(
field("left", $._expr),
field("operator", alias(operator, $.operator)),
field("right", $._expr),
)
)
)
);
},
sequence_expression: ($) => seq("(", sepBy(";", $._expr), ")"),
}
});
// vim: sw=2