Changeset 224 for cpp/frams


Ignore:
Timestamp:
04/16/14 19:28:15 (10 years ago)
Author:
Maciej Komosinski
Message:

Multiline strings quoted in three quotation marks or three apostrophes (instead of <<<)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/vm/framscript.l

    r221 r224  
    1111#define YY_NEVER_INTERACTIVE 1
    1212
    13 static SString quoteDoubleQuotes(const SString &src);
     13static SString quoteMultiline(const SString &src);
    1414
    1515%}
     
    1919%x asmcode2
    2020%x comment
    21 %x heredoc
     21%x multiline
    2222
    2323%%
     
    3131"null"            {framscriptlval.setEmpty(); return CONSTANT;}
    3232\"([^\\\"]|\\.)*\" {framscriptlval.setString(SString(yytext+1,yyleng-2));return CONSTANT;}
    33 "<<<".*\n            {trctx.herelimit=SString(yytext+3,yyleng-4); trctx.tmp=""; BEGIN heredoc; }
     33\"\"\"             {trctx.tmp=""; trctx.multilimit='\"'; BEGIN multiline;}
     34"'''"              {trctx.tmp=""; trctx.multilimit='\''; BEGIN multiline;}
    3435"@line "[0-9]+\n   {trctx.line=atol(yytext+6);}
    3536"@file "[^\n\t\r]+\n {trctx.srcname=SString(yytext+6,yyleng-7);}
     
    7172<comment>\*\/       BEGIN 0;
    7273<comment>.          ;
    73 <heredoc>[^\n]*\n   {
    74                     trctx.line++;
    75                     SString tmp(yytext,yyleng-1);
    76                     if (tmp==trctx.herelimit)
    77                         { framscriptlval.setString(quoteDoubleQuotes(trctx.tmp)); BEGIN 0; return CONSTANT;}
     74<multiline>.*       {
     75                    char* end=strstr(yytext,trctx.multilimit=='\"' ? "\"\"\"" : "'''");
     76                    if (end)
     77                       {
     78                       trctx.tmp+=SString(yytext,end-yytext);
     79                       framscriptlval.setString(quoteMultiline(trctx.tmp));
     80                       yyless((end-yytext)+3);
     81                       BEGIN 0;
     82                       return CONSTANT;
     83                       }
    7884                    else
    79                         { trctx.tmp+=tmp; trctx.tmp+="\n";}
     85                       trctx.tmp+=SString(yytext,yyleng);
    8086                    }
     87<multiline>\n       {trctx.line++; trctx.tmp+="\n";}
    8188[ \t\r\f]           ;
    8289\n                  {trctx.line++;}
     
    102109%%
    103110
    104 //exclusively for heredoc, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO)
    105 static SString quoteDoubleQuotes(const SString &src)
     111//exclusively for multiline, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO)
     112static SString quoteMultiline(const SString &src)
    106113{
    107114int len=src.len();
     
    110117while(len>0)
    111118        {
    112         if (*t=='\"')
    113                 ret+="\\\"";
    114         else
    115                  ret+=*t;
     119        switch(*t)
     120            {
     121            case '\"': ret+="\\\""; break;
     122            case '\\': ret+="\\\\"; break;
     123            default: ret+=*t;
     124            }
    116125        t++; len--;
    117126        }
Note: See TracChangeset for help on using the changeset viewer.