ast_generator/parser.ne

93 lines
2.3 KiB
Plaintext
Raw Normal View History

2024-07-21 20:56:42 +01:00
@preprocessor typescript
@{%
import * as moo from "https://deno.land/x/moo@0.5.1-deno.2/mod.ts";
import * as ast from "./ast.out.ts";
const lexer: any = moo.compile({
newline: { match: /[\n]+/, lineBreaks: true },
whitespace: /[ \t]+/,
singleLineComment: /\/\/.*?$/,
multiLineComment: { match: /\*[^*]*\*+(?:[^/*][^*]*\*+)*/, lineBreaks: true },
2024-07-29 01:54:28 +01:00
codeBlock: {
match: /\{%[^%]*?%\}/,
lineBreaks: true,
value: s => s.slice(2, s.length - 2),
},
2024-07-21 20:56:42 +01:00
name: {
match: /[a-zA-Z0-9_]+/,
type: moo.keywords({
keyword: [],
}),
},
lparen: "(",
rparen: ")",
lbrace: "{",
rbrace: "}",
lbracket: "[",
rbracket: "]",
colon: ":",
comma: ",",
questionmark: "?",
});
%}
@lexer lexer
file -> elements {% id %}
elements -> _ (element elementTail _):? {% v => v[1] ? [v[1][0], ...v[1][1]] : [] %}
elementTail -> (nl__ element):* {% v => v[0].map(w => w[1]) %}
2024-07-29 01:54:28 +01:00
element -> codeBlock {% v => ast.Statement.CodeBlock(v[0]) %}
| enum {% v => ast.Statement.Enum(v[0]) %}
| node {% v => ast.Statement.Node(v[0]) %}
codeBlock -> %codeBlock {% v => ast.CodeBlock(v[0].value) %}
2024-07-21 20:56:42 +01:00
enum -> name _ "{" nodes "}" {% v => ast.Enum(v[0], v[3]) %}
nodes -> _ (node nodeTail _):? {% v => v[1] ? [v[1][0], ...v[1][1]] : [] %}
nodeTail -> (nl__ node):* {% v => v[0].map(w => w[1]) %}
2024-07-24 11:31:06 +01:00
node -> name (_ paramList):? {% v => ast.Node(v[0], v[1] ? v[1][1] : []) %}
paramList -> "(" params ")" {% v => v[1] %}
2024-07-21 20:56:42 +01:00
params -> _ (param paramTail (_ ","):? _):? {% v => v[1] ? [v[1][0], ...v[1][1]] : [] %}
paramTail -> ("," _ param):* {% v => v[0].map(w => w[2]) %}
2024-07-21 22:35:34 +01:00
param -> namedParam {% id %}
| unnamedParam {% id %}
2024-07-24 11:31:06 +01:00
namedParam -> name _ ":" _ type {% v => ast.Param.Named(v[0], v[4]) %}
2024-07-21 22:35:34 +01:00
2024-07-24 11:31:06 +01:00
unnamedParam -> type {% v => ast.Param.Unnamed(v[0]) %}
2024-07-21 20:56:42 +01:00
type -> optional {% id %}
| multiple {% id %}
2024-07-24 11:31:06 +01:00
| name {% v => ast.Type.Name(v[0]) %}
2024-07-21 20:56:42 +01:00
2024-07-24 11:31:06 +01:00
optional -> type "?" {% v => ast.Type.Optional(v[0]) %}
2024-07-21 20:56:42 +01:00
2024-07-24 11:31:06 +01:00
multiple -> type "[" "]" {% v => ast.Type.Multiple(v[0]) %}
2024-07-21 20:56:42 +01:00
2024-07-21 22:35:34 +01:00
name -> %name {% v => ast.Name(v[0].value, v[0].line, v[0].col) %}
2024-07-21 20:56:42 +01:00
_ -> __:?
2024-07-24 11:31:06 +01:00
__ -> (%whitespace|%newline|%singleLineComment|%multiLineComment):+
2024-07-21 20:56:42 +01:00
nl__ -> sl_ (%newline sl_):+
sl_ -> sl__:?
sl__ -> (%whitespace|%singleLineComment|%multiLineComment):+