3

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';
hara
  • 3,274
  • 4
  • 37
  • 55
  • I've tried that and I'm getting error "Generated package 'myTerminals' may not be empty" at generate line. How did you avoid that? – mvmn Jan 12 '17 at 13:15

1 Answers1

5

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: '/#' -> '#/'
Aubin
  • 14,617
  • 9
  • 61
  • 84
Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • Hi, Thanks for your reply!I didn't understand: can i have two different xtext files one defining the grammar and the other one the grammar's terminals in the same xtext project? I tried the third solution...with two xtext files in the same project i get an exception(see edit). – hara Apr 14 '11 at 09:20
  • You have to make sure that the grammar file names follow the convention for Java classes. That is, your org.xvr.language.sh.ShTerminal must reside in a java source folder in the package org.xvr.language.sh and the file must be named ShTerminal.xtext. – Sebastian Zarnekow Apr 14 '11 at 09:42
  • yes..the file reside in the package org.xvr.language.sh as the grammar and it is called ShTerminals.xtext..what could be the probelm? – hara Apr 14 '11 at 15:07
  • Your grammar is called ShTerminals but you refer to it as ShTerminal (without the s). That should cause the problem. – Sebastian Zarnekow Apr 15 '11 at 07:21