source: cpp/frams/vm/framscript.l @ 1175

Last change on this file since 1175 was 973, checked in by Maciej Komosinski, 4 years ago

Increased SString and std::string compatibility: introduced length(), size(), and capacity(), and removed legacy methods that have std::string equivalents

  • Property svn:eol-style set to native
File size: 4.8 KB
RevLine 
[163]1%{
[335]2// This file is a part of the Framsticks SDK.
3// Copyright (C) 2002-2015  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
[163]4// Refer to http://www.framsticks.com/ for further information.
5
6#include "framscript-defs.h"
7#include "framscript.tab.cpp.h"
8#include <math.h>
9
10#define YY_INPUT(buf,result,maxsize) {result=trctx.in->Vread(buf,1,maxsize);}
11#define YY_NEVER_INTERACTIVE 1
12
[224]13static SString quoteMultiline(const SString &src);
[221]14
[163]15%}
16
17%option noyywrap
18%x asmcode
19%x asmcode2
20%x comment
[224]21%x multiline
[163]22
23%%
24
[468]25[0-9]+\.[0-9]*                   |
26[0-9]+\.[0-9]*[eE][+-]?[0-9]+    |
27[0-9]+*[eE][+-]?[0-9]+           {if (framscriptlval.parseNumber(yytext,TDouble)==(yytext+yyleng)) return CONSTANT; else { trctx.err->printf("Invalid floating point number: '%s', ",SString(yytext,yyleng).c_str()); return INVALID_NUMBER;} }
28
29[0-9]+                |
300x[0-9a-fA-F]+        {if (framscriptlval.parseNumber(yytext,TInt)==(yytext+yyleng)) return CONSTANT; else { trctx.err->printf("Invalid integer: '%s', ",SString(yytext,yyleng).c_str()); return INVALID_NUMBER;} }
31
[163]32"null"            {framscriptlval.setEmpty(); return CONSTANT;}
33\"([^\\\"]|\\.)*\" {framscriptlval.setString(SString(yytext+1,yyleng-2));return CONSTANT;}
[224]34\"\"\"             {trctx.tmp=""; trctx.multilimit='\"'; BEGIN multiline;}
35"'''"              {trctx.tmp=""; trctx.multilimit='\''; BEGIN multiline;}
[332]36"@line "[0-9]+\n   {trctx.line=atol(yytext+6);trctx.linechanged=true;}
[950]37"@file "[^\n\t\r]+\n {trctx.setSrcname(SString(yytext+6,yyleng-7),true);trctx.namechanged=true;}
[163]38
39[a-zA-Z_][a-zA-Z0-9_]* { int t=lookupToken(yytext);
40                         if (t>=0) return t;
41                         else
42                            {
43                            framscriptlval.setString(yytext);
44                            return framscriptIsObjectName(yytext)?OBJNAME:IDENT;
45                            }
46                       }
[477]47
48"->"               return ARROW;
[163]49"=="               return EQUAL;
50"<>"               return NOT_EQUAL;
51"!="               return NOT_EQUAL;
52">="               return GEQUAL;
53"<="               return LEQUAL;
54
55"+="                return ASSIGN_ADD;
56"-="                return ASSIGN_SUB;
57"*="                return ASSIGN_MUL;
58"/="                return ASSIGN_DIV;
59"%="                return ASSIGN_MOD;
60
61"++"                return PLUSPLUS;
62"--"                return MINUSMINUS;
63
64"&&"                return LOGIC_AND;
65"||"                return LOGIC_OR;
66
67"<<"                return LSHIFT;
68">>"                return RSHIFT;
69
[332]70<INITIAL,asmcode,asmcode2>\/\/.*\n         {trctx.line++;trctx.linechanged=true;} // komentarz jednoliniowy
[163]71<INITIAL,asmcode,asmcode2>\/\/[^\n]*       ;               // komentarz ale nie ma potem \n
72
73<INITIAL>\/\*       BEGIN comment;
[332]74<comment>\n         {trctx.line++;trctx.linechanged=true;}
[163]75<comment>\*\/       BEGIN 0;
76<comment>.          ;
[224]77<multiline>.*       {
78                    char* end=strstr(yytext,trctx.multilimit=='\"' ? "\"\"\"" : "'''");
79                    if (end)
80                       {
[382]81                       trctx.tmp+=string(yytext,end-yytext);
82                       framscriptlval.setString(quoteMultiline(SString(trctx.tmp.c_str())));
[224]83                       yyless((end-yytext)+3);
84                       BEGIN 0;
85                       return CONSTANT;
86                       }
[221]87                    else
[382]88                       trctx.tmp+=string(yytext,yyleng);
[221]89                    }
[332]90<multiline>\n       {trctx.line++; trctx.linechanged=true; trctx.tmp+="\n";}
[163]91[ \t\r\f]           ;
[332]92\n                  {trctx.line++; trctx.linechanged=true;}
[394]93.                   return (int)(unsigned char)yytext[0];
[163]94
95"asm"[ \t\n\r]*"{"  {
96                    BEGIN asmcode;
97                    char *t=yytext;
98                    while(t=strchr(t+1,'\n')) trctx.line++;
[332]99                    trctx.linechanged=true;
[163]100                    return ASM;
101                    }
102<asmcode>.*\n {
103              char *t=yytext;
[332]104              trctx.line++; trctx.linechanged=true;
[163]105              while ((*t==' ')||(*t=='\t')) t++;
106              if (*t=='}') {yyless((t-yytext)+1); BEGIN 0; return '}';}
107              char *e=yytext+yyleng-1;
108              while (e[-1]=='\r') e--;
109              framscriptlval.setString(SString(t,e-t));
110              return ASMLINE;
111              }
112
113%%
[221]114
[224]115//exclusively for multiline, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO)
116static SString quoteMultiline(const SString &src)
[221]117{
[973]118int len=src.length();
[955]119SString ret;
120ret.reserve((len*11)/10+10);
[348]121const char*t=src.c_str();
[221]122while(len>0)
123        {
[224]124        switch(*t)
125            {
126            case '\"': ret+="\\\""; break;
127            case '\\': ret+="\\\\"; break;
128            default: ret+=*t;
129            }
[221]130        t++; len--;
131        }
132return ret;
133}
134
[163]135void framscript_init_lex()
136{
137BEGIN 0;
138yyrestart(0);
139}
140
141void framscript_cleanup_lex()
142{
143yy_delete_buffer(yy_current_buffer);
144}
Note: See TracBrowser for help on using the repository browser.