Add <,> operators support to cps-path
[cps.git] / cps-path-parser / src / main / antlr4 / org / onap / cps / cpspath / parser / antlr4 / CpsPath.g4
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2023 TechMahindra Ltd
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 grammar CpsPath ;
23
24 cpsPath : ( prefix | descendant | incorrectPrefix ) multipleLeafConditions? textFunctionCondition? containsFunctionCondition? ancestorAxis? invalidPostFix?;
25
26 ancestorAxis : SLASH KW_ANCESTOR COLONCOLON ancestorPath ;
27
28 ancestorPath : yangElement ( SLASH yangElement)* ;
29
30 textFunctionCondition : SLASH leafName OB KW_TEXT_FUNCTION EQ StringLiteral CB ;
31
32 containsFunctionCondition : OB KW_CONTAINS_FUNCTION OP AT leafName COMMA StringLiteral CP CB ;
33
34 parent : ( SLASH yangElement)* ;
35
36 prefix : parent SLASH containerName ;
37
38 descendant : SLASH prefix ;
39
40 incorrectPrefix : SLASH SLASH SLASH+ ;
41
42 yangElement : containerName listElementRef? ;
43
44 containerName : QName ;
45
46 listElementRef :  OB leafCondition ( booleanOperators leafCondition)* CB ;
47
48 multipleLeafConditions : OB leafCondition ( booleanOperators leafCondition)* CB ;
49
50 leafCondition : AT leafName comparativeOperators ( IntegerLiteral | StringLiteral) ;
51
52 leafName : QName ;
53
54 booleanOperators : ( KW_AND | KW_OR ) ;
55
56 comparativeOperators : ( EQ | GT | LT | GE | LE ) ;
57
58 invalidPostFix : (AT | CB | COLONCOLON | comparativeOperators ).+ ;
59
60 /*
61  * Lexer Rules
62  * Most of the lexer rules below are inspired by
63  * https://raw.githubusercontent.com/antlr/grammars-v4/master/xpath/xpath31/XPath31.g4
64  */
65
66 AT : '@' ;
67 CB : ']' ;
68 COLONCOLON : '::' ;
69 EQ : '=' ;
70 OB : '[' ;
71 SLASH : '/' ;
72 COMMA : ',' ;
73 OP : '(' ;
74 CP : ')' ;
75 GT : '>' ;
76 LT : '<' ;
77 GE : '>=' ;
78 LE : '<=' ;
79
80 // KEYWORDS
81
82 KW_ANCESTOR : 'ancestor' ;
83 KW_AND : 'and' ;
84 KW_TEXT_FUNCTION: 'text()' ;
85 KW_OR : 'or' ;
86 KW_CONTAINS_FUNCTION: 'contains' ;
87
88 IntegerLiteral : FragDigits ;
89 // Add below type definitions for leafvalue comparision in https://jira.onap.org/browse/CPS-440
90 DecimalLiteral : ('.' FragDigits) | (FragDigits '.' [0-9]*) ;
91 DoubleLiteral : (('.' FragDigits) | (FragDigits ('.' [0-9]*)?)) [eE] [+-]? FragDigits ;
92 StringLiteral : ('"' (FragEscapeQuot | ~[^"])*? '"') | ('\'' (FragEscapeApos | ~['])*? '\'') ;
93 fragment FragEscapeQuot : '""' ;
94 fragment FragEscapeApos : '\'' ;
95 fragment FragDigits : [0-9]+ ;
96
97 QName  : FragQName ;
98 NCName : FragmentNCName ;
99 fragment FragQName : FragPrefixedName | FragUnprefixedName ;
100 fragment FragPrefixedName : FragPrefix ':' FragLocalPart ;
101 fragment FragUnprefixedName : FragLocalPart ;
102 fragment FragPrefix : FragmentNCName ;
103 fragment FragLocalPart : FragmentNCName ;
104 fragment FragNCNameStartChar
105   :  'A'..'Z'
106   |  '_'
107   | 'a'..'z'
108   | '\u00C0'..'\u00D6'
109   | '\u00D8'..'\u00F6'
110   | '\u00F8'..'\u02FF'
111   | '\u0370'..'\u037D'
112   | '\u037F'..'\u1FFF'
113   | '\u200C'..'\u200D'
114   | '\u2070'..'\u218F'
115   | '\u2C00'..'\u2FEF'
116   | '\u3001'..'\uD7FF'
117   | '\uF900'..'\uFDCF'
118   | '\uFDF0'..'\uFFFD'
119   | '\u{10000}'..'\u{EFFFF}'
120   ;
121 fragment FragNCNameChar
122   :  FragNCNameStartChar | '-' | '.' | '0'..'9'
123   |  '\u00B7' | '\u0300'..'\u036F'
124   |  '\u203F'..'\u2040'
125   ;
126 fragment FragmentNCName : FragNCNameStartChar FragNCNameChar* ;
127
128 // https://www.w3.org/TR/REC-xml/#NT-Char
129
130 fragment FragChar : '\u0009' | '\u000a' | '\u000d'
131   | '\u0020'..'\ud7ff'
132   | '\ue000'..'\ufffd'
133   | '\u{10000}'..'\u{10ffff}'
134   ;
135
136 // Skip all Whitespace
137 Whitespace : ('\u000d' | '\u000a' | '\u0020' | '\u0009')+ -> skip ;
138
139 // handle characters which failed to match any other token (otherwise Antlr will ignore them)
140 ErrorCharacter : . ;