2016-01-22 51 views
1

我正在爲Markdown語法編寫XText語法。在降價中,h1可以用#heading來編寫。用於降價的XText語法

所以,標題可以匹配除換行符之外的任何內容。

grammar org.example.domainmodel.DomainModel with org.eclipse.xtext.common.Terminals 

generate domainModel "http://www.example.org/domainmodel/DomainModel" 

DomainModel: 
    (elements += Element)* 
; 

Element: 
    Section1 | Section2 
; 

Section1: 
    '#' name += HEADING '\n' 
; 

Section2: 
    '##' name += HEADING '\n' 
; 

terminal HEADING: (('A'..'Z') | '_'| ('a'..'z') | ('0'..'9') | '-')* ; 

但是這給錯誤爲:

The following token definitions can never be matched because prior tokens match the same input: RULE_INT

而且,標題不能有任何這方面的特殊字符。

寫這個語法的正確方法是什麼?

回答

1

,而不是使用一個新的終端規則標題中,使用已經定義終端規則ID:

Section1: 
    '#' name = ID '\n' 
; 

Section2: 
    '##' name = ID '\n' 
; 
+0

使用ID將不允許在標題中使用的特殊字符。我嘗試使用'terminal HEADING:!('\ n')*;'作爲終端規則。但是它給了我一個錯誤:'以下令牌定義永遠不能匹配,因爲先前的令牌匹配相同的輸入:RULE_ID,RULE_INT,RULE_ANY_OTHER'。 – apoorvam

+0

從您的語法中刪除通用終端。問題出在xtext第一場比賽勝利。在你的情況下,HEADING可以與INT或ID終端規則匹配。 因此,從第一行刪除這個 與org.eclipse.xtext.common.Terminals 也修改您的HEADING終端規則是這樣的: terminal HEADING:(('''''')|'_'|( ''''''')|('0'...'9')|' - ')(('A'..'Z')|'_'|('a'..'z' )|('0'..'9')|' - ')*; – Mohsin