Merge "Apostrophe handling in CpsPathParser"
[cps.git] / cps-path-parser / src / main / antlr4 / org / onap / cps / cpspath / parser / antlr4 / CpsPath.g4
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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 /*
23  * Parser Rules
24  * Some of the parser rules below are inspired by
25  * https://github.com/antlr/grammars-v4/blob/master/xpath/xpath31/XPath31Parser.g4
26  */
27
28 grammar CpsPath ;
29
30 cpsPath : ( prefix | descendant | incorrectPrefix ) multipleLeafConditions? textFunctionCondition? containsFunctionCondition? ancestorAxis? invalidPostFix?;
31
32 ancestorAxis : SLASH KW_ANCESTOR COLONCOLON ancestorPath ;
33
34 ancestorPath : yangElement ( SLASH yangElement)* ;
35
36 textFunctionCondition : SLASH leafName OB KW_TEXT_FUNCTION EQ StringLiteral CB ;
37
38 containsFunctionCondition : OB KW_CONTAINS_FUNCTION OP AT leafName COMMA StringLiteral CP CB ;
39
40 parent : ( SLASH yangElement)* ;
41
42 prefix : parent SLASH containerName ;
43
44 descendant : SLASH prefix ;
45
46 incorrectPrefix : SLASH SLASH SLASH+ ;
47
48 yangElement : containerName listElementRef? ;
49
50 containerName : QName ;
51
52 listElementRef :  OB leafCondition ( booleanOperators leafCondition)* CB ;
53
54 multipleLeafConditions : OB leafCondition ( booleanOperators leafCondition)* CB ;
55
56 leafCondition : AT leafName comparativeOperators ( IntegerLiteral | StringLiteral) ;
57
58 leafName : QName ;
59
60 booleanOperators : ( KW_AND | KW_OR ) ;
61
62 comparativeOperators : ( EQ | GT | LT | GE | LE ) ;
63
64 invalidPostFix : (AT | CB | COLONCOLON | comparativeOperators ).+ ;
65
66 /*
67  * Lexer Rules
68  * Most of the lexer rules below are inspired by
69  * https://github.com/antlr/grammars-v4/blob/master/xpath/xpath31/XPath31Lexer.g4
70  */
71
72 AT : '@' ;
73 CB : ']' ;
74 COLONCOLON : '::' ;
75 EQ : '=' ;
76 OB : '[' ;
77 SLASH : '/' ;
78 COMMA : ',' ;
79 OP : '(' ;
80 CP : ')' ;
81 GT : '>' ;
82 LT : '<' ;
83 GE : '>=' ;
84 LE : '<=' ;
85
86 // KEYWORDS
87
88 KW_ANCESTOR : 'ancestor' ;
89 KW_AND : 'and' ;
90 KW_TEXT_FUNCTION: 'text()' ;
91 KW_OR : 'or' ;
92 KW_CONTAINS_FUNCTION: 'contains' ;
93
94 IntegerLiteral : FragDigits ;
95 // Add below type definitions for leafvalue comparision in https://jira.onap.org/browse/CPS-440
96 DecimalLiteral : ('.' FragDigits) | (FragDigits '.' [0-9]*) ;
97 DoubleLiteral : (('.' FragDigits) | (FragDigits ('.' [0-9]*)?)) [eE] [+-]? FragDigits ;
98 StringLiteral : '"' (~["] | FragEscapeQuot)* '"' | '\'' (~['] | FragEscapeApos)* '\'' ;
99 fragment FragEscapeQuot : '""' ;
100 fragment FragEscapeApos : '\'\'';
101 fragment FragDigits : [0-9]+ ;
102
103 QName  : FragQName ;
104 NCName : FragmentNCName ;
105 fragment FragQName : FragPrefixedName | FragUnprefixedName ;
106 fragment FragPrefixedName : FragPrefix ':' FragLocalPart ;
107 fragment FragUnprefixedName : FragLocalPart ;
108 fragment FragPrefix : FragmentNCName ;
109 fragment FragLocalPart : FragmentNCName ;
110 fragment FragNCNameStartChar
111   :  'A'..'Z'
112   |  '_'
113   | 'a'..'z'
114   | '\u00C0'..'\u00D6'
115   | '\u00D8'..'\u00F6'
116   | '\u00F8'..'\u02FF'
117   | '\u0370'..'\u037D'
118   | '\u037F'..'\u1FFF'
119   | '\u200C'..'\u200D'
120   | '\u2070'..'\u218F'
121   | '\u2C00'..'\u2FEF'
122   | '\u3001'..'\uD7FF'
123   | '\uF900'..'\uFDCF'
124   | '\uFDF0'..'\uFFFD'
125   | '\u{10000}'..'\u{EFFFF}'
126   ;
127 fragment FragNCNameChar
128   :  FragNCNameStartChar | '-' | '.' | '0'..'9'
129   |  '\u00B7' | '\u0300'..'\u036F'
130   |  '\u203F'..'\u2040'
131   ;
132 fragment FragmentNCName : FragNCNameStartChar FragNCNameChar* ;
133
134 // https://www.w3.org/TR/REC-xml/#NT-Char
135
136 fragment FragChar : '\u0009' | '\u000a' | '\u000d'
137   | '\u0020'..'\ud7ff'
138   | '\ue000'..'\ufffd'
139   | '\u{10000}'..'\u{10ffff}'
140   ;
141
142 // Skip all Whitespace
143 Whitespace : ('\u000d' | '\u000a' | '\u0020' | '\u0009')+ -> skip ;
144
145 // handle characters which failed to match any other token (otherwise Antlr will ignore them)
146 ErrorCharacter : . ;