Skip to content

Commit

Permalink
Add notation for containers list array/list/set/vector as '[' <values…
Browse files Browse the repository at this point in the history
… separated by commas with optional trailing comma> ']'
  • Loading branch information
Cypher1 committed Oct 4, 2024
1 parent 3bdfb84 commit 13b9d7e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tree-sitter-tako/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,24 @@ module.exports = grammar({
// TODO: add the actual grammar rules
source_file: ($) => seq(optional($.shebang), separated_one(optional($._expression), $.heading)),
_expression: ($) => choice(
$._operator_expression, // Consider keeping this name to support editing?
$.call,
$.parens,
$.block,
$._expression_not_literal,
$.string_literal,
$._number,
$.hex_literal,
$.color,
),
_expression_not_literal: ($) => choice(
$._operator_expression, // Consider keeping this name to support editing?
$.parens,
$.container,
$.call,
$.block,
$.ident,
),
block: ($) => seq('{', optional($._expression), '}'),
parens: ($) => seq('(', $._expression ,')'),
call: ($) => left(PREC.call, seq($._expression, '(', separated($._expression, ','), optional(','), ')')),
container: ($) => seq('[', separated($._expression, ','), optional(',') ,']'),
call: ($) => left(PREC.call, seq($._expression_not_literal, '(', separated($._expression, ','), optional(','), ')')),
_operator_expression: ($) => {
return choice(...ALL_OPERATORS.map(([name, _operator_parser]) => {
try {
Expand Down
140 changes: 140 additions & 0 deletions tree-sitter-tako/test/corpus/simple_exprs.tk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,146 @@ Simple values with infix sequence
)
)

==================
Simple values with parens
==================

(0)

---
(source_file
(parens
(int_literal)
)
)

==================
Simple values with more parens
==================

((0))

---
(source_file
(parens
(parens
(int_literal)
)
)
)

==================
Simple values with mismatched parens
==================

((0()

---
(source_file
(parens
(parens
(int_literal)
(ERROR))
(MISSING ")")
)
)

==================
Empty Block
==================

{}

---
(source_file
(block)
)

==================
Empty Block with suffix sequence
==================

{};

---
(source_file
(sequence (block))
)

==================
Empty Container notation
==================

[]

---
(source_file
(container)
)


==================
Container notation with a single value
==================

[1]

---
(source_file
(container
(int_literal)
)
)

==================
Container notation with multiple values
==================

[1,2,3,4,]

---
(source_file
(container
(int_literal)
(int_literal)
(int_literal)
(int_literal)
)
)

==================
Container notation containing multiple identifiers
==================

[a,b,c,d,]

---
(source_file
(container
(ident)
(ident)
(ident)
(ident)
)
)


==================
Container notation containing multiple subexpressions
==================

[3+a,b*c,c=0,"Wow",]

---
(source_file
(container
(add (int_literal) (ident))
(mul (ident) (ident))
(assign (ident) (int_literal))
(string_literal)
)
)

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

0 comments on commit 13b9d7e

Please sign in to comment.