问题
I am writing a simple little DSL for specifying constraints on messages, and Have been trying without success for a while to embed XBase expressions into the language.
The Grammar looks like this:
grammar org.xtext.businessrules.BusinessRules with org.eclipse.xtext.xbase.Xbase
//import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
generate businessRules "http://www.xtext.org/businessrules/BusinessRules"
Start:
rules+=Constraint*;
Constraint:
{Constraint}
'FOR' 'PAYLOAD' payload=PAYLOAD 'ELEMENT' element=ID 'CONSTRAINED BY' constraint=XExpression;
PAYLOAD:
"SimulationSessionEvents"
|"stacons"
|"any"
;
Range:
'above' min=INT ('below' max=INT)?
|'below' max=INT ('above' min=INT)?
;
When trying to parse a file such as:
FOR PAYLOAD SimulationSessionEvents ELEMENT matrix CONSTRAINED BY ...
I can't get it to work for ... = any kind of Arithmetic expression, although it works for ...= loop or if expression, or even just a number. As soon as I do something like '-5' or '4-5' it says Couldn't resolve reference to JvmIdentifiableElement '-'
, even though the Xbase.xtext Grammar looks like it allows these expressions.
I don't think I'm missing any Jars, as it doesn't complain when I run the mwe workflow, but only when trying to parse the input file.
Any help would be much appreciated.
edit: The buildpath for the project is the following:
C:\Program Files\eclipse\plugins\org.eclipse.xtext_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.ecore.xmi_2.7.0.v20120127-1122.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.ecore_2.7.0.v20120127-1122.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.common_2.7.0.v20120127-1122.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtext.util_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\com.google.guava_10.0.1.1.jar
C:\Program Files\eclipse\plugins\com.google.inject_3.0.0.no_aop.jar
C:\Program Files\eclipse\plugins\javax.inject_1.0.0.v20091030.jar
C:\Program Files\eclipse\plugins\org.antlr.runtime_3.2.0.v201101311130.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.mwe.core_1.2.1.v201112070431.jar
C:\Program Files\eclipse\plugins\org.apache.commons.cli_1.2.0.v201105210650.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.mwe2.runtime_2.2.0.v201112070431.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.mwe.utils_1.2.1.v201112070431.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtext.xbase_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtext.common.types_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\org.apache.log4j_1.2.15.v201012070815.jar
C:\Program Files\eclipse\plugins\org.apache.commons.logging_1.0.4.v201101211617.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtext.generator_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtend_1.1.0.v201108020519.jar
C:\Program Files\eclipse\plugins\com.ibm.icu_4.4.2.v20110823.jar
C:\Program Files\eclipse\plugins\org.eclipse.xpand_1.1.0.v201108020519.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtend.typesystem.emf_1.0.1.v201108020519.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.codegen.ecore_2.7.0.v20120130-0943.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.codegen_2.6.0.v20120130-0943.jar
C:\Program Files\eclipse\plugins\de.itemis.xtext.antlr_2.0.0.v201108011202.jar
C:\Program Files\eclipse\plugins\org.antlr.generator_3.2.0.v201108011202.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.mwe2.lib_2.2.0.v201112070431.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtext.xbase.lib_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\org.eclipse.xtend2.lib_2.2.1.v201112130541.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.mwe2.launch_2.2.0.v201112071250.jar
C:\Program Files\eclipse\plugins\org.eclipse.emf.mwe2.language_2.2.0.v201112071250.jar
回答1:
You have to put the Xtend library on the classpath of the project that contains the DSL file.
Select 'Build Path' from the context menu and choose 'Add libraries'. Pick the Xtend library and it should work for you.
回答2:
Problem fixed, it turns out you need to include the xbase.lib jar on the build path of the project that actually declares models of your DSL.
来源:https://stackoverflow.com/questions/11204094/how-to-embed-xbase-expressions-in-an-xtext-dsl