Skip to content

Commit

Permalink
Big improvements to definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cypher1 committed Oct 1, 2024
1 parent ca544f0 commit 6f3bbf8
Show file tree
Hide file tree
Showing 7 changed files with 3,270 additions and 912 deletions.
72 changes: 45 additions & 27 deletions tree-sitter-tako/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@ const PREC = {
or: 2,
range: 1,
assign: 0,
closure: -1,
comma: -1,
has_type: -2,
closure: -3,
};

const OPERATORS = [
[PREC.and, '&&'],
[PREC.or, '||'],
[PREC.bitand, '&'],
[PREC.bitor, '|'],
[PREC.bitxor, '^'],
[PREC.comparative, choice('==', '!=', '<', '<=', '>', '>=')],
[PREC.shift, choice('<<', '>>')],
[PREC.additive, choice('+', '-')],
[PREC.multiplicative, choice('*', '/', '%')],
['comma', ','],
['assign', '='],
['has_type', ':'],
['and', '&&'],
['or', '||'],
['bitand', '&'],
['bitor', '|'],
['bitxor', '^'],
['comparative', choice('==', '!=', '<', '<=', '>', '>=')],
['shift', choice('<<', '>>')],
['additive', choice('+', '-')],
['multiplicative', choice('*', '/', '%')],
];

const separated_one = (entry, delimiter) => {
Expand All @@ -40,42 +45,54 @@ const separated = (entry, delimiter) => {
return optional(separated_one(entry, delimiter));
};

function operators_gen() {
const operators = {};
for (const [name, operator] of OPERATORS) {
const precedence = PREC[name];
operators[name] = ($) => prec.left(precedence, seq(
field('left', $._expression),
// @ts-ignore
field('operator', operator),
field('right', $._expression),
));
}
return operators;
}

module.exports = grammar({
name: 'tako',

extras: ($) => [$.nesting_comment, $.single_line_comment, "\r", "\n", "\t", " "],
rules: {
// TODO: add the actual grammar rules
source_file: ($) => seq(optional($.shebang), optional($._non_empty_body)),
_non_empty_body: ($) => separated_one($._statement, ';'),
_statement: ($) => choice(
$.block,
$.definition,
$._expression
),
block: ($) => seq('{', optional($._non_empty_body), '}'),
definition_target: ($) => seq($.ident, optional($.definition_arguments)),
definition_arguments: ($) => seq('(', separated($.argument_definition, ','), optional(','), ')'),
argument_definition: ($) => seq($.definition_target, optional(seq('=', $._expression))),
definition: ($) => seq($.definition_target, '=', $._expression),
_expression: ($) => choice(
$.binary_expression,
$._binary_expression, // Consider keeping this name to support editing?
$.call,
seq('(', $._expression ,')'),
$.string_literal,
$.number,
$._number,
$.hex_literal,
$.color,
$.ident
$.ident,
),
binary_expression: ($) => {
return choice(...OPERATORS.map(([precedence, operator]) => prec.left(precedence, seq(
field('left', $._expression),
// @ts-ignore
field('operator', operator),
field('right', $._expression),
))));
call: ($) => seq($._expression, '(', separated($._expression, ','), optional(','), ')'),
_binary_expression: ($) => {
return choice(...OPERATORS.map(([name, _operator_parser]) => {
try {
return ($)[name]; // Get the parser 'named'.
} catch (e) {
e.message = `OPERATOR: ${name} ('${operator}') PRECEDENCE: '${precedence}'.\n${e.message}`;
throw e;
}
}));
},
number: ($) => choice(
_number: ($) => choice(
$.int_literal,
$.float_literal
),
Expand Down Expand Up @@ -129,5 +146,6 @@ module.exports = grammar({
// TODO: Add semver.
shebang: (_) => seq('#!', /[^\n\r]*/),
single_line_comment: (_) => seq('//', /.*/),
...operators_gen(),
}
});
2 changes: 1 addition & 1 deletion tree-sitter-tako/queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
(hex_literal) @number
(nesting_comment) @comment
(single_line_comment) @comment
(definition ) @function
(assign) @function
Loading

0 comments on commit 6f3bbf8

Please sign in to comment.