Add example grammars for XML and source-mapping:mapping#196
Add example grammars for XML and source-mapping:mapping#196hildjj merged 1 commit intopeggyjs:1.3from
Conversation
| const loc = location() | ||
| loc.start.offset -= n.length; | ||
| loc.start.column -= n.length; | ||
| error(`Expected end tag "${other}" but got "${n}"`, loc) |
There was a problem hiding this comment.
This is the only interesting part of the grammar, other than figuring out what the XML spec means exactly by -.
examples/source-mappings.peggy
Outdated
| } | ||
|
|
||
| source_fields = source:field source_line:field source_col:field name:field? { | ||
| const ret = { source, source_line, source_col } |
There was a problem hiding this comment.
If you add this field then the final object will enumerate properties in the same order as they is defined in the parsed string
| const ret = { source, source_line, source_col } | |
| const ret = { gen_col: 0, source, source_line, source_col } |
examples/xml.peggy
Outdated
|
|
||
| // Character Data | ||
|
|
||
| CharData = value:$(!']]>' [^<&])* { return value ? { |
There was a problem hiding this comment.
Probably it is better to use named rule unless this grammar is not a relatively straightforward rewrite of the grammar from the standard.
| CharData = value:$(!']]>' [^<&])* { return value ? { | |
| CharData = value:$(!CDEnd [^<&])* { return value ? { |
There was a problem hiding this comment.
Done, and replaced all other ']]>' as well.
examples/xml.peggy
Outdated
| function convertAttr(attr, error) { | ||
| const ret = {} | ||
| for (const {name, value, loc} of attr) { | ||
| if (ret[name]) { | ||
| error(`Duplicate attribute "${name}"`, loc) | ||
| } | ||
| ret[name] = value | ||
| } | ||
| return ret | ||
| } |
There was a problem hiding this comment.
Because you always supply the same argument to the 2nd parameter it is better just to declare this function in the per-parse initializer.
examples/xml.peggy
Outdated
| loc.start.offset -= n.length; | ||
| loc.start.column -= n.length; | ||
| error(`Expected end tag "${other}" but got "${n}"`, loc) | ||
| } { return n } |
There was a problem hiding this comment.
You can use plucking together with semantic predicates, maybe we should show that capability here:
// Returns result of the `Name` if `&{...}` matched
popName = @n:Name &{ ... }There was a problem hiding this comment.
I really like this. Done.
|
@Mingun let me know when you're done and I'll push changes. |
Mingun
left a comment
There was a problem hiding this comment.
Maybe you should add some comments to source-mappings.peggy that explains some tricks with bits. In any case, LGTM
|
Added some comments and did a slight rework of source-mappings to make it a little clearer. |
To understand source-maps better, I wrote a grammar for the "mapping" field a few weeks ago. In the discussion about #194, I realized we didn't have an XML grammar, so I whipped one up from the spec text.