-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.peg
54 lines (43 loc) · 1.15 KB
/
grammar.peg
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
// MUSHcode Grammar with Uniform AST Structure
// Lemuel Canady, Jr (lem@digibear.io)
start = expression*
expression = _ e:(function / percent_substitution / string_literal / word) _ {
return e;
}
function = bracket_function / standard_function
bracket_function = "[" _ f:standard_function _ "]" {
return f;
}
standard_function = call:word "(" _ args:arg_list? _ ")" {
return {
type: "function",
operator: {
type: "word",
value: call
},
args: args || []
};
}
arg_list = head:arg tail:(_ "," _ arg)* {
return [head, ...tail.map(item => item[3])].filter(arg => arg !== undefined);
}
arg = e:(function / percent_substitution / string_literal / word) { return e; } / "" { return null; }
word = w:[^%\[\](),\s"]+ {
return {
type: "word",
value: w.join("")
};
}
string_literal = '"' content:([^"]* ('""' [^"]*)*) '"' {
return {
type: "string",
value: content.join("").replace(/""/g, '"')
};
}
percent_substitution = "%" substitution:[a-zA-Z0-9]+ {
return {
type: "substitution",
value: substitution.join("")
};
}
_ = [ \t\n\r]*