Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Access AST generated in Xtexteditor
icon5.gif  Access AST generated in Xtexteditor [message #1444013] Mon, 13 October 2014 14:32 Go to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
I would like to have access to the AST generated for the written code in myDSL language for example to traverse the AST.
or if I want to traverse the AST how can I traverse it?

Thanks
Re: Access AST generated in Xtexteditor [message #1444020 is a reply to message #1444013] Mon, 13 October 2014 14:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

depends from which context you want to do this. e.g.
http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/

can you elaborate when/how/in whichz context you want to do the traversal?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Access AST generated in Xtexteditor [message #1444060 is a reply to message #1444020] Mon, 13 October 2014 15:27 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
assuming I have a language mydsl and I ran the Xtext project in Eclipse then I wrote a file.mydsl
After I save my file I want to traverse the AST of the grammar of mydsl generated for the code written in file.mydsl
Re: Access AST generated in Xtexteditor [message #1444093 is a reply to message #1444060] Mon, 13 October 2014 16:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi this is what the I generator hook is for. Simply step through the xtext tutorial

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Access AST generated in Xtexteditor [message #1444539 is a reply to message #1444093] Tue, 14 October 2014 07:57 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
I have read most of the turorial but I just couldn't get where exactly can I print the AST tree in a text file... I am sorry I am a newbie in Xtext and need a push Smile
Re: Access AST generated in Xtexteditor [message #1444589 is a reply to message #1444539] Tue, 14 October 2014 09:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i am not sure what you mean by "print"

have a look at the generator from the tutorial

Writing a Code Generator With Xtend
package org.example.domainmodel.generator
 
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import org.example.domainmodel.domainmodel.Entity
import org.example.domainmodel.domainmodel.Feature
import org.eclipse.xtext.naming.IQualifiedNameProvider
 
import com.google.inject.Inject
 
class DomainmodelGenerator implements IGenerator {
 
  @Inject extension IQualifiedNameProvider
 
  override void doGenerate(Resource resource, IFileSystemAccess fsa) {
    for(e: resource.allContents.toIterable.filter(Entity)) {
      fsa.generateFile(
        e.fullyQualifiedName.toString("/") + ".java",
        e.compile)
    }
  }
 
  def compile(Entity e) ''' 
    «IF e.eContainer.fullyQualifiedName != null»
      package «e.eContainer.fullyQualifiedName»;
    «ENDIF»
    
    public class «e.name» «IF e.superType != null
            »extends «e.superType.fullyQualifiedName» «ENDIF»{
      «FOR f:e.features»
        «f.compile»
      «ENDFOR»
    }
  '''
 
  def compile(Feature f) '''
    private «f.type.fullyQualifiedName» «f.name»;
    
    public «f.type.fullyQualifiedName» get«f.name.toFirstUpper»() {
      return «f.name»;
    }
    
    public void set«f.name.toFirstUpper»(«f.type.fullyQualifiedName» «f.name») {
      this.«f.name» = «f.name»;
    }
  '''
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Access AST generated in Xtexteditor [message #1444609 is a reply to message #1444589] Tue, 14 October 2014 10:00 Go to previous messageGo to next message
Eleanor Richie is currently offline Eleanor RichieFriend
Messages: 125
Registered: August 2014
Senior Member
Can I get the AST from XtextEditor and Xtextresource and Xtextdocument and use readonly? if no, how can I use readonly ?
Re: Access AST generated in Xtexteditor [message #1444621 is a reply to message #1444609] Tue, 14 October 2014 10:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi I still do not know what your starting point is. What is the trigger for using the ast. I showed you two hooks (command via rightklick and via builder on save

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Access AST generated in Xtexteditor [message #1444637 is a reply to message #1444621] Tue, 14 October 2014 10:41 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
p.s. and of course you can call IXtextDocument.readonly but you want have to if you have already a resource as you have in the igenerator

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Can Xtext support System Verilog
Next Topic:How to transform my dsl to more target languages?
Goto Forum:
  


Current Time: Fri Apr 19 21:28:14 GMT 2024

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

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

Back to the top