Quantico
Quantico

Reputation: 2446

define a terminal that is a subset of ID in xtext

I would like to to create a terminal that is somewhat can be matched to ID, but not fully. While ID is

terminal ID         : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;

the terminal I would like to define is

terminal TYPE: (('a'..'z'|'A'..'Z')?('a'..'z'|'A'..'Z'|'_'|'0'..'9')*)?

because TYPE can match ID I am getting RULE_ID errors, what can I do in that case?

______EDIT__________

Domainmodel :
    (elements+=XType)*;

terminal TYPE: ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

MyID: 
    TYPE | ID
;

XType:
    DataType | Entity;

DataType:
    'datatype' name=MyID;

Entity:
    'entity' name=MyID ('extends' superType=[Entity])? '{'
        (features+=Feature)*
    '}';

Feature:
    (many?='many')? name=MyID ':' type=[XType];

Model (base on the blog example)

datatype String

entity Blog {
    title: String
    title2: String
    many posts: Post
    many Posts: Post
}

entity HasAuthor {
    author: String
}

entity Post extends HasAuthor {
    title: String
    content: String
    many comments: Comment
}

entity Comment extends HasAuthor {
    content: String
}

Upvotes: 1

Views: 149

Answers (1)

Christian Dietrich
Christian Dietrich

Reputation: 11868

You can introduce a Datatype rule

MyID: ID | TYPE;

And a value converter for MyID and use that at the places you used ID

Or you forget type and do the check for the restricted range inside the validator

Domainmodel :
    (elements+=XType)*;

terminal TYPE: ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

MyID: 
    TYPE | ID
;

XType:
    DataType | Entity;

DataType:
    'datatype' name=MyID;

Entity:
    'entity' name=MyID ('extends' superType=[Entity|MyID])? '{'
        (features+=Feature)*
    '}';

Feature:
    (many?='many')? name=MyID ':' type=[XType|MyID];

Upvotes: 1

Related Questions