Merge "edify: accept long string literal."

am: d379b4410b

* commit 'd379b4410b6f7c7f08751709d08ba6f819acf69d':
  edify: accept long string literal.
This commit is contained in:
Yabin Cui
2016-01-27 23:23:53 +00:00
committed by android-build-merger
2 changed files with 16 additions and 13 deletions
+11 -13
View File
@@ -16,6 +16,7 @@
*/ */
#include <string.h> #include <string.h>
#include <string>
#include "expr.h" #include "expr.h"
#include "yydefs.h" #include "yydefs.h"
@@ -25,9 +26,7 @@ int gLine = 1;
int gColumn = 1; int gColumn = 1;
int gPos = 0; int gPos = 0;
// TODO: enforce MAX_STRING_LEN during lexing std::string string_buffer;
char string_buffer[MAX_STRING_LEN];
char* string_pos;
#define ADVANCE do {yylloc.start=gPos; yylloc.end=gPos+yyleng; \ #define ADVANCE do {yylloc.start=gPos; yylloc.end=gPos+yyleng; \
gColumn+=yyleng; gPos+=yyleng;} while(0) gColumn+=yyleng; gPos+=yyleng;} while(0)
@@ -43,7 +42,7 @@ char* string_pos;
\" { \" {
BEGIN(STR); BEGIN(STR);
string_pos = string_buffer; string_buffer.clear();
yylloc.start = gPos; yylloc.start = gPos;
++gColumn; ++gColumn;
++gPos; ++gPos;
@@ -54,36 +53,35 @@ char* string_pos;
++gColumn; ++gColumn;
++gPos; ++gPos;
BEGIN(INITIAL); BEGIN(INITIAL);
*string_pos = '\0'; yylval.str = strdup(string_buffer.c_str());
yylval.str = strdup(string_buffer);
yylloc.end = gPos; yylloc.end = gPos;
return STRING; return STRING;
} }
\\n { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\n'; } \\n { gColumn += yyleng; gPos += yyleng; string_buffer.push_back('\n'); }
\\t { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\t'; } \\t { gColumn += yyleng; gPos += yyleng; string_buffer.push_back('\t'); }
\\\" { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\"'; } \\\" { gColumn += yyleng; gPos += yyleng; string_buffer.push_back('\"'); }
\\\\ { gColumn += yyleng; gPos += yyleng; *string_pos++ = '\\'; } \\\\ { gColumn += yyleng; gPos += yyleng; string_buffer.push_back('\\'); }
\\x[0-9a-fA-F]{2} { \\x[0-9a-fA-F]{2} {
gColumn += yyleng; gColumn += yyleng;
gPos += yyleng; gPos += yyleng;
int val; int val;
sscanf(yytext+2, "%x", &val); sscanf(yytext+2, "%x", &val);
*string_pos++ = val; string_buffer.push_back(static_cast<char>(val));
} }
\n { \n {
++gLine; ++gLine;
++gPos; ++gPos;
gColumn = 1; gColumn = 1;
*string_pos++ = yytext[0]; string_buffer.push_back(yytext[0]);
} }
. { . {
++gColumn; ++gColumn;
++gPos; ++gPos;
*string_pos++ = yytext[0]; string_buffer.push_back(yytext[0]);
} }
} }
+5
View File
@@ -18,6 +18,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string>
#include "expr.h" #include "expr.h"
#include "parser.h" #include "parser.h"
@@ -151,6 +153,9 @@ int test() {
expect("greater_than_int(x, 3)", "", &errors); expect("greater_than_int(x, 3)", "", &errors);
expect("greater_than_int(3, x)", "", &errors); expect("greater_than_int(3, x)", "", &errors);
// big string
expect(std::string(8192, 's').c_str(), std::string(8192, 's').c_str(), &errors);
printf("\n"); printf("\n");
return errors; return errors;