* src/lex.c (literalchar): Simply return the char you decoded, non

longer mess around with obstacks and int pointers.
Adjust all callers.
This commit is contained in:
Akim Demaille
2001-12-14 16:03:28 +00:00
parent 92790e5bf0
commit 2648a72df7
4 changed files with 53 additions and 58 deletions

View File

@@ -1,3 +1,9 @@
2001-12-14 Akim Demaille <akim@epita.fr>
* src/lex.c (literalchar): Simply return the char you decoded, non
longer mess around with obstacks and int pointers.
Adjust all callers.
2001-12-14 Akim Demaille <akim@epita.fr> 2001-12-14 Akim Demaille <akim@epita.fr>
* src/lex.c (literalchar): Don't escape the special characters, * src/lex.c (literalchar): Don't escape the special characters,

View File

@@ -140,67 +140,62 @@ xgetc (FILE *f)
} }
/*-----------------------------------------------------------------. /*---------------------------------------------------------------.
| Read one literal character from FINPUT. Process \-escapes. | | Read one literal character from FINPUT, process \-escapes, and |
| Append the char to OUT and assign it *PCODE. Return 1 unless the | | return the character. |
| character is an unescaped `term' or \n report error for \n. | `---------------------------------------------------------------*/
`-----------------------------------------------------------------*/
int char
literalchar (struct obstack *out, int *pcode, char term) literalchar (void)
{ {
int c; int c;
int code; int res;
int wasquote = 0;
c = xgetc (finput); c = xgetc (finput);
if (c == '\n') if (c == '\n')
{ {
complain (_("unescaped newline in constant")); complain (_("unescaped newline in constant"));
ungetc (c, finput); ungetc (c, finput);
code = '?'; res = '?';
wasquote = 1;
} }
else if (c != '\\') else if (c != '\\')
{ {
code = c; res = c;
if (c == term)
wasquote = 1;
} }
else else
{ {
c = xgetc (finput); c = xgetc (finput);
if (c == 't') if (c == 't')
code = '\t'; res = '\t';
else if (c == 'n') else if (c == 'n')
code = '\n'; res = '\n';
else if (c == 'a') else if (c == 'a')
code = '\007'; res = '\007';
else if (c == 'r') else if (c == 'r')
code = '\r'; res = '\r';
else if (c == 'f') else if (c == 'f')
code = '\f'; res = '\f';
else if (c == 'b') else if (c == 'b')
code = '\b'; res = '\b';
else if (c == 'v') else if (c == 'v')
code = '\013'; res = '\013';
else if (c == '\\') else if (c == '\\')
code = '\\'; res = '\\';
else if (c == '\'') else if (c == '\'')
code = '\''; res = '\'';
else if (c == '\"') else if (c == '\"')
code = '\"'; res = '\"';
else if (c <= '7' && c >= '0') else if (c <= '7' && c >= '0')
{ {
code = 0; res = 0;
while (c <= '7' && c >= '0') while (c <= '7' && c >= '0')
{ {
code = (code * 8) + (c - '0'); res = (res * 8) + (c - '0');
if (code >= 256 || code < 0) if (res >= 256 || res < 0)
{ {
complain (_("octal value outside range 0...255: `\\%o'"), complain (_("octal value outside range 0...255: `\\%o'"),
code); res);
code &= 0xFF; res &= 0xFF;
break; break;
} }
c = xgetc (finput); c = xgetc (finput);
@@ -210,21 +205,21 @@ literalchar (struct obstack *out, int *pcode, char term)
else if (c == 'x') else if (c == 'x')
{ {
c = xgetc (finput); c = xgetc (finput);
code = 0; res = 0;
while (1) while (1)
{ {
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
code *= 16, code += c - '0'; res *= 16, res += c - '0';
else if (c >= 'a' && c <= 'f') else if (c >= 'a' && c <= 'f')
code *= 16, code += c - 'a' + 10; res *= 16, res += c - 'a' + 10;
else if (c >= 'A' && c <= 'F') else if (c >= 'A' && c <= 'F')
code *= 16, code += c - 'A' + 10; res *= 16, res += c - 'A' + 10;
else else
break; break;
if (code >= 256 || code < 0) if (res >= 256 || res < 0)
{ {
complain (_("hexadecimal value above 255: `\\x%x'"), code); complain (_("hexadecimal value above 255: `\\x%x'"), res);
code &= 0xFF; res &= 0xFF;
break; break;
} }
c = xgetc (finput); c = xgetc (finput);
@@ -237,14 +232,11 @@ literalchar (struct obstack *out, int *pcode, char term)
badchar[0] = c; badchar[0] = c;
complain (_("unknown escape sequence: `\\' followed by `%s'"), complain (_("unknown escape sequence: `\\' followed by `%s'"),
quote (badchar)); quote (badchar));
code = '?'; res = '?';
} }
} /* has \ */ } /* has \ */
if (out) return res;
obstack_1grow (out, code);
*pcode = code;
return !wasquote;
} }
@@ -356,19 +348,17 @@ lex (void)
/* parse the literal token and compute character code in code */ /* parse the literal token and compute character code in code */
{ {
int code; int code = literalchar ();
obstack_1grow (&token_obstack, '\''); obstack_1grow (&token_obstack, '\'');
literalchar (&token_obstack, &code, '\''); obstack_1grow (&token_obstack, code);
c = getc (finput); c = getc (finput);
if (c != '\'') if (c != '\'')
{ {
int discode;
complain (_("use \"...\" for multi-character literal tokens")); complain (_("use \"...\" for multi-character literal tokens"));
while (1) while (literalchar () != '\'')
if (!literalchar (0, &discode, '\'')) /* Skip. */;
break;
} }
obstack_1grow (&token_obstack, '\''); obstack_1grow (&token_obstack, '\'');
obstack_1grow (&token_obstack, '\0'); obstack_1grow (&token_obstack, '\0');
@@ -388,8 +378,12 @@ lex (void)
obstack_1grow (&token_obstack, '\"'); obstack_1grow (&token_obstack, '\"');
/* Read up to and including ". */ /* Read up to and including ". */
while (literalchar (&token_obstack, &code, '\"')) do
/* nothing */; {
code = literalchar ();
obstack_1grow (&token_obstack, code);
}
while (code != '\"');
obstack_1grow (&token_obstack, '\0'); obstack_1grow (&token_obstack, '\0');
token_buffer = obstack_finish (&token_obstack); token_buffer = obstack_finish (&token_obstack);
@@ -543,7 +537,7 @@ parse_percent_token (void)
arg_offset = obstack_object_size (&token_obstack); arg_offset = obstack_object_size (&token_obstack);
/* Read up to and including `"'. Do not append the closing /* Read up to and including `"'. Do not append the closing
`"' in the output: it's not part of the ARG. */ `"' in the output: it's not part of the ARG. */
while (literalchar (NULL, &code, '"')) while ((code = literalchar ()) != '"')
obstack_1grow (&token_obstack, code); obstack_1grow (&token_obstack, code);
} }
/* else: should be an error. */ /* else: should be an error. */

View File

@@ -73,7 +73,7 @@ void read_type_name PARAMS ((FILE *fin));
entry found. */ entry found. */
token_t lex PARAMS ((void)); token_t lex PARAMS ((void));
int literalchar PARAMS ((struct obstack *out, int *pcode, char term)); char literalchar PARAMS ((void));
token_t parse_percent_token PARAMS ((void)); token_t parse_percent_token PARAMS ((void));

View File

@@ -985,13 +985,8 @@ parse_dquoted_param (const char *from)
return NULL; return NULL;
} }
for (;;) while ((c = literalchar ()) != '"')
{ obstack_1grow (&param_obstack, c);
if (literalchar (NULL, &c, '\"'))
obstack_1grow (&param_obstack, c);
else
break;
}
obstack_1grow (&param_obstack, '\0'); obstack_1grow (&param_obstack, '\0');
param = obstack_finish (&param_obstack); param = obstack_finish (&param_obstack);