Skip to main content



      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 08:16 Go to next message
Eclipse UserFriend
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 05:07] by Moderator

Re: How to implement such a different ImportedNamespace? [message #1061361 is a reply to message #1060981] Fri, 31 May 2013 06:25 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: How to implement such a different ImportedNamespace? [message #1061788 is a reply to message #1061361] Tue, 04 June 2013 01:13 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 17:17:51 EDT 2025

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

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

Back to the top