16 * Standard scanner routine used by parser.c and some output
17 * formats. It keeps a succession of temporary-storage strings in
18 * stdscan_tempstorage, which can be cleared using stdscan_reset.
20 static char **stdscan_tempstorage
= NULL
;
21 static int stdscan_tempsize
= 0, stdscan_templen
= 0;
22 #define STDSCAN_TEMP_DELTA 256
24 static void stdscan_pop(void)
26 nasm_free(stdscan_tempstorage
[--stdscan_templen
]);
29 void stdscan_reset(void)
31 while (stdscan_templen
> 0)
36 * Unimportant cleanup is done to avoid confusing people who are trying
37 * to debug real memory leaks
39 void stdscan_cleanup(void)
42 nasm_free(stdscan_tempstorage
);
45 static char *stdscan_copy(char *p
, int len
)
49 text
= nasm_malloc(len
+ 1);
50 strncpy(text
, p
, len
);
53 if (stdscan_templen
>= stdscan_tempsize
) {
54 stdscan_tempsize
+= STDSCAN_TEMP_DELTA
;
55 stdscan_tempstorage
= nasm_realloc(stdscan_tempstorage
,
59 stdscan_tempstorage
[stdscan_templen
++] = text
;
64 char *stdscan_bufptr
= NULL
;
65 int stdscan(void *private_data
, struct tokenval
*tv
)
67 char ourcopy
[MAX_KEYWORD
+ 1], *r
, *s
;
69 (void)private_data
; /* Don't warn that this parameter is unused */
71 while (isspace(*stdscan_bufptr
))
74 return tv
->t_type
= 0;
76 /* we have a token; either an id, a number or a char */
77 if (isidstart(*stdscan_bufptr
) ||
78 (*stdscan_bufptr
== '$' && isidstart(stdscan_bufptr
[1]))) {
79 /* now we've got an identifier */
82 if (*stdscan_bufptr
== '$') {
88 /* read the entire buffer to advance the buffer pointer but... */
89 while (isidchar(*stdscan_bufptr
))
92 /* ... copy only up to IDLEN_MAX-1 characters */
93 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
< IDLEN_MAX
?
94 stdscan_bufptr
- r
: IDLEN_MAX
- 1);
96 if (is_sym
|| stdscan_bufptr
- r
> MAX_KEYWORD
)
97 return tv
->t_type
= TOKEN_ID
; /* bypass all other checks */
99 for (s
= tv
->t_charptr
, r
= ourcopy
; *s
; s
++)
102 /* right, so we have an identifier sitting in temp storage. now,
103 * is it actually a register or instruction name, or what? */
104 return nasm_token_hash(ourcopy
, tv
);
105 } else if (*stdscan_bufptr
== '$' && !isnumchar(stdscan_bufptr
[1])) {
107 * It's a $ sign with no following hex number; this must
108 * mean it's a Here token ($), evaluating to the current
109 * assembly location, or a Base token ($$), evaluating to
110 * the base of the current segment.
113 if (*stdscan_bufptr
== '$') {
115 return tv
->t_type
= TOKEN_BASE
;
117 return tv
->t_type
= TOKEN_HERE
;
118 } else if (isnumstart(*stdscan_bufptr
)) { /* now we've got a number */
121 bool is_float
= false;
127 if (*stdscan_bufptr
== '$') {
133 c
= *stdscan_bufptr
++;
135 if (!is_hex
&& (c
== 'e' || c
== 'E')) {
137 if (*stdscan_bufptr
== '+' || *stdscan_bufptr
== '-') {
138 /* e can only be followed by +/- if it is either a
139 prefixed hex number or a floating-point number */
143 } else if (c
== 'H' || c
== 'h' || c
== 'X' || c
== 'x') {
145 } else if (c
== 'P' || c
== 'p') {
147 if (*stdscan_bufptr
== '+' || *stdscan_bufptr
== '-')
149 } else if (isnumchar(c
) || c
== '_')
156 stdscan_bufptr
--; /* Point to first character beyond number */
158 if (has_e
&& !is_hex
) {
159 /* 1e13 is floating-point, but 1e13h is not */
164 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
);
165 return tv
->t_type
= TOKEN_FLOAT
;
167 r
= stdscan_copy(r
, stdscan_bufptr
- r
);
168 tv
->t_integer
= readnum(r
, &rn_error
);
171 /* some malformation occurred */
172 return tv
->t_type
= TOKEN_ERRNUM
;
174 tv
->t_charptr
= NULL
;
175 return tv
->t_type
= TOKEN_NUM
;
177 } else if (*stdscan_bufptr
== '\'' || *stdscan_bufptr
== '"' ||
178 *stdscan_bufptr
== '`') {
179 /* a char constant */
182 stdscan_bufptr
= nasm_skip_string(tv
->t_charptr
= stdscan_bufptr
);
184 tv
->t_inttwo
= nasm_unquote(tv
->t_charptr
);
186 return tv
->t_type
= TOKEN_ERRNUM
; /* unmatched quotes */
187 stdscan_bufptr
++; /* skip over final quote */
188 tv
->t_integer
= readstrnum(tv
->t_charptr
, tv
->t_inttwo
, &rn_warn
);
189 /* Issue: can't readily check rn_warn, because we might be in
190 a db family context... */
191 return tv
->t_type
= TOKEN_NUM
;
192 } else if (*stdscan_bufptr
== ';') {
193 /* a comment has happened - stay */
194 return tv
->t_type
= 0;
195 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '>') {
197 return tv
->t_type
= TOKEN_SHR
;
198 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '<') {
200 return tv
->t_type
= TOKEN_SHL
;
201 } else if (stdscan_bufptr
[0] == '/' && stdscan_bufptr
[1] == '/') {
203 return tv
->t_type
= TOKEN_SDIV
;
204 } else if (stdscan_bufptr
[0] == '%' && stdscan_bufptr
[1] == '%') {
206 return tv
->t_type
= TOKEN_SMOD
;
207 } else if (stdscan_bufptr
[0] == '=' && stdscan_bufptr
[1] == '=') {
209 return tv
->t_type
= TOKEN_EQ
;
210 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '>') {
212 return tv
->t_type
= TOKEN_NE
;
213 } else if (stdscan_bufptr
[0] == '!' && stdscan_bufptr
[1] == '=') {
215 return tv
->t_type
= TOKEN_NE
;
216 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '=') {
218 return tv
->t_type
= TOKEN_LE
;
219 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '=') {
221 return tv
->t_type
= TOKEN_GE
;
222 } else if (stdscan_bufptr
[0] == '&' && stdscan_bufptr
[1] == '&') {
224 return tv
->t_type
= TOKEN_DBL_AND
;
225 } else if (stdscan_bufptr
[0] == '^' && stdscan_bufptr
[1] == '^') {
227 return tv
->t_type
= TOKEN_DBL_XOR
;
228 } else if (stdscan_bufptr
[0] == '|' && stdscan_bufptr
[1] == '|') {
230 return tv
->t_type
= TOKEN_DBL_OR
;
231 } else /* just an ordinary char */
232 return tv
->t_type
= (uint8_t)(*stdscan_bufptr
++);