Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using XPath for AST querying
Using XPath for AST querying [message #1722085] Tue, 02 February 2016 16:46 Go to next message
Alexandr Petrov is currently offline Alexandr PetrovFriend
Messages: 1
Registered: February 2016
Junior Member
Hello,

I'm trying to implement code generator for DSL. I write grammar in XText and now write generator with Xtend.

I think, it would be useful to have ability to find some AST nodes in declarative style, e.g. by XPath query. Now I have to write some recursive functions to traverse within AST..

Sample code, that I want to simplify with XPath:

 @Inject
 private IQualifiedNameProvider nameProvider;
 
 private boolean flagObjectParent = false;
 private QualifiedName origQualName;
 private EObject origObject;
 private String qualNameString = "";
 /**
  * Получить строку fullQualifiedName с разделителем "_".
  * @param obj узел direct_declarator для переменной или функции в AST-дереве
  * @return Строка fullQualifiedName (разделитель "_") для переданного direct_declarator
  */ 
 def public String getQualifiedName(EObject obj) {
  if (!flagObjectParent) {
   qualNameString = "";
   origObject = obj;
   origQualName = nameProvider.getFullyQualifiedName(obj);
   flagObjectParent = true;
  }
  if ((obj != null) && ((origObject.eContainer).eContainer instanceof init_declarator)) {
   if (obj instanceof function_definition) {
    for (segmIterator: origQualName.segments) {
     if (segmIterator == origQualName.getSegment(origQualName.segmentCount-1))
      qualNameString = qualNameString.concat("_" + obj.decl.dir_decl.get_var_decl.name);
     qualNameString = qualNameString.concat("_" + segmIterator); 
    }
    flagObjectParent = false;
    return qualNameString.substring(1);
   }
   else {
    getQualifiedName(obj.eContainer);
   }
  } else {
   flagObjectParent = false;
   return origQualName.toString("_");
  }
 }


I couldn't find any sample of using XPath for such task. Might be somebody know it?

Thanks,
Alexander
Re: Using XPath for AST querying [message #1725612 is a reply to message #1722085] Fri, 04 March 2016 23:10 Go to previous messageGo to next message
Harald Weiner is currently offline Harald WeinerFriend
Messages: 23
Registered: January 2015
Location: Linz, Austria
Junior Member
I am not sure if this is possible, but I think it would require some work-arounds. Xtext is parsing a text file using the ANTLR tool to read in the file and returns EMF EObjects and EMF resources. They are more Java-like. Only the serialization (saving the EMF objects on the hard disk) might use the XMI (XML intermediate) file format. Also the meta-models of these models themselves are stored as XMI files. So you might parse text files with Xtext and save the resulting EMF objects as XMI files. And then you can query the XMI files with XPath. There is also a dedicated query language for EMF, called EMFIncquery, maybe you would like to take a look on it at http://wiki.eclipse.org/VIATRA/Query
Re: Using XPath for AST querying [message #1725677 is a reply to message #1725612] Sun, 06 March 2016 17:16 Go to previous message
Harald Weiner is currently offline Harald WeinerFriend
Messages: 23
Registered: January 2015
Location: Linz, Austria
Junior Member
Yeah, just found it in the Emf eclipse modeling framework book (steinberg et al.) at page 487/488 in the sub-chapter DOM conversion: persist (save the resulting xtext model to a xml-resource and then invoke the getDOMHelper function of the XMLResource. Do something like:
ResourceSet rs = new ResourceSetImpl (); 
XmlResource res = (XmlResource) rs.createResouce (uri); 
res.getContents ().add (myModel); 
Map options = new HashMap (); 
options.put (XMLResource.OPTIONS_KEEP_DEFAULT_CONTENT, Boolean.TRUE); 
Document doc = res.save (null, options, null); 
DOMHelper helper = res.getDOMHelper (); 
String query = "//foo [@bar > 50]"; 
NodeList nodes = XPathAPI.selectNodeList (doc, query); 
...
Previous Topic:Add additional code to inferred code in a method body
Next Topic:Create xmi file in runtime through saving
Goto Forum:
  


Current Time: Sat Apr 20 02:26:19 GMT 2024

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

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

Back to the top