Commit 5d7ee2fa by Neil Booth Committed by Neil Booth

cpplex.c (spell_token): New function.

	* cpplex.c (spell_token): New function.
	(spell_string, spell_comment, spell_name): fold into
	spell_token.
	(I, S): Add macros.
	(E, H): Remove macros.
	(save_comment): Save comment opening characters too.
	(_cpp_lex_file): Update to use spell_token.  Tidy up comment
	handling.
	* cpplib.h (I, S): Add macros.
	(E, H): Remove macros.
	(TTYPE_TABLE): Update entries for new speller.
	(SYNTAX_ASSERT): Remove.

From-SVN: r33817
parent fb4527c3
Wed 10 May 09:36:47 2000 Neil Booth <NeilB@earthling.net>
* cpplex.c (spell_token): New function.
(spell_string, spell_comment, spell_name): fold into
spell_token.
(I, S): Add macros.
(E, H): Remove macros.
(save_comment): Save comment opening characters too.
(_cpp_lex_file): Update to use spell_token. Tidy up comment
handling.
* cpplib.h (I, S): Add macros.
(E, H): Remove macros.
(TTYPE_TABLE): Update entries for new speller.
(SYNTAX_ASSERT): Remove.
Wed 10 May 09:08:30 2000 Neil Booth <NeilB@earthling.net> Wed 10 May 09:08:30 2000 Neil Booth <NeilB@earthling.net>
* cpplex.c (_cpp_lex_line): Maintain PREV_WHITESPACE flag * cpplex.c (_cpp_lex_line): Maintain PREV_WHITESPACE flag
......
...@@ -84,12 +84,6 @@ void _cpp_lex_line PARAMS ((cpp_reader *, cpp_toklist *)); ...@@ -84,12 +84,6 @@ void _cpp_lex_line PARAMS ((cpp_reader *, cpp_toklist *));
static void _cpp_output_list PARAMS ((cpp_reader *, cpp_toklist *)); static void _cpp_output_list PARAMS ((cpp_reader *, cpp_toklist *));
unsigned int spell_string PARAMS ((unsigned char *, cpp_toklist *,
cpp_token *token));
unsigned int spell_comment PARAMS ((unsigned char *, cpp_toklist *,
cpp_token *token));
unsigned int spell_name PARAMS ((unsigned char *, cpp_toklist *,
cpp_token *token));
static unsigned char * spell_token PARAMS ((cpp_reader *, cpp_token *, static unsigned char * spell_token PARAMS ((cpp_reader *, cpp_token *,
cpp_toklist *, unsigned char *, cpp_toklist *, unsigned char *,
int)); int));
...@@ -120,13 +114,17 @@ typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *, ...@@ -120,13 +114,17 @@ typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
#define IMMED_TOKEN() (!(cur_token->flags & PREV_WHITESPACE)) #define IMMED_TOKEN() (!(cur_token->flags & PREV_WHITESPACE))
#define PREV_TOKEN_TYPE (cur_token[-1].type) #define PREV_TOKEN_TYPE (cur_token[-1].type)
#define SPELL_TEXT 0 /* Order here matters. Those beyond SPELL_NONE store their spelling
#define SPELL_HANDLER 1 in the token list, and it's length in the token->val.name.len. */
#define SPELL_CHAR 2 #define SPELL_OPERATOR 0
#define SPELL_NONE 3 #define SPELL_CHAR 1
#define SPELL_NONE 2
#define T(e, s) {SPELL_TEXT, s}, #define SPELL_IDENT 3
#define H(e, s) {SPELL_HANDLER, (PTR) s}, #define SPELL_STRING 4
#define T(e, s) {SPELL_OPERATOR, s},
#define I(e, s) {SPELL_IDENT, s},
#define S(e, s) {SPELL_STRING, s},
#define C(e, s) {SPELL_CHAR, s}, #define C(e, s) {SPELL_CHAR, s},
#define N(e, s) {SPELL_NONE, s}, #define N(e, s) {SPELL_NONE, s},
...@@ -137,7 +135,8 @@ static const struct token_spelling ...@@ -137,7 +135,8 @@ static const struct token_spelling
} token_spellings [N_TTYPES + 1] = {TTYPE_TABLE {0, 0} }; } token_spellings [N_TTYPES + 1] = {TTYPE_TABLE {0, 0} };
#undef T #undef T
#undef H #undef I
#undef S
#undef C #undef C
#undef N #undef N
...@@ -147,10 +146,10 @@ static const struct token_spelling ...@@ -147,10 +146,10 @@ static const struct token_spelling
#define BACKUP_DIGRAPH(ttype) do { \ #define BACKUP_DIGRAPH(ttype) do { \
BACKUP_TOKEN(ttype); cur_token->flags |= DIGRAPH;} while (0) BACKUP_TOKEN(ttype); cur_token->flags |= DIGRAPH;} while (0)
/* If there is this many bytes in a buffer, you have enough room to /* An upper bound on the number of bytes needed to spell a token,
spell the token, including preceding whitespace. */ including preceding whitespace. */
#define TOKEN_LEN(token) (5 + (token_spellings[token->type].type == \ #define TOKEN_LEN(token) (5 + (token_spellings[token->type].type > \
SPELL_HANDLER ? token->val.name.len: 0)) SPELL_NONE ? token->val.name.len: 0))
#endif #endif
...@@ -2760,9 +2759,11 @@ parse_string2 (pfile, list, name, terminator) ...@@ -2760,9 +2759,11 @@ parse_string2 (pfile, list, name, terminator)
: "null character preserved")); : "null character preserved"));
} }
/* The character C helps us distinguish comment types: '*' = C style, /* The character TYPE helps us distinguish comment types: '*' = C
'-' = Chill-style and '/' = C++ style. For code simplicity, the style, '-' = Chill-style and '/' = C++ style. For code simplicity,
stored comment includes any C-style comment terminator. */ the stored comment includes the comment start and any terminator. */
#define COMMENT_START_LEN 2
static void static void
save_comment (list, from, len, tok_no, type) save_comment (list, from, len, tok_no, type)
cpp_toklist *list; cpp_toklist *list;
...@@ -2772,6 +2773,9 @@ save_comment (list, from, len, tok_no, type) ...@@ -2772,6 +2773,9 @@ save_comment (list, from, len, tok_no, type)
unsigned int type; unsigned int type;
{ {
cpp_token *comment; cpp_token *comment;
unsigned char *buffer;
len += COMMENT_START_LEN;
if (list->comments_used == list->comments_cap) if (list->comments_used == list->comments_cap)
expand_comment_space (list); expand_comment_space (list);
...@@ -2780,12 +2784,24 @@ save_comment (list, from, len, tok_no, type) ...@@ -2780,12 +2784,24 @@ save_comment (list, from, len, tok_no, type)
expand_name_space (list, len); expand_name_space (list, len);
comment = &list->comments[list->comments_used++]; comment = &list->comments[list->comments_used++];
comment->type = type; comment->type = CPP_COMMENT;
comment->aux = tok_no; comment->aux = tok_no;
comment->val.name.len = len; comment->val.name.len = len;
comment->val.name.offset = list->name_used; comment->val.name.offset = list->name_used;
memcpy (list->namebuf + list->name_used, from, len); buffer = list->namebuf + list->name_used;
if (type == '*')
{
*buffer++ = '/';
*buffer++ = '*';
}
else
{
*buffer++ = type;
*buffer++ = type;
}
memcpy (buffer, from, len - COMMENT_START_LEN);
list->name_used += len; list->name_used += len;
} }
...@@ -2956,8 +2972,7 @@ _cpp_lex_line (pfile, list) ...@@ -2956,8 +2972,7 @@ _cpp_lex_line (pfile, list)
"multi-line comment"); "multi-line comment");
if (!CPP_OPTION (pfile, discard_comments)) if (!CPP_OPTION (pfile, discard_comments))
save_comment (list, cur, buffer->cur - cur, save_comment (list, cur, buffer->cur - cur,
cur_token - 1 - list->tokens, c == '/' cur_token - 1 - list->tokens, c);
? CPP_CPP_COMMENT: CPP_CHILL_COMMENT);
cur = buffer->cur; cur = buffer->cur;
/* Back-up to first '-' or '/'. */ /* Back-up to first '-' or '/'. */
...@@ -2988,7 +3003,7 @@ _cpp_lex_line (pfile, list) ...@@ -2988,7 +3003,7 @@ _cpp_lex_line (pfile, list)
"comment end '*/' split across lines"); "comment end '*/' split across lines");
if (!CPP_OPTION (pfile, discard_comments)) if (!CPP_OPTION (pfile, discard_comments))
save_comment (list, cur, buffer->cur - cur, save_comment (list, cur, buffer->cur - cur,
cur_token - 1 - list->tokens, CPP_C_COMMENT); cur_token - 1 - list->tokens, c);
cur = buffer->cur; cur = buffer->cur;
cur_token -= 2; cur_token -= 2;
...@@ -3278,79 +3293,6 @@ _cpp_lex_line (pfile, list) ...@@ -3278,79 +3293,6 @@ _cpp_lex_line (pfile, list)
"invalid preprocessing directive"); "invalid preprocessing directive");
} }
/* Token spelling functions. Used for output of a preprocessed file,
stringizing and token pasting. They all assume sufficient buffer
is allocated, and return exactly how much they used. */
/* Needs buffer of 3 + len. */
unsigned int
spell_string (buffer, list, token)
unsigned char *buffer;
cpp_toklist *list;
cpp_token *token;
{
unsigned char c, *orig_buff = buffer;
size_t len;
if (token->type == CPP_WSTRING || token->type == CPP_WCHAR)
*buffer++ = 'L';
c = token->type == CPP_STRING || token->type == CPP_WSTRING ? '"': '\'';
*buffer++ = c;
len = token->val.name.len;
memcpy (buffer, list->namebuf + token->val.name.offset, len);
buffer += len;
*buffer++ = c;
return buffer - orig_buff;
}
/* Needs buffer of len + 2. */
unsigned int
spell_comment (buffer, list, token)
unsigned char *buffer;
cpp_toklist *list;
cpp_token *token;
{
size_t len;
if (token->type == CPP_C_COMMENT)
{
*buffer++ = '/';
*buffer++ = '*';
}
else if (token->type == CPP_CPP_COMMENT)
{
*buffer++ = '/';
*buffer++ = '/';
}
else
{
*buffer++ = '-';
*buffer++ = '-';
}
len = token->val.name.len;
memcpy (buffer, list->namebuf + token->val.name.offset, len);
return len + 2;
}
/* Needs buffer of len. */
unsigned int
spell_name (buffer, list, token)
unsigned char *buffer;
cpp_toklist *list;
cpp_token *token;
{
size_t len;
len = token->val.name.len;
memcpy (buffer, list->namebuf + token->val.name.offset, len);
buffer += len;
return len;
}
/* Write the spelling of a token TOKEN to BUFFER. The buffer must /* Write the spelling of a token TOKEN to BUFFER. The buffer must
already contain the enough space to hold the token's spelling. If already contain the enough space to hold the token's spelling. If
WHITESPACE is true, and the token was preceded by whitespace, WHITESPACE is true, and the token was preceded by whitespace,
...@@ -3373,7 +3315,7 @@ spell_token (pfile, token, list, buffer, whitespace) ...@@ -3373,7 +3315,7 @@ spell_token (pfile, token, list, buffer, whitespace)
switch (token_spellings[token->type].type) switch (token_spellings[token->type].type)
{ {
case SPELL_TEXT: case SPELL_OPERATOR:
{ {
const unsigned char *spelling; const unsigned char *spelling;
unsigned char c; unsigned char c;
...@@ -3388,12 +3330,26 @@ spell_token (pfile, token, list, buffer, whitespace) ...@@ -3388,12 +3330,26 @@ spell_token (pfile, token, list, buffer, whitespace)
} }
break; break;
case SPELL_HANDLER: case SPELL_IDENT:
memcpy (buffer, list->namebuf + token->val.name.offset,
token->val.name.len);
buffer += token->val.name.len;
break;
case SPELL_STRING:
{ {
speller s; unsigned char c;
s = (speller) token_spellings[token->type].speller; if (token->type == CPP_WSTRING || token->type == CPP_WCHAR)
buffer += s (buffer, list, token); *buffer++ = 'L';
c = '\'';
if (token->type == CPP_STRING || token->type == CPP_WSTRING)
c = '"';
*buffer++ = c;
memcpy (buffer, list->namebuf + token->val.name.offset,
token->val.name.len);
buffer += token->val.name.len;
*buffer++ = c;
} }
break; break;
...@@ -3448,29 +3404,30 @@ _cpp_output_list (pfile, list) ...@@ -3448,29 +3404,30 @@ _cpp_output_list (pfile, list)
cpp_reader *pfile; cpp_reader *pfile;
cpp_toklist *list; cpp_toklist *list;
{ {
unsigned int comment_no = 0; cpp_token *token, *comment, *comment_before = 0;
cpp_token *token, *comment_token = 0;
if (list->comments_used > 0) if (list->comments_used > 0)
comment_token = list->tokens + list->comments[0].aux; {
comment = &list->comments[0];
comment_before = &list->tokens[comment->aux];
}
token = &list->tokens[0]; token = &list->tokens[0];
do do
{ {
/* Output comments if -C. */ /* Output comments if -C. */
if (token == comment_token) while (token == comment_before)
{ {
cpp_token *comment = &list->comments[comment_no]; /* Make space for the comment, and copy it out. */
do CPP_RESERVE (pfile, TOKEN_LEN (comment));
{ pfile->limit = spell_token (pfile, comment, list, pfile->limit, 0);
CPP_RESERVE (pfile, TOKEN_LEN (comment));
pfile->limit += spell_comment (pfile->limit, list, comment); /* Stop if no comments left, or no more comments appear
comment_no++, comment++; before the current token. */
if (comment_no == list->comments_used) comment++;
break; if (comment == list->comments + list->comments_used)
comment_token = comment->aux + list->tokens; break;
} comment_before = &list->tokens[comment->aux];
while (comment_token == token);
} }
CPP_RESERVE (pfile, TOKEN_LEN (token)); CPP_RESERVE (pfile, TOKEN_LEN (token));
......
...@@ -109,33 +109,31 @@ typedef struct cpp_name cpp_name; ...@@ -109,33 +109,31 @@ typedef struct cpp_name cpp_name;
T(CPP_MAX, ">?") \ T(CPP_MAX, ">?") \
C(CPP_OTHER, 0) /* stray punctuation */ \ C(CPP_OTHER, 0) /* stray punctuation */ \
\ \
H(CPP_NAME, spell_name) /* word */ \ I(CPP_NAME, 0) /* word */ \
N(CPP_INT, 0) /* 23 */ \ N(CPP_INT, 0) /* 23 */ \
N(CPP_FLOAT, 0) /* 3.14159 */ \ N(CPP_FLOAT, 0) /* 3.14159 */ \
H(CPP_NUMBER, spell_name) /* 34_be+ta */ \ I(CPP_NUMBER, 0) /* 34_be+ta */ \
H(CPP_CHAR, spell_string) /* 'char' */ \ S(CPP_CHAR, 0) /* 'char' */ \
H(CPP_WCHAR, spell_string) /* L'char' */ \ S(CPP_WCHAR, 0) /* L'char' */ \
H(CPP_STRING, spell_string) /* "string" */ \ S(CPP_STRING, 0) /* "string" */ \
H(CPP_WSTRING, spell_string) /* L"string" */ \ S(CPP_WSTRING, 0) /* L"string" */ \
\ \
H(CPP_C_COMMENT, spell_comment) /* Only if output comments. */ \ I(CPP_COMMENT, 0) /* Only if output comments. */ \
H(CPP_CPP_COMMENT, spell_comment) /* Only if output comments. */ \ N(CPP_MACRO_ARG, 0) /* Macro argument. */ \
H(CPP_CHILL_COMMENT, spell_comment) /* Only if output comments. */ \ N(CPP_SUBLIST, 0) /* Sublist. */ \
N(CPP_MACRO_ARG, 0) /* Macro argument. */ \ T(CPP_VSPACE, "\n") /* End of line. */ \
N(CPP_SUBLIST, 0) /* Sublist. */ \ N(CPP_EOF, 0) /* End of file. */ \
T(CPP_VSPACE, "\n") /* End of line. */ \ N(CPP_HEADER_NAME, 0) /* <stdio.h> in #include */ \
N(CPP_EOF, 0) /* End of file. */ \ N(CPP_ASSERTION, 0) /* (...) in #assert */ \
N(CPP_HEADER_NAME, 0) /* <stdio.h> in #include */ \
N(CPP_ASSERTION, 0) /* (...) in #assert */ \
\ \
/* Obsolete - will be removed when no code uses them still. */ \ /* Obsolete - will be removed when no code uses them still. */ \
H(CPP_COMMENT, 0) /* Only if output comments. */ \ N(CPP_HSPACE, 0) /* Horizontal white space. */ \
N(CPP_HSPACE, 0) /* Horizontal white space. */ \ N(CPP_DIRECTIVE, 0) /* #define and the like */ \
N(CPP_DIRECTIVE, 0) /* #define and the like */ \ N(CPP_MACRO, 0) /* Like a NAME, but expanded. */
N(CPP_MACRO, 0) /* Like a NAME, but expanded. */
#define T(e, s) e, #define T(e, s) e,
#define H(e, s) e, #define I(e, s) e,
#define S(e, s) e,
#define C(e, s) e, #define C(e, s) e,
#define N(e, s) e, #define N(e, s) e,
enum cpp_ttype enum cpp_ttype
...@@ -144,7 +142,8 @@ enum cpp_ttype ...@@ -144,7 +142,8 @@ enum cpp_ttype
N_TTYPES N_TTYPES
}; };
#undef T #undef T
#undef H #undef I
#undef S
#undef C #undef C
#undef N #undef N
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment