1 /*
2  *
3   ***** BEGIN LICENSE BLOCK *****
4 
5   Copyright (C) 2009-2019 Olof Hagsand
6   Copyright (C) 2020 Olof Hagsand and Rubicon Communications, LLC(Netgate)
7 
8   This file is part of CLIXON.
9 
10   Licensed under the Apache License, Version 2.0 (the "License");
11   you may not use this file except in compliance with the License.
12   You may obtain a copy of the License at
13 
14     http://www.apache.org/licenses/LICENSE-2.0
15 
16   Unless required by applicable law or agreed to in writing, software
17   distributed under the License is distributed on an "AS IS" BASIS,
18   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   See the License for the specific language governing permissions and
20   limitations under the License.
21 
22   Alternatively, the contents of this file may be used under the terms of
23   the GNU General Public License Version 3 or later (the "GPL"),
24   in which case the provisions of the GPL are applicable instead
25   of those above. If you wish to allow use of your version of this file only
26   under the terms of the GPL, and not to allow others to
27   use your version of this file under the terms of Apache License version 2,
28   indicate your decision by deleting the provisions above and replace them with
29   the  notice and other provisions required by the GPL. If you do not delete
30   the provisions above, a recipient may use your version of this file under
31   the terms of any one of the Apache License version 2 or the GPL.
32 
33   ***** END LICENSE BLOCK *****
34  *
35  * "instance-id" is a subset of XPath and defined in RF7950 Sections 9.13 and 14.
36  * BNF:
37  *  instance-identifier = ("/" (node-identifier [key-predicate+ | leaf-list-predicate | pos]))+
38  *  key-predicate       = "[" key-predicate-expr "]"
39  *  key-predicate-expr  = node-identifier "=" quoted-string
40  *  leaf-list-predicate = "[" leaf-list-predicate-expr  "]"
41  *  leaf-list-predicate-expr = "." "=" quoted-string
42  *  pos                 = "[" positive-integer-value "]"
43  *  node-identifier     = [prefix ":"] identifier
44  *  quoted-string       = (DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)
45  *  positive-integer-value = (non-zero-digit DIGIT*)
46  *  prefix              = identifier
47  *  identifier          = (ALPHA | "_")(ALPHA | DIGIT | "_" | "-" | ".")*
48  */
49 
50 %{
51 
52 #include "clixon_config.h"
53 
54 #include <stdio.h>
55 #include <string.h>
56 #include <stdint.h>
57 #include <errno.h>
58 
59 #include "clixon_instance_id_parse.tab.h" /* generated */
60 
61 #include <cligen/cligen.h>
62 
63 #include "clixon_queue.h"
64 #include "clixon_hash.h"
65 #include "clixon_handle.h"
66 #include "clixon_yang.h"
67 #include "clixon_log.h"
68 #include "clixon_string.h"
69 #include "clixon_xml.h"
70 #include "clixon_path.h"
71 #include "clixon_instance_id_parse.h"
72 
73 /* Redefine main lex function so that you can send arguments to it: _yy is added to arg list */
74 #define YY_DECL int clixon_instance_id_parselex(void *_iy)
75 
76 /* Dont use input function (use user-buffer) */
77 #define YY_NO_INPUT
78 
79 /* typecast macro */
80 #define _IY ((clixon_instance_id_yacc *)_iy)
81 
82 #define     MAXBUF  4*4*64*1024
83 
84 #undef clixon_instance_id_parsewrap
85 int
clixon_instance_id_parsewrap(void)86 clixon_instance_id_parsewrap(void)
87 {
88     return 1;
89 }
90 
91 %}
92 
93 namestart  [A-Z_a-z]
94 namechar   [A-Z_a-z\-\.0-9]
95 identifier {namestart}{namechar}*
96 uint       [1-9][0-9]*
97 
98 %x INIT
99 %s STRDQ
100 %s STRSQ
101 
102 %%
103 
104 <INIT,STRDQ,STRSQ>[ \t]
105 <INIT,STRDQ,STRSQ>\n       { _IY->iy_linenum++; }
106 <INIT,STRDQ,STRSQ>\r
107 <INIT,STRDQ,STRSQ><<EOF>>  { return X_EOF; }
108 
109 <INIT>\/           { return SLASH;}
110 <INIT>\[           { return LSQBR;}
111 <INIT>\]           { return RSQBR;}
112 <INIT>\=           { return EQUAL; }
113 <INIT>\:           { return COLON; }
114 <INIT>\.           { return DOT; }
115 <INIT>\"           { _IY->iy_lex_state=INIT;BEGIN(STRDQ); return DQUOTE; }
116 <INIT>\'           { _IY->iy_lex_state=INIT;BEGIN(STRSQ); return SQUOTE; }
117 <INIT>{identifier} { clixon_instance_id_parselval.string = strdup(yytext);
118                      return IDENTIFIER; }
119 <INIT>{uint}       { clixon_instance_id_parselval.string = strdup(yytext);
120                      return UINT; }
121 <INIT>.            { clixon_instance_id_parseerror(_IY, "LEXICAL ERROR\n"); return -1; }
122 
123 <STRDQ>[^\"]+         { clixon_instance_id_parselval.string = strdup(yytext); return STRING; }
124 <STRDQ>\"             { BEGIN(_IY->iy_lex_state); return DQUOTE; }
125 
126 <STRSQ>[^\']+         { clixon_instance_id_parselval.string = strdup(yytext); return STRING; }
127 <STRSQ>\'             { BEGIN(_IY->iy_lex_state); return SQUOTE; }
128 
129 
130 %%
131 
132 /*! Initialize scanner.
133  */
134 int
135 instance_id_scan_init(clixon_instance_id_yacc *iy)
136 {
137   BEGIN(INIT);
138   iy->iy_lexbuf = yy_scan_string(iy->iy_parse_string);
139 #if 1 /* XXX: just to use unput to avoid warning  */
140   if (0)
141     yyunput(0, "");
142 #endif
143 
144   return 0;
145 }
146 
147 /*
148  * free buffers
149  * Even within Flex version 2.5 (this is assumed), freeing buffers is different.
150  */
151 int
152 instance_id_scan_exit(clixon_instance_id_yacc *iy)
153 {
154     yy_delete_buffer(iy->iy_lexbuf);
155     clixon_instance_id_parselex_destroy();  /* modern */
156     return 0;
157 }
158 
159