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))
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:33:49 +02:00
|
|
|
const PREC = {
|
2022-06-01 20:11:52 +02:00
|
|
|
assign: 6,
|
2022-06-01 17:33:49 +02:00
|
|
|
multiplicative: 5,
|
|
|
|
additive: 4,
|
|
|
|
comparative: 3,
|
|
|
|
and: 2,
|
|
|
|
or: 1,
|
|
|
|
};
|
|
|
|
|
2022-06-01 16:23:43 +02:00
|
|
|
module.exports = grammar({
|
|
|
|
name: "tiger",
|
|
|
|
|
2022-06-01 17:36:39 +02:00
|
|
|
// Ensure we don't extract keywords from tokens
|
|
|
|
word: ($) => $.identifier,
|
|
|
|
|
2022-06-01 20:07:00 +02:00
|
|
|
conflicts: ($) => [
|
|
|
|
[$._lvalue, $.array_expression],
|
|
|
|
],
|
|
|
|
|
2022-06-01 16:23:43 +02:00
|
|
|
rules: {
|
2022-06-01 19:33:42 +02:00
|
|
|
source_file: ($) => choice(
|
|
|
|
$._expr,
|
2022-06-01 20:37:24 +02:00
|
|
|
optional($._declaration_chunks),
|
2022-06-01 19:33:42 +02:00
|
|
|
),
|
|
|
|
|
2022-06-01 20:35:22 +02:00
|
|
|
// Expressions {{{
|
|
|
|
|
2022-06-01 19:33:42 +02:00
|
|
|
_expr: ($) => choice(
|
2022-06-01 19:40:44 +02:00
|
|
|
$.nil_literal,
|
2022-06-01 19:33:42 +02:00
|
|
|
$.integer_literal,
|
|
|
|
$.string_literal,
|
2022-06-01 19:34:31 +02:00
|
|
|
|
2022-06-01 19:48:54 +02:00
|
|
|
$.array_expression,
|
2022-06-01 19:54:09 +02:00
|
|
|
$.record_expression,
|
2022-06-01 19:48:54 +02:00
|
|
|
|
2022-06-01 20:07:00 +02:00
|
|
|
$._lvalue,
|
|
|
|
|
2022-06-01 19:58:40 +02:00
|
|
|
$.function_call,
|
|
|
|
|
2022-06-01 19:34:31 +02:00
|
|
|
$.unary_expression,
|
|
|
|
$.binary_expression,
|
|
|
|
$.sequence_expression,
|
2022-06-01 20:11:52 +02:00
|
|
|
|
|
|
|
$.assignment_expression,
|
2022-06-01 20:18:10 +02:00
|
|
|
|
|
|
|
$.if_expression,
|
2022-06-01 20:19:56 +02:00
|
|
|
$.while_expression,
|
2022-06-01 20:23:04 +02:00
|
|
|
$.for_expression,
|
2022-06-01 20:23:46 +02:00
|
|
|
$.break_expression,
|
2022-06-01 20:36:14 +02:00
|
|
|
$.let_expression,
|
2022-06-01 19:33:42 +02:00
|
|
|
),
|
|
|
|
|
2022-06-01 19:40:44 +02:00
|
|
|
nil_literal: (_) => "nil",
|
|
|
|
|
2022-06-01 19:33:42 +02:00
|
|
|
integer_literal: (_) => /[0-9]+/,
|
|
|
|
|
|
|
|
string_literal: ($) => seq(
|
|
|
|
'"',
|
|
|
|
repeat(choice($.escape_sequence, /[^"\\]+/)),
|
|
|
|
'"',
|
|
|
|
),
|
|
|
|
|
2022-06-01 17:36:39 +02:00
|
|
|
// NOTE: includes reserved identifiers
|
|
|
|
identifier: (_) => /[_a-zA-Z0-9]+/,
|
|
|
|
|
2022-06-01 19:33:42 +02:00
|
|
|
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
|
|
|
|
2022-06-01 20:07:00 +02:00
|
|
|
_lvalue: ($) => choice(
|
|
|
|
$.identifier,
|
|
|
|
$.record_value,
|
|
|
|
$.array_value,
|
|
|
|
),
|
|
|
|
|
|
|
|
record_value: ($) => seq(
|
|
|
|
field("record", $._lvalue),
|
|
|
|
".",
|
|
|
|
field("field", $.identifier),
|
|
|
|
),
|
|
|
|
|
|
|
|
array_value: ($) => seq(
|
|
|
|
field("array", $._lvalue),
|
|
|
|
"[",
|
|
|
|
field("index", $._expr),
|
|
|
|
"]",
|
|
|
|
),
|
|
|
|
|
2022-06-01 19:58:40 +02:00
|
|
|
function_call: ($) => seq(
|
|
|
|
field("function", $.identifier),
|
|
|
|
"(",
|
|
|
|
field("arguments", sepBy(",", $._expr)),
|
|
|
|
")",
|
|
|
|
),
|
|
|
|
|
2022-06-01 19:34:31 +02:00
|
|
|
unary_expression: ($) => seq(
|
|
|
|
field("operator", alias("-", $.operator)),
|
|
|
|
field("expression", $._expr),
|
|
|
|
),
|
|
|
|
|
|
|
|
binary_expression: ($) => {
|
|
|
|
const table = [
|
2022-06-01 17:33:49 +02:00
|
|
|
[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
|
2022-06-01 17:33:49 +02:00
|
|
|
[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), ")"),
|
2022-06-01 19:48:54 +02:00
|
|
|
|
|
|
|
array_expression: ($) => seq(
|
|
|
|
field("type", $.identifier),
|
|
|
|
"[",
|
|
|
|
field("size", $._expr),
|
|
|
|
"]",
|
|
|
|
"of",
|
|
|
|
field("init", $._expr),
|
|
|
|
),
|
2022-06-01 19:54:09 +02:00
|
|
|
|
|
|
|
record_expression: ($) => seq(
|
|
|
|
field("type", $.identifier),
|
|
|
|
"{",
|
|
|
|
sepBy(
|
|
|
|
",",
|
|
|
|
seq(
|
|
|
|
field("field", $.identifier),
|
|
|
|
"=",
|
|
|
|
field("init", $._expr),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
"}",
|
|
|
|
),
|
2022-06-01 20:11:52 +02:00
|
|
|
|
|
|
|
assignment_expression: ($) => prec.right(
|
|
|
|
PREC.assign,
|
|
|
|
seq(
|
|
|
|
field("left", $._lvalue),
|
|
|
|
":=",
|
|
|
|
field("right", $._expr),
|
|
|
|
),
|
|
|
|
),
|
2022-06-01 20:18:10 +02:00
|
|
|
|
|
|
|
if_expression: ($) => prec.right(
|
|
|
|
seq(
|
|
|
|
"if",
|
|
|
|
field("condition", $._expr),
|
|
|
|
"then",
|
|
|
|
field("consequence", $._expr),
|
|
|
|
optional(
|
|
|
|
seq(
|
|
|
|
"else",
|
|
|
|
field("alternative", $._expr),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-06-01 20:19:56 +02:00
|
|
|
|
|
|
|
while_expression: ($) => seq(
|
|
|
|
"while",
|
|
|
|
field("condition", $._expr),
|
|
|
|
"do",
|
|
|
|
field("body", $._expr),
|
|
|
|
),
|
2022-06-01 20:23:04 +02:00
|
|
|
|
|
|
|
for_expression: ($) => seq(
|
|
|
|
"for",
|
|
|
|
field("index", $.identifier),
|
|
|
|
":=",
|
|
|
|
field("start", $._expr),
|
|
|
|
"to",
|
|
|
|
field("end", $._expr),
|
|
|
|
"do",
|
|
|
|
field("body", $._expr),
|
|
|
|
),
|
2022-06-01 20:23:46 +02:00
|
|
|
|
|
|
|
break_expression: (_) => "break",
|
2022-06-01 20:35:22 +02:00
|
|
|
|
2022-06-01 20:36:14 +02:00
|
|
|
let_expression: ($) => seq(
|
|
|
|
"let",
|
|
|
|
field("declarations", optional($._declaration_chunks)),
|
|
|
|
"in",
|
|
|
|
field("body", sepBy(";", $._expr)),
|
|
|
|
"end",
|
|
|
|
),
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
// Declarations {{{
|
|
|
|
|
|
|
|
_declaration_chunks: ($) => repeat1($._declaration_chunk),
|
|
|
|
|
2022-06-01 20:48:32 +02:00
|
|
|
_declaration_chunk: ($) => prec.left(
|
|
|
|
choice(
|
2022-06-01 20:52:20 +02:00
|
|
|
repeat1($.type_declaration),
|
2022-06-01 20:48:32 +02:00
|
|
|
repeat1(choice($.function_declaration, $.primitive_declaration)),
|
|
|
|
$.variable_declaration,
|
|
|
|
$.import_declaration,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2022-06-01 20:52:20 +02:00
|
|
|
type_declaration: ($) => seq(
|
|
|
|
"type",
|
|
|
|
field("name", $.identifier),
|
|
|
|
"=",
|
|
|
|
field("value", $._type)
|
|
|
|
),
|
|
|
|
|
|
|
|
_type: ($) => choice(
|
|
|
|
$.type_alias,
|
2022-06-01 20:53:39 +02:00
|
|
|
$.record_type,
|
2022-06-01 20:54:35 +02:00
|
|
|
$.array_type,
|
2022-06-01 20:52:20 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
type_alias: ($) => $.identifier,
|
|
|
|
|
2022-06-01 20:53:39 +02:00
|
|
|
record_type: ($) => seq(
|
|
|
|
"{",
|
|
|
|
sepBy(",", $._typed_field),
|
|
|
|
"}",
|
|
|
|
),
|
|
|
|
|
|
|
|
_typed_field: ($) => seq(
|
|
|
|
field("name", $.identifier),
|
|
|
|
":",
|
|
|
|
field("type", $.identifier),
|
|
|
|
),
|
|
|
|
|
2022-06-01 20:54:35 +02:00
|
|
|
array_type: ($) => seq(
|
|
|
|
"array",
|
|
|
|
"of",
|
|
|
|
field("element_type", $.identifier),
|
|
|
|
),
|
|
|
|
|
2022-06-01 20:48:32 +02:00
|
|
|
function_declaration: ($) => seq(
|
|
|
|
"function",
|
|
|
|
$._function_declaration_common,
|
|
|
|
"=",
|
|
|
|
field("body", $._expr),
|
|
|
|
),
|
|
|
|
|
|
|
|
primitive_declaration: ($) => seq(
|
|
|
|
"primitive",
|
|
|
|
$._function_declaration_common,
|
|
|
|
),
|
|
|
|
|
|
|
|
_function_declaration_common: ($) => seq(
|
|
|
|
field("name", $.identifier),
|
|
|
|
field("parameters", $.parameters),
|
2022-06-01 22:07:05 +02:00
|
|
|
optional(seq(":", field("return_type", $.identifier))),
|
2022-06-01 20:48:32 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
parameters: ($) => seq(
|
|
|
|
"(",
|
|
|
|
field("parameters", sepBy(",", $._typed_field)),
|
|
|
|
")",
|
|
|
|
),
|
|
|
|
|
2022-06-01 20:41:25 +02:00
|
|
|
variable_declaration: ($) => seq(
|
|
|
|
"var",
|
|
|
|
field("name", $.identifier),
|
|
|
|
optional(seq(":", field("type", $.identifier))),
|
|
|
|
":=",
|
|
|
|
field("value", $._expr),
|
|
|
|
),
|
|
|
|
|
2022-06-01 20:36:14 +02:00
|
|
|
import_declaration: ($) => seq(
|
|
|
|
"import",
|
|
|
|
field("file", $.string_literal),
|
|
|
|
),
|
|
|
|
|
2022-06-01 20:35:22 +02:00
|
|
|
// }}}
|
2022-06-01 16:23:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// vim: sw=2
|