1 %{
2 /*
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
4 *
5 * HDF is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
7 * See the LICENSE file in the root of this repository for complete details.
8 */
9
10 #include <stdio.h>
11 #include <stdint.h>
12 #include "hcs_compiler.h"
13
14 #define YYDEBUG 1
15 #define YYERROR_VERBOSE 1
16 #define OBJPTR ParserObject*
17
18 void HcsCompilererror (const char *s);
19 int yylex (void);
20 extern int HcsCompilerlineno;
21 %}
22
23
24 %union {
25 uint64_t i;
26 char *s;
27 ParserObject *o;
28 }
29
30 %token<i> NUMBER
31 %token<i> TEMPLATE
32 %token<s> LITERAL
33 %token<s> STRING
34 %token<s> REF_PATH
35 %token<s> FILE_PATH
36
37 %token EOL
38 %token ROOT
39 %token INCLUDE
40 %token DELETE
41
42
43 %type<o> ConfigTermList ConfigNode ConfigTerm NumberList ConstStringList ConfigBlock NodeTemplate
44 %type<s> NodePath
45
46
47 %%
48 ConfigTable
49 :
50 | IncludeList
51 | IncludeList
52 RootNode
53 | RootNode
54 ;
55
56 RootNode
57 : ROOT
58 '{' { $<o>$ = HcsGetParserRoot(); if ($<o>$ == NULL) YYABORT; }
59 ConfigTermList
60 '}' { HcsAstAddChild($<o>3, $4); }
61 ;
62
63 IncludeList
64 : INCLUDE STRING { if (HcsProcessInclude($2, HcsCompilerlineno)) YYABORT; }
65 | INCLUDE STRING IncludeList { if (HcsProcessInclude($2, HcsCompilerlineno)) YYABORT; }
66 ;
67
68 ConfigTermList
69 : { $$ = NULL; }
70 | ConfigTerm
71 ';'
72 ConfigTermList { $$ = HcsAstAddPeer($1, $3); }
73 | ConfigBlock
74 ConfigTermList { $$ = HcsAstAddPeer($1, $2); }
75 ;
76
77 ConfigBlock
78 : ConfigNode
79 | NodeTemplate
80
81 ConfigNode
82 : LITERAL
83 '{' { $<o>$ = HcsNewConfigNode($1, CONFIG_NODE_NOREF, NULL); if ($<o>$ == NULL) YYABORT; }
84 ConfigTermList
85 '}' { $$ = HcsAstAddChild($<o>3, $4); }
86 | LITERAL ':' NodePath '{' { $<o>$ = HcsNewConfigNode($1, CONFIG_NODE_COPY, $3); if ($<o>$ == NULL) YYABORT; }
87 ConfigTermList
88 '}' { $$ = HcsAstAddChild($<o>5, $6); }
89 | LITERAL ':' '&' NodePath '{' { $<o>$ = HcsNewConfigNode($1, CONFIG_NODE_REF, $4); if ($<o>$ == NULL) YYABORT; }
90 ConfigTermList
91 '}' { $$ = HcsAstAddChild($<o>6, $7); }
92 | LITERAL ':' DELETE '{' { $<o>$ = HcsNewConfigNode($1, CONFIG_NODE_DELETE, NULL); if ($<o>$ == NULL) YYABORT; }
93 ConfigTermList
94 '}' { $$ = HcsAstAddChild($<o>5, $6); }
95 | LITERAL ':' ':'
96 NodePath
97 '{' { $<o>$ = HcsNewConfigNode($1, CONFIG_NODE_INHERIT, $4); if ($<o>$ == NULL) YYABORT; }
98 ConfigTermList
99 '}' { $$ = HcsAstAddChild($<o>6, $7); }
100 ;
101
102 NodeTemplate : TEMPLATE LITERAL
103 '{' { $<o>$ = HcsNewConfigNode($2, CONFIG_NODE_TEMPLATE, NULL); if ($<o>$ == NULL) YYABORT; }
104 ConfigTermList
105 '}' { $$ = HcsAstAddChild($<o>4, $5); }
106
107 NodePath
108 : REF_PATH { $$ = $1; }
109 | LITERAL { $$ = $1; }
110 ;
111
112 ConfigTerm
113 : LITERAL '=' STRING { $$ = HcsNewConfigTerm($1, HcsNewParserObject(NULL, PARSEROP_STRING, (uint64_t)$3));
114 if ($$ == NULL) YYABORT; }
115 | LITERAL '=' NUMBER { $$ = HcsNewConfigTerm($1, HcsNewParserObject(NULL, PARSEROP_UINT64, (uint64_t)$3));
116 if ($$ == NULL) YYABORT; }
117 | LITERAL '=' '[' ']' { HcsCompilererror("Not allow empty array"); YYABORT;}
118 | LITERAL '=' '[' NumberList ']' { OBJPTR obj = HcsNewParserObject(NULL, PARSEROP_ARRAY, (uint64_t)NULL);
119 if (obj == NULL) YYABORT;
120 $$ = HcsNewConfigTerm($1, HcsAstAddChild(obj, $4));
121 if ($$ == NULL) YYABORT; }
122 | LITERAL '=' '[' ConstStringList ']' { OBJPTR obj = HcsNewParserObject(NULL, PARSEROP_ARRAY, (uint64_t)NULL);
123 if (obj == NULL) YYABORT;
124 $$ = HcsNewConfigTerm($1, HcsAstAddChild(obj, $4));
125 if ($$ == NULL) YYABORT; }
126 | LITERAL '=' '&' NodePath { OBJPTR obj = HcsNewParserObject(NULL, PARSEROP_NODEREF, (uint64_t)$4);
127 if (obj == NULL) YYABORT;
128 $$ = HcsNewConfigTerm($1, obj);
129 if ($$ == NULL) YYABORT; }
130 | LITERAL '=' DELETE { $$ = HcsNewConfigTerm($1,
131 HcsNewParserObject(NULL, PARSEROP_DELETE, (uint64_t)NULL)); }
132 ;
133
134 NumberList
135 : NUMBER { $$ = HcsNewParserObject(NULL, PARSEROP_UINT64, $1); if ($$ == NULL) YYABORT; }
136 | NUMBER ',' NumberList { OBJPTR obj = HcsNewParserObject(NULL, PARSEROP_UINT64, $1); if (obj == NULL) YYABORT;
137 $$ = HcsAstAddPeer(obj, $3); }
138 ;
139
140 ConstStringList
141 : STRING { $$ = HcsNewParserObject(NULL, PARSEROP_STRING, (uint64_t)$1); if ($$ == NULL) YYABORT;}
142 | STRING ',' ConstStringList { OBJPTR obj = HcsNewParserObject(NULL, PARSEROP_STRING, (uint64_t)$1); if (obj == NULL) YYABORT;
143 $$ = HcsAstAddPeer(obj, $3); }
144 ;
145
146 %%
147 extern char *HcsCompilertext;
148 extern int HcsCompilerlineno;
149
150 const char*HcsGetCurrentSourceName();
151
HcsGetCurrentSourceLine(void)152 int32_t HcsGetCurrentSourceLine(void) {
153 return HcsCompilerlineno;
154 }
155
HcsSetCurrentSourceLine(int32_t lineNumber)156 void HcsSetCurrentSourceLine(int32_t lineNumber) {
157 HcsCompilerlineno = lineNumber;
158 }
159
HcsCompilererror(const char * s)160 void HcsCompilererror (const char *s) {
161 fprintf(stderr, "Error: %s:%d\n\t%s\n", HcsGetCurrentSourceName(), HcsCompilerlineno, s);
162 }
163
164 void HcsCompilerrestart(FILE *input_file);
165
HcsParserRestart(FILE * inputFile)166 void HcsParserRestart(FILE *inputFile) {
167 HcsCompilerrestart(inputFile);
168 }
169