-
Notifications
You must be signed in to change notification settings - Fork 0
/
filigreeGrammar.ne
185 lines (164 loc) · 4.49 KB
/
filigreeGrammar.ne
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@{%
let moo = require('moo');
let lexer = moo.compile({
ruleName: /[a-zA-Z0-9_-]+/, // this is also used for modifier names
eq: " = ",
lbrak: "[",
rbrak: "]",
lang: "<",
rang: ">",
or: "/",
dot: ".",
// ignorable noise between lines:
// optional whitespace
// optional comment to the end of the line (starting with "#")
// a required newline
// optional whitespace starting the next line
// This assumes the string ends in a newline! You must ensure that's the case
// (The Filigree class adds a newline at the end for you)
nl: { match: /[ \t]*(?:#[^\n]*)?\n[ \t]*/, lineBreaks: true },
// general string characters
nonControlChars: /[^[/\]<.>=\n]/, // not one of [ | ] < . > = \n
//nl: { match: /[ \t]*\n[ \t]*/, lineBreaks: true }, //
//comment: /[ \t]*\/\/[^\n]*/, // In other words, ws* "//" anything-but-newline*
//_: /[ \t]+/,
});
export type FDecl = {
id : number,
kind : 'decl',
name : string,
value : FExpr,
}
export type FExpr =
FSeq
| FRef
| FChoose
| FLiteral
export type FSeq = {
id : number,
kind : 'seq',
children: FExpr[],
}
export type FRef = {
id : number,
kind : 'ref',
name : string,
mods : string[],
}
export type FChoose = {
id : number,
kind : 'choose',
children : FExpr[],
}
export type FLiteral = {
id : number,
kind : 'literal',
text : string,
}
let flatten = (arr : any[]) : any[] => {
let result : any[] = [];
for (let item of arr) {
if (Array.isArray(item)) {
result = result.concat(flatten(item));
} else {
result.push(item);
}
}
return result;
}
%}
@preprocessor typescript
@lexer lexer
#@builtin "whitespace.ne" # `_` means arbitrary amount of whitespace
#@builtin "number.ne" # `int`, `decimal`, and `percentage` number primitives
ruleDecls -> %nl:* (ruleDecl %nl:*):* {% ([nl, pairs]) : FDecl[] => {
return flatten(pairs).filter((x : any) => x.type !== 'nl');
} %}
# foo = bar
ruleDecl -> %ruleName " = " seq {% ([ruleName, _, seq]) : FDecl => ({
id: -1,
kind: 'decl',
name: ruleName.value,
value: seq,
}) %}
# a<b>c
seq -> literal:? (tool literal:?):* {% ([firstLiteral, pairs]) : FSeq => {
// pairs is an array of [[tool], literal]
// the literals are null if not present
let sequence = [firstLiteral];
for (let pair of pairs) {
sequence.push(pair[0][0]);
sequence.push(pair[1]);
}
return {
id: -1,
kind: 'seq',
children: sequence.filter(x => x !== null),
};
} %}
tool ->
ref
| chooseMultiLine
| chooseOneLine
# <foo>
ref -> "<" %ruleName (%dot %ruleName):* ">" {% (parts : any[]) : FRef => {
parts = flatten(parts);
parts = parts.slice(1, parts.length-1); // remove < and >
let name = parts.shift().value;
let mods : string[] = [];
while (parts.length > 0) {
let mod = parts.shift();
if (mod.type !== 'dot') {
mods.push(mod.value);
}
}
return {
id: -1,
kind: 'ref',
name: name,
mods: mods,
};
} %}
# [a\nb]
chooseMultiLine => "[" %nl seq (%nl seq):* %nl "]" {% (parts: any[]) : FChoose => {
let children = flatten(parts);
//console.log('----------------\\');
//console.log(JSON.stringify(children, null, 4));
children = children.filter(child =>
child !== null && child.type !== 'lbrak' && child.type !== 'nl' && child.type !== 'rbrak'
);
// remove empty lines
children = children.filter(child =>
!( child.kind === 'seq' && child.children.length === 0 )
//&& ! (child.kind === 'seq' && child.children.length === 1 && child.children[0]
);
//console.log('----------------');
//console.log(JSON.stringify(children, null, 4));
//console.log('----------------/');
return {
id: -1,
kind: 'choose',
children: children,
};
} %}
# [a|b]
chooseOneLine -> "[" seq (%or seq):* "]" {% (parts: any[]) : FChoose => {
let children = flatten(parts);
children = children.filter(child =>
child.type !== 'lbrak' && child.type !== 'or' && child.type !== 'rbrak'
);
return {
id: -1,
kind: 'choose',
children: children,
};
} %}
# any other text
literal -> (%ruleName | %dot | %nonControlChars):+ {% (pieces) : FLiteral => {
let text = pieces[0].map((p : any) => p[0].value).join('');
return {
id: -1,
kind: 'literal',
text: text
};
}%}