forked from boo-lang/boo-extensions
/
TokensMacro.boo
41 lines (32 loc) · 1.04 KB
/
TokensMacro.boo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace Boo.OMeta
import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
import Boo.Lang.PatternMatching
macro tokens:
"""
Generates token rules. The generated code relies on the existence of a
parameterized token rule.
From:
tokens:
eq = "="
id = ++letters
it generates:
eq = "=" >> value ^ makeToken("eq", value)
id = ++letters >> value ^ makeToken("id", value)
tokens = eq | id
EQ = token["eq"]
ID = token["id"]
"""
block as Block = tokens.ParentNode
rules = []
for stmt in tokens.Body.Statements:
match stmt:
case ExpressionStatement(Expression: [| $name = $pattern |]):
e = [| $name = (("" ^ makeToken("here", null, input, null)) >> start, $pattern >> value) ^ makeToken($(name.ToString()), value, (start as Token).start, null) |]
e.LexicalInfo = stmt.LexicalInfo
block.Add(e)
tokenRule = [| $(ReferenceExpression(Name: name.ToString().ToUpper())) = token[$(name.ToString())] |]
e.LexicalInfo = stmt.LexicalInfo
block.Add(tokenRule)
rules.Add(name)
block.Add([| tokens = $(choicesRuleFrom(rules)) |])