using reserved keyword in grammar [message #1757939] |
Wed, 22 March 2017 06:41 |
Henry Henry 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 |
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.08338 seconds