[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] | 13 | static 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]+ | |
---|
| 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 | |
---|
[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;} |
---|
| 37 | "@file "[^\n\t\r]+\n {trctx.srcname=SString(yytext+6,yyleng-7);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) |
---|
| 116 | static SString quoteMultiline(const SString &src) |
---|
[221] | 117 | { |
---|
| 118 | int len=src.len(); |
---|
| 119 | SString ret((len*11)/10+10); |
---|
[348] | 120 | const char*t=src.c_str(); |
---|
[221] | 121 | while(len>0) |
---|
| 122 | { |
---|
[224] | 123 | switch(*t) |
---|
| 124 | { |
---|
| 125 | case '\"': ret+="\\\""; break; |
---|
| 126 | case '\\': ret+="\\\\"; break; |
---|
| 127 | default: ret+=*t; |
---|
| 128 | } |
---|
[221] | 129 | t++; len--; |
---|
| 130 | } |
---|
| 131 | return ret; |
---|
| 132 | } |
---|
| 133 | |
---|
[163] | 134 | void framscript_init_lex() |
---|
| 135 | { |
---|
| 136 | BEGIN 0; |
---|
| 137 | yyrestart(0); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void framscript_cleanup_lex() |
---|
| 141 | { |
---|
| 142 | yy_delete_buffer(yy_current_buffer); |
---|
| 143 | } |
---|