-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.peg.js
146 lines (146 loc) · 4.71 KB
/
grammar.peg.js
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
start
= [ \t\n]* definitions [ \t\n]+ body:body [ \t\n]*
{ return body; }
/ [ \t\n]* body:body [ \t\n]*
{ return body; }
definitions
= definition [ \t\n]+ definitions
/ definition
definition
= "define" [ \t\n]+ chars:[A-Za-z]+ [ \t\n]+ exprs:expressions
{ userDefines[chars.join("")] = exprs; }
body
= from:from_clause [ \t\n]+ to:to_clause [ \t\n]+ match:match_clause
{ return { match: match, from: from, to: to } }
/ match:match_clause
{ return { match: match } }
from_clause
= "from" [ \t\n]+ locator:start_locator
{ return locator; }
start_locator
= "anywhere" { return "anywhere"; }
/ "linestart" { return "linestart"; }
to_clause
= "to" [ \t\n]+ locator:end_locator
{ return locator; }
end_locator
= "anywhere" { return "anywhere"; }
/ "lineend" { return "lineend"; }
match_clause
= "match" [ \t\n]* settings:match_settings [ \t\n]* "{" [ \t\n]+ exprs:expressions [ \t\n]* "}"
{ return { ast: exprs, settings: settings.join("") }; }
/ "match" [ \t\n]+ "{" [ \t\n]* exprs:expressions [ \t\n]* "}"
{ return { ast: exprs }; }
match_settings
= lhs:match_setting [ \t\n]+ rhs:match_settings
{ return lhs.concat(rhs); }
/ setting:match_setting
{ return setting; }
match_setting
= "caseinsensitive" { return ["i"]; }
/ "casesensitive" { return []; }
/ "global" { return ["g"]; }
/ "multiline" { return ["m"]; }
expressions
= expr:expression [ \t\n]+ "and" [ \t\n]+ exprs:expressions { return { conjunction: { lhs: expr, rhs: exprs } };}
/ expr:expression [ \t\n]+ "or" [ \t\n]+ exprs:expressions { return { disjunction: { lhs: expr, rhs: exprs } };}
/ expr:expression { return expr;}
expression
= quantifier:quantifier [ \t\n]+ block:block
{ return { quantifier: quantifier.quantifier, block: block.block, qualifiers: block.qualifiers }; }
/ block:block
{ return { block: block.block, qualifiers: block.qualifiers }; }
/ quantifier:quantifier [ \t\n]+ set:set
{ return { quantifier: quantifier.quantifier, set: set.set }; }
/ set:set
{ return { set: set.set }; }
/ quantifier:quantifier [ \t\n]+ literal:literal
{ return { quantifier: quantifier.quantifier, literal: literal.literal }; }
/ literal:literal
{ return { literal: literal.literal }; }
/ quantifier:quantifier [ \t\n]+ ident:user_defined
{
var obj = {};
obj.quantifier = quantifier.quantifier;
var type = Object.keys(ident)[0];
obj[type] = ident[type];
return obj;
}
/ ident:user_defined
{ return ident; }
block
= qualifiers:block_qualifiers [ \t\n]* "{" [ \t\n]* exprs:expressions [ \t\n]* "}"
{ return { block: exprs, qualifiers: qualifiers }; }
/ "{" [ \t\n]* exprs:expressions [ \t\n]* "}"
{ return { block: exprs }; }
block_qualifiers
= lhs:block_qualifier [ \t\n]+ rhs:block_qualifiers
{ return lhs.concat(rhs); }
/ qualifier:block_qualifier
{ return qualifier; }
block_qualifier
= "capture" { return ["capture"]; }
/ "positive_lookahead" { return ["positive_lookahead"]; }
/ "negative_lookahead" { return ["negative_lookahead"]; }
quantifier
= quantifier:quantifier_range [ \t\n]+ "lazy"
{
var quantifier = quantifier.quantifier;
quantifier.lazy = true;
return { quantifier: quantifier };
}
/ quantifier:quantifier_range
{ return { quantifier: quantifier.quantifier }; }
quantifier_range
= leftDigits:[0-9]+ "orMore"
{ return {
quantifier: { lower: parseInt(leftDigits.join(""), 10),
upper: Infinity }};
}
/ leftDigits:[0-9]+ "to" rightDigits:[0-9]+
{ return {
quantifier: { lower: parseInt(leftDigits.join(""), 10),
upper: parseInt(rightDigits.join(""), 10) }
};}
set
= "not" [ \t\n]+ set:set
{
if(set.set) {
return { negatedSet: set.set };
}
}
/ "[" [ \t\n]* elements:set_elements [ \t\n]* "]"
{
var set = [];
for(var i = 0; i < elements.length; ++i) {
if(elements[i].literal) {
set = set.concat(elements[i].literal);
}
}
return {
set: set
};
}
set_elements
= element:set_element [ \t\n]* "," [ \t\n]* more_elements:set_elements
{ return [ element ].concat(more_elements); }
/ element:set_element
{ return [ element ]; }
set_element
= literal
literal
= "\"" chars:[^"]* "\""
{ return {
literal: chars
};}
/ "digit" { return { literal: ["\\d"] }; }
/ "whitespace" { return { literal: ["\\s"] }; }
/ "wordchar" { return { literal: ["\\w"] }; }
/ "char" { return { literal: ["CHAR"] }; }
user_defined
= char:[A-Z] chars:[A-Za-z]*
{
var id = char + chars.join("");
if(userDefines[id]) { return userDefines[id]; }
throw new Error("Undefined symbol: '" + id + "'");
}