Merge "Add contains condition 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 EQ ( IntegerLiteral | StringLiteral) ;
51
52 leafName : QName ;
53
54 booleanOperators : ( KW_AND | KW_OR ) ;
55
56 invalidPostFix : (AT | CB | COLONCOLON | EQ ).+ ;
57
58 /*
59  * Lexer Rules
60  * Most of the lexer rules below are inspired by
61  * https://raw.githubusercontent.com/antlr/grammars-v4/master/xpath/xpath31/XPath31.g4
62  */
63
64 AT : '@' ;
65 CB : ']' ;
66 COLONCOLON : '::' ;
67 EQ : '=' ;
68 OB : '[' ;
69 SLASH : '/' ;
70 COMMA : ',' ;
71 OP : '(' ;
72 CP : ')' ;
73
74 // KEYWORDS
75
76 KW_ANCESTOR : 'ancestor' ;
77 KW_AND : 'and' ;
78 KW_TEXT_FUNCTION: 'text()' ;
79 KW_OR : 'or' ;
80 KW_CONTAINS_FUNCTION: 'contains' ;
81
82 IntegerLiteral : FragDigits ;
83 // Add below type definitions for leafvalue comparision in https://jira.onap.org/browse/CPS-440
84 DecimalLiteral : ('.' FragDigits) | (FragDigits '.' [0-9]*) ;
85 DoubleLiteral : (('.' FragDigits) | (FragDigits ('.' [0-9]*)?)) [eE] [+-]? FragDigits ;
86 StringLiteral : ('"' (FragEscapeQuot | ~[^"])*? '"') | ('\'' (FragEscapeApos | ~['])*? '\'') ;
87 fragment FragEscapeQuot : '""' ;
88 fragment FragEscapeApos : '\'' ;
89 fragment FragDigits : [0-9]+ ;
90
91 QName  : FragQName ;
92 NCName : FragmentNCName ;
93 fragment FragQName : FragPrefixedName | FragUnprefixedName ;
94 fragment FragPrefixedName : FragPrefix ':' FragLocalPart ;
95 fragment FragUnprefixedName : FragLocalPart ;
96 fragment FragPrefix : FragmentNCName ;
97 fragment FragLocalPart : FragmentNCName ;
98 fragment FragNCNameStartChar
99   :  'A'..'Z'
100   |  '_'
101   | 'a'..'z'
102   | '\u00C0'..'\u00D6'
103   | '\u00D8'..'\u00F6'
104   | '\u00F8'..'\u02FF'
105   | '\u0370'..'\u037D'
106   | '\u037F'..'\u1FFF'
107   | '\u200C'..'\u200D'
108   | '\u2070'..'\u218F'
109   | '\u2C00'..'\u2FEF'
110   | '\u3001'..'\uD7FF'
111   | '\uF900'..'\uFDCF'
112   | '\uFDF0'..'\uFFFD'
113   | '\u{10000}'..'\u{EFFFF}'
114   ;
115 fragment FragNCNameChar
116   :  FragNCNameStartChar | '-' | '.' | '0'..'9'
117   |  '\u00B7' | '\u0300'..'\u036F'
118   |  '\u203F'..'\u2040'
119   ;
120 fragment FragmentNCName : FragNCNameStartChar FragNCNameChar* ;
121
122 // https://www.w3.org/TR/REC-xml/#NT-Char
123
124 fragment FragChar : '\u0009' | '\u000a' | '\u000d'
125   | '\u0020'..'\ud7ff'
126   | '\ue000'..'\ufffd'
127   | '\u{10000}'..'\u{10ffff}'
128   ;
129
130 // Skip all Whitespace
131 Whitespace : ('\u000d' | '\u000a' | '\u0020' | '\u0009')+ -> skip ;
132
133 // handle characters which failed to match any other token (otherwise Antlr will ignore them)
134 ErrorCharacter : . ;