1 | %{ |
---|
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. |
---|
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-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]+ | |
---|
30 | 0x[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 | |
---|
32 | "null" {framscriptlval.setEmpty(); return CONSTANT;} |
---|
33 | \"([^\\\"]|\\.)*\" {framscriptlval.setString(SString(yytext+1,yyleng-2));return CONSTANT;} |
---|
34 | \"\"\" {trctx.tmp=""; trctx.multilimit='\"'; BEGIN multiline;} |
---|
35 | "'''" {trctx.tmp=""; trctx.multilimit='\''; BEGIN multiline;} |
---|
36 | "@line "[0-9]+\n {trctx.line=atol(yytext+6);trctx.linechanged=true;} |
---|
37 | "@file "[^\n\t\r]+\n {trctx.setSrcname(SString(yytext+6,yyleng-7),true);trctx.namechanged=true;} |
---|
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 | } |
---|
47 | |
---|
48 | "->" return ARROW; |
---|
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 | |
---|
70 | <INITIAL,asmcode,asmcode2>\/\/.*\n {trctx.line++;trctx.linechanged=true;} // komentarz jednoliniowy |
---|
71 | <INITIAL,asmcode,asmcode2>\/\/[^\n]* ; // komentarz ale nie ma potem \n |
---|
72 | |
---|
73 | <INITIAL>\/\* BEGIN comment; |
---|
74 | <comment>\n {trctx.line++;trctx.linechanged=true;} |
---|
75 | <comment>\*\/ BEGIN 0; |
---|
76 | <comment>. ; |
---|
77 | <multiline>.* { |
---|
78 | char* end=strstr(yytext,trctx.multilimit=='\"' ? "\"\"\"" : "'''"); |
---|
79 | if (end) |
---|
80 | { |
---|
81 | trctx.tmp+=string(yytext,end-yytext); |
---|
82 | framscriptlval.setString(quoteMultiline(SString(trctx.tmp.c_str()))); |
---|
83 | yyless((end-yytext)+3); |
---|
84 | BEGIN 0; |
---|
85 | return CONSTANT; |
---|
86 | } |
---|
87 | else |
---|
88 | trctx.tmp+=string(yytext,yyleng); |
---|
89 | } |
---|
90 | <multiline>\n {trctx.line++; trctx.linechanged=true; trctx.tmp+="\n";} |
---|
91 | [ \t\r\f] ; |
---|
92 | \n {trctx.line++; trctx.linechanged=true;} |
---|
93 | . return (int)(unsigned char)yytext[0]; |
---|
94 | |
---|
95 | "asm"[ \t\n\r]*"{" { |
---|
96 | BEGIN asmcode; |
---|
97 | char *t=yytext; |
---|
98 | while(t=strchr(t+1,'\n')) trctx.line++; |
---|
99 | trctx.linechanged=true; |
---|
100 | return ASM; |
---|
101 | } |
---|
102 | <asmcode>.*\n { |
---|
103 | char *t=yytext; |
---|
104 | trctx.line++; trctx.linechanged=true; |
---|
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 | %% |
---|
114 | |
---|
115 | //exclusively for multiline, but it should not be needed, there is some unexpected difference between quoting \n and \" in framscript parsing (TODO) |
---|
116 | static SString quoteMultiline(const SString &src) |
---|
117 | { |
---|
118 | int len=src.length(); |
---|
119 | SString ret; |
---|
120 | ret.reserve((len*11)/10+10); |
---|
121 | const char*t=src.c_str(); |
---|
122 | while(len>0) |
---|
123 | { |
---|
124 | switch(*t) |
---|
125 | { |
---|
126 | case '\"': ret+="\\\""; break; |
---|
127 | case '\\': ret+="\\\\"; break; |
---|
128 | default: ret+=*t; |
---|
129 | } |
---|
130 | t++; len--; |
---|
131 | } |
---|
132 | return ret; |
---|
133 | } |
---|
134 | |
---|
135 | void framscript_init_lex() |
---|
136 | { |
---|
137 | BEGIN 0; |
---|
138 | yyrestart(0); |
---|
139 | } |
---|
140 | |
---|
141 | void framscript_cleanup_lex() |
---|
142 | { |
---|
143 | yy_delete_buffer(yy_current_buffer); |
---|
144 | } |
---|