]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Add quotes to the known control sequences... darn. Add stringification via #
authorWolfgang (Blub) Bumiller <blub@speed.at>
Sun, 18 Nov 2012 13:26:40 +0000 (14:26 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Sun, 18 Nov 2012 13:26:40 +0000 (14:26 +0100)
ftepp.c
lexer.c

diff --git a/ftepp.c b/ftepp.c
index 96c62e76581292649db51e21dfed8f61f68d4761..fb89682547fd0f107b050e65f58377ad4b9664b4 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
@@ -438,6 +438,47 @@ static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
     return false;
 }
 
+static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
+{
+    char        chs[2];
+    const char *ch;
+    chs[1] = 0;
+    switch (token->token) {
+        case TOKEN_STRINGCONST:
+            ch = token->value;
+            while (*ch) {
+                switch (*ch) {
+                    case '\\': ftepp_out(ftepp, "\\\\", false); break;
+                    case '"':  ftepp_out(ftepp, "\\\"", false); break;
+                    default:
+                        chs[0] = *ch;
+                        ftepp_out(ftepp, chs, false);
+                        break;
+                }
+                ++ch;
+            }
+            break;
+        case TOKEN_WHITE:
+            ftepp_out(ftepp, " ", false);
+            break;
+        case TOKEN_EOL:
+            ftepp_out(ftepp, "\\n", false);
+            break;
+        default:
+            ftepp_out(ftepp, token->value, false);
+            break;
+    }
+}
+
+static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
+{
+    size_t i;
+    ftepp_out(ftepp, "\"", false);
+    for (i = 0; i < vec_size(param->tokens); ++i)
+        ftepp_stringify_token(ftepp, param->tokens[i]);
+    ftepp_out(ftepp, "\"", false);
+}
+
 static bool ftepp_preprocess(ftepp_t *ftepp);
 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
 {
@@ -448,6 +489,8 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
     size_t    o, pi, pv;
     lex_file *inlex;
 
+    int nextok;
+
     /* really ... */
     if (!vec_size(macro->output))
         return true;
@@ -473,10 +516,22 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
                 }
                 break;
             case '#':
-                if (o + 1 < vec_size(macro->output) && macro->output[o+1]->token == '#') {
-                    /* raw concatenation */
-                    ++o;
-                    break;
+                if (o + 1 < vec_size(macro->output)) {
+                    nextok = macro->output[o+1]->token;
+                    if (nextok == '#') {
+                        /* raw concatenation */
+                        ++o;
+                        break;
+                    }
+                    if ( (nextok == TOKEN_IDENT    ||
+                          nextok == TOKEN_KEYWORD  ||
+                          nextok == TOKEN_TYPENAME) &&
+                        macro_params_find(macro, macro->output[o+1]->value, &pi))
+                    {
+                        ++o;
+                        ftepp_stringify(ftepp, &params[pi]);
+                        break;
+                    }
                 }
                 ftepp_out(ftepp, "#", false);
                 break;
diff --git a/lexer.c b/lexer.c
index 34da7d8a97fa1cf107ceef47cb9be2a3518dc9b7..bdea72232a0f400a5ebb54e3609ec5205c85c992 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -573,7 +573,17 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
         if (ch == quote)
             return TOKEN_STRINGCONST;
 
-        if (!lex->flags.preprocessing && ch == '\\') {
+        if (lex->flags.preprocessing && ch == '\\') {
+            lex_tokench(lex, ch);
+            ch = lex_getch(lex);
+            if (ch == EOF) {
+                lexerror(lex, "unexpected end of file");
+                lex_ungetch(lex, EOF); /* next token to be TOKEN_EOF */
+                return (lex->tok.ttype = TOKEN_ERROR);
+            }
+            lex_tokench(lex, ch);
+        }
+        else if (ch == '\\') {
             ch = lex_getch(lex);
             if (ch == EOF) {
                 lexerror(lex, "unexpected end of file");
@@ -583,6 +593,8 @@ static int GMQCC_WARN lex_finish_string(lex_file *lex, int quote)
 
             switch (ch) {
             case '\\': break;
+            case '\'': break;
+            case '"':  break;
             case 'a':  ch = '\a'; break;
             case 'b':  ch = '\b'; break;
             case 'r':  ch = '\r'; break;