0

I need to parse a text in the below format:

text = "text" text "text" ............   

What terminal rule do I need to write to solve this scenario?

Below is my text file:

//grammar com.provar.eclipse.xtext.TestExpression with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations
grammar com.provar.eclipse.xtext.TestExpression with org.eclipse.xtext.common.Terminals

generate testExpression "http://www.provar.com/xtext/TestExpression"
//import 'http://www.eclipse.org/xtext/xbase/Xbase' as xbase

TestExpression:
    (body+=TopLevelElement)*
    ;

TopLevelElement returns Expression:
    FreeText | Expression 
    ;

Literal returns Expression:
    ValueLiteral
    ;
    
ValueLiteral returns Expression
    : BooleanLiteral 
    | RealLiteral  // must be placed before IntegerLiteral
    | IntegerLiteral 
    | NullLiteral 
    | StringLiteral
    ;   
        
BooleanLiteral returns Expression : 
    {BooleanLiteral} value = BooleanValue
    ;
    
IntegerLiteral returns Expression: 
    {IntegerLiteral} value= RadixIntValue
    ;
    
StringLiteral returns Expression: 
    {StringLiteral} value = STRING
    ;
    
RealLiteral returns Expression:
    {RealLiteral} value = RealValue
    ;
    
// Note: NullLiteral has a java null value as its value
NullLiteral returns Expression:
    {NullLiteral} "null"
    ;
    
//RegexpLiteral:
//  pattern = REGULAR_EXPR
//  ;
//  
//SimplePatternLiteral:
//  pattern = SIMPLE_PATTERN
//  ;
    
// Has conversion rule
BooleanValue: ("true" | "false" );

// Has conversion rule
RealValue: REAL ;

// Has conversion rule that handles decimal, octal, and hexadecimal values but returns an Integer
IntValue: INT | HEX ;

// Has conversion rule that handles decimal, octal, and hexadecimal values with radix
RadixIntValue: INT | HEX ;

FreeText returns Expression:
    {FreeText} value = FREE_TEXT;


ParanthesizedExpression returns Expression: 
    '(' Expression ')'
    ;   

// All expressions except variable and value definitions     
Expression returns Expression
    : OrExpression 
    ;

OrExpression returns Expression:
    AndExpression ({BinaryOpExpression.leftExpr=current} functionName=("or" | "OR") rightExpr=AndExpression)*
    ;
    
AndExpression returns Expression :
      RelationalExpression ({BinaryOpExpression.leftExpr=current} functionName=("and" | "AND") rightExpr=RelationalExpression)*
    ;

RelationalExpression returns Expression :
    AdditiveExpression ({BinaryOpExpression.leftExpr=current} 
        functionName=RelationalOperator rightExpr=AdditiveExpression)*
    ;

AdditiveExpression returns Expression :
    MultiplicativeExpression ({BinaryOpExpression.leftExpr=current} functionName=("+" | "-") rightExpr=MultiplicativeExpression)*
    ;

MultiplicativeExpression returns Expression :
    ConcatenateExpression ({BinaryOpExpression.leftExpr=current} functionName=("*" | "/" | "%") rightExpr=ConcatenateExpression)*
    ;

ConcatenateExpression returns Expression :
    CallExpression ({BinaryOpExpression.leftExpr=current} functionName=("&") rightExpr=CallExpression)*
    ;

CallExpression returns Expression: 
    PrimaryExpression ({FunctionCall.name = current} "(" (parameterList = ParameterList)? ")")*
    ;

PrimaryExpression returns Expression:
    Literal
    | ParanthesizedExpression
    //| CallExpression
    | ToplevelVariableAccess
    ;


ToplevelVariableAccess returns Expression:
    "$" QualifiedVariableAccess {ToplevelVariableAccess.qualifiedVariableAccess=current}
    | QualifiedVariableAccess
;

QualifiedVariableAccess returns Expression:
    {QualifiedVariableAccess} paths += FilteredVariableAccess ("." paths += FilteredVariableAccess)*
    //{QualifiedVariableAccess} paths += FilteredVariableAccess+
    //FilteredVariableAccess ({QualifiedVariableAccess.parentVariable=current} '.' childVariable=FilteredVariableAccess)*
;

FilteredVariableAccess returns Expression:
    SimpleVariableAccess ({FilteredVariableAccess.parentVariable=current} '[' filter=Expression ']')*
;

SimpleVariableAccess returns Expression:
    {SimpleVariableAccess} name = VALID_ID
;

ParameterList returns Expression: 
    parameters += Parameter ("," parameters += Parameter)*
    ;
    
Parameter returns Expression: 
    expr = Expression 
    ;


RelationalOperator 
    : "=" | "!=" | "<>"
    | ">=" | "<=" | ">" | "<"
    | "~" | "!~"
    ;

REAL hidden(): INT '.' (EXT_INT | INT); // INT ? '.' (EXT_INT | INT);

terminal EXT_INT: INT ('e'|'E')('-'|'+') INT;   

terminal FREE_TEXT:
    '}' ( '\\{' | !('{') )* ('{');
    
    
terminal VALID_ID:
    ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
    
terminal HEX:
    ('0x'|'0X') ('0'..'9'|'a'..'f'|'A'..'F'|'_')+ 
    ('#' (('b'|'B')('i'|'I') | ('l'|'L')))?;
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • why use a terminal rule at all and not a Datatype rule combining STRING & ID – Christian Dietrich Aug 18 '20 at 15:56
  • As mentioned by you I Tried the below scenario and it still fails: `VALID_ID hidden (WS,SL_COMMENT): ID ('='|'"'|':' ID)*;` I am a little new to using xtext, so please pardon if I am not able to catch up correctly. What should be the Valid_Id to handle such scenarios? – Ravi Bodla Aug 25 '20 at 10:06
  • The Above converts my String as below `{dfas = fa f == = = "daa " "ada " oip} ----> {dfas = fa f == = =}daa ada {oip}` – Ravi Bodla Aug 25 '20 at 11:18
  • sry i cannot follow you. from what i have seen in your question i would have expected ValidID: (ID | STRING)+; – Christian Dietrich Aug 25 '20 at 11:35
  • If you look in the code above, I am returning Expressions based on some criteria, Like If brackets () are present then return a CallExpression, If [] are present then return filteredExpression. If I use STRING in an ID, It will not consider these expressions. So, Based on these inputs I need to validate a String in the format `{"text" text "text" ............ }` – Ravi Bodla Aug 25 '20 at 12:03
  • Thank you @ChristianDietrich, The first solution worked for me. Not using the Valid_Id as a Terminal Rule. Can you please share me a few links which could help me reason this behavior of the code. `VALID_ID hidden(WS): ID (ID)*;` – Ravi Bodla Aug 26 '20 at 05:13
  • https://www.eclipse.org/Xtext/documentation/301_grammarlanguage.html#datatype-rules https://blogs.itemis.com/en/xtext-hint-identifiers-conflicting-with-keywords – Christian Dietrich Aug 26 '20 at 05:15

0 Answers0