Grammar parsing problem [message #1798695] |
Thu, 22 November 2018 18:51  |
Eclipse User |
|
|
|
Hi all, I have the following grammar(please, see it below) and it's not allow the code
class test {
testKey : text;
testIndex : text;
key testKey:text;
index testkey;
}
With the error message
mismatched input ';' expecting ':'
I know that it happens because ANTLR tries to recognize the string "index testkey;" as variable and ask for the variable declaration. But is it possible to change the grammar to fix this issue, not allow of using a variable before the declaration, or I need to add the precedence checking into validator?
grammar org.blockchain.rell.Rell with org.eclipse.xtext.common.Terminals
generate rell "http://www.blockchain.org/rell/Rell"
Model:
entities+=ClassDefinition*
operations+=Operation*;
ClassDefinition:
'class' name=ID ('extends' superType=[ClassDefinition])? '{'
attributeListField+=AttributeListField*
specificationList+=Specification*
'}';
Specification:
((key=Key | index=Index | ((key=Key index=Index)) name=[VariableDeclaration])) ';';
AttributeListField:
key=Key? index=Index? attributeList+=RelAttrubutesList ';';
Operation:
"operation" name=ID "(" parameters=RelAttrubutesList? ")" "{" statements+=Statement* "}";
Statement:
(relation=Relational | variable=Variable | varInit=VariableInit) ';';
Variable:
declaration=VariableDeclaration (('=' expression=Expression)?);
Relational:
Update | Delete | Create;
Update:
'update' entity=[ClassDefinition] '(' conditions=Conditions? ')' '{' variableList+=VariableInit
(',' variableList+=VariableInit*)? '}';
Delete:
'delete' entity=[ClassDefinition] '(' conditions=Conditions? ')';
Create:
'create' entity=[ClassDefinition] '(' conditions=Conditions? ')';
Conditions:
elements+=ConditionElement (',' elements+=ConditionElement*)?;
ConditionElement:
name=ID ('==' | '!=' | '>' | '<' | '>=' | '<=') expr=Expression;
VariableInit:
name=[VariableDeclaration] '=' expression=Expression;
Expression:
or=Or;
Or returns Expression:
And ({Or.left=current} "or" right=And)*;
And returns Expression:
Equality ({And.left=current} "and" right=Equality)*;
Equality returns Expression:
Comparison ({Equality.left=current} op=("==" | "!=")
right=Comparison)*;
Comparison returns Expression:
PlusOrMinus ({Comparison.left=current} op=(">=" | "<=" | ">" | "<")
right=PlusOrMinus)*;
PlusOrMinus returns Expression:
MulOrDiv (({Plus.left=current} '+' | {Minus.left=current} '-')
right=MulOrDiv)*;
MulOrDiv returns Expression:
Primary ({MulOrDiv.left=current} op=('*' | '/')
right=Primary)*;
Primary returns Expression:
'(' Expression ')' |
{Not} "not" expression=Primary |
Atomic;
Atomic returns Expression:
{IntConstant} value=INT |
{StringConstant} value=STRING |
{BoolConstant} value=('true' | 'false') |
{VariableRef} value=[VariableDeclaration];
RelAttrubutesList:
value+=Variable (',' value+=Variable)*;
VariableDeclaration:
name=Name | name=Pubkey | name=Tuid | (name=ID ':' type=TypeReference);
TypeReference:
primitive=PrimitiveType | entityType=ClassType;
PrimitiveType:
primitiveType=(Text | Tuid | Pubkey|Name | Timestamp | Integer | Json | ByteArray | Boolean);
ClassType:
entityRef=[ClassDefinition];
Name:
'name';
Pubkey:
'pubkey';
Timestamp:
'timestamp';
Tuid:
'tuid';
Boolean:
'bool';
Json:
'json';
Integer:
'integer';
Text:
'text';
ByteArray:
'byte_array';
Key:
'key';
Index:
'index';
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04901 seconds