Files
grammar/grammar.js

90 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-03-15 14:40:13 +11:00
/**
* @file Ground VM textual representation highlighter
* @author Maxwell Jeffress <maxwelljeffress@proton.me>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: "ground",
extras: $ => [
/\s/,
$.comment,
],
rules: {
source_file: $ => repeat($._statement),
_statement: $ => choice(
$.label_definition,
$.function_definition,
$.struct_definition,
$.instruction,
),
comment: $ => token(seq('#', /.*/)),
label_definition: $ => seq('@', $.identifier),
function_definition: $ => seq(
'fun',
field('name', $.function_reference),
field('return_type', $.type_reference),
repeat(seq(field('arg_type', $.type_reference), field('arg_name', $.direct_reference))),
repeat($._statement),
'endfun'
),
struct_definition: $ => seq(
'struct',
field('name', $.type_reference),
repeat($._statement),
'endstruct'
),
instruction: $ => seq(
field('keyword', $.keyword),
repeat($._argument)
),
keyword: $ => choice(
'if', 'jump', 'end', 'input', 'print', 'println',
'set', 'init', 'gettype', 'exists', 'setlist', 'setlistat',
'getlistat', 'getlistsize', 'listappend', 'getstrsize', 'getstrcharat',
'add', 'subtract', 'multiply', 'divide', 'equal', 'inequal', 'not',
'greater', 'lesser', 'stoi', 'stod', 'tostring', 'return', 'call',
'use', 'extern', 'getfield', 'setfield'
),
_argument: $ => choice(
$.value_reference,
$.direct_reference,
$.line_reference,
$.function_reference,
$.type_reference,
$.string,
$.int,
$.double,
$.char,
$.bool
),
value_reference: $ => seq('$', $.identifier),
direct_reference: $ => seq('&', $.identifier),
line_reference: $ => seq('%', $.identifier),
function_reference: $ => seq('!', $.identifier),
type_reference: $ => seq('-', $.identifier),
identifier: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
string: $ => /"[^"]*"/,
int: $ => /-?\d+/,
double: $ => /-?\d+\.\d+/,
char: $ => /'[^']'/,
bool: $ => choice('true', 'false'),
}
});