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 | |
---|
13 | static 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;} |
---|
26 | 0x[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);} |
---|
36 | "@file "[^\n\t\r]+\n {trctx.srcname=SString(yytext+6,yyleng-7);} |
---|
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++;} // komentarz jednoliniowy |
---|
68 | <INITIAL,asmcode,asmcode2>\/\/[^\n]* ; // komentarz ale nie ma potem \n |
---|
69 | |
---|
70 | <INITIAL>\/\* BEGIN comment; |
---|
71 | <comment>\n {trctx.line++;} |
---|
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.tmp+="\n";} |
---|
88 | [ \t\r\f] ; |
---|
89 | \n {trctx.line++;} |
---|
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 | return ASM; |
---|
97 | } |
---|
98 | <asmcode>.*\n { |
---|
99 | char *t=yytext; |
---|
100 | trctx.line++; |
---|
101 | while ((*t==' ')||(*t=='\t')) t++; |
---|
102 | if (*t=='}') {yyless((t-yytext)+1); BEGIN 0; return '}';} |
---|
103 | char *e=yytext+yyleng-1; |
---|
104 | while (e[-1]=='\r') e--; |
---|
105 | framscriptlval.setString(SString(t,e-t)); |
---|
106 | return ASMLINE; |
---|
107 | } |
---|
108 | |
---|
109 | %% |
---|
110 | |
---|
111 | //exclusively for multiline, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO) |
---|
112 | static SString quoteMultiline(const SString &src) |
---|
113 | { |
---|
114 | int len=src.len(); |
---|
115 | SString ret((len*11)/10+10); |
---|
116 | const char*t=(const char*)src; |
---|
117 | while(len>0) |
---|
118 | { |
---|
119 | switch(*t) |
---|
120 | { |
---|
121 | case '\"': ret+="\\\""; break; |
---|
122 | case '\\': ret+="\\\\"; break; |
---|
123 | default: ret+=*t; |
---|
124 | } |
---|
125 | t++; len--; |
---|
126 | } |
---|
127 | return ret; |
---|
128 | } |
---|
129 | |
---|
130 | void framscript_init_lex() |
---|
131 | { |
---|
132 | BEGIN 0; |
---|
133 | yyrestart(0); |
---|
134 | } |
---|
135 | |
---|
136 | void framscript_cleanup_lex() |
---|
137 | { |
---|
138 | yy_delete_buffer(yy_current_buffer); |
---|
139 | } |
---|