【发布时间】:2015-10-29 06:20:16
【问题描述】:
我正在尝试编写一个 xtext 解析器来解析一种简单的标记语言。标记使用双字符来设置文本样式。 !!用于粗体。我正在努力研究如何创建语法,特别是如何处理双字符符号。举个例子:
The following text !!is bold! !! but not this.
我想将其解析为以下 AST:
- 线
- 线路
- 文本“以下文本”
- BoldText“是粗体!”
- 文本“但不是这个。”
- 线路
有人有什么好的方法吗?
我应该使用:
terminal BOLD: '!!'
或
Bold : '!' '!'
我在想我必须使用第二条规则。为了处理这个问题,我必须有单字符终端,然后对所有内容使用解析器规则。
我现在的语法是:
grammar org.xtext.example.mydsl.MyDsl
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Lines:
lines+=Line*
;
Line:
{Line} content+=(PlainText|BoldText)*
NL
;
PlainText:
text = Text
;
Text returns ecore::EString:
(CHAR|WS)+
;
BoldText:
BOLD
{BoldText} text += PlainText*
BOLD
;
terminal BOLD: '!!';
terminal WS: (' ' | '\t')+;
terminal NL: '\r'? '\n';
terminal CHAR: !(' '|'\t'|'\r'|'\n');
但是这会收到警告,因为它可以匹配 Text 中的 PlainText OR (CHAR|WS)+ 的重复,我不知道如何摆脱它?
【问题讨论】:
-
我忘了说我需要捕获空白并在新行上分割。
标签: java compiler-construction antlr xtext