Skip to content

Commit

Permalink
(treesitter) Add effect syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Memorytaco committed Oct 7, 2024
1 parent 14643a1 commit 7399069
Show file tree
Hide file tree
Showing 5 changed files with 100,544 additions and 90,572 deletions.
54 changes: 40 additions & 14 deletions utils/tree-sitter-yolang/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ module.exports = grammar({
declaration: $ => choice(
$.data,
$.let_binding,
$.fixity
$.fixity,
$.effect,
),

// type declaration
Expand All @@ -90,28 +91,33 @@ module.exports = grammar({
))),

// type expression
type: $ => prec.right(1, seq(
type: $ => prec.left(1, seq(
choice(
seq("forall", repeat1(choice(
alias($.id, $.type_var_id),
// TODO: refine semantic of ":", after it should be kind expression instead of type
wrap("(", ")", seq(alias($.id, $.type_var_id), choice("=", "~", ":"), $.type))
)),".", $.type),
alias("->", $.type_reserved_function),
alias(choice("->", "=>"), $.type_reserved_operator),
$.operator,
$.type_var,
$.type_unit,
$.type_tuple,
wrap("(", ")", $.type),
$.handler_type,
$.record_type,
$.primitive_type
),
repeat(choice($.type, $.type_effect_suffix)),
)),

handler_type: $ => wrap("{", "}", seq(sep1(",", seq(optional(".."), $.type)), ";;", $.type)),
record_type: $ => wrapsep("{", "}", ",", seq(alias($.identifier, $.record_field), "=", $.type)),

// type effect suffix
type_effect_suffix: $ => choice(
alias(seq("!", token.immediate(/'?[A-Za-z_]\w*/)), $.type_eff_name),
wrapsep("!{", "}", ",", choice($.identifier, $.type_implicit_var))
wrapsep("!{", "}", ",", choice($.type, seq("..", choice($.identifier, $.type_implicit_var))))
),
type_unit: _$ => /\(\s*\)/,
type_tuple: $ => wrap("(", ")", sep1(",", $.type)),
Expand Down Expand Up @@ -149,28 +155,42 @@ module.exports = grammar({
// let binding includes effect block, value definition and function definition
let_binding: $ => statement("let", choice($.id, $.force_id, $.operator_id), optional(seq(":", $.type)), choice(
seq("=", $.expression),
repeat1(seq("|", sep1(",", $.pattern), "=", $.expression)),
seq("do", wrapsep("{", "}", ";;", $.id))
repeat1(seq("|", sep1(",", $.or_pattern), "=", $.expression)),
seq("do", choice($.do_block, $.expression))
)),

// expression
expression: $ => prec.left(1, seq(
choice(
$.macro_id, $.identifier, $.number,
$.string, $.string_cons, $.operator,
$.handler,
seq("forall", repeat1($.id), ".", $.expression),
seq("let", sep1(",", seq($.pattern, optional(seq("or", $.expression)), "=", $.expression)), "in", $.expression),
seq(wrapsep1("|", "|", ",", $.pattern), $.expression),
seq(optional(seq("with", sep1(",", $.identifier))), "do", wrapsep("{", "}", ";;", choice(
seq("let", $.pattern, "<-", $.expression),
seq("const", sep1(",", seq($.pattern, optional(seq("or", $.expression)), "=", $.expression))),
$.expression
))),
wrapsep1("[", "]", "|", seq(sep1(",", $.pattern), "=", $.expression)),
seq("let", sep1(",", seq($.or_pattern, "=", $.expression)), "in", $.expression),
seq(wrapsep1("|", "|", ",", $.or_pattern), $.expression),
seq(optional(seq("with", sep1(",", $.expression))), "do", choice($.do_block, $.expression)),
wrapsep1("[", "]", "|", seq(sep1(",", $.or_pattern), "=", $.expression)),
wrapsep("(", ")", ",", $.expression),
), optional(choice(seq(":", $.type), repeat1($.expression)))
)),

or_pattern: $ => seq($.pattern, optional(seq("or", $.expression))),

do_block: $ => wrapsep("{", "}", ";;", choice(
seq("let", $.or_pattern, "<-", $.expression),
seq("const", sep1(",", seq($.or_pattern, "=", $.expression))),
$.expression
)),

handler: $ => prec(1, seq("handler", choice(
seq(sep1(",", alias($.id, $.method)), alias($.id, $.handler_resume), "=", $.expression),
seq(optional(seq("forall", repeat(choice($.id, $.force_id)))), wrapsep1("|", "|", ",", $.type), wrapsep("{", "}", ",",
seq(alias($.id, $.method), alias($.id, $.handler_resume), choice(
seq("=", $.expression),
repeat1(seq("|", sep1(",", $.or_pattern), "=", $.expression)))
)))
))),

// definition of pattern
pattern: $ => prec.left(1, seq(
choice(
Expand All @@ -192,6 +212,12 @@ module.exports = grammar({
pattern_unit: _$ => /\(\s*\)/,


// effect
effect: $ => statement("effect", choice(
seq(optional(seq(repeat($.id), ".")), sep1(",", alias($.id, $.method)), ":", $.type),
seq(repeat1($.id), wrapsep("{", "}", ",", seq(alias($.id, $.method), ":", $.type)))
)),

// definition of terminal symbol
hex_integer: _$ => /0[xX][_A-Za-z0-9]+/,
octet_integer: _$ => /0[oO][_0-7]+/,
Expand Down
36 changes: 26 additions & 10 deletions utils/tree-sitter-yolang/queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@
"#!(" @type.builtin
")" @type.builtin)

(type "forall" @keyword)
(record_field) @variable.member

(type ["forall" ":" "."] @keyword)
(type "=" @type.builtin)
(type "~" @type.builtin)
(type ":" @keyword.operator)
(type_unit) @type.builtin
(type_tuple "(" @type.builtin "," ")" @type.builtin)
(type_effect_suffix "!{" @keyword.operator "}" @keyword.operator)
(type_eff_name) @type.builtin
(type_reserved_function) @type.builtin
(type_reserved_operator) @type.builtin
(type (operator) @operator)
(primitive_compound (operator) @operator)
(type_effect_suffix ".." @keyword)
(handler_type [";;" ".."] @keyword)
(handler_type "{" @type.builtin "}" @type.builtin)
(record_type "=" @keyword)
(record_type "{" @type.builtin "}" @type.builtin)

(primitive_reverse_atom_prim) @type.builtin
(primitive_reverse_atom) @keyword.operator
Expand All @@ -54,17 +60,16 @@
(data "{" @keyword.operator "," @keyword.operator "}" @keyword.operator)

;; binding
(let_binding "let" @keyword)
(let_binding "=" @keyword.operator)
(let_binding "|" @keyword.operator)
(let_binding ["let" "do" "=" "|"] @keyword)

;; expression
(expression (number) @number)
(expression ":" @keyword.operator)
(expression "forall" @keyword "." @keyword.operator)
(expression ["<-" "|" "="] @keyword.operator)
(expression ["let" "do" "with" "in" "or" "const"] @keyword)
(expression "forall" @keyword "." @keyword)
(expression [":" "|" "[" "]"] @keyword)
(do_block ["let" "const" "<-" "|" "="] @keyword)
(expression ["let" "do" "with" "in" "or"] @keyword)
(string_cons (string_cons_var) @attribute)
(handler ["forall" "|"] @keyword)

;; operator fixity
(fixity "_" @keyword.operator)
Expand All @@ -81,6 +86,17 @@
(pattern "@" "(" @keyword.operator "," ")" @keyword.operator)
(pattern (number) @number)
(pattern "!" @keyword.operator "->" @keyword.operator)
(or_pattern "or" @keyword)

;; effects
(effect "effect" @keyword)
(effect ["." ":"] @keyword.operator)
(effect "{" @keyword "}" @keyword)
(effect (method) @variable.member)
(handler "handler" @keyword)
(handler (handler_resume) @variable.builtin)
(handler "=" @keyword)
(handler (method) @variable.member)

;; macro
(macro_id) @variable.parameter.builtin
Loading

0 comments on commit 7399069

Please sign in to comment.