13 * Standard scanner routine used by parser.c and some output
14 * formats. It keeps a succession of temporary-storage strings in
15 * stdscan_tempstorage, which can be cleared using stdscan_reset.
17 static char **stdscan_tempstorage
= NULL
;
18 static int stdscan_tempsize
= 0, stdscan_templen
= 0;
19 #define STDSCAN_TEMP_DELTA 256
21 static void stdscan_pop(void)
23 nasm_free(stdscan_tempstorage
[--stdscan_templen
]);
26 void stdscan_reset(void)
28 while (stdscan_templen
> 0)
33 * Unimportant cleanup is done to avoid confusing people who are trying
34 * to debug real memory leaks
36 void stdscan_cleanup(void)
39 nasm_free(stdscan_tempstorage
);
42 static char *stdscan_copy(char *p
, int len
)
46 text
= nasm_malloc(len
+ 1);
47 strncpy(text
, p
, len
);
50 if (stdscan_templen
>= stdscan_tempsize
) {
51 stdscan_tempsize
+= STDSCAN_TEMP_DELTA
;
52 stdscan_tempstorage
= nasm_realloc(stdscan_tempstorage
,
56 stdscan_tempstorage
[stdscan_templen
++] = text
;
61 char *stdscan_bufptr
= NULL
;
62 int stdscan(void *private_data
, struct tokenval
*tv
)
64 char ourcopy
[MAX_KEYWORD
+ 1], *r
, *s
;
66 (void)private_data
; /* Don't warn that this parameter is unused */
68 while (isspace(*stdscan_bufptr
))
71 return tv
->t_type
= 0;
73 /* we have a token; either an id, a number or a char */
74 if (isidstart(*stdscan_bufptr
) ||
75 (*stdscan_bufptr
== '$' && isidstart(stdscan_bufptr
[1]))) {
76 /* now we've got an identifier */
80 if (*stdscan_bufptr
== '$') {
86 /* read the entire buffer to advance the buffer pointer but... */
87 while (isidchar(*stdscan_bufptr
))
90 /* ... copy only up to IDLEN_MAX-1 characters */
91 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
< IDLEN_MAX
?
92 stdscan_bufptr
- r
: IDLEN_MAX
- 1);
94 if (is_sym
|| stdscan_bufptr
- r
> MAX_KEYWORD
)
95 return tv
->t_type
= TOKEN_ID
; /* bypass all other checks */
97 for (s
= tv
->t_charptr
, r
= ourcopy
; *s
; s
++)
100 /* right, so we have an identifier sitting in temp storage. now,
101 * is it actually a register or instruction name, or what? */
102 if ((t
= nasm_token_hash(ourcopy
, tv
)) != -1)
105 return tv
->t_type
= TOKEN_ID
;
106 } else if (*stdscan_bufptr
== '$' && !isnumchar(stdscan_bufptr
[1])) {
108 * It's a $ sign with no following hex number; this must
109 * mean it's a Here token ($), evaluating to the current
110 * assembly location, or a Base token ($$), evaluating to
111 * the base of the current segment.
114 if (*stdscan_bufptr
== '$') {
116 return tv
->t_type
= TOKEN_BASE
;
118 return tv
->t_type
= TOKEN_HERE
;
119 } else if (isnumstart(*stdscan_bufptr
)) { /* now we've got a number */
122 r
= stdscan_bufptr
++;
123 while (isnumchar(*stdscan_bufptr
))
126 if (*stdscan_bufptr
== '.') {
128 * a floating point constant
131 while (isnumchar(*stdscan_bufptr
) ||
132 ((stdscan_bufptr
[-1] == 'e'
133 || stdscan_bufptr
[-1] == 'E')
134 && (*stdscan_bufptr
== '-' || *stdscan_bufptr
== '+'))) {
137 tv
->t_charptr
= stdscan_copy(r
, stdscan_bufptr
- r
);
138 return tv
->t_type
= TOKEN_FLOAT
;
140 r
= stdscan_copy(r
, stdscan_bufptr
- r
);
141 tv
->t_integer
= readnum(r
, &rn_error
);
144 return tv
->t_type
= TOKEN_ERRNUM
; /* some malformation occurred */
145 tv
->t_charptr
= NULL
;
146 return tv
->t_type
= TOKEN_NUM
;
147 } else if (*stdscan_bufptr
== '\'' || *stdscan_bufptr
== '"') { /* a char constant */
148 char quote
= *stdscan_bufptr
++, *r
;
150 r
= tv
->t_charptr
= stdscan_bufptr
;
151 while (*stdscan_bufptr
&& *stdscan_bufptr
!= quote
)
153 tv
->t_inttwo
= stdscan_bufptr
- r
; /* store full version */
154 if (!*stdscan_bufptr
)
155 return tv
->t_type
= TOKEN_ERRNUM
; /* unmatched quotes */
156 stdscan_bufptr
++; /* skip over final quote */
157 tv
->t_integer
= readstrnum(r
, tv
->t_inttwo
, &rn_warn
);
158 /* FIXME: rn_warn is not checked! */
159 return tv
->t_type
= TOKEN_NUM
;
160 } else if (*stdscan_bufptr
== ';') { /* a comment has happened - stay */
161 return tv
->t_type
= 0;
162 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '>') {
164 return tv
->t_type
= TOKEN_SHR
;
165 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '<') {
167 return tv
->t_type
= TOKEN_SHL
;
168 } else if (stdscan_bufptr
[0] == '/' && stdscan_bufptr
[1] == '/') {
170 return tv
->t_type
= TOKEN_SDIV
;
171 } else if (stdscan_bufptr
[0] == '%' && stdscan_bufptr
[1] == '%') {
173 return tv
->t_type
= TOKEN_SMOD
;
174 } else if (stdscan_bufptr
[0] == '=' && stdscan_bufptr
[1] == '=') {
176 return tv
->t_type
= TOKEN_EQ
;
177 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '>') {
179 return tv
->t_type
= TOKEN_NE
;
180 } else if (stdscan_bufptr
[0] == '!' && stdscan_bufptr
[1] == '=') {
182 return tv
->t_type
= TOKEN_NE
;
183 } else if (stdscan_bufptr
[0] == '<' && stdscan_bufptr
[1] == '=') {
185 return tv
->t_type
= TOKEN_LE
;
186 } else if (stdscan_bufptr
[0] == '>' && stdscan_bufptr
[1] == '=') {
188 return tv
->t_type
= TOKEN_GE
;
189 } else if (stdscan_bufptr
[0] == '&' && stdscan_bufptr
[1] == '&') {
191 return tv
->t_type
= TOKEN_DBL_AND
;
192 } else if (stdscan_bufptr
[0] == '^' && stdscan_bufptr
[1] == '^') {
194 return tv
->t_type
= TOKEN_DBL_XOR
;
195 } else if (stdscan_bufptr
[0] == '|' && stdscan_bufptr
[1] == '|') {
197 return tv
->t_type
= TOKEN_DBL_OR
;
198 } else /* just an ordinary char */
199 return tv
->t_type
= (uint8_t)(*stdscan_bufptr
++);