Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to implement such a different ImportedNamespace?
How to implement such a different ImportedNamespace? [message #1060981] Wed, 29 May 2013 12:16 Go to next message
Matthew Liu is currently offline Matthew LiuFriend
Messages: 9
Registered: March 2013
Junior Member
Hi all,

Xtext documentation (15 Minutes Tutorial) gives an example to implement the following language (the main purpose is to show how to use importedNamespace to support 'Packages" and 'Import').


    // datatypes.dmodel, the first file
    datatype String

    // commons.dmodel, the second file
    package my.company.common {
       entity HasAuthor {
          author: String
       }
    }


    // blogs.dmodel, the third file
    package my.company.blog {
       import my.company.common.*
       entity Blog {
          title: String
          many posts: Post
       }
       entity Post extends my.company.common.HasAuthor {
          title: String
          content: String
          many comments: Comment
       }
       entity Comment extends HasAuthor {
          content: String
       }
    }


The following is the full grammar.

grammar org.example.domainmodel.Domainmodel with
    org.eclipse.xtext.common.Terminals
     
    generate domainmodel "www.example.org/domainmodel/Domainmodel"
     
    Domainmodel:
       (elements += AbstractElement)*
    ;
     
    PackageDeclaration:
        'package' name = QualifiedName '{'
           (elements += AbstractElement)*
        '}'
    ;
     
    AbstractElement:
        PackageDeclaration | Type | Import
    ;
     
    QualifiedName:
        ID ('.' ID)*
    ;
     
    Import:
       'import' importedNamespace = QualifiedNameWithWildcard
    ;
    QualifiedNameWithWildcard:
        QualifiedName '.*'?
    ;
    Type:
        DataType | Entity
    ;
    DataType:
        'datatype' name=ID
    ;
    Entity:
      'entity' name = ID ('extends' superType = [Entity | QualifiedName])? '{'
         (features += Feature)*
      '}'
    ;
    Feature:
        (many ?= 'many')? name = ID ':' type = [Type | QualifiedName]
    ;



However, I want my language to support the following 'Import' mechanism.
import * from my.company.common // equivalent to "import my.company.common.*"
import my.company.common // no '.*', equivalent to "import my.company.common.*"
import HasAuthor from my.company.common // equivalent to "import my.company.common.HasAuthor", that is, only "HasAuthor" is imported

Could anyone please give some advices on how to implement this kind of 'Import' mechanism? Thanks in advance!

Best Regards,
Matthew

[Updated on: Sun, 02 June 2013 09:07]

Report message to a moderator

Re: How to implement such a different ImportedNamespace? [message #1061361 is a reply to message #1060981] Fri, 31 May 2013 10:25 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

if I see it correctly, this is matter of binding your own ImportedNamespaceAwareLocalScopeProvider and override the method internalGetImportedNamespaceResolvers. Your imports are just a syntactic variant of the normal imports. So adapting the grammar along the following line should be a way to go

Import: (lastSegment=('*'|ID))? 'from' qname=QualifiedName;

In the above methods you simple callect all relevant Import objects and create the corresponding QualifiedName with potential wild card.

A datatype rule

Import: (ID|'*')? 'from' QualifiedName;

might be an option as well. In this case you would not have to adapt the scope provider, but you will have to register a value converter turning the String into a properly formed qualified name with wild card.

At the moment, I would tend to take option one.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: How to implement such a different ImportedNamespace? [message #1061788 is a reply to message #1061361] Tue, 04 June 2013 05:13 Go to previous message
Matthew Liu is currently offline Matthew LiuFriend
Messages: 9
Registered: March 2013
Junior Member
Hello Alex,

Thanks a lot for your advice!

Best Regards,
Matthew Liu
Previous Topic:how to use xtext features in a html textarea
Next Topic:Errors after upgrading from Xtext 2.3 to 2.4
Goto Forum:
  


Current Time: Fri Apr 26 04:58:54 GMT 2024

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

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

Back to the top