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