interplane.Get Omega
Read in your language

tree-sitter-omega

aien-dev/tree-sitter-omega

Brought by drakestapleton

branch main Branches Tags History

tree-sitter-omega / grammar.js

197 lines · 7496 bytes Raw Blame History
1/**
2 * @file Omega (.om, Omegese) grammar for tree-sitter
3 * @author Drake Stapleton <drake@aienos.com>
4 * @license Apache-2.0 WITH LLVM-exception
5 *
6 * Source of truth for the language: aien-dev/omega-mv
7 * docs/one_layer/OMEGESE.md (sections 3, 9, 10, 12)
8 * seed/omc/omc.om (fn tok / tok2 / tok_num / tok_text: the lexer; fn kw / kw2: reserved words)
9 *
10 * Omega is line oriented. A line holds one statement; words are separated by blanks; `(` and `)`
11 * are words of their own; a text literal is one word in double quotes. Values are prefix
12 * expressions whose arity comes from the words themselves (and, for calls, from the callee's
13 * signature), so this grammar keeps a line's value as a flat sequence of words and gives structure
14 * to the parts that are context free: blocks (`fn`/`if`/`else`/`while`/`for`/`layout` ... `end`),
15 * declarations, and the statement kind a line starts with. Operator and type words are ordinary
16 * identifiers here and are coloured by queries/highlights.scm, because OM-1 units may use the OM-2
17 * words (`open`, `field`, `rotr`, ...) as names.
18 */
19
20/// <reference types="tree-sitter-cli/dsl" />
21// @ts-check
22
23const words = ($) => repeat1($._word);
24
25// A block's lines: the newline that ends the header, then statement lines. The `block` node holds
26// the statements (absent when the block is empty).
27const block = ($, name) => seq($._sep, optional(field(name, $.block)));
28
29export default grammar({
30 name: 'omega',
31
32 extras: ($) => [/[ \t\r]/, $.comment],
33
34 word: ($) => $.identifier,
35
36 rules: {
37 source_file: ($) =>
38 seq(
39 optional($._sep),
40 optional(seq($._top_level, repeat(seq($._sep, $._top_level)), optional($._sep))),
41 ),
42
43 _sep: ($) => repeat1($._newline),
44 _newline: (_) => '\n',
45
46 _top_level: ($) =>
47 choice(
48 $.profile,
49 $.global_declaration,
50 $.text_declaration,
51 $.const_declaration,
52 $.struct_declaration,
53 $.layout_declaration,
54 $.abstract_declaration,
55 $.holds_declaration,
56 $.function_definition,
57 ),
58
59 // `om2`, `om2 diag`, `om2 fmt`, `om2 release` ... (OMEGESE.md 9.13, 12) and the IR-mode header `ir` (11).
60 profile: ($) => seq(field('name', choice('om2', 'ir')), repeat(field('option', $.identifier))),
61
62 global_declaration: ($) => seq('global', field('name', $.identifier), words($)),
63 text_declaration: ($) => seq('text', field('name', $.identifier), field('value', $.string)),
64 const_declaration: ($) => seq('const', field('name', $.identifier), words($)),
65 // `struct Name field type [len] ... end` is one line (OMEGESE.md 9.7).
66 struct_declaration: ($) => seq('struct', field('name', $.identifier), repeat($._word), 'end'),
67 abstract_declaration: ($) => seq('abstract', field('name', $.identifier), words($)),
68 holds_declaration: ($) => seq('holds', field('name', $.identifier), words($)),
69
70 // `layout NAME SIZE`, field lines, `end` (OMEGESE.md 9.13 External layout).
71 layout_declaration: ($) =>
72 seq(
73 'layout',
74 field('name', $.identifier),
75 repeat($._word),
76 $._sep,
77 repeat(seq($.field_declaration, $._sep)),
78 'end',
79 ),
80 field_declaration: ($) => seq('field', field('name', $.identifier), repeat($._word)),
81
82 // `[export] fn NAME (param mode? type len?)* to TYPE [inline]` ... `end`
83 function_definition: ($) =>
84 seq(
85 optional('export'),
86 'fn',
87 field('name', $.identifier),
88 optional(field('parameters', $.parameter_list)),
89 'to',
90 field('return_type', $.return_type),
91 block($, 'body'),
92 'end',
93 ),
94 parameter_list: ($) => words($),
95 return_type: ($) => words($),
96
97 _statement: ($) =>
98 choice(
99 $.if_statement,
100 $.while_statement,
101 $.for_statement,
102 $.return_statement,
103 $.put_statement,
104 $.trap_statement,
105 $.exit_statement,
106 $.break_statement,
107 $.continue_statement,
108 $.assignment,
109 $.expression_statement,
110 ),
111
112 block: ($) => repeat1(seq($._statement, $._sep)),
113
114 if_statement: ($) =>
115 seq(
116 'if',
117 field('condition', $.expression),
118 block($, 'consequence'),
119 optional(field('alternative', $.else_clause)),
120 'end',
121 ),
122 else_clause: ($) => seq('else', block($, 'body')),
123
124 while_statement: ($) =>
125 seq(
126 'while',
127 field('condition', $.expression),
128 optional(seq('bound', field('bound', $._word))),
129 block($, 'body'),
130 'end',
131 ),
132
133 for_statement: ($) =>
134 seq(
135 'for',
136 field('counter', $.identifier),
137 field('count', $.expression),
138 block($, 'body'),
139 'end',
140 ),
141
142 return_statement: ($) => seq('ret', optional(field('value', $.expression))),
143 put_statement: ($) => seq('put', field('arguments', $.expression)),
144 trap_statement: ($) => seq('trap', field('code', $.expression)),
145 exit_statement: ($) => seq('exit', field('value', $.expression)),
146 break_statement: (_) => 'break',
147 continue_statement: (_) => 'continue',
148
149 assignment: ($) => seq(field('name', $.identifier), '=', field('value', $.expression)),
150
151 // A line that is a call, an assignment written without `=`, or a contract clause
152 // (`requires`/`ensures`/`invariant` are only reserved inside OM-2 units).
153 expression_statement: ($) => $.expression,
154
155 expression: ($) => words($),
156
157 _word: ($) =>
158 choice($.identifier, $.integer, $.float, $.string, $.parenthesized, $.unsized),
159
160 parenthesized: ($) => seq('(', repeat($._word), ')'),
161
162 // `NAME view T *`: an unsized view (OMEGESE.md 9.13 Unsized views).
163 unsized: (_) => '*',
164
165 identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/,
166
167 // Decimal (with optional leading minus) and the OM-2 hex form `0x` + 1..16 lowercase digits.
168 integer: (_) => token(choice(/-?[0-9]+/, /0x[0-9a-f]+/)),
169
170 // `digits.digits[(e|E)[+|-]digits][f32|f64]` (OMEGESE.md 9.9); the exponent form without a dot
171 // is accepted here too because omc's tok_num treats any word holding `.`, `e`, `E` or `f` as a float.
172 float: (_) =>
173 token(
174 choice(
175 /-?[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?(f32|f64)?/,
176 /-?[0-9]+[eE][+-]?[0-9]+(f32|f64)?/,
177 /-?[0-9]+(f32|f64)/,
178 ),
179 ),
180
181 // Escapes: \n \t \\ \" (others are compile error 22, still one word to the lexer). Inside text
182 // the comment extra must never win: `" note "` and `"\nnote ..."` are text (omc tok_text reads to
183 // the closing quote), so string_content outranks comment (prec 2 > 1) and also covers the blanks
184 // that would otherwise be skipped as extras.
185 string: ($) =>
186 seq('"', repeat(choice($.string_content, $.escape_sequence)), token.immediate('"')),
187 string_content: (_) => token.immediate(prec(2, /[^"\\\n]+/)),
188 escape_sequence: (_) => token.immediate(/\\[^\n]/),
189
190 // `note ...` is a line the compiler does not read beyond its first word (OMEGESE.md 9.5; omc
191 // reads it only where a line starts, after blanks). The word is exactly `note`: omc isnote ends
192 // it at a blank, a parenthesis or the end, so `notes` and `noted` are names. A bare `note` is a
193 // keyword (lexed as an identifier, then matched whole against the keywords), and the longer
194 // `note text` / `note(...)` token outmatches the identifier `note`.
195 comment: (_) => choice('note', token(/note[ \t\r()][^\n]*/)),
196 },
197});