4 * @ use xxx_string() serial of functions, instead of buff opr. put caching buff
5 * pointer in state structure.
6 * @ lookup first token in comment string for MACRO.
7 * + define MACRO process (#define, #if, #include, #pragma)
8 * @ state-doc: c89-cmnt/ext-cmnt/dquoted-str/here-doc/help-doc/macro-code
9 * @ put global env into a struct.
12 * # $'', $ flag, and using.
14 * # ${}, $ flag, envar state.
15 * # foo () {}, func-flag, {} paired-token,
19 * @ readuntil-doc: c99-cmnt/sh-cmnt/backquoted-str/quoted-str/func-code
23 * bracket & quote & string.
24 * @ //, #, comment prefix (readuntil).
25 * # use caching buffer.
27 * @ '', ``, $'', $``, get string/code from pair-char to pair-char.
29 * @ "", $"", get string in string-state.
30 * @ (), $(), <(), >(), foo(){} code-block.
31 * @ $[], $(()), expr string.
32 * @ $XXX, ${}, parsing env-var format in envar-state.
35 #include "../StrBuff.h"
37 /* TBD: use CACHING_STR */
46 char *token_str
= NULL
;
47 TOKEN_ID_PROC pfn_token_proc
= NULL
;
49 #define BYTE_MASK(n) ((1<<((n)<<3)) - 1)
55 char mask_char
= '\\';
59 char endstr
[MAX_RSVWD_LEN
];
62 extern void token_ungetc (char c
, SRC_FILE_DESC
*pfile
);
64 char read_getc (SRC_FILE_DESC
*pfile
)
66 extern char token_getc (SRC_FILE_DESC
*pfile
);
70 if ((pfile
->iTokenStrLen
- pfile
->iPrevIdx
) > 0)
72 c
= pfile
->aTokenStrBuff
[pfile
->iPrevIdx
];
76 c
= token_getc(pfile
);
81 void read_ungetc (char c
, SRC_FILE_DESC
*pfile
)
83 if (pfile
->iPrevIdx
> 0)
86 pfile
->aTokenStrBuff
[pfile
->iPrevIdx
] = c
;
89 token_ungetc(c
, pfile
);
92 int read_token_str (SRC_FILE_DESC
*pfile
)
98 * \retval: 0, in normal.
101 int readuntil (SRC_FILE_DESC
*pfile
)
107 /* cotinus string must be less then 10MByte. */
108 while (idx
< 0xa00000)
110 c
= read_getc(pfile
);
112 // printf("c=%c(%x)(%x)\n", c, c, endstr[0]);
113 // ignore NULL char in txt src file.
116 else if (c
== mask_char
)
118 c
= read_getc(pfile
);
120 else if (c
== pfx_char
)
124 else if (c
== pair_begin
)
128 else if (c
== pair_end
)
142 else if (endstr
[0] == 0)
144 read_ungetc(c
, pfile
);
145 if (count
== 0) break;
147 else if (c
== endstr
[0])
151 while (i
< endstr_len
&& (c
=read_getc(pfile
)) == endstr
[i
]) i
++;
155 read_ungetc(c
, pfile
);
156 while (--i
) read_ungetc(endstr
[i
], pfile
);
158 // memcpy(&str_buff[str_idx], endstr, i);
160 // str_buff[str_idx] = 0;
164 if (count
== 0) break;
168 //str_buff[str_idx] = c;
170 append_string(&strbuff
, &c
, 1);
171 // str_buff[str_idx] = 0;
173 //str_buff[str_idx] = 0;
174 // if (str_idx == sizeof(str_buff)-1)
181 * c99 comment string proc.
183 int on_c99_cmnt_proc (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
185 printf("################################################ c99 comment\n");
195 /* TBD: use xxx_string() function to store txt. */
196 /* ignore overlimited comment string. */
197 //while (readuntil(gpcurr_src_file) == -1) str_idx = 0;
198 readuntil(gpcurr_src_file
);
200 // printf("str_idx = %d\n", str_idx);
208 * sh comment string proc.
210 int on_sh_cmnt_proc (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
215 int Macro_TokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
);
217 printf("################################################ sh comment\n");
224 c
= read_getc(psrcfile
);
225 str_buff
[str_idx
] = c
;
227 str_buff
[str_idx
] = 0;
229 if (c
== ' ' || c
== '\t')
234 str_buff
[str_idx
] = 0;
236 ret
= Macro_TokenProc(&str_buff
[1], str_idx
-1, gpcurr_src_file
->pstCurrTokenState
);
239 * no preproc cmd matching, it's a sh-cmnt.
240 * otherwize, return token.
245 /* if it's not macro defination, append to T_WORD buffer. */
246 append_string(&strbuff
, str_buff
, str_idx
);
256 /* ignore overlimited comment string. */
257 //while (readuntil(gpcurr_src_file) == -1) str_idx = 0;
258 readuntil(gpcurr_src_file
);
264 char trans_char_tbl
[128] = {
265 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
268 /* ' ' ! " # $ % & ' ( ) * + , - . / */
269 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2
270 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
271 -16,-17,-18, -19,-20, -21,-22, -23,-24, -25,0, 0, 0, 0, 0, 0, // 3
272 /* @ A B C D E F G H I J K L M N O */
273 0, -26,-27, -28,-29, -30,-31, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
274 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 5
276 /* ` a b c d e f g h i j k l m n o */
277 0, -26,-27, -28,-29, -30,'\f', 0, 0, 0, 0, 0, 0, 0, '\n', 0, // 6
278 /* p q r s t u v w x y z { | } ~ */
279 0, 0, '\r', 0, '\t', 0, '\v', 0, -127, 0, 0, 0, 0, 0, 0, 0, // 7
282 int str2hex (char *str
, int iidx
, int oidx
)
285 unsigned long long val
= 0;
287 char *pbuff
= &str
[iidx
];
293 * max hex string len is 32, it's a 128bit hex value string.
297 c
= trans_char_tbl
[pbuff
[i
]>>1];
298 if (c
< 0 || c
== '\f')
300 tmp
[i
] = ((c
<-15) ? (c
+16) : 0x0f);
308 for (; i
>=0; i
--) val
+= tmp
[i
]<<(chars
- i
);
312 for (i
=0; i
<chars
>>2; i
++)
314 pbuff
[i
] = (char)(val
>>i
);
320 int translate_char (char *str
, int len
)
331 for (i
=0; i
<len
; i
++)
338 c
= trans_char_tbl
[c
&0x7f];
339 str
[j
] = (c
> 0) ? c
: str
[i
];
343 c
= str2hex(str
, i
, j
);
344 j
+= (int)((c
+1)>>1);
362 int on_quote (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
364 printf("################################################ single quote\n");
374 /* ignore overlimited comment string. */
375 //while (readuntil(gpcurr_src_file) == -1) str_idx = 0;
376 readuntil(gpcurr_src_file
);
378 /* TBD: with $ flag, translate \X to corresponding char. */
379 if (CHK_STATE_FLAG(psrcfile
->pstCurrTokenState
, TF_DOLLAR
))
381 //CLR_STATE_FLAG(psrcfile->pstCurrTokenState, TF_DOLLAR);
382 strbuff
.text_size
= translate_char(strbuff
.text
, strbuff
.text_size
);
383 strbuff
.text
[strbuff
.text_size
] = 0;
384 STATE_RECOVERY(psrcfile
);
393 int on_bquote (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
395 printf("################################################ back quote\n");
404 /* ignore overlimited comment string. */
405 //while (readuntil(gpcurr_src_file) == -1) str_idx = 0;
406 readuntil(gpcurr_src_file
);
408 return T_WORD_BACK_QUOTE
;
411 int on_ext_cmnt (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
413 printf("################################################ ext comment\n");
415 if (evt
== EVT_ON_ENTER
)
420 else if (evt
== EVT_ON_EXIT
)
422 // do cmnt proc and return token.
427 int on_c89_cmnt (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
429 printf("a ################################################ c89 comment\n");
431 if (evt
== EVT_ON_ENTER
)
434 //strcpy(str_buff, "/*");
436 append_string(&strbuff
, "/*", 2);
439 else if (evt
== EVT_ON_EXIT
)
442 // do cmnt proc and return token.
443 //strcat(str_buff, "*/");
445 append_string(&strbuff
, "*/", 2);
447 // printf("str_idx=%d; str_buff='%s'\n", str_idx, str_buff);
451 ////////////////////////////////////
464 append_string(&strbuff
, "/*", 2);
466 /* TBD: use xxx_string() function to store txt. */
467 /* ignore overlimited comment string. */
468 //while (readuntil(gpcurr_src_file) == -1) str_idx = 0;
469 readuntil(gpcurr_src_file
);
471 //strcat(str_buff, "*/");
472 append_string(&strbuff
, "*/", 2);
473 // printf("str_idx = %d\n", str_idx);
480 int on_dquote (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
482 printf("################################################ dual quote\n");
492 * code-block, sub-script. stdout is main-script's stdout.
494 * code-block, sub-script. stdout is cmd-line buffer.
496 int on_cb_bracket1 (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
498 printf("################################################ bracket1 ()\n");
502 /* func param list */
507 int on_bracket2 (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
509 printf("################################################ bracket2 (())\n");
511 if (evt
== EVT_ON_ENTER
)
513 if (CHK_STATE_FLAG(psrcfile
->pstCurrTokenState
, TF_DOLLAR
))
515 STATE_RECOVERY(psrcfile
);
516 STATE_SWITCH(psrcfile
, STATE_EXPR
);
520 else if (evt
== EVT_ON_EXIT
)
522 if (CHK_STATE_FLAG(psrcfile
->pstCurrTokenState
, TF_DOLLAR
))
524 STATE_RECOVERY(psrcfile
);
532 int on_dbracket1 (int evt
, int token
, register const char *str
, register unsigned int len
, SRC_FILE_DESC
*psrcfile
)
534 printf("################################################ dbracket1 [[]]\n");
536 if (evt
== EVT_ON_ENTER
)
538 if (CHK_STATE_FLAG(psrcfile
->pstCurrTokenState
, TF_DOLLAR
))
540 STATE_RECOVERY(psrcfile
);
541 STATE_SWITCH(psrcfile
, STATE_EXPR
);
545 else if (evt
== EVT_ON_EXIT
)
547 if (STATE_CHK(psrcfile
, STATE_EXPR
))
549 STATE_RECOVERY(psrcfile
);
565 char *state_token_str
= NULL
;
566 TOKEN_ID_PROC pfn_pair_token_proc
= NULL
;
571 * @ use three func to process word/pair/other token.
573 int id_str_matching (char *str
, int str_len
, const struct token_id
*pstTokenIdTbl
, short *ptoken_len_tbl
)
579 int str_idx_bak
= str_idx
;
581 if (!pstTokenIdTbl
) return 0;
583 // printf("%s: %s(%s)(%d)\n", __FILE__, __FUNCTION__, str, str_len);
584 // printf("pstTokenIdTbl = 0x%08x\n", pstTokenIdTbl);
585 // printf("ptoken_len_tbl = 0x%08x\n", ptoken_len_tbl);
586 // printf("len = 0x%08x\n", len);
588 for (k
=0; len
>0; k
++)
590 plentbl
= ptoken_len_tbl
;
591 // printf("str=%s(%d)\n", str, k);
592 for (i
=0; i
<MAX_TOKEN_ID_TBL_SIZE
; i
++)
594 // printf("XXXXXXXXXXXXXXXXXXXXXXXXXX\n");
595 if (pstTokenIdTbl
[i
].name
== NULL
&&
596 pstTokenIdTbl
[i
].tokenstr
== NULL
&&
597 pstTokenIdTbl
[i
].token
== 0 &&
598 pstTokenIdTbl
[i
].flags
== 0 &&
599 pstTokenIdTbl
[i
].proc
== NULL
)
601 // printf("pstTokenIdTbl[%d].name = %s(0x%08x)(%s)\n", i, "token", pstTokenIdTbl[i].name, pstTokenIdTbl[i].tokenstr);
603 register int token_id_len
= *plentbl
;
606 if (len
< token_id_len
) continue;
608 /* TBD: normally, this addr area 'chars[3]' should not be used for user accessing. */
609 pstr
= (pstTokenIdTbl
[i
].chars
[3]) ? pstTokenIdTbl
[i
].name
: (char *)&pstTokenIdTbl
[i
].name
;
610 // printf("@%d.[%s]: %s(%d)(%d)\n", i, pstTokenIdTbl[i].tokenstr, pstr, token_id_len, pstTokenIdTbl[i].token);
612 for (j
=0; j
<token_id_len
; j
++)
614 if (pstr
[j
] != str
[j
])
617 if (j
== token_id_len
)
619 // printf("@%d.[%s]: %s(%d)(%d)[%s]\n", i, pstTokenIdTbl[i].tokenstr, pstr, token_id_len, pstTokenIdTbl[i].token, str);
621 if (pstr
[token_id_len
] != 0)
623 state_token_str
= &pstr
[token_id_len
];
624 pfn_pair_token_proc
= pstTokenIdTbl
[i
].proc
;
627 pfn_token_proc
= (TOKEN_ID_PROC
)pstTokenIdTbl
[i
].proc
;
628 token_len
= token_id_len
;
630 token_state
= pstTokenIdTbl
[i
].state
;
633 recent_token
= pstTokenIdTbl
[i
].token
;
637 return pstTokenIdTbl
[i
].token
;
639 else if (pstr
[token_id_len
])
641 for (h
=0,j
=token_id_len
; j
<(token_id_len
<<1); h
++,j
++)
643 if (pstr
[j
] != str
[h
])
646 if (j
== (token_id_len
<<1))
648 // printf("&%d.[%s]: %s(%d)(%d)\n", i, pstTokenIdTbl[i].tokenstr, &pstr[token_id_len], token_id_len, pstTokenIdTbl[i].token+1);
650 pfn_token_proc
= (TOKEN_ID_PROC
)pstTokenIdTbl
[i
].proc
;
651 token_len
= token_id_len
;
652 token_str
= &pstr
[token_id_len
];
653 token_state
= pstTokenIdTbl
[i
].state
;
656 recent_token
= pstTokenIdTbl
[i
].token
+1;
660 return pstTokenIdTbl
[i
].token
+1;
664 str_buff
[str_idx
] = *str
;
667 fprintf(stderr
, "err: word string buffer is over limited.\n");
672 str_idx
= str_idx_bak
;
678 * @ only matching whole word.
680 int rsvwd_str_matching (char *str
, int str_len
, const struct token_id
*pstTokenIdTbl
, short *ptoken_len_tbl
)
686 int str_idx_bak
= str_idx
;
688 if (!pstTokenIdTbl
) return 0;
690 // printf("%s: %s(%s)(%d)\n", __FILE__, __FUNCTION__, str, str_len);
691 // printf("pstTokenIdTbl = 0x%08x\n", pstTokenIdTbl);
692 // printf("ptoken_len_tbl = 0x%08x\n", ptoken_len_tbl);
693 // printf("len = 0x%08x\n", len);
695 for (k
=0; len
>0; k
++)
697 plentbl
= ptoken_len_tbl
;
698 // printf("str=%s(%d)\n", str, k);
699 for (i
=0; i
<MAX_TOKEN_ID_TBL_SIZE
; i
++)
701 // printf("XXXXXXXXXXXXXXXXXXXXXXXXXX\n");
702 if (pstTokenIdTbl
[i
].name
== NULL
&&
703 pstTokenIdTbl
[i
].tokenstr
== NULL
&&
704 pstTokenIdTbl
[i
].token
== 0 &&
705 pstTokenIdTbl
[i
].flags
== 0 &&
706 pstTokenIdTbl
[i
].proc
== NULL
)
708 // printf("pstTokenIdTbl[%d].name = %s(0x%08x)(%s)\n", i, "token", pstTokenIdTbl[i].name, pstTokenIdTbl[i].tokenstr);
710 register int token_id_len
= *plentbl
;
713 if (len
< token_id_len
) continue;
715 /* TBD: normally, this addr area 'chars[3]' should not be used for user accessing. */
716 pstr
= (pstTokenIdTbl
[i
].chars
[3]) ? pstTokenIdTbl
[i
].name
: (char *)&pstTokenIdTbl
[i
].name
;
717 // printf("@%d.[%s]: %s(%d)(%d)\n", i, pstTokenIdTbl[i].tokenstr, pstr, token_id_len, pstTokenIdTbl[i].token);
719 for (j
=0; j
<token_id_len
; j
++)
721 if (pstr
[j
] != str
[j
])
724 if (j
== token_id_len
)
726 // printf("@%d.[%s]: %s(%d)(%d)[%s]\n", i, pstTokenIdTbl[i].tokenstr, pstr, token_id_len, pstTokenIdTbl[i].token, str);
728 if (pstr
[token_id_len
] != 0)
730 state_token_str
= &pstr
[token_id_len
];
731 pfn_pair_token_proc
= pstTokenIdTbl
[i
].proc
;
734 pfn_token_proc
= (TOKEN_ID_PROC
)pstTokenIdTbl
[i
].proc
;
735 token_len
= token_id_len
;
737 token_state
= pstTokenIdTbl
[i
].state
;
740 recent_token
= pstTokenIdTbl
[i
].token
;
744 return pstTokenIdTbl
[i
].token
;
746 else if (pstr
[token_id_len
])
748 for (h
=0,j
=token_id_len
; j
<(token_id_len
<<1); h
++,j
++)
750 if (pstr
[j
] != str
[h
])
753 if (j
== (token_id_len
<<1))
755 // printf("&%d.[%s]: %s(%d)(%d)\n", i, pstTokenIdTbl[i].tokenstr, &pstr[token_id_len], token_id_len, pstTokenIdTbl[i].token+1);
757 pfn_token_proc
= (TOKEN_ID_PROC
)pstTokenIdTbl
[i
].proc
;
758 token_len
= token_id_len
;
759 token_str
= &pstr
[token_id_len
];
760 token_state
= pstTokenIdTbl
[i
].state
;
763 recent_token
= pstTokenIdTbl
[i
].token
+1;
767 return pstTokenIdTbl
[i
].token
+1;
771 str_buff
[str_idx
] = *str
;
774 fprintf(stderr
, "err: word string buffer is over limited.\n");
779 str_idx
= str_idx_bak
;
786 * general proc for text/string. it store string into text buffer.
792 int Txt_TokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
796 //strcat(str_buff, blank_str);
797 //str_idx += strlen(blank_str);
798 append_string(&strbuff
, blank_str
, -1);
801 //strcat(str_buff, str);
802 append_string(&strbuff
, str
, -1);
803 pstTokenState
->token_len
= strlen(str
);
804 //str_idx += pstTokenState->token_len;
806 // printf("Txt_TokenProc(%s) (%s) %d, %d\n", str_buff, str, pstTokenState->token_len, str_idx);
810 int String_PunctTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
817 * general proc for text/string. it store string into text buffer.
823 int Macro_TokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
828 char MacroCMDBuff
[16];
830 // printf("Macro_TokenProc(%s)\n", str);
832 /* matching first following word for marco cmd */
833 for (i
=0; i
< strbuff
.text_size
&& i
<sizeof(MacroCMDBuff
); i
++)
835 if (strbuff
.text
[i
] == ' ') break;
836 MacroCMDBuff
[i
] = strbuff
.text
[i
];
840 // printf("%s(%d)\n", MacroCMDBuff, i);
842 str_idx_bak
= str_idx
;
845 ret
= rsvwd_str_matching (
851 str_idx
= str_idx_bak
;
857 * set mask flag/state, the following char will be treated as a WORD char.
859 int Def_MaskTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
861 printf("OnMaskTokenProc(%s)\n", str
);
868 * lookup reserved-word table, maching correspoding token.
869 * or return as a normal T_WORD.
871 int Def_WordTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
873 int len
= strlen(str
);
876 printf("OnWordTokenProc(%s)\n", str
);
878 /* xxx: macro define replacing. */
880 pfn_token_proc
= NULL
;
885 //str_buff[str_idx] = *str;
887 //str_buff[str_idx] = 0;
888 append_string(&strbuff
, str
, 1);
893 if (is_empty_string(&strbuff
))
895 /* T_WORD buffer is empty, do token matching work. */
896 ret
= rsvwd_str_matching (
899 pstTokenState
->pstStateInfo
->astTokenTypeTbl
[TYPE_WORD
].pstTokenTbl
,
900 pstTokenState
->pstStateInfo
->api16TokenNameLen
[TYPE_WORD
]);
902 pstTokenState
->pfnTokenProc
= pfn_token_proc
;
903 // pstTokenState->recent_token = recent_token;
905 pstTokenState
->token_len
= token_len
;
907 pstTokenState
->token_str
= token_str
;
909 /* return valid token */
910 if (ret
!= 0) return ret
;
913 /* otherwize, append it to T_WORD buffer. */
914 //strcpy(str_buff, str);
915 //str_idx = strlen(str);
916 append_string(&strbuff
, str
, -1);
920 #define UINT_MAX (-1 & (~0x80000000))
925 * there are three kinds of punct string.
926 * @ prefix-char, this char/str let the program entering a new state.
927 * @ pair-char, also can trig new state.
928 * @ operation-char, for grammar element token.
929 * @ normal-char, T_WORD.
930 * in this function, it recognize and split string, to get one token by looking
931 * up pfx-char/pair-char/opr-char table.
933 int Def_PunctTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
938 int opr_token
, pfx_token
, pair_token
;
939 int opr_idx
, pfx_idx
, pair_idx
;
940 int opr_len
, pfx_len
, pair_len
;
941 TOKEN_ID_PROC opr_proc
= NULL
, pfx_proc
= NULL
, pair_proc
= NULL
;
943 char *tmp_token_str
[3];
945 printf("OnPunctTokenProc(%s)\n", str
);
947 pfn_token_proc
= NULL
;
954 //str_buff[str_idx] = *str;
956 //str_buff[str_idx] = 0;
957 append_string(&strbuff
, str
, 1);
961 pfx
= id_str_matching (
964 pstTokenState
->pstStateInfo
->astTokenTypeTbl
[TYPE_PFX
].pstTokenTbl
,
965 pstTokenState
->pstStateInfo
->api16TokenNameLen
[TYPE_PFX
]);
966 pfx_token
= recent_token
;
971 pfx_proc
= pfn_token_proc
;
973 tmp_token_str
[0] = token_str
;
974 // printf("pfx = %d\n", pfx);
975 // printf("str_idx = %d\n", str_idx);
976 // printf("pfx_len = %d\n", pfx_len);
979 pair
= id_str_matching (
982 pstTokenState
->pstStateInfo
->astTokenTypeTbl
[TYPE_PAIR
].pstTokenTbl
,
983 pstTokenState
->pstStateInfo
->api16TokenNameLen
[TYPE_PAIR
]);
984 pair_token
= recent_token
;
985 pair_len
= token_len
;
989 pair_proc
= pfn_token_proc
;
991 tmp_token_str
[1] = token_str
;
992 // printf("pair = %d\n", pair);
993 // printf("str_idx = %d\n", str_idx);
994 // printf("pair_len = %d\n", pair_len);
996 opr
= id_str_matching (
999 pstTokenState
->pstStateInfo
->astTokenTypeTbl
[TYPE_PUNCT
].pstTokenTbl
,
1000 pstTokenState
->pstStateInfo
->api16TokenNameLen
[TYPE_PUNCT
]);
1001 opr_token
= recent_token
;
1002 opr_len
= token_len
;
1006 opr_proc
= pfn_token_proc
;
1008 tmp_token_str
[2] = token_str
;
1009 // printf("opr = %d\n", opr);
1010 // printf("str_idx = %d\n", str_idx);
1011 // printf("opr_len = %d\n", opr_len);
1021 pair_idx
= UINT_MAX
;
1030 // printf("pfx = %d,%d,%d\n", pfx, pfx_idx, pfx_len);
1031 // printf("pair = %d,%d,%d\n", pair, pair_idx, pair_len);
1032 // printf("opr = %d,%d,%d\n", opr, opr_idx, opr_len);
1034 pfx_idx
<= pair_idx
&& pfx_idx
<= opr_idx
&&
1035 pfx_len
>= pair_len
&& pfx_len
>= opr_len
)
1037 // printf("xxxxxxxxxxxxxxxx1(%d)\n", pfx);
1038 recent_token
= pfx_token
;
1039 token_len
= pfx_len
;
1041 pfn_token_proc
= pfx_proc
;
1042 pstTokenState
->token_str
= tmp_token_str
[0];
1046 pair_idx
<= pfx_idx
&& pair_idx
<= opr_idx
&&
1047 pair_len
>= pfx_len
&& pair_len
>= opr_len
)
1049 /* entering paired token, and need to be switched to new state. */
1050 if (token_state
!= 0)
1052 if (state_token_str
)
1054 pstTokenState
->pfnPairTokenProc
= pfn_pair_token_proc
;
1055 pstTokenState
->state_token_str
= state_token_str
;
1056 pstTokenState
->paired_token
= pair
+1;
1059 pstTokenState
->paired_state
= token_state
;
1061 // printf("xxxxxxxxxxxxxxxx2 (%s) pstTokenState->paired_token (%d)\n", pstTokenState->state_token_str, pstTokenState->paired_token);
1062 recent_token
= pair_token
;
1063 token_len
= pair_len
;
1065 pfn_token_proc
= pair_proc
;
1066 pstTokenState
->token_str
= tmp_token_str
[1];
1070 opr_idx
<= pfx_idx
&& opr_idx
<= pair_idx
&&
1071 opr_len
>= pfx_len
&& opr_len
>= pair_len
)
1073 // printf("xxxxxxxxxxxxxxxx3(%d)\n", opr);
1074 recent_token
= opr_token
;
1075 token_len
= opr_len
;
1077 pfn_token_proc
= opr_proc
;
1078 pstTokenState
->token_str
= tmp_token_str
[2];
1081 // printf("matching ret = %d\n", ret);
1083 pstTokenState
->pfnTokenProc
= pfn_token_proc
;
1084 pstTokenState
->recent_token
= recent_token
;
1086 pstTokenState
->token_len
= token_len
;
1088 state_token_str
= 0;
1091 if (ret
== pfx
&& ret
!= 0)
1097 /* no token matching, append it to T_WORD buffer. */
1100 //strcpy(str_buff, str);
1101 //str_idx = strlen(str);
1102 append_string(&strbuff
, str
, -1);
1104 // printf("return T_WORD(%s)\n", str);
1108 /* token matched, append string before token to T_WORD buffer */
1111 //memcpy(str_buff, str, str_idx);
1112 //str_buff[str_idx] = 0;
1113 append_string(&strbuff
, str
, str_idx
);
1115 * append T_WORD part string len for token_len,
1116 * it's used for readed token string length.
1118 pstTokenState
->token_len
+= str_idx
;
1120 pstTokenState
->recent_token
= ret
;
1124 // printf("return ret = %d\n", ret);
1129 int Def_SpaceTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1131 printf("OnSpaceTokenProc(%s)\n", str
);
1136 //str_buff[str_idx] = *str;
1138 //str_buff[str_idx] = 0;
1139 append_string(&strbuff
, str
, 1);
1146 strcpy(blank_str
, str
);
1147 //str_idx = strlen(str);
1149 pstTokenState
->token_len
= strlen(str
) + 1;
1154 int Def_NewlineTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1156 printf("OnNewlineTokenProc(%s)\n", str
);
1161 //str_buff[str_idx] = *str;
1163 //str_buff[str_idx] = 0;
1164 append_string(&strbuff
, str
, 1);
1170 pstTokenState
->token_len
= 1;
1171 pstTokenState
->nl_cnt
= strlen(str
) - 1;
1172 // printf("NL(%d)\n", pstTokenState->nl_cnt);
1174 /* in cmd state, new line reset token id num */
1177 if (STATE_VCHK(pstTokenState, STATE_CMD))
1178 pstTokenState->iTokenIdNum = 0;
1184 int Def_EOFTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1186 printf("OnEOFTokenProc(%s)\n", str
);
1194 int Envar_WordTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1196 int ret
= Def_WordTokenProc(str
, type
, pstTokenState
);
1198 /* store to prev state. */
1200 if (STATE_VCHK(pstTokenState, STATE_DOLLAR))
1202 STATE_RECOVERY(gpcurr_src_file);
1206 /* envar expanssion */
1209 /* ungetc() to buffer */
1221 int Envar_PunctTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1223 int ret
= Def_PunctTokenProc(str
, type
, pstTokenState
);
1225 /* store to prev state. */
1227 if (STATE_VCHK(pstTokenState, STATE_DOLLAR))
1229 STATE_RECOVERY(gpcurr_src_file);
1234 if (ret
== T_LBRACKET1
)
1236 STATE_SWITCH(gpcurr_src_file
, STATE_CODE
);
1240 else if (ret
== T_DLBRACKET1
|| ret
== T_LBRACKET2
)
1242 STATE_SWITCH(gpcurr_src_file
, STATE_EXPR
);
1246 else if (ret
== T_LBRACKET3
)
1248 /* XXX: envar expanssion */
1259 /* XXX: expanssion */
1272 int Block_PunctTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1274 int ret
= Def_WordTokenProc(str
, type
, pstTokenState
);
1276 Txt_TokenProc(str
, type
, pstTokenState
);
1278 /* TBD: append string to buffer */
1286 int Cmnt_WordTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1288 //int ret = Def_WordTokenProc(str, type, pstTokenState);
1290 /* TBD: append string to buffer */
1292 return Txt_TokenProc(str
, type
, pstTokenState
);
1300 int Cmnt_PunctTokenProc (char *str
, TOKEN_TYPE type
, TOKEN_PROC_STATE
*pstTokenState
)
1302 //int ret = Def_PunctTokenProc(str, type, pstTokenState);
1304 /* TBD: append string to buffer */
1306 return Txt_TokenProc(str
, type
, pstTokenState
);
1311 #include "codeblock.c"