作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始掌握 Xtext,但在用多个换行符分隔语义部分时仍然遇到一些麻烦。
grammar org.example.dsl.MyDsl hidden(WS)
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate words "http://www.example.org/dsl
Document:
sections+=Paragraph+
;
Paragraph:
lines+=Text+
( -> NL)
;
Text:
value=WordGroup
NL
;
WordGroup: SIMPLE_WORD+;
terminal SIMPLE_WORD:
('0'..'9' | 'a'..'z' | 'A'..'Z')
('0'..'9' | 'a'..'z' | 'A'..'Z' | '-' | '_' | '.')*
;
terminal NL: ('\r'? '\n');
terminal WS: (' ' | '\t');
这没关系...
@Test
def void happyPath() {
val model = parseHelper.parse('''
The quick brown fox
Jumps over the lazy dog
''')
assertThat(model, notNullValue())
assertThat(model.eResource.errors, equalTo(#[]))
assertThat(model.sections.size(), equalTo(1))
assertThat(model.sections.get(0).lines.size(), equalTo(2))
// It works!
}
但这不是...
@Test
def void noTrailingNewlines() {
val model = parseHelper.parse('''
The quick brown fox
Jumps over the lazy dog
''')
assertThat(model, notNullValue())
assertThat(model.eResource.errors, equalTo(#[]))
// Fail ^^^ XtextSyntaxDiagnostic: null:2 mismatched input '<EOF>' expecting RULE_NL
assertThat(model.sections.size(), equalTo(1))
assertThat(model.sections.get(0).lines.size(), equalTo(2))
}
两者都应该是有效的可解析文本,但如果这是找到的最后一个字符,我无法让它接受单个 NL。
我尝试了明显的 ( -> NL?)
...
Paragraph:
lines+=Text+
( -> NL?)
;
...这实际上确实导致测试通过,直到现在我才收到 ANTLR 警告。
正如我所怀疑的那样,这只是把问题推到一边......
@Test
def void multipleParagraphs() {
val model = parseHelper.parse('''
The quick brown fox
Jumps over the lazy dog
But only on days that end in Y
''')
assertThat(model, notNullValue())
assertThat(model.eResource.errors, equalTo(#[]))
assertThat(model.sections.size(), equalTo(2)) //Expected: <2> but: was <1>
}
最佳答案
我们有一个赢家!
非常感谢@christian-dietrich 和@redknite,他们非常耐心地帮助我解决了这个问题。
Document:
sections+=Paragraph
(NL sections+=Paragraph)*
;
Paragraph:
lines+=Text+
;
Text:
value=WordGroup
NL
;
@Test
def void singleParagraph() {
val model = parseHelper.parse('''
The quick brown fox
Jumps over the lazy dog
''')
assertThat(model, notNullValue())
assertThat(model.eResource.errors, equalTo(#[]))
val doc = model.document
assertThat(doc, notNullValue())
assertThat(doc.sections.size(), equalTo(1))
assertThat(doc.sections.get(0).lines.size(), equalTo(2))
}
@Test
def void multiParagraph() {
val model = parseHelper.parse('''
The quick brown fox
Jumps over the lazy dog
But only on days that end in Y
''')
assertThat(model, notNullValue())
assertThat(model.eResource.errors, equalTo(#[]))
val doc = model.document
assertThat(doc, notNullValue())
assertThat(doc.sections.size(), equalTo(2))
assertThat(doc.sections.get(0).lines.size(), equalTo(2))
}
都通过了!
关于antlr - Xtext/ANTLR 中多行文本的终止 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61786533/
我是一名优秀的程序员,十分优秀!