Fix Absolute Path to list with Integer/String key
[cps.git] / cps-path-parser / src / main / antlr4 / org / onap / cps / cpspath / parser / antlr4 / CpsPath.g4
index a4fd58e..40ad410 100644 (file)
@@ -1,12 +1,13 @@
 /*
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix Foundation
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021-2022 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  *  You may obtain a copy of the License at
  *
  *        http://www.apache.org/licenses/LICENSE-2.0
+ *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
 grammar CpsPath ;
 
-cpsPath: (cpsPathWithSingleLeafCondition | cpsPathWithDescendant | cpsPathWithDescendantAndLeafConditions) ancestorAxis? ;
-
-ancestorAxis: SLASH KW_ANCESTOR COLONCOLON ancestorPath ;
-
-ancestorPath: yangElement (SLASH yangElement)* ;
+cpsPath : ( prefix | descendant | incorrectPrefix ) multipleLeafConditions? textFunctionCondition? ancestorAxis? invalidPostFix?;
 
-cpsPathWithSingleLeafCondition: prefix singleValueCondition postfix? ;
+ancestorAxis : SLASH KW_ANCESTOR COLONCOLON ancestorPath ;
 
-/*
-No need to ditinguish between cpsPathWithDescendant | cpsPathWithDescendantAndLeafConditions really!
-See https://jira.onap.org/browse/CPS-436
-*/
-
-cpsPathWithDescendant: descendant ;
+ancestorPath : yangElement ( SLASH yangElement)* ;
 
-cpsPathWithDescendantAndLeafConditions: descendant multipleValueConditions ;
+textFunctionCondition : SLASH leafName OB KW_TEXT_FUNCTION EQ StringLiteral CB ;
 
-descendant: SLASH prefix ;
+prefix : ( SLASH yangElement)* SLASH containerName ;
 
-prefix: (SLASH yangElement)* SLASH containerName ;
+descendant : SLASH prefix ;
 
-postfix: (SLASH yangElement)+ ;
+incorrectPrefix : SLASH SLASH SLASH+ ;
 
-yangElement: containerName listElementRef? ;
+yangElement : containerName listElementRef? ;
 
-containerName: QName ;
+containerName : QName ;
 
-listElementRef: multipleValueConditions ;
+listElementRef :  OB leafCondition ( KW_AND leafCondition)* CB ;
 
-singleValueCondition: '[' leafCondition ']' ;
+multipleLeafConditions : OB leafCondition ( KW_AND leafCondition)* CB ;
 
-multipleValueConditions: '[' leafCondition (' and ' leafCondition)* ']' ;
+leafCondition : AT leafName EQ ( IntegerLiteral | StringLiteral) ;
 
-leafCondition: '@' leafName '=' (IntegerLiteral | StringLiteral ) ;
+leafName : QName ;
 
-//To Confirm: defintion of Lefname with external xPath grammar
-leafName: QName ;
+invalidPostFix : (AT | CB | COLONCOLON | EQ ).+ ;
 
 /*
  * Lexer Rules
- * Most of the lexer rules below are 'imporetd' from
+ * Most of the lexer rules below are inspired by
  * https://raw.githubusercontent.com/antlr/grammars-v4/master/xpath/xpath31/XPath31.g4
  */
 
-SLASH : '/';
+AT : '@' ;
+CB : ']' ;
 COLONCOLON : '::' ;
+EQ : '=' ;
+OB : '[' ;
+SLASH : '/' ;
 
 // KEYWORDS
 
 KW_ANCESTOR : 'ancestor' ;
+KW_AND : 'and' ;
+KW_TEXT_FUNCTION: 'text()' ;
 
 IntegerLiteral : FragDigits ;
 // Add below type definitions for leafvalue comparision in https://jira.onap.org/browse/CPS-440
@@ -76,7 +73,7 @@ DecimalLiteral : ('.' FragDigits) | (FragDigits '.' [0-9]*) ;
 DoubleLiteral : (('.' FragDigits) | (FragDigits ('.' [0-9]*)?)) [eE] [+-]? FragDigits ;
 StringLiteral : ('"' (FragEscapeQuot | ~[^"])*? '"') | ('\'' (FragEscapeApos | ~['])*? '\'') ;
 fragment FragEscapeQuot : '""' ;
-fragment FragEscapeApos : '\'';
+fragment FragEscapeApos : '\'' ;
 fragment FragDigits : [0-9]+ ;
 
 QName  : FragQName ;
@@ -108,7 +105,7 @@ fragment FragNCNameChar
   |  '\u00B7' | '\u0300'..'\u036F'
   |  '\u203F'..'\u2040'
   ;
-fragment FragmentNCName : FragNCNameStartChar FragNCNameChar*  ;
+fragment FragmentNCName : FragNCNameStartChar FragNCNameChar* ;
 
 // https://www.w3.org/TR/REC-xml/#NT-Char
 
@@ -116,7 +113,7 @@ fragment FragChar : '\u0009' | '\u000a' | '\u000d'
   | '\u0020'..'\ud7ff'
   | '\ue000'..'\ufffd'
   | '\u{10000}'..'\u{10ffff}'
- ;
 ;
 
 // Skip all Whitespace
 Whitespace : ('\u000d' | '\u000a' | '\u0020' | '\u0009')+ -> skip ;