use strbuff instead of str_buff & str_idx
[build-config.git] / src / config / lxrgmr-code / lxr / config.lexproc.c
blob240a9c5e3b5204c82c02a4cf79aa8d6a14e00f1f
2 #include <stdio.h>
4 #include "../token_id.h"
6 void STATE_SET (int state)
8 BEGIN(state);
11 int on_help_proc (register const char *str, register unsigned int len)
13 // printf("HELP:\n");
14 BEGIN(HELP_TXT);
15 return 0;
19 #include "zconf.c"
21 /*********************************************************************
22 * lxr proc code.
23 *********************************************************************/
25 int on_token_proc (int ret)
27 // extern char str_buff[8192];
28 // extern int str_idx;
29 extern SRC_FILE_DESC *gpcurr_src_file;
30 char *pstr;
32 TOKEN_PROC_STATE *pstTokenState = gpcurr_src_file->pstCurrTokenState;
33 const char *str = NULL;
36 * display token id, token name, and it's content.
38 str = name_look_up_token(ret);
39 if (str)
41 printf("<%d> %s: ", ret, str);
42 if (!strcmp(str, "T_EOL"))
44 printf("\n");
45 return 0;
47 else if (!strcmp(str, "T_HELPTEXT"))
48 printf("\n");
49 else if (!strcmp(str, "T_WORD_QUOTE"))
50 printf("\n");
51 else if (!strcmp(str, "T_C89_CMNT"))
52 printf("\n");
54 //if (str_idx != 0)
55 if (is_empty_string(&strbuff))
56 pstr = pstTokenState->token_str;
57 else
58 pstr = strbuff.text;
60 // printf("str_idx = %d\n", str_idx);
61 // printf("pstr = %d\n", pstr);
62 printf("%s\n", pstr);
65 if (yylval.id)
67 if (yylval.id->token == ret)
68 printf("%s\n", yylval.id->name);
69 else
70 printf("%s\n", yylval.string);
75 // TBD: append gmr parser func here.
77 return 0;
81 * set content flag automatically.
82 * referenced from script.l(script.lex.c).
84 #define RETURN(token) g_content_before_cmnt=1; return token;
86 int const_string (CACHING_STR *pstr_cache, int token)
88 if (is_unallocated_string(&strbuff))
90 alloc_string(&strbuff, yytext, yyleng);
91 yylval.string = strbuff.text;
93 last_token = token;
94 return token;
96 else
97 append_string(&strbuff, yytext, yyleng);
99 RETURN( T_WORD );
102 #if 1
104 int yywrap (void)
106 return 1;
109 int lex_c_preproc_hdr (void)
111 /* only works with newline started. */
112 if (g_content_before_cmnt == 0)
115 * XXX: lookup macro rsv-word name table, if not matching, it's a comment string.
116 * XXX: invoke OnCPreProc().
118 printf("C-PREPROC:\n%s", yytext);
119 g_cpreproc_flag=1;
120 BEGIN(C_PREPROC);
122 else
124 g_cpreproc_flag=0;
125 printf("SH-CMNT:\n%s", yytext);
126 BEGIN(SH_CMNT_STATE);
127 // return T_EOL;
130 return 0;
133 int lex_sh_cmnt (void)
135 // printf("%s\n", yytext);
136 BEGIN(INITIAL);
138 if (!is_unallocated_string(&strbuff))
140 last_token = 0;
141 yylval.string = strbuff.text;
142 RETURN( T_WORD );
145 return T_EOL;
148 int lex_c_preproc_param (void)
150 printf("%s\n", yytext);
152 if (g_cpreproc_flag == 1)
154 // XXX: replace cpreproc code here.
155 BEGIN(INITIAL);
157 else
159 BEGIN(INITIAL);
161 g_cpreproc_flag = 0;
163 BEGIN(INITIAL);
164 if (!is_unallocated_string(&strbuff))
166 last_token = 0;
167 yylval.string = strbuff.text;
168 RETURN( T_WORD );
171 return 0;
174 void lex_c89_cmnt_nl (void)
177 * if there are content before comment, matching '\n' same as '* /' for ending expr.
178 * if there are no content before comment, just matching '* /' for ending expr.
179 * this kind of implementation is in order to avoid conflect with shell cmd '/ *'.
181 * TBD:
182 * @ there should be a piece of code to caching token and string if in this condition.
183 * if '\n' detected as a ending expr for comment, get token from caching buffer.
185 if (g_cmnt_state == EN_CMNT_BEGIN)
186 g_cmnt_state=EN_CMNT;
188 if (g_content_before_cmnt != 0 && g_cmnt_state != EN_CMNT_EXTEND)
191 * treat '/*' as a normal string.
192 * put back other string to buffer.
193 * TBD: append to T_WORD or other token.
195 /* do not unput() idx=0, or it will be treated as a comment again. */
196 for (int i=cmnt_idx; i>0; i--)
197 unput(cmnt_buff[i]);
199 BEGIN(INITIAL);
200 // return T_CMNT_STR;
202 else
204 cmnt_buff[cmnt_idx]='\n';
205 cmnt_idx++;
206 cmnt_buff[cmnt_idx]=0;
210 void lex_c89_cmnt_tail (void)
212 if (g_cmnt_state != EN_CMNT_EXTEND)
214 g_cmnt_state=EN_CMNT_NONE;
215 printf("C89-CMNT: \n%s%s\n", cmnt_buff, yytext);
216 BEGIN(INITIAL);
217 // g_content_before_cmnt=0;
218 // return T_CMNT_STR;
220 else
222 if (yyleng == 2)
225 * ignore / * * / comment in / ** ** /
227 cmnt_buff[cmnt_idx]='*';
228 cmnt_idx++;
229 cmnt_buff[cmnt_idx]='/';
230 cmnt_idx++;
231 cmnt_buff[cmnt_idx]=0;
233 else
235 // '\*\*\/' ext comment
236 g_cmnt_state=EN_CMNT_NONE;
237 printf("C89-EXT-CMNT: \n%s%s\n", cmnt_buff, yytext);
238 BEGIN(INITIAL);
239 g_content_before_cmnt=0;
240 // return T_CMNT_STR;
246 enum ENM_PUNCT_TYPE {
247 EN_PUNCT_NORMAL = 1,
248 EN_PUNCT_CMNT,
249 EN_PUNCT_DQUOTE,
250 EN_PUNCT_SUBSCRIPT,
251 EN_PUNCT_HELPTEXT,
255 int lex_punct (int state)
257 const struct token_id *id = punct_look_up_token(yytext[0]);
259 if (id == NULL)
262 * no opr-char and pfx-char matched,
263 * punct will be seen as a normal char in string word.
265 //unput(yytext[0]);
266 append_string(&strbuff, yytext, yyleng);
267 return 0;
269 else if (TOKEN_FLAG(id->flags) == TF_OPCHAR)
271 /* return token if it is a signature */
272 yylval.id = id;
273 return id->token;
275 else if (TOKEN_FLAG(id->flags) == T_PFX_CHAR)
278 * if it is a T_PFX_CHAR char, entering state
279 * TBD:
281 // const struct token_id *id = punct_look_up_token(yytext, yyleng);
282 // push_state(curr_state);
283 // BEGIN(state);
284 return 0;
286 else
287 printf("err: (%s)should not be running here.\n", yytext);
289 return 0;
292 int lex_token_word (void)
295 * if there are chars before a symbol string,
296 * symbol is seen as a word.
298 if (!is_unallocated_string(&strbuff))
300 // printf("T_WORD:%s\n", yytext);
301 append_string(&strbuff, yytext, yyleng);
302 yylval.string = strbuff.text;
303 RETURN( T_WORD );
306 const struct token_id *id = token_id_lookup(yytext, yyleng);
308 // if (id)
309 // {
310 // printf("SYM(0x%02x): %s\n", yylval.id, yytext);
311 // }
312 // else
313 // printf("WORD: %s\n", yytext);
315 // TBD:
316 current_pos.file = current_file;
317 current_pos.lineno = yylineno;
318 // TBD: non-command token does not effective.
319 if (id && id->flags & TF_COMMAND)
321 if (id &&
322 (id->flags & TF_COMMAND ||
323 id->flags & TF_PARAM ||
324 id->flags & TF_OPTION))
327 yylval.id = id;
328 RETURN( id->token );
330 alloc_string(&strbuff, yytext, yyleng);
331 yylval.string = strbuff.text;
333 RETURN( T_WORD );
336 #endif