From 5f40c5b3846074c1aba903440c785ec499c392d1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 31 Aug 2011 07:11:31 -0400 Subject: [PATCH] runtime: handle string + char literals in goc2c My string literal was being rewritten from "runtime.SysReserve(%p, %D) = error %d" to "runtime.SysReserve ( %p , %D ) = error %d" R=iant CC=golang-dev https://golang.org/cl/4972051 --- src/pkg/runtime/goc2c.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/pkg/runtime/goc2c.c b/src/pkg/runtime/goc2c.c index 61236e226f..fcac9c0600 100644 --- a/src/pkg/runtime/goc2c.c +++ b/src/pkg/runtime/goc2c.c @@ -196,13 +196,14 @@ getchar_skipping_comments(void) } /* - * Read and return a token. Tokens are delimited by whitespace or by - * [(),{}]. The latter are all returned as single characters. + * Read and return a token. Tokens are string or character literals + * or else delimited by whitespace or by [(),{}]. + * The latter are all returned as single characters. */ static char * read_token(void) { - int c; + int c, q; char *buf; unsigned int alc, off; const char* delims = "(),{}"; @@ -217,7 +218,26 @@ read_token(void) alc = 16; buf = xmalloc(alc + 1); off = 0; - if (strchr(delims, c) != NULL) { + if(c == '"' || c == '\'') { + q = c; + buf[off] = c; + ++off; + while (1) { + if (off+2 >= alc) { // room for c and maybe next char + alc *= 2; + buf = xrealloc(buf, alc + 1); + } + c = getchar_no_eof(); + buf[off] = c; + ++off; + if(c == q) + break; + if(c == '\\') { + buf[off] = getchar_no_eof(); + ++off; + } + } + } else if (strchr(delims, c) != NULL) { buf[off] = c; ++off; } else {