我正在为 Markdown 语法编写 XText 语法。在markdown中,h1可以用#heading
写。
因此,标题可以匹配换行符以外的任何内容。
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
此外,标题不能有任何特殊字符。
编写此语法的正确方法是什么?
不使用新的终端规则 HEADING,而是使用已定义的终端规则 ID:
Section1:
'#' name = ID 'n'
;
Section2:
'##' name = ID 'n'
;