Skip to content

Commit

Permalink
Progress on binary operations and nested comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Cypher1 committed Sep 29, 2024
1 parent 56c9fca commit 16e5cc3
Show file tree
Hide file tree
Showing 7 changed files with 711 additions and 56 deletions.
55 changes: 48 additions & 7 deletions tree-sitter-tako/grammar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// For now we match rust.
// https://doc.rust-lang.org/reference/expressions.html#expression-precedence
const PREC = {
call: 15,
field: 14,
try: 13,
unary: 12,
cast: 11,
multiplicative: 10,
additive: 9,
shift: 8,
bitand: 7,
bitxor: 6,
bitor: 5,
comparative: 4,
and: 3,
or: 2,
range: 1,
assign: 0,
closure: -1,
};

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

Expand All @@ -10,19 +32,38 @@ module.exports = grammar({
_unterminated_statement: ($) => choice(
$.definition,
$._block,
$.expression
$._expression
),
_block: ($) => seq('{', optional($._body), '}'),
definition: ($) => seq($.ident, '=', $.expression),
expression: ($) => $._inner_expression,
_inner_expression: ($) => choice(
seq('(', $.expression ,')'),
$.string,
definition: ($) => seq($.ident, '=', $._expression),
_expression: ($) => choice(
$.binary_expression,
seq('(', $._expression ,')'),
$.string_literal,
$.number,
$.hex_literal,
$.color,
$.ident
),
binary_expression: ($) => {
const table = [
[PREC.and, '&&'],
[PREC.or, '||'],
[PREC.bitand, '&'],
[PREC.bitor, '|'],
[PREC.bitxor, '^'],
[PREC.comparative, choice('==', '!=', '<', '<=', '>', '>=')],
[PREC.shift, choice('<<', '>>')],
[PREC.additive, choice('+', '-')],
[PREC.multiplicative, choice('*', '/', '%')],
];
return choice(...table.map(([precedence, operator]) => prec.left(precedence, seq(
field('left', $._expression),
// @ts-ignore
field('operator', operator),
field('right', $._expression),
))));
},
number: ($) => choice(
$.int_literal,
$.float_literal
Expand Down Expand Up @@ -55,7 +96,7 @@ module.exports = grammar({
_hex_char_6: (_) => /[a-fA-F0-9_]{6}/,
_hex_char_8: (_) => /[a-fA-F0-9_]{8}/,
ident: (_) => /[a-zA-Z][a-zA-Z0-9_]*/,
string: $ => seq(
string_literal: $ => seq(
'"',
repeat(choice(
$.escape_sequence,
Expand Down
3 changes: 1 addition & 2 deletions tree-sitter-tako/queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
(exists) @keyword
(given) @keyword
(ident) @type
(string) @string
(int_literal) @number
(float_literal) @number
(hex_literal) @number
(nesting_comment) @comment
(single_line_comment) @comment
(definition) @function
(definition ) @function
Loading

0 comments on commit 16e5cc3

Please sign in to comment.