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

Last change on this file since 221 was 221, checked in by Maciej Komosinski, 10 years ago

"here document" in framscript, ​http://www.framsticks.com/common/script/framscript-lang.html#heredoc

  • Property svn:eol-style set to native
File size: 4.1 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 quoteDoubleQuotes(const SString &src);
14
15%}
16
17%option noyywrap
18%x asmcode
19%x asmcode2
20%x comment
21%x heredoc
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"<<<".*\n            {trctx.herelimit=SString(yytext+3,yyleng-4); trctx.tmp=""; BEGIN heredoc; }
34"@line "[0-9]+\n   {trctx.line=atol(yytext+6);}
35"@file "[^\n\t\r]+\n {trctx.srcname=SString(yytext+6,yyleng-7);}
36
37[a-zA-Z_][a-zA-Z0-9_]* { int t=lookupToken(yytext);
38                         if (t>=0) return t;
39                         else
40                            {
41                            framscriptlval.setString(yytext);
42                            return framscriptIsObjectName(yytext)?OBJNAME:IDENT;
43                            }
44                       }
45"=="               return EQUAL;
46"<>"               return NOT_EQUAL;
47"!="               return NOT_EQUAL;
48">="               return GEQUAL;
49"<="               return LEQUAL;
50
51"+="                return ASSIGN_ADD;
52"-="                return ASSIGN_SUB;
53"*="                return ASSIGN_MUL;
54"/="                return ASSIGN_DIV;
55"%="                return ASSIGN_MOD;
56
57"++"                return PLUSPLUS;
58"--"                return MINUSMINUS;
59
60"&&"                return LOGIC_AND;
61"||"                return LOGIC_OR;
62
63"<<"                return LSHIFT;
64">>"                return RSHIFT;
65
66<INITIAL,asmcode,asmcode2>\/\/.*\n         {trctx.line++;} // komentarz jednoliniowy
67<INITIAL,asmcode,asmcode2>\/\/[^\n]*       ;               // komentarz ale nie ma potem \n
68
69<INITIAL>\/\*       BEGIN comment;
70<comment>\n         {trctx.line++;}
71<comment>\*\/       BEGIN 0;
72<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;}
78                    else
79                        { trctx.tmp+=tmp; trctx.tmp+="\n";}
80                    }
81[ \t\r\f]           ;
82\n                  {trctx.line++;}
83.                   return yytext[0];
84
85"asm"[ \t\n\r]*"{"  {
86                    BEGIN asmcode;
87                    char *t=yytext;
88                    while(t=strchr(t+1,'\n')) trctx.line++;
89                    return ASM;
90                    }
91<asmcode>.*\n {
92              char *t=yytext;
93              trctx.line++;
94              while ((*t==' ')||(*t=='\t')) t++;
95              if (*t=='}') {yyless((t-yytext)+1); BEGIN 0; return '}';}
96              char *e=yytext+yyleng-1;
97              while (e[-1]=='\r') e--;
98              framscriptlval.setString(SString(t,e-t));
99              return ASMLINE;
100              }
101
102%%
103
104//exclusively for heredoc, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO)
105static SString quoteDoubleQuotes(const SString &src)
106{
107int len=src.len();
108SString ret((len*11)/10+10);
109const char*t=(const char*)src;
110while(len>0)
111        {
112        if (*t=='\"')
113                ret+="\\\"";
114        else
115                 ret+=*t;
116        t++; len--;
117        }
118return ret;
119}
120
121void framscript_init_lex()
122{
123BEGIN 0;
124yyrestart(0);
125}
126
127void framscript_cleanup_lex()
128{
129yy_delete_buffer(yy_current_buffer);
130}
Note: See TracBrowser for help on using the repository browser.