我正在嘗試爲OkudaKit寫一個XML語法。我注意到捆綁的HTML語法適用於簡單的XML,但是由於命名空間元素或屬性失敗,所以我想要做的第一件事就是添加對這些元素或屬性的支持。使用HTML語法爲模板,我定義ns
,然後將其添加到tagName
和attrName
:OkudaKit的XML語法
@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 grammar guide中找到它。
它像一個冠軍。非常感謝您在OkudaKit&ParseKit上的工作。 – alexantd
嗨託德,非常感謝ParseKit&OkudaKit!我正在從Python [* Lexical Analysis *](http://docs.python.org/release/2.7.3/reference/lexical_analysis.html)和[* Full Grammar *](http:/ /docs.python.org/reference/grammar.html),但每次我嘗試解析'allMatchesFor:'(在'PKTrack。m''調用'throwTrackExceptionWithPreviousState:parser:',這會引發'Track Exception',其原因如下:'After:1010 6411 ... 3232 ... \ n Expected:Symbol = \ n Found:61'。我嘗試使用示例javascript.grammar和.css沒有運氣。任何想法我可能做錯了什麼? – chown
@chown,也許嘗試關閉'#define USE_TRACK'。 –