20230322
[shlib.git] / sample / lxgr / lxr.l.shlib
blobd0728222ac99dc4245bb41cd29bac9c959522548
2 ##############################################################################
3 # lxr.l
4 #    this file list token (rsv-word/signature), normal word, quated string, and
5 # append processing code after it if it needed.
6 #    generally, those content should be defined:
7 # charset: fundamental char set name.
8 # blank/newline: ignore blank, or recognized in other defination. record 
9 #    newline.
10 # comment:
11 # maskchar: char of "\". some will process it in quated string, but some will
12 #    process it in code as a mask char.
13 # word: general word.
14 # symbol: it can be used for variable name.
15 # rsv-word(token): some fixed word defined as token.
16 # signature(token): punct char set.
17 # quatedstr: quated string with ", ', `.
18 # specialchar: such as $ in shell.
19 # EOF: file terminator processing.
20 # <some-others>: some particular content.
21 #    rsv-word/signature/specialchar can be defined in other place. in code, or
22 # another lxr define file.
23 #    lexer processing variables define.
24 # yytext, yyleng: current matching string pointer
25 #     and length.
26 # yylval: 
27 # unput(yytext[0]): put a char back to original processing content.
28 # BEGIN COMMAND: entering state. INITIAL is the default state.
29 ##############################################################################
32 # define T_WORD, T_TOKEN
34 . shlibinc
35 # it let return with a string value.
36 include gplib.shlib
37 include stdio.shlib
38 include attr.shlib
39 #include qstk.shlib
42 ##############################################################################
44 ##############################################################################
46 # lastest token.
47 last=
49 # tmp str for quote char type storage.
50 str=
52 # comment state flag.
53 cmnt_flag=0
55 # current matching string and length.
56 yytext=
57 yyleng=
59 # if $attr_domain is null, use env var access.
61 attr_set_domain ()
63         :
67 # fdesc: swith to dest state, set domain.
69 BEGIN ()
71         if [[ $1 == INITIAL ]]; then
72                 attr_set_domain $1
73         else
74                 attr_set_domain ""
75         fi
78 str_buff=""
81 # init a new string in buffer variable.
83 new_string ()
85         declare -g str_buff=""
89 # fsyntax: append_string <str> [len]
90 # fdesc: append string to curr string buffer.
92 append_string ()
94         eval "str_buff+=\"\${1: 0:$2}\""
98 # fdesc: check if stack is empty.
100 stk_empty ()
102         :
106 # fdesc: pop string.
108 stk_pop ()
110         :
114 # fdesc: push string.
116 stk_push ()
118         :
122 # fdesc: 
124 zconf_endblock ()
126         :
131 ##############################################################################
133 ##############################################################################
135 # [lxr-charset]
136 t='[^[:space:]]'
137 a='[[:alpha:]]'
138 n='[A-Za-z0-9_]'
139 s='[[:punct:]]'
141 # [lxr-token]
144 # blank & newline
146 def_blank='[[:blank:]]*'
147 def_blank_ln='[[:blank:]]*\n'
148 on_blank_ln ()
150         return T_EOL;
154 # comment, sh-lang and c style.
156 def_sh_cmnt='[ \t]*#.*'
157 def_sh_cmnt_ln='[[:blank:]]*#.*\n'
158 on_sh_cmnt_ln ()
160         return T_EOL;
162 def_cpp_cmnt_ln=[[:blank:]]*//.*\n
163 on_cpp_cmnt_ln ()
165         return T_EOL;
167 def_cpp_cmnt_nl='[[:blank:]]*/\*'
168 on_c_cmnt_begin ()
170         BEGIN COMMENT
171         cmnt_flag=0;
173 # C89 style comment#
174 state_list+=" COMMENT"
175 state_array+=( "COMMENT" )
176 attr COMMENT::def_c_cmnt='[^*/]*'
177 COMMENT::on_c_cmnt ()
179         #append_string(yytext, yyleng);
180         if [[ yytext =~ "\n" ]]; then
181                 cmnt_flag=1;
182         fi
184 attr COMMENT::def_c_cmnt_end='\*/'
185 COMMENT::on_c_cmnt_end ()
187         BEGIN INITIAL;
188         last="T_COMMENT";
189         #return T_COMMENT;
190         if [[ cmnt_flag == 1 ]]; then
191                 # if comment is multi line, return as a T_EOL.#
192                 return T_EOL;
193         fi
195 attr COMMENT::def_c_cmnt_star='*'
196 COMMENT::on_c_cmnt_star ()
198         # append_string("*", 1);
199         :
201 attr COMMENT::def_c_cmnt_slash='*'
202 COMMENT::on_c_cmnt_slash ()
204         # append_string("*", 1);
205         :
208 #attr COMMENT::def_c_cmnt_eof=''
209 #COMMENT::on_c_cmnt_slash ()
210 #<<EOF>>        {
211 #       : # err
215 # signature token define.
217 def_token_sign_langle="<"
218 on_token_sign_langle ()
219 { last="<"; return T_LANGLE; }
220 def_token_sign_rangle=">"
221 on_token_sign_rangle ()
222 { last=">"; return T_RANGLE; }
223 def_token_sign_lbrace="{"
224 on_token_sign_lbrace ()
225 { last="{"; return T_LBRACE; }
226 def_token_sign_rbrace="}"
227 on_token_sign_rbrace ()
228 { last="}"; return T_RBRACE; }
229 def_token_sign_lbracket="["
230 on_token_sign_lbracket ()
231 { last="["; return T_LBRACKET; }
232 def_token_sign_rbracket="]"
233 on_token_sign_rbracket ()
234 { last="]"; return T_RBRACKET; }
237 # signature token define.
239 def_token_sign_general="${s}+"
240 on_token_sign_general ()
242         const struct kconf_id 
243         local id=
244         
245         id=$(kconf_id_lookup "$yytext" $yyleng);
246 #       current_pos.file = current_file;
247 #       current_pos.lineno = yylineno;
248         if [[ id && id_flags == TF_COMMAND ]]; then
249                 yylval.id = id;
250                 return id_token;
251         fi
252         
253         alloc_string "$yytext" $yyleng;
254         yylval.string = text;
255         last="T_WORD";
256         return T_WORD;
259 # symbol word or rsv-word.
261 def_token_symb_general="${a}${n}+"
262 on_token_symb_general ()
264 #       const struct kconf_id
265         local id=
266         
267         id = kconf_id_lookup "$yytext" $yyleng;
268 #       current_pos.file = current_file;
269 #       current_pos.lineno = yylineno;
270         if [[ -n id && id_flags == TF_COMMAND ]]; then
271                 yylval.id = id;
272                 return id_token;
273         fi
274         alloc_string "$yytext" $yyleng;
275 #       yylval.string = text;
276         last="T_WORD";
277         return T_WORD;
280 # normal word.
282 def_token_word_general="${n}+"
283 on_token_word_general ()
285 #       const struct kconf_id
286         local id=
287         
288         id = kconf_id_lookup "$yytext" $yyleng;
289 #       current_pos.file = current_file;
290 #       current_pos.lineno = yylineno;
291         if [[ id && id_flags == TF_COMMAND ]]; then
292                 yylval.id = id;
293                 return id_token;
294         fi
295         alloc_string "$yytext" $yyleng;
296 #       yylval.string = text;
297         last="T_WORD";
298         return T_WORD;
301 # string word.
302 # text without blanks, and include none-blank display char.
303 # it's used as regex string.
305 def_string_word_general="[[:space:]]${t}+[[:space:]]"
306 on_string_word_general ()
308         const struct kconf_id
309         local id=
310         
311         id=$(kconf_id_lookup "$yytext" $yyleng);
312 #       current_pos.file = current_file;
313 #       current_pos.lineno = yylineno;
314         if [[ id && id_flags == TF_COMMAND ]]; then
315                 yylval.id = id;
316                 return id_token;
317         fi
318         alloc_string "$yytext" $yyleng;
319 #       yylval.string = text;
320         last="T_WORD";
321         return T_WORD;
323 def_any_char="."
324 on_any_char ()
325 { warn_ignored_character "$yytext"; }
326 def_nl="\n"
327 on_nl ()
329         BEGIN INITIAL;
330         return T_EOL;
333 def_mask="\\."
334 on_mask ()
336         # mask char
337         :
340 def_quate_begin="\"|'"
341 on_quate_begin ()
343         str="${yytext[0]}";
344         new_string;
345         BEGIN STRING;
347 # single line string just for symbol#
348 state_list+=" STRING"
349 state_array+=( "STRING" )
350 attr STRING::def_quate_string="[^'\"\\]*"
351 STRING::on_quate_string ()
353         append_string "$yytext" yyleng;
355 attr STRING::def_quote_mask='\\.?'
356 STRING::on_quote_mask ()
358         # '\' mask signature#
359         append_string "${yytext:1}" $((yyleng - 1));
361 attr STRING::def_quote_end="'|\""
362 STRING::on_quote_end ()
364         if [[ $str == ${yytext[0]} ]]; then
365                 BEGIN INITIAL;
366 #               yylval.string = text;
367                 last="T_WORD_QUOTE";
368                 return T_WORD_QUOTE;
369         else
370                 append_string "$yytext" 1;
371         fi
373 #<<EOF>>        {
374 #       ; # err
378 # multi-line string for code block.
379 # entering this state by function enter_state("BLOCK").
381 def_block_begin="[[:space:]]\{[[:space:]]"
382 on_block_begin ()
384         if [[ "$last" =~ ">" ]]; then
385                 # BRACE in state define
386                 last="T_LBRACE";
387                 return T_LBRACE;
388         else
389                 # it's a response code of lex define.
390                 str="${yytext[0]}";
391                 new_string;
392                 append_string "{\n" 2;
393                 # entering block
394                 BEGIN BLOCK;
395         fi
398 # entering from "[[:space:]]\{[[:space:]]", and end with
399 # "[[:space:]]\}[[:space:]]". append string with "[^\{\}]*".
400 # since the single "{" or "}" should be recognized, this
401 # will avoid "}" mis-matching with multi "{}" pair in block.
402 # so, the ending "}" should be matching with 
403 # "[^\{\}]*[[:space:]]\}[[:space:]]". if mathcing "[^\{\}]*"
404 # and "[[:space:]]\}[[:space:]]" seperatly, "[^\{\}]*" will
405 # matching with "[[:space:]]", which is before "}", then,
406 # "[[:space:]]\}[[:space:]]" is not matched, but "}" is matched.
407 # if put checking code in "}" process code,
409 state_list+=" BLOCK"
410 state_array+=( "BLOCK" )
411         #
412         # code block is terminated with a } and a newline, whatever
413         # there are comments between them.
414         #
415 attr BLOCK::def_block_str_end='[^\{\}]*[[:space:]]\}[[:space:]]*(\#[^\n]*)?\n'
416 BLOCK::on_block_str_end ()
418         append_string "$yytext" $yyleng;
419         if [[ $(stk_empty) == true ]]; then
420                 zconf_endblock;
421                 BEGIN INITIAL;
422                 return T_BLOCKTEXT;
423         else
424                 c=$(stk_pop);
425         fi
427 attr BLOCK::def_block_str='[^\{\}]*'
428 BLOCK::on_block_str ()
430         append_string "$yytext" $yyleng;
432 attr BLOCK::def_block_lbrace='\{'
433 BLOCK::on_block_lbrace ()
435         append_string "{" 1;
436         stk_push '{';
438 attr BLOCK::def_block_rbrace='\}'
439 BLOCK::on_block_rbrace ()
441         if [[ $(stk_empty) == true ]]; then
442                 #
443                 # should not entering here.
444                 # it means there are no "[[:space:]]" char matching before "{".
445                 # or, it will matching the first item of syntax define.
446                 #
447                 #dbgout("there must be blank char before and after '}'.\n");
448                 append_string "\n}" 2;
449                 zconf_endblock;
450                 BEGIN INITIAL;
451                 last="T_BLOCKTEXT"
452                 return T_BLOCKTEXT;
453         else
454                 append_string "}" 1;
455                 c=$(stk_pop);
456         fi
459 def_eof=''
460 on_eof ()
462 #       if (current_file) {
463 #               zconf_endfile();
464 #               return T_EOL;
465 #       }
466 #       fclose(yyin);
467 #       yyterminate();
468         :