我正在尝试为 OkudaKit 编写 XML 语法。我注意到捆绑的 HTML 语法适用于简单的 XML,但对于命名空间元素或属性会失败,所以我要做的第一件事就是添加对这些的支持。使用 HTML 语法作为模板,我定义ns
并将其添加到tagName
and attrName
:
@multiLineComments = '<!--' '-->';
@commentState = '<';
@commentState.fallbackState = delimitState;
@delimitedString = '<?' '?>' nil;
@delimitedString = '<!DOCTYPE' '>' nil;
@delimitState.fallbackState = symbolState;
@start = any*;
any = element | text | doctype | pi | comment;
pi = DelimitedString('<?', '?>');
doctype = DelimitedString('<!DOCTYPE', '>');
element = emptyTag | startTag elementContent* endTag;
elementContent = element | text | comment | pi;
text = /[^<]+/;
emptyTag = lt tagName attr* fwdSlash gt;
startTag = lt junk? tagName attr* gt;
endTag = lt fwdSlash tagName gt;
ns = Word colon;
tagName = ns? Word;
attr = attrName eq attrValue;
attrName = ns? Word;
attrValue = QuotedString;
eq = '=';
lt = '<';
gt = '>';
fwdSlash = '/';
colon = ':';
comment = Comment;
似乎它应该可以工作(如果我正确理解“?”,我可能不会)但是输出搞砸了。下面是我的测试文档:
<?xml version="1.0" encoding="utf-8"?>
<test cats:dogs="television">
<peas vegetable="box" >
<orange />
<!-- the following makes no sense -->
<blue lion:mouse="cold"/>
<red car:desk="apple">
< envelope></ envelope>
<![CDATA[lorem ipsum]]>
<dwarves>
<dwarf>Sleepy</dwarf>
<dwarf>Dopey</dwarf>
</dwarves>
</red>
</peas>
</test>
这就是我损坏的语法破坏它之后的样子(忽略格式,那是 NSXMLDocument 的漂亮打印):
<?xml version="1.0" encoding="utf-8"?>
< :cats=dogstest"television">
< =vegetable"box"peas>
<>orange</>orange
<!-- the following makes no sense -->
< :lion=mouseblue"cold"></>blue
< :car=deskred"apple">< envelope></ envelope> lorem ipsum<>dwarves
<>dwarfSleepy</>dwarf
<>dwarfDopey</>dwarf
</>dwarves
</>red
</>peas
</>test
我正在解释“?” 意思是“可选”,但我认为这是错误的。我没有在ParseKit 语法指南中找到它。