Skip to content

Commit c280547

Browse files
committed
Catch duplicate attributes
1 parent 02586bf commit c280547

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

examples/xml.peggy

+14-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ function clumpStrings(vals) {
2828
}
2929
return ret
3030
}
31+
32+
function convertAttr(attr, error) {
33+
const ret = {}
34+
for (const {name, value, loc} of attr) {
35+
if (ret[name]) {
36+
error(`Duplicate attribute "${name}"`, loc)
37+
}
38+
ret[name] = value
39+
}
40+
return ret
41+
}
3142
}}
3243
{
3344
const names = []
@@ -173,13 +184,14 @@ element
173184
STag = '<' name:pushName attr:(S @Attribute)* S? '>' { return {
174185
type: 'element',
175186
name,
176-
attr,
187+
attr: convertAttr(attr, error),
177188
}}
178189

179190
Attribute = name:Name Eq value:AttValue { return {
180191
type: 'attribute',
181192
name,
182193
value,
194+
loc: location(),
183195
}}
184196
ETag = '</' popName S? '>'
185197

@@ -210,7 +222,7 @@ content = c1:CharData? children:((element / Reference / CDSect / PI / Comment) C
210222
EmptyElemTag = '<' name:Name attr:(S @Attribute)* S? '/>' { return {
211223
type: 'element',
212224
name,
213-
attr,
225+
attr: convertAttr(attr, error),
214226
}}
215227

216228
// Elements in the DTD

0 commit comments

Comments
 (0)