XText: use custom terminals definitions

hara picture hara · Apr 14, 2011 · Viewed 7.4k times · Source

I'm totally new to XText.

When you define a grammar using XText you could specify a second grammar and use the definitions it declares as it is said here:

grammar org.eclipse.xtext.example.Domainmodel with org.eclipse.xtext.common.Terminals

In Xtext each grammar has a unique name, which like public Java classes needs to reflect the location of the file within the Java classpath. In our case the grammar file is located in /org/eclipse/xtext/example/Domainmodel.xtext therefore the name of the grammar is org.eclipse.xtext.example.Domainmodel. The second part of that statement ( with org.eclipse.xtext.common.Terminals) states, that this grammar reuses and overrides rules from the specified grammar. The org.eclipse.xtext.common.Terminals is a library grammar shipped with Xtext and predefines the most common terminal rules

I'm developing an XText plugin and i would like to define my own terminal symbols in a separated file. Is it possible? How can i do that?

I tried both to create a new Xtext file and append it after org.eclipse.xtext.common.Terminals and to add just the mine but both solutions don't compile.

Thanks.

EDIT

If i use two xtext files in the same project, one for the grammar and one for the grammar's terminals i get the following exception launching the mwe2 file:

java.lang.IllegalStateException: Problem parsing 'classpath:/org/xvr/language/sh/ShaderDsl.xtext':[XtextLinkingDiagnostic: null:1 Couldn't resolve reference to Grammar 'org.xvr.language.sh.ShTerminal'., XtextLinkingDiagnostic: null:9 Couldn't resolve reference to AbstractRule 'ID'., TransformationDiagnostic: null:14 Cannot create datatype INVARIANT (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:17 Cannot create datatype PRECISION (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:19 Cannot create datatype HIGH_PRECISION (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:20 Cannot create datatype MEDIUM_PRECISION (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:21 Cannot create datatype LOW_PRECISION (ErrorCode: NoSuchTypeAvailable)]

the two xtext files are:

the grammar

grammar org.xvr.language.sh.ShaderDsl with org.xvr.language.sh.ShTerminal //org.eclipse.xtext.common.Terminals

generate shaderDsl "http://www.xvr.org/language/sh/ShaderDsl"
....

and the grammar's terminals

grammar org.xvr.language.sh.ShTerminals with org.eclipse.xtext.common.Terminals
generate shTerminals "http://www.xvr.org/language/sh/ShTerminals"

terminal Test : 'test';

Answer

Sebastian Zarnekow picture Sebastian Zarnekow · Apr 14, 2011

You have plenty of options (all of them are documented in the online help):

  1. Define your terminal rules right in the grammar that your are currently working with.
  2. Create a new dedicated grammar for terminal symbols similar to what we did with the common.Terminals. Use that one instead of the common Terminals.
  3. Create a new dedicated grammar for terminal symbols, reuse the common.Terminals in that grammar and use your own terminal grammar in your actual language.

I'd recommend to just override the terminals that you want to change right in your language (option 1) or if your want to define multiple languages with the same set of terminals I'd use (option 3) or combine both options, e.g.

grammar org.mycompany.MyTerminals with org.eclipse.xtext.common.Terminals

terminal ID: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal SL_COMMENT: '--' !('\n'|'\r')* ('\r'? '\n')?;

==

grammar org.mycompany.MyLanguage with org.mycompany.MyTerminals

MyModel: name=ID other=ANOTHER;

terminal ANOTHER: '/#' -> '#/'