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);
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 (nasm_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
++)
100 *r
++ = nasm_tolower(*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 quoted string */
180 char start_quote
= *stdscan_bufptr
;
181 tv
->t_charptr
= stdscan_bufptr
;
182 tv
->t_inttwo
= nasm_unquote(tv
->t_charptr
, &stdscan_bufptr
);
183 if (*stdscan_bufptr
!= start_quote
)
184 return tv
->t_type
= TOKEN_ERRSTR
;
185 stdscan_bufptr
++; /* Skip final quote */
186 return tv
->t_type
= TOKEN_STR
;
187 } else if (*stdscan_bufptr
== ';') {
188 /* a comment has happened - stay */
189 return tv
->t_type
= 0;
190 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '>') {
192 return tv
->t_type
= TOKEN_SHR
;
193 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '<') {
195 return tv
->t_type
= TOKEN_SHL
;
196 } else if (stdscan_bufptr
[0] == '/' && stdscan_bufptr
[1] == '/') {
198 return tv
->t_type
= TOKEN_SDIV
;
199 } else if (stdscan_bufptr
[0] == '%' && stdscan_bufptr
[1] == '%') {
201 return tv
->t_type
= TOKEN_SMOD
;
202 } else if (stdscan_bufptr
[0] == '=' && stdscan_bufptr
[1] == '=') {
204 return tv
->t_type
= TOKEN_EQ
;
205 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '>') {
207 return tv
->t_type
= TOKEN_NE
;
208 } else if (stdscan_bufptr
[0] == '!' && stdscan_bufptr
[1] == '=') {
210 return tv
->t_type
= TOKEN_NE
;
211 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '=') {
213 return tv
->t_type
= TOKEN_LE
;
214 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '=') {
216 return tv
->t_type
= TOKEN_GE
;
217 } else if (stdscan_bufptr
[0] == '&' && stdscan_bufptr
[1] == '&') {
219 return tv
->t_type
= TOKEN_DBL_AND
;
220 } else if (stdscan_bufptr
[0] == '^' && stdscan_bufptr
[1] == '^') {
222 return tv
->t_type
= TOKEN_DBL_XOR
;
223 } else if (stdscan_bufptr
[0] == '|' && stdscan_bufptr
[1] == '|') {
225 return tv
->t_type
= TOKEN_DBL_OR
;
226 } else /* just an ordinary char */
227 return tv
->t_type
= (uint8_t)(*stdscan_bufptr
++);