-
Notifications
You must be signed in to change notification settings - Fork 1
/
atom.pegjs
189 lines (155 loc) · 3.53 KB
/
atom.pegjs
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
186
187
188
189
// function
{
// concat({a:1, b:2}, {b:3}) => {a:1, b:[2, 3]}
function concat(a, b) {
for(let key of Object.keys(b)) {
if(a.hasOwnProperty(key)) {
if(!Array.isArray(a[key])) a[key] = [a[key]];
a[key] = a[key].concat(b[key]);
} else {
a = Object.assign(a, {[key]: b[key]});
}
}
return a;
}
}
// Main
XML
= _ value:value _ { console.log(value); return value; }
value
= head:head
content:content {
return Object.assign(head, content);
}
content
= multi / line / cdata / html
// Sign
_ = [\t\n\r ]*
left_arrow = _ "<" _
right_arrow = _ ">" _
quotation = _ '"' _
equal = _ "=" _
backslash = _ "/" _
begin_cdata = _ "<![CDATA[" _
end_cdata = _ "]]>" _
begin_xml = _ "<?xml" _
end_xml = _ "?>" _
// markup
StartTag "STARTTAG"
= left_arrow
key:TagName
_
members: (
head:keyword
tail:(_ m:keyword { return m; })* {
return [head].concat(tail).reduce((prev, curr) => Object.assign(prev, curr));
}
)?
right_arrow {
return { [key]: members === null ? '' : members.length === 1 ? members[0] : members };
}
EndTag "ENDTAG"
= left_arrow
backslash
TagName:TagName
right_arrow {
return TagName;
}
TagName = chars:[a-zA-Z_:?]+ { return chars.join('').replace(/:/g, '-').replace(/\?/, ''); }
TagValue = chars:[^<]+ { return chars.join(''); }
// value
head
= begin_xml
members: (
head:keyword
tail:(_ m:keyword { return m; })* {
return [head].concat(tail).reduce((prev, curr) => Object.assign(prev, curr));
}
)
end_xml {
return members;
}
multi
= StartTag:StartTag
content: (
head:content
tail:content* {
return [head].concat(tail);
}
)?
EndTag:EndTag {
if(Object.keys(StartTag)[0] !== EndTag) {
throw new Error(`StartTag ${StartTag[0]} no equal to EndTag ${EndTag}`);
}
if(StartTag[EndTag]) {
if(content === null) StartTag[EndTag].value = '';
else {
content.reduce((prev, curr) => concat(prev, curr), StartTag[EndTag]);
}
return StartTag;
} else {
if(content.length === 1 && Object.keys(content[0]).toString() === 'value') return { [EndTag]: content[0].value };
return { [EndTag]: content.reduce((prev, curr) => concat(prev, curr)) };
}
}
line "LINE"
= left_arrow
key:TagName
_
members:(
head:keyword
tail:(_ m:keyword { return m; })* {
return [head].concat(tail).reduce((prev, curr) => concat(prev, curr));
}
)?
backslash
right_arrow {
return { [key]: members === null ? '' : members.length === 1 ? members[0] : members };
}
cdata "CDATA"
= begin_cdata
text:text
end_cdata{
return { value: text.join('') };
}
text "text"
= x:(!("]]>") .)* {
return x.map(y => y[1]);
}
html "html"
= text: [^<]+ {
return { value: text.join('') };
}
keyword "keyword"
= k:TagName
equal
v:string {
return { [k]: v }
}
// Copy
string
= quotation chars:char* quotation
{
return chars.join("");
}
char
= unescaped
/ escape
sequence:(
'"'
/ "\\"
/ "/"
/ "b" { return "\b"; }
/ "f" { return "\f"; }
/ "n" { return "\n"; }
/ "r" { return "\r"; }
/ "t" { return "\t"; }
/ "u" digits:$(HEX HEX HEX HEX)
{
return String.fromCharCode(parseInt(digits, 16));
}
)
{ return sequence; }
escape = "\\"
unescaped = [^\0-\x1F\x22\x5C]
HEX = [0-9a-f]i