@@ -42,19 +42,19 @@ bool is_digit(char c)
42
42
43
43
bool is_hex (char c )
44
44
{
45
- return (( c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'f' ) || c == 'x' ||
46
- (c >= 'A' && c <= 'F' ) );
45
+ return (c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'f' ) ||
46
+ (c >= 'A' && c <= 'F' );
47
47
}
48
48
49
49
bool is_numeric (char buffer [])
50
50
{
51
51
bool hex = false;
52
52
int size = strlen (buffer );
53
53
54
- if (size > 2 )
55
- hex = ! strncmp ( buffer , "0x" , 2 ) ;
54
+ if (size > 2 && buffer [ 0 ] == '0' && ( buffer [ 1 ] | 32 ) == 'x' )
55
+ hex = true ;
56
56
57
- for (int i = 0 ; i < size ; i ++ ) {
57
+ for (int i = hex ? 2 : 0 ; i < size ; i ++ ) {
58
58
if (hex && !is_hex (buffer [i ]))
59
59
return false;
60
60
if (!hex && !is_digit (buffer [i ]))
@@ -177,9 +177,38 @@ token_t lex_token_internal(bool aliasing)
177
177
178
178
if (is_digit (next_char )) {
179
179
int i = 0 ;
180
- do {
180
+ token_str [i ++ ] = next_char ;
181
+ read_char (false);
182
+
183
+ if (token_str [0 ] == '0' && ((next_char | 32 ) == 'x' )) {
184
+ /* Hexadecimal: starts with 0x or 0X */
181
185
token_str [i ++ ] = next_char ;
182
- } while (is_hex (read_char (false)));
186
+
187
+ read_char (false);
188
+ if (!is_hex (next_char ))
189
+ error ("Invalid hex literal: expected hex digit after 0x" );
190
+
191
+ do {
192
+ token_str [i ++ ] = next_char ;
193
+ } while (is_hex (read_char (false)));
194
+
195
+ } else if (token_str [0 ] == '0' ) {
196
+ /* Octal: starts with 0 but not followed by 'x' */
197
+ while (is_digit (next_char )) {
198
+ if (next_char >= '8' )
199
+ error ("Invalid octal digit: must be in range 0-7" );
200
+ token_str [i ++ ] = next_char ;
201
+ read_char (false);
202
+ }
203
+
204
+ } else {
205
+ /* Decimal */
206
+ while (is_digit (next_char )) {
207
+ token_str [i ++ ] = next_char ;
208
+ read_char (false);
209
+ }
210
+ }
211
+
183
212
token_str [i ] = 0 ;
184
213
skip_whitespace ();
185
214
return T_numeric ;
0 commit comments