@@ -42,19 +42,19 @@ bool is_digit(char c)
4242
4343bool is_hex (char c )
4444{
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' );
4747}
4848
4949bool is_numeric (char buffer [])
5050{
5151 bool hex = false;
5252 int size = strlen (buffer );
5353
54- if (size > 2 )
55- hex = ! strncmp ( buffer , "0x" , 2 ) ;
54+ if (size > 2 && buffer [ 0 ] == '0' && ( buffer [ 1 ] | 32 ) == 'x' )
55+ hex = true ;
5656
57- for (int i = 0 ; i < size ; i ++ ) {
57+ for (int i = hex ? 2 : 0 ; i < size ; i ++ ) {
5858 if (hex && !is_hex (buffer [i ]))
5959 return false;
6060 if (!hex && !is_digit (buffer [i ]))
@@ -177,9 +177,38 @@ token_t lex_token_internal(bool aliasing)
177177
178178 if (is_digit (next_char )) {
179179 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 */
181185 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+
183212 token_str [i ] = 0 ;
184213 skip_whitespace ();
185214 return T_numeric ;
0 commit comments