#include "tokenizer.h"
#include "files.h"
-static void countlines(char*text, int len) {
- int t;
- for(t=0;t<len;t++) {
- if(text[t]=='\n') {
- current_line++;
- current_column=0;
- } else {
- current_column++;
- }
- }
-}
-
static int verbose = 1;
static void dbg(const char*format, ...)
{
}
void warning(const char*format, ...)
{
+ return;
char buf[1024];
int l;
va_list arglist;
switch(*s) {
case '\\': if(o) o[len] = '\\';s++;len++; break;
case '"': if(o) o[len] = '"';s++;len++; break;
+ case '\'': if(o) o[len] = '\'';s++;len++; break;
case 'b': if(o) o[len] = '\b';s++;len++; break;
case 'f': if(o) o[len] = '\f';s++;len++; break;
case 'n': if(o) o[len] = '\n';s++;len++; break;
else
return T_UINT;
}
+static inline int setfloat(double v)
+{
+ avm2_lval.number_float = v;
+ return T_FLOAT;
+}
static inline int handlefloat()
{
char l = (yytext[0]=='-');
char*max = l?"1073741824":"2147483647";
- if(yyleng-l>10)
- syntaxerror("integer overflow");
+ if(yyleng-l>10) {
+ warning("integer overflow: %s", s);
+ return handlefloat();
+ }
if(yyleng-l==10) {
int t;
for(t=0;t<yyleng-l;t++) {
- if(yytext[l+t]>max[t])
- syntaxerror("integer overflow %s > %s", s+l,max);
+ if(yytext[l+t]>max[t]) {
+ warning("integer overflow: %s", s);
+ return handlefloat();
+ }
else if(yytext[l+t]<max[t])
break;
}
{
char l = (yytext[0]=='-')+2;
- if(yyleng-l>8)
- syntaxerror("integer overflow");
+ if(yyleng-l>8) {
+ char*s = nrbuf();
+ syntaxerror("integer overflow %s", s);
+ }
+
int t;
unsigned int v = 0;
for(t=l;t<yyleng;t++) {
c>='A' && c<='F')
v|=(c&0x0f)+9;
}
- if(l && v>1073741824)
- syntaxerror("signed integer overflow");
- if(!l && v>2147483647)
- syntaxerror("unsigned integer overflow");
+ if(l && v>1073741824) {
+ char*s = nrbuf();
+ warning("signed integer overflow: %s", s);
+ return setfloat(v);
+ }
+ if(!l && v>2147483647) {
+ char*s = nrbuf();
+ warning("unsigned integer overflow: %s", s);
+ return setfloat(v);
+ }
if(l==3) {
return setint(-(int)v);
int t;
for(t=len-1;t>=0;--t) {
if(text[t]!=' ' &&
- text[t]!='.')
+ text[t]!=':')
break;
}
char*s = malloc(t+1);
avm2_lval.id = s;
}
+static int handleregexp()
+{
+ char*s = malloc(yyleng);
+ int len=yyleng-1;
+ memcpy(s, yytext+1, len);
+ s[len] = 0;
+ int t;
+ for(t=len;t>=0;--t) {
+ if(s[t]=='/') {
+ s[t] = 0;
+ break;
+ }
+ }
+ avm2_lval.regexp.pattern = s;
+ if(t==len) {
+ avm2_lval.regexp.options = 0;
+ } else {
+ avm2_lval.regexp.options = s+t+1;
+ }
+ return T_REGEXP;
+}
+
void initialize_scanner();
#define YY_USER_INIT initialize_scanner();
-#define c() {countlines(yytext, yyleng);}
+/* count the number of lines+columns consumed by this token */
+static inline void l() {
+ int t;
+ for(t=0;t<yyleng;t++) {
+ if(yytext[t]=='\n') {
+ current_line++;
+ current_column=0;
+ } else {
+ current_column++;
+ }
+ }
+}
+/* count the number of columns consumed by this token */
+static inline void c() {
+ current_column+=yyleng;
+}
//Boolean {c();return m(KW_BOOLEAN);}
//int {c();return m(KW_INT);}
%%
-{SINGLELINE_COMMENT} {c(); /* single line comment */}
-{MULTILINE_COMMENT} {c(); /* multi line comment */}
+{SINGLELINE_COMMENT} {l(); /* single line comment */}
+{MULTILINE_COMMENT} {l(); /* multi line comment */}
[/][*] {syntaxerror("syntax error: unterminated comment", yytext);}
-^include{S}+{STRING}{S}*/\n {c();handleInclude(yytext, yyleng, 1);}
-^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {c();handleInclude(yytext, yyleng, 0);}
-{STRING} {c(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
+^include{S}+{STRING}{S}*/\n {l();handleInclude(yytext, yyleng, 1);}
+^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {l();handleInclude(yytext, yyleng, 0);}
+{STRING} {l(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
<BEGINNING,REGEXPOK>{
-{REGEXP} {c(); BEGIN(INITIAL);return m(T_REGEXP);}
+{REGEXP} {c(); BEGIN(INITIAL);return handleregexp();}
{HEXWITHSIGN} {c(); BEGIN(INITIAL);return handlehex();}
{INTWITHSIGN} {c(); BEGIN(INITIAL);return handleint();}
{FLOATWITHSIGN} {c(); BEGIN(INITIAL);return handlefloat();}
}
\xef\xbb\xbf {/* utf 8 bom */}
-{S} {c();}
+{S} {l();}
{HEXINT} {c(); BEGIN(INITIAL);return handlehex();}
{INT} {c(); BEGIN(INITIAL);return handleint();}
3rr0r {/* for debugging: generates a tokenizer-level error */
syntaxerror("3rr0r");}
-{NAME}{S}*:{S}*for/{_} {c();handleLabel(yytext, yyleng-3);return T_FOR;}
-{NAME}{S}*:{S}*do/{_} {c();handleLabel(yytext, yyleng-2);return T_DO;}
-{NAME}{S}*:{S}*while/{_} {c();handleLabel(yytext, yyleng-5);return T_WHILE;}
-{NAME}{S}*:{S}*switch/{_} {c();handleLabel(yytext, yyleng-6);return T_SWITCH;}
+{NAME}{S}*:{S}*for/{_} {l();handleLabel(yytext, yyleng-3);return T_FOR;}
+{NAME}{S}*:{S}*do/{_} {l();handleLabel(yytext, yyleng-2);return T_DO;}
+{NAME}{S}*:{S}*while/{_} {l();handleLabel(yytext, yyleng-5);return T_WHILE;}
+{NAME}{S}*:{S}*switch/{_} {l();handleLabel(yytext, yyleng-6);return T_SWITCH;}
for {c();avm2_lval.id="";return T_FOR;}
do {c();avm2_lval.id="";return T_DO;}
while {c();avm2_lval.id="";return T_WHILE;}
[/][=] {c();return m(T_DIVBY);}
[%][=] {c();return m(T_MODBY);}
[*][=] {c();return m(T_MULBY);}
+[|][=] {c();return m(T_ORBY);}
[>][>][=] {c();return m(T_SHRBY);}
[<][<][=] {c();return m(T_SHLBY);}
[>][>][>][=] {c();return m(T_USHRBY);}
static {c();return m(KW_STATIC);}
import {c();return m(KW_IMPORT);}
typeof {c();return m(KW_TYPEOF);}
+throw {c();return m(KW_THROW);}
class {c();return m(KW_CLASS);}
const {c();return m(KW_CONST);}
catch {c();return m(KW_CATCH);}
null {c();return m(KW_NULL);}
else {c();return m(KW_ELSE);}
case {c();return m(KW_CASE);}
+with {c();return m(KW_WITH);}
use {c();return m(KW_USE);}
new {c();return m(KW_NEW);}
get {c();return m(KW_GET);}
{NAME} {c();BEGIN(INITIAL);return mkid(T_IDENTIFIER);}
[+-\/*^~@$!%&\(=\[\]\{\}|?:;,<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);}
-[\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);}
+[\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);}
. {char c1=yytext[0];
char buf[128];
exit(1);
yyterminate();
}
-<<EOF>> {c();
+<<EOF>> {l();
void*b = leave_file();
if (!b) {
yyterminate();