Can I remove things from AST [message #886539] |
Fri, 15 June 2012 06:24 |
|
I want to support conditional compiling in my language without using pre-processor.
What I did is to remove all things in the inactive area from the AST before the AST is linked.
as the code showes
protected void beforeModelLinked(EObject model,
IDiagnosticConsumer diagnosticsConsumer) {
for(TreeIterator<EObject> it = model.eAllContents();it.hasNext();){
EObject obj = it.next();
if(obj instanceof ConditionalElement){
String target = ((ConditionalElement) obj).getTarget();
if(defined((ConditionalElement) obj,target)){
((ConditionalElement) obj).getFalseElements().clear();
}else{
((ConditionalElement) obj).getTrueElements().clear();
}
}
}
super.beforeModelLinked(model, diagnosticsConsumer);
}
But sometimes when I edit the file, I will get exception as following. Especially when I change "#define B" to "#define BC", I will meet this problem.
org.eclipse.emf.common.util.BasicEList$BasicIndexOutOfBoundsException: index=1, size=0
at org.eclipse.emf.common.util.BasicEList.get(BasicEList.java:352)
at org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImpl.hasNext(EContentsEList.java:484)
at org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImpl.next(EContentsEList.java:565)
at org.eclipse.emf.common.util.AbstractTreeIterator.next(AbstractTreeIterator.java:139)
at org.xtext.example.mydsl.MyDslLinker.beforeModelLinked(MyDslLinker.java:34)
The grammar I used is:
Model:
elements += Element*
;
Element:
Type
| Var
| ConditionalElement
| ConditionalControl
| Expr
;
Type:
'type' name = ID
;
Var:
name = ID ':' type = [Type]
;
ConditionalElement:
'#ifdef' target = ID
trueElements += Element *
(
'#else'
falseElements += Element *
)?
'#endif'
;
ConditionalControl:
'#define' name = ID
;
Expr:
'expr' var = [Var]
;
Unused :
t = [ConditionalControl]
;
and the test.mydsl is:
type int
i : int
expr i
#define B
#ifdef B
i1 : int
#else
i2 : intttt
#endif
#ifdef B
expr i2
expr x
#else
expr i11111
expr x
#endif
[Updated on: Fri, 15 June 2012 06:53] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02859 seconds