How can I use Xtext to parse languages with semantic whitespace? I'm trying to write a grammar for CoffeeScript and I can't find any good documentation on this.
相关问题
- Correctly parse PDF paragraphs with Python
- Eclipse and Mylyn : how to disable grey files in t
- R: eval(parse()) error message: cannot ope
- Installing Pydev for Eclipse throws error
- Error in Scala Compiler: java.lang.AssertionError:
相关文章
- selenium+eclipse 打开网页时报错
- How do I get from a type to the TryParse method?
- Eclipse failing to open
- Eclipse how can I indent C++ preprocessor macros
- Why is FindBugs ignoring my check for null?
- Eclipse cleanup - what are the “.index” files - ca
- Eclipse plugin to find out unused methods in a cla
- Spring NamespaceHandler issue when launching Maven
Version 2.8 of Xtext comes with support for Whitespace-Aware Languages. This version ships with the "Home Automation Example" that you can use as a template.
AFAIK, you can't.
In case of parsing Python-like languages, you'd need the lexer to emit
INDENT
andDEDENT
tokens. For that to happen, you'd need semantic predicates to be supported inside lexer rules (Xtext'sterminal
rules) that would first check if the current-position-in-line of the next character int the input equals 0 (the beginning of the line) and is a' '
or'\t'
.But browsing through the documentation, I don't see this is supported by Xtext at the moment. Since Xtext 2.0, support has been added for semantic predicates in production rules (see: 6.2.8. Syntactic Predicates), but not in terminal rules.
The only way to do this with Xtext would be to let the lexer produce terminal spaces and line-breaks, but this would make an utter mess of your production rules.
If you want to parse such a language using Java (and a Java oriented parser generator) I'd recommend ANTLR, in which you can emit such
INDENT
andDEDENT
tokens quite easily. But if you're keen on Eclipse integration, then I don't see how you'd be able to do this using Xtext, sorry.For people interested in CoffeeScript, Adam Schmideg has an Eclipse plugin that uses XText.
For people interested in parsing Python-like DSL's in XText, Ralf Ebert's code for Todotext mentioned above is no longer available from Github but you can find it in the Eclipse test repository. See the original thread about this work and the Eclipse issue that was raised about it.
I have been playing with this code today and my conclusion is it no longer works in the current version of XText. When XText is used in Eclipse, I think it does "partial parsing". This is not compatible with the stateful lexer you need to process indentation sensative languages. So I suspect even if you patch the lexer, the Eclipse editor does not work. In the issue, it looks like Ralf proposed patches to address these issues, but looking into the XText source, these changes seem long gone? If I am wrong and someone can get it to work, I would be very interested?
There is a different implementation here but I cannot get that to work with the current version of XText either.
Instead I have switched to parboiled which does supports indentation based grammars out the box.
Here's an example whitespace sensitive language in XText