Skip to content

Commit

Permalink
Add optionally postfix (or infix) operator ';' sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
Cypher1 committed Oct 4, 2024
1 parent bb53b82 commit 3bdfb84
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
19 changes: 15 additions & 4 deletions tree-sitter-tako/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const PREC = {
assign: 0,
closure: -2,
sequence: -3,
postfix_sequence: -3,
};

const RIGHT_OPERATORS = [
Expand All @@ -43,7 +42,6 @@ const RIGHT_OPERATORS = [
const OPERATORS = [
['field', '.'],
['has_type', ':'],
['sequence', ';'],
['and', '&&'],
['or', '||'],
['bitand', '&'],
Expand All @@ -66,6 +64,9 @@ const OPERATORS = [

const POSTFIX_OPERATORS = [
['try', '?'],
];

const OPTIONALLY_POSTFIX_OPERATORS = [
['sequence', ';'],
];

Expand All @@ -79,6 +80,7 @@ const ALL_OPERATORS = [
...OPERATORS,
...RIGHT_OPERATORS,
...POSTFIX_OPERATORS,
...OPTIONALLY_POSTFIX_OPERATORS,
...UNARY_OPERATORS,
];

Expand Down Expand Up @@ -108,6 +110,14 @@ function operators_gen() {
field('right', $._expression),
));
}
for (const [name, operator] of OPTIONALLY_POSTFIX_OPERATORS) {
const precedence = PREC[name];
operators[name] = ($) => right(precedence, seq(
field('left', $._expression),
field('operator', operator),
field('right', optional($._expression)),
));
}
for (const [name, operator] of POSTFIX_OPERATORS) {
const precedence = PREC[name];
operators[name] = ($) => left(precedence, seq(
Expand All @@ -131,18 +141,19 @@ module.exports = grammar({
rules: {
// TODO: add the actual grammar rules
source_file: ($) => seq(optional($.shebang), separated_one(optional($._expression), $.heading)),
block: ($) => seq('{', optional($._expression), '}'),
_expression: ($) => choice(
$._operator_expression, // Consider keeping this name to support editing?
$.call,
seq('(', $._expression ,')'),
$.parens,
$.block,
$.string_literal,
$._number,
$.hex_literal,
$.color,
$.ident,
),
block: ($) => seq('{', optional($._expression), '}'),
parens: ($) => seq('(', $._expression ,')'),
call: ($) => left(PREC.call, seq($._expression, '(', separated($._expression, ','), optional(','), ')')),
_operator_expression: ($) => {
return choice(...ALL_OPERATORS.map(([name, _operator_parser]) => {
Expand Down
4 changes: 2 additions & 2 deletions tree-sitter-tako/test/corpus/definitions.tk
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test(a=3,b=2) = 3
)
)
==================
Constant Definition using empty block
Constant Definition using empty Block
==================
test = {
}
Expand All @@ -91,7 +91,7 @@ test = {
)
)
==================
Constant Definition using block
Constant Definition using Block
==================
test = {
3
Expand Down
2 changes: 1 addition & 1 deletion tree-sitter-tako/test/corpus/number.tk
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ Color wrong number of digits
==================
#ff00aaa
---
(source_file (color) (ERROR))
(source_file (color) (ERROR (ident)))
27 changes: 27 additions & 0 deletions tree-sitter-tako/test/corpus/simple_exprs.tk
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
==================
Simple value with postfix sequence
==================

0;

---
(source_file
(sequence
(int_literal)
)
)

==================
Simple values with infix sequence
==================

0;1

---
(source_file
(sequence
(int_literal)
(int_literal)
)
)

==================
Simple Expressions
==================
Expand Down

0 comments on commit 3bdfb84

Please sign in to comment.