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

Last change on this file since 332 was 332, checked in by Maciej Komosinski, 10 years ago
  • use source/code mapping for line number and file information in vm error messages
  • mandatory parentheses in typeof()
  • int() float() string() added
  • typeof() returns plain names of core types instead of numeric constants
short-circuit evaluation of && and
  • fixed switch/case fall through (previously each case not ending with break jumped to default: (when present))
  • case labels do not have to be constant
  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1%{
2// This file is a part of the Framsticks GDK.
3// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
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
13static SString quoteMultiline(const SString &src);
14
15%}
16
17%option noyywrap
18%x asmcode
19%x asmcode2
20%x comment
21%x multiline
22
23%%
24
25\$[0-9a-fA-F]+ {int i; sscanf(yytext+1,"%x",&i); framscriptlval.setInt(i); return CONSTANT;}
260x[0-9a-fA-F]+ {int i; sscanf(yytext+2,"%x",&i); framscriptlval.setInt(i); return CONSTANT;}
27[0-9]+\.[0-9]*    {framscriptlval.setDouble(atof(yytext)); return CONSTANT;}
28[0-9]+            {framscriptlval.setInt(atoi(yytext)); return CONSTANT;}
29[0-9]+\.[0-9]*[eE][+-]?[0-9]+    {framscriptlval.setDouble(atof(yytext)); return CONSTANT;}
30[0-9]+*[eE][+-]?[0-9]+           {framscriptlval.setDouble(atof(yytext)); return CONSTANT;}
31"null"            {framscriptlval.setEmpty(); return CONSTANT;}
32\"([^\\\"]|\\.)*\" {framscriptlval.setString(SString(yytext+1,yyleng-2));return CONSTANT;}
33\"\"\"             {trctx.tmp=""; trctx.multilimit='\"'; BEGIN multiline;}
34"'''"              {trctx.tmp=""; trctx.multilimit='\''; BEGIN multiline;}
35"@line "[0-9]+\n   {trctx.line=atol(yytext+6);trctx.linechanged=true;}
36"@file "[^\n\t\r]+\n {trctx.srcname=SString(yytext+6,yyleng-7);trctx.namechanged=true;}
37
38[a-zA-Z_][a-zA-Z0-9_]* { int t=lookupToken(yytext);
39                         if (t>=0) return t;
40                         else
41                            {
42                            framscriptlval.setString(yytext);
43                            return framscriptIsObjectName(yytext)?OBJNAME:IDENT;
44                            }
45                       }
46"=="               return EQUAL;
47"<>"               return NOT_EQUAL;
48"!="               return NOT_EQUAL;
49">="               return GEQUAL;
50"<="               return LEQUAL;
51
52"+="                return ASSIGN_ADD;
53"-="                return ASSIGN_SUB;
54"*="                return ASSIGN_MUL;
55"/="                return ASSIGN_DIV;
56"%="                return ASSIGN_MOD;
57
58"++"                return PLUSPLUS;
59"--"                return MINUSMINUS;
60
61"&&"                return LOGIC_AND;
62"||"                return LOGIC_OR;
63
64"<<"                return LSHIFT;
65">>"                return RSHIFT;
66
67<INITIAL,asmcode,asmcode2>\/\/.*\n         {trctx.line++;trctx.linechanged=true;} // komentarz jednoliniowy
68<INITIAL,asmcode,asmcode2>\/\/[^\n]*       ;               // komentarz ale nie ma potem \n
69
70<INITIAL>\/\*       BEGIN comment;
71<comment>\n         {trctx.line++;trctx.linechanged=true;}
72<comment>\*\/       BEGIN 0;
73<comment>.          ;
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                       }
84                    else
85                       trctx.tmp+=SString(yytext,yyleng);
86                    }
87<multiline>\n       {trctx.line++; trctx.linechanged=true; trctx.tmp+="\n";}
88[ \t\r\f]           ;
89\n                  {trctx.line++; trctx.linechanged=true;}
90.                   return yytext[0];
91
92"asm"[ \t\n\r]*"{"  {
93                    BEGIN asmcode;
94                    char *t=yytext;
95                    while(t=strchr(t+1,'\n')) trctx.line++;
96                    trctx.linechanged=true;
97                    return ASM;
98                    }
99<asmcode>.*\n {
100              char *t=yytext;
101              trctx.line++; trctx.linechanged=true;
102              while ((*t==' ')||(*t=='\t')) t++;
103              if (*t=='}') {yyless((t-yytext)+1); BEGIN 0; return '}';}
104              char *e=yytext+yyleng-1;
105              while (e[-1]=='\r') e--;
106              framscriptlval.setString(SString(t,e-t));
107              return ASMLINE;
108              }
109
110%%
111
112//exclusively for multiline, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO)
113static SString quoteMultiline(const SString &src)
114{
115int len=src.len();
116SString ret((len*11)/10+10);
117const char*t=(const char*)src;
118while(len>0)
119        {
120        switch(*t)
121            {
122            case '\"': ret+="\\\""; break;
123            case '\\': ret+="\\\\"; break;
124            default: ret+=*t;
125            }
126        t++; len--;
127        }
128return ret;
129}
130
131void framscript_init_lex()
132{
133BEGIN 0;
134yyrestart(0);
135}
136
137void framscript_cleanup_lex()
138{
139yy_delete_buffer(yy_current_buffer);
140}
Note: See TracBrowser for help on using the repository browser.