Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » using reserved keyword in grammar
using reserved keyword in grammar [message #1757939] Wed, 22 March 2017 06:41 Go to next message
Henry Henry is currently offline Henry HenryFriend
Messages: 4
Registered: January 2015
Junior Member
Hi, I am having issues with my grammar whereby I need to consider keywords as valid name (due to some legacy code reason). After searching in some forums I come to conclusion that I need to use KEYWORDID where KEYWORDID contains ID rule and all possible keywords that can also be used as a name.

Example below: the third attribute: "date" is used as a name as well as a possible type.

entity SampleEntity {
	attribute user:string "User"
        attribute startDate:date "Start date"
	attribute date:string "Implementation Date"
}



However, when I start adding 'date' into KEYWORDID rule, xtext complains "Decision can match input such as "'date'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input"

Any suggestions how I can overcome this problem?

My sample grammar is as defined below:

grammar com.MyEntity with org.eclipse.xtext.common.Terminals

generate myentity "http://myentity.com/MyEntity"

/***** MAIN RULES *****/
EntityModel:
	'package' name=FullyQualifiedName
	imports+=(Import)*
	declarations+=(Declaration)*;

Declaration:
	Enum | Entity;

Import:
	'import entity' importedNamespace=FullyQualifiedName;

Enum:
	'enum' name=KEYWORDID '{'
	(enumTypes+=EnumTypes)*
	'}';

EnumTypes:
	name=KEYWORDID ':' enumDescription=STRING;

Entity:
	'entity' name=KEYWORDID ('extends' superType=[Entity|FullyQualifiedName])? '{'
	(attributes+=Attribute)*
	'}';

Attribute:
	'attribute' name=KEYWORDID (':' type=ArrayOrNonArrayDataType)?;

ArrayOrNonArrayDataType:
	nonArrayType=DataType | '[' arrayType=DataType ']';
	
DataType:
	declRef=[Declaration|FullyQualifiedName] | prim=PrimitiveType;
	
enum PrimitiveType:
	STRING='string' | INTEGER='int' | BOOLEAN='boolean' | DATE='date' | DATETIME='datetime' | DOUBLE='double';

/***** PREDEFINED DATATYPE AND ENUM*****/

FullyQualifiedName:
	KEYWORDID ('.' KEYWORDID)*;

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

KEYWORDID:
	ID | 'attribute' |
	'entity' |
	'enum' |
	'false' |
	'import' |
	'module' |
	'optional' |
	'true' | 'date'; 

[Updated on: Wed, 22 March 2017 06:46]

Report message to a moderator

Re: using reserved keyword in grammar [message #1757940 is a reply to message #1757939] Wed, 22 March 2017 06:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
hi,

the problem is here:

DataType:
declRef=[Declaration|FullyQualifiedName] | prim=PrimitiveType
;

=> Date is a valid FullyQualifiedName and a valid PrimitiveType

you need to handle that e.g.

DataType:
	declRef=[Declaration|FullyQualifiedNameForDeclRef] | prim=PrimitiveType
	;

FullyQualifiedNameForDeclRef:
	KEYWORDID_NONPRIMITIVE ('.' KEYWORDID)*
;

KEYWORDID_NONPRIMITIVE:
	ID | 'attribute' |
	'entity' |
	'enum' |
	'false' |
	'import' |
	'module' |
	'optional' |
	'true'
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: using reserved keyword in grammar [message #1757941 is a reply to message #1757939] Wed, 22 March 2017 06:55 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
just use the normal "ID" out-of-the-box.
To enter a keyword as ID you have to prefix it with a "^":
enum ^date

use the validation to protect from unwanted keywords
Re: using reserved keyword in grammar [message #1757942 is a reply to message #1757941] Wed, 22 March 2017 07:04 Go to previous message
Henry Henry is currently offline Henry HenryFriend
Messages: 4
Registered: January 2015
Junior Member
Thanks a lot Christian! It works!
Previous Topic:Xtend Graph Transformation
Next Topic:Cross Reference to nested Elements
Goto Forum:
  


Current Time: Sun Dec 08 00:15:22 GMT 2024

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

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

Back to the top