Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Can I remove things from AST
icon5.gif  Can I remove things from AST [message #886539] Fri, 15 June 2012 06:24 Go to next message
Kevin Sun is currently offline Kevin SunFriend
Messages: 32
Registered: August 2010
Location: China
Member

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

Re: Can I remove things from AST [message #886574 is a reply to message #886539] Fri, 15 June 2012 07:43 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Kevin,

you should not try to modify the semantic model in the linker. That will
cause all sorts of undesired side effects. I'd recommend to filter the
linking errors or things like that instead of removing elements from the
AST.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 15.06.12 08:24, schrieb Kevin Sun:
> 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:
>
> 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
>
Re: Can I remove things from AST [message #886715 is a reply to message #886574] Fri, 15 June 2012 14:12 Go to previous message
Kevin Sun is currently offline Kevin SunFriend
Messages: 32
Registered: August 2010
Location: China
Member

Thanks for your reply. I will try to filter error messages as you said. And, by making all named elements in the inactive parts has no qualified name, I can make them unreferencable by others.
Previous Topic:How to add Annotations to the grammar
Next Topic:QualifiedName resolving
Goto Forum:
  


Current Time: Thu Apr 25 23:15:30 GMT 2024

Powered by FUDForum. Page generated in 0.02709 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top