diff options
Diffstat (limited to 'src/llex.js')
-rw-r--r-- | src/llex.js | 440 |
1 files changed, 253 insertions, 187 deletions
diff --git a/src/llex.js b/src/llex.js index 438f288..d9af318 100644 --- a/src/llex.js +++ b/src/llex.js @@ -1,67 +1,124 @@ "use strict"; -const assert = require('assert'); - -const defs = require('./defs.js'); +const { + constant_types: { LUA_TLNGSTR }, + thread_status: { LUA_ERRSYNTAX }, + to_luastring +} = require('./defs.js'); +const { + LUA_MINBUFFER, + MAX_INT, + lua_assert +} = require('./llimits.js'); const ldebug = require('./ldebug.js'); const ldo = require('./ldo.js'); -const ljstype = require('./ljstype.js'); +const { + lisdigit, + lislalnum, + lislalpha, + lisspace, + lisxdigit +} = require('./ljstype.js'); const lobject = require('./lobject.js'); -const lstring = require('./lstring.js'); +const { + luaS_bless, + luaS_hash, + luaS_hashlongstr, + luaS_new +} = require('./lstring.js'); const ltable = require('./ltable.js'); -const llimits = require('./llimits.js'); -const lzio = require('./lzio.js'); -const TS = defs.thread_status; -const char = defs.char; +const { + EOZ, + luaZ_buffer, + luaZ_buffremove, + luaZ_resetbuffer, + luaZ_resizebuffer +} = require('./lzio.js'); const FIRST_RESERVED = 257; -const LUA_ENV = defs.to_luastring("_ENV", true); +const LUA_ENV = to_luastring("_ENV", true); + +/* terminal symbols denoted by reserved words */ +const TK_AND = FIRST_RESERVED; +const TK_BREAK = FIRST_RESERVED + 1; +const TK_DO = FIRST_RESERVED + 2; +const TK_ELSE = FIRST_RESERVED + 3; +const TK_ELSEIF = FIRST_RESERVED + 4; +const TK_END = FIRST_RESERVED + 5; +const TK_FALSE = FIRST_RESERVED + 6; +const TK_FOR = FIRST_RESERVED + 7; +const TK_FUNCTION = FIRST_RESERVED + 8; +const TK_GOTO = FIRST_RESERVED + 9; +const TK_IF = FIRST_RESERVED + 10; +const TK_IN = FIRST_RESERVED + 11; +const TK_LOCAL = FIRST_RESERVED + 12; +const TK_NIL = FIRST_RESERVED + 13; +const TK_NOT = FIRST_RESERVED + 14; +const TK_OR = FIRST_RESERVED + 15; +const TK_REPEAT = FIRST_RESERVED + 16; +const TK_RETURN = FIRST_RESERVED + 17; +const TK_THEN = FIRST_RESERVED + 18; +const TK_TRUE = FIRST_RESERVED + 19; +const TK_UNTIL = FIRST_RESERVED + 20; +const TK_WHILE = FIRST_RESERVED + 21; +/* other terminal symbols */ +const TK_IDIV = FIRST_RESERVED + 22; +const TK_CONCAT = FIRST_RESERVED + 23; +const TK_DOTS = FIRST_RESERVED + 24; +const TK_EQ = FIRST_RESERVED + 25; +const TK_GE = FIRST_RESERVED + 26; +const TK_LE = FIRST_RESERVED + 27; +const TK_NE = FIRST_RESERVED + 28; +const TK_SHL = FIRST_RESERVED + 29; +const TK_SHR = FIRST_RESERVED + 30; +const TK_DBCOLON = FIRST_RESERVED + 31; +const TK_EOS = FIRST_RESERVED + 32; +const TK_FLT = FIRST_RESERVED + 33; +const TK_INT = FIRST_RESERVED + 34; +const TK_NAME = FIRST_RESERVED + 35; +const TK_STRING = FIRST_RESERVED + 36; const RESERVED = { - /* terminal symbols denoted by reserved words */ - TK_AND: FIRST_RESERVED, - TK_BREAK: FIRST_RESERVED + 1, - TK_DO: FIRST_RESERVED + 2, - TK_ELSE: FIRST_RESERVED + 3, - TK_ELSEIF: FIRST_RESERVED + 4, - TK_END: FIRST_RESERVED + 5, - TK_FALSE: FIRST_RESERVED + 6, - TK_FOR: FIRST_RESERVED + 7, - TK_FUNCTION: FIRST_RESERVED + 8, - TK_GOTO: FIRST_RESERVED + 9, - TK_IF: FIRST_RESERVED + 10, - TK_IN: FIRST_RESERVED + 11, - TK_LOCAL: FIRST_RESERVED + 12, - TK_NIL: FIRST_RESERVED + 13, - TK_NOT: FIRST_RESERVED + 14, - TK_OR: FIRST_RESERVED + 15, - TK_REPEAT: FIRST_RESERVED + 16, - TK_RETURN: FIRST_RESERVED + 17, - TK_THEN: FIRST_RESERVED + 18, - TK_TRUE: FIRST_RESERVED + 19, - TK_UNTIL: FIRST_RESERVED + 20, - TK_WHILE: FIRST_RESERVED + 21, - /* other terminal symbols */ - TK_IDIV: FIRST_RESERVED + 22, - TK_CONCAT: FIRST_RESERVED + 23, - TK_DOTS: FIRST_RESERVED + 24, - TK_EQ: FIRST_RESERVED + 25, - TK_GE: FIRST_RESERVED + 26, - TK_LE: FIRST_RESERVED + 27, - TK_NE: FIRST_RESERVED + 28, - TK_SHL: FIRST_RESERVED + 29, - TK_SHR: FIRST_RESERVED + 30, - TK_DBCOLON: FIRST_RESERVED + 31, - TK_EOS: FIRST_RESERVED + 32, - TK_FLT: FIRST_RESERVED + 33, - TK_INT: FIRST_RESERVED + 34, - TK_NAME: FIRST_RESERVED + 35, - TK_STRING: FIRST_RESERVED + 36 + "TK_AND": TK_AND, + "TK_BREAK": TK_BREAK, + "TK_DO": TK_DO, + "TK_ELSE": TK_ELSE, + "TK_ELSEIF": TK_ELSEIF, + "TK_END": TK_END, + "TK_FALSE": TK_FALSE, + "TK_FOR": TK_FOR, + "TK_FUNCTION": TK_FUNCTION, + "TK_GOTO": TK_GOTO, + "TK_IF": TK_IF, + "TK_IN": TK_IN, + "TK_LOCAL": TK_LOCAL, + "TK_NIL": TK_NIL, + "TK_NOT": TK_NOT, + "TK_OR": TK_OR, + "TK_REPEAT": TK_REPEAT, + "TK_RETURN": TK_RETURN, + "TK_THEN": TK_THEN, + "TK_TRUE": TK_TRUE, + "TK_UNTIL": TK_UNTIL, + "TK_WHILE": TK_WHILE, + "TK_IDIV": TK_IDIV, + "TK_CONCAT": TK_CONCAT, + "TK_DOTS": TK_DOTS, + "TK_EQ": TK_EQ, + "TK_GE": TK_GE, + "TK_LE": TK_LE, + "TK_NE": TK_NE, + "TK_SHL": TK_SHL, + "TK_SHR": TK_SHR, + "TK_DBCOLON": TK_DBCOLON, + "TK_EOS": TK_EOS, + "TK_FLT": TK_FLT, + "TK_INT": TK_INT, + "TK_NAME": TK_NAME, + "TK_STRING": TK_STRING }; -const R = RESERVED; - const luaX_tokens = [ "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", @@ -70,7 +127,7 @@ const luaX_tokens = [ "//", "..", "...", "==", ">=", "<=", "~=", "<<", ">>", "::", "<eof>", "<number>", "<integer>", "<name>", "<string>" -]; +].map((e, i)=>to_luastring(e)); class SemInfo { constructor() { @@ -110,28 +167,28 @@ class LexState { const save = function(ls, c) { let b = ls.buff; if (b.n + 1 > b.buffer.length) { - if (b.buffer.length >= llimits.MAX_INT/2) - lexerror(ls, defs.to_luastring("lexical element too long", true), 0); + if (b.buffer.length >= MAX_INT/2) + lexerror(ls, to_luastring("lexical element too long", true), 0); let newsize = b.buffer.length*2; - lzio.luaZ_resizebuffer(ls.L, b, newsize); + luaZ_resizebuffer(ls.L, b, newsize); } b.buffer[b.n++] = c < 0 ? 255 + c + 1 : c; }; const luaX_token2str = function(ls, token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ - return lobject.luaO_pushfstring(ls.L, defs.to_luastring("'%c'", true), token); + return lobject.luaO_pushfstring(ls.L, to_luastring("'%c'", true), token); } else { let s = luaX_tokens[token - FIRST_RESERVED]; - if (token < R.TK_EOS) /* fixed format (symbols and reserved words)? */ - return lobject.luaO_pushfstring(ls.L, defs.to_luastring("'%s'", true), defs.to_luastring(s)); + if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ + return lobject.luaO_pushfstring(ls.L, to_luastring("'%s'", true), s); else /* names, strings, and numerals */ - return defs.to_luastring(s); + return s; } }; const currIsNewline = function(ls) { - return ls.current === char['\n'] || ls.current === char['\r']; + return ls.current === 10 /* ('\n').charCodeAt(0) */ || ls.current === 13 /* ('\r').charCodeAt(0) */; }; const next = function(ls) { @@ -150,14 +207,14 @@ const save_and_next = function(ls) { */ const luaX_newstring = function(ls, str) { let L = ls.L; - let ts = lstring.luaS_new(L, str); - let o = ltable.luaH_set(L, ls.h, new lobject.TValue(defs.CT.LUA_TLNGSTR, ts)); + let ts = luaS_new(L, str); + let o = ltable.luaH_set(L, ls.h, new lobject.TValue(LUA_TLNGSTR, ts)); if (o.ttisnil()) { /* not in use yet? */ o.setbvalue(true); } else { /* string already present */ /* HACK: Workaround lack of ltable 'keyfromval' */ - let tpair = ls.h.strong.get(lstring.luaS_hashlongstr(ts)); - assert(tpair.value == o); + let tpair = ls.h.strong.get(luaS_hashlongstr(ts)); + lua_assert(tpair.value == o); /* fengari addition */ ts = tpair.key.tsvalue(); /* re-use value previously stored */ } return ts; @@ -169,12 +226,12 @@ const luaX_newstring = function(ls, str) { */ const inclinenumber = function(ls) { let old = ls.current; - assert(currIsNewline(ls)); + lua_assert(currIsNewline(ls)); next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls.current !== old) next(ls); /* skip '\n\r' or '\r\n' */ - if (++ls.linenumber >= llimits.MAX_INT) - lexerror(ls, defs.to_luastring("chunk has too many lines", true), 0); + if (++ls.linenumber >= MAX_INT) + lexerror(ls, to_luastring("chunk has too many lines", true), 0); }; const luaX_setinput = function(L, ls, z, source, firstchar) { @@ -185,7 +242,7 @@ const luaX_setinput = function(L, ls, z, source, firstchar) { ls.L = L; ls.current = firstchar; ls.lookahead = { - token: R.TK_EOS, + token: TK_EOS, seminfo: new SemInfo() }; ls.z = z; @@ -193,12 +250,12 @@ const luaX_setinput = function(L, ls, z, source, firstchar) { ls.linenumber = 1; ls.lastline = 1; ls.source = source; - ls.envn = lstring.luaS_bless(L, LUA_ENV); - lzio.luaZ_resizebuffer(L, ls.buff, llimits.LUA_MINBUFFER); /* initialize buffer */ + ls.envn = luaS_bless(L, LUA_ENV); + luaZ_resizebuffer(L, ls.buff, LUA_MINBUFFER); /* initialize buffer */ }; const check_next1 = function(ls, c) { - if (ls.current === c.charCodeAt(0)) { + if (ls.current === c) { next(ls); return true; } @@ -222,17 +279,17 @@ const check_next2 = function(ls, set) { const read_numeral = function(ls, seminfo) { let expo = "Ee"; let first = ls.current; - assert(ljstype.lisdigit(ls.current)); + lua_assert(lisdigit(ls.current)); save_and_next(ls); - if (first === char['0'] && check_next2(ls, "xX")) /* hexadecimal? */ + if (first === 48 /* ('0').charCodeAt(0) */ && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { if (check_next2(ls, expo)) /* exponent part? */ check_next2(ls, "-+"); /* optional exponent sign */ - if (ljstype.lisxdigit(ls.current)) + if (lisxdigit(ls.current)) save_and_next(ls); - else if (ls.current === char['.']) + else if (ls.current === 46 /* ('.').charCodeAt(0) */) save_and_next(ls); else break; } @@ -240,24 +297,24 @@ const read_numeral = function(ls, seminfo) { // save(ls, 0); let obj = new lobject.TValue(); - if (lobject.luaO_str2num(lzio.luaZ_buffer(ls.buff), obj) === 0) /* format error? */ - lexerror(ls, defs.to_luastring("malformed number", true), R.TK_FLT); + if (lobject.luaO_str2num(luaZ_buffer(ls.buff), obj) === 0) /* format error? */ + lexerror(ls, to_luastring("malformed number", true), TK_FLT); if (obj.ttisinteger()) { seminfo.i = obj.value; - return R.TK_INT; + return TK_INT; } else { - assert(obj.ttisfloat()); + lua_assert(obj.ttisfloat()); seminfo.r = obj.value; - return R.TK_FLT; + return TK_FLT; } }; const txtToken = function(ls, token) { switch (token) { - case R.TK_NAME: case R.TK_STRING: - case R.TK_FLT: case R.TK_INT: + case TK_NAME: case TK_STRING: + case TK_FLT: case TK_INT: // save(ls, 0); - return lobject.luaO_pushfstring(ls.L, defs.to_luastring("'%s'", true), lzio.luaZ_buffer(ls.buff)); + return lobject.luaO_pushfstring(ls.L, to_luastring("'%s'", true), luaZ_buffer(ls.buff)); default: return luaX_token2str(ls, token); } @@ -266,8 +323,8 @@ const txtToken = function(ls, token) { const lexerror = function(ls, msg, token) { msg = ldebug.luaG_addinfo(ls.L, msg, ls.source, ls.linenumber); if (token) - lobject.luaO_pushfstring(ls.L, defs.to_luastring("%s near %s"), msg, txtToken(ls, token)); - ldo.luaD_throw(ls.L, TS.LUA_ERRSYNTAX); + lobject.luaO_pushfstring(ls.L, to_luastring("%s near %s"), msg, txtToken(ls, token)); + ldo.luaD_throw(ls.L, LUA_ERRSYNTAX); }; const luaX_syntaxerror = function(ls, msg) { @@ -282,9 +339,9 @@ const luaX_syntaxerror = function(ls, msg) { const skip_sep = function(ls) { let count = 0; let s = ls.current; - assert(s === char['['] || s === char[']']); + lua_assert(s === 91 /* ('[').charCodeAt(0) */ || s === 93 /* (']').charCodeAt(0) */); save_and_next(ls); - while (ls.current === char['=']) { + while (ls.current === 61 /* ('=').charCodeAt(0) */) { save_and_next(ls); count++; } @@ -301,23 +358,24 @@ const read_long_string = function(ls, seminfo, sep) { let skip = false; for (; !skip ;) { switch (ls.current) { - case lzio.EOZ: { /* error */ + case EOZ: { /* error */ let what = seminfo ? "string" : "comment"; let msg = `unfinished long ${what} (starting at line ${line})`; - lexerror(ls, defs.to_luastring(msg), R.TK_EOS); + lexerror(ls, to_luastring(msg), TK_EOS); break; } - case char[']']: { + case 93 /* (']').charCodeAt(0) */: { if (skip_sep(ls) === sep) { save_and_next(ls); /* skip 2nd ']' */ skip = true; } break; } - case char['\n']: case char['\r']: { - save(ls, char['\n']); + case 10 /* ('\n').charCodeAt(0) */: + case 13 /* ('\r').charCodeAt(0) */: { + save(ls, 10 /* ('\n').charCodeAt(0) */); inclinenumber(ls); - if (!seminfo) lzio.luaZ_resetbuffer(ls.buff); + if (!seminfo) luaZ_resetbuffer(ls.buff); break; } default: { @@ -333,41 +391,41 @@ const read_long_string = function(ls, seminfo, sep) { const esccheck = function(ls, c, msg) { if (!c) { - if (ls.current !== lzio.EOZ) + if (ls.current !== EOZ) save_and_next(ls); /* add current to buffer for error message */ - lexerror(ls, msg, R.TK_STRING); + lexerror(ls, msg, TK_STRING); } }; const gethexa = function(ls) { save_and_next(ls); - esccheck(ls, ljstype.lisxdigit(ls.current), defs.to_luastring("hexadecimal digit expected", true)); + esccheck(ls, lisxdigit(ls.current), to_luastring("hexadecimal digit expected", true)); return lobject.luaO_hexavalue(ls.current); }; const readhexaesc = function(ls) { let r = gethexa(ls); r = (r << 4) + gethexa(ls); - lzio.luaZ_buffremove(ls.buff, 2); /* remove saved chars from buffer */ + luaZ_buffremove(ls.buff, 2); /* remove saved chars from buffer */ return r; }; const readutf8desc = function(ls) { let i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ save_and_next(ls); /* skip 'u' */ - esccheck(ls, ls.current === char['{'], defs.to_luastring("missing '{'", true)); + esccheck(ls, ls.current === 123 /* ('{').charCodeAt(0) */, to_luastring("missing '{'", true)); let r = gethexa(ls); /* must have at least one digit */ save_and_next(ls); - while (ljstype.lisxdigit(ls.current)) { + while (lisxdigit(ls.current)) { i++; r = (r << 4) + lobject.luaO_hexavalue(ls.current); - esccheck(ls, r <= 0x10FFFF, defs.to_luastring("UTF-8 value too large", true)); + esccheck(ls, r <= 0x10FFFF, to_luastring("UTF-8 value too large", true)); save_and_next(ls); } - esccheck(ls, ls.current === char['}'], defs.to_luastring("missing '}'", true)); + esccheck(ls, ls.current === 125 /* ('}').charCodeAt(0) */, to_luastring("missing '}'", true)); next(ls); /* skip '}' */ - lzio.luaZ_buffremove(ls.buff, i); /* remove saved chars from buffer */ + luaZ_buffremove(ls.buff, i); /* remove saved chars from buffer */ return r; }; @@ -381,12 +439,12 @@ const utf8esc = function(ls) { const readdecesc = function(ls) { let r = 0; /* result accumulator */ let i; - for (i = 0; i < 3 && ljstype.lisdigit(ls.current); i++) { /* read up to 3 digits */ - r = 10 * r + ls.current - char['0']; + for (i = 0; i < 3 && lisdigit(ls.current); i++) { /* read up to 3 digits */ + r = 10 * r + ls.current - 48 /* ('0').charCodeAt(0) */; save_and_next(ls); } - esccheck(ls, r <= 255, defs.to_luastring("decimal escape too large", true)); - lzio.luaZ_buffremove(ls.buff, i); /* remove read digits from buffer */ + esccheck(ls, r <= 255, to_luastring("decimal escape too large", true)); + luaZ_buffremove(ls.buff, i); /* remove read digits from buffer */ return r; }; @@ -395,43 +453,46 @@ const read_string = function(ls, del, seminfo) { while (ls.current !== del) { switch (ls.current) { - case lzio.EOZ: - lexerror(ls, defs.to_luastring("unfinished string", true), R.TK_EOS); + case EOZ: + lexerror(ls, to_luastring("unfinished string", true), TK_EOS); break; - case char['\n']: - case char['\r']: - lexerror(ls, defs.to_luastring("unfinished string", true), R.TK_STRING); + case 10 /* ('\n').charCodeAt(0) */: + case 13 /* ('\r').charCodeAt(0) */: + lexerror(ls, to_luastring("unfinished string", true), TK_STRING); break; - case char['\\']: { /* escape sequences */ + case 92 /* ('\\').charCodeAt(0) */: { /* escape sequences */ save_and_next(ls); /* keep '\\' for error messages */ let will; let c; switch(ls.current) { - case char['a']: c = 7 /* \a isn't valid JS */; will = 'read_save'; break; - case char['b']: c = char['\b']; will = 'read_save'; break; - case char['f']: c = char['\f']; will = 'read_save'; break; - case char['n']: c = char['\n']; will = 'read_save'; break; - case char['r']: c = char['\r']; will = 'read_save'; break; - case char['t']: c = char['\t']; will = 'read_save'; break; - case char['v']: c = char['\v']; will = 'read_save'; break; - case char['x']: c = readhexaesc(ls); will = 'read_save'; break; - case char['u']: utf8esc(ls); will = 'no_save'; break; - case char['\n']: case char['\r']: - inclinenumber(ls); c = char['\n']; will = 'only_save'; break; - case char['\\']: case char['"']: case char['\'']: + case 97 /* ('a').charCodeAt(0) */: c = 7 /* \a isn't valid JS */; will = 'read_save'; break; + case 98 /* ('b').charCodeAt(0) */: c = 8 /* ('\b').charCodeAt(0) */; will = 'read_save'; break; + case 102 /* ('f').charCodeAt(0) */: c = 12 /* ('\f').charCodeAt(0) */; will = 'read_save'; break; + case 110 /* ('n').charCodeAt(0) */: c = 10 /* ('\n').charCodeAt(0) */; will = 'read_save'; break; + case 114 /* ('r').charCodeAt(0) */: c = 13 /* ('\r').charCodeAt(0) */; will = 'read_save'; break; + case 116 /* ('t').charCodeAt(0) */: c = 9 /* ('\t').charCodeAt(0) */; will = 'read_save'; break; + case 118 /* ('v').charCodeAt(0) */: c = 11 /* ('\v').charCodeAt(0) */; will = 'read_save'; break; + case 120 /* ('x').charCodeAt(0) */: c = readhexaesc(ls); will = 'read_save'; break; + case 117 /* ('u').charCodeAt(0) */: utf8esc(ls); will = 'no_save'; break; + case 10 /* ('\n').charCodeAt(0) */: + case 13 /* ('\r').charCodeAt(0) */: + inclinenumber(ls); c = 10 /* ('\n').charCodeAt(0) */; will = 'only_save'; break; + case 92 /* ('\\').charCodeAt(0) */: + case 34 /* ('"').charCodeAt(0) */: + case 39 /* ('\'').charCodeAt(0) */: c = ls.current; will = 'read_save'; break; - case lzio.EOZ: will = 'no_save'; break; /* will raise an error next loop */ - case char['z']: { /* zap following span of spaces */ - lzio.luaZ_buffremove(ls.buff, 1); /* remove '\\' */ + case EOZ: will = 'no_save'; break; /* will raise an error next loop */ + case 122 /* ('z').charCodeAt(0) */: { /* zap following span of spaces */ + luaZ_buffremove(ls.buff, 1); /* remove '\\' */ next(ls); /* skip the 'z' */ - while (ljstype.lisspace(ls.current)) { + while (lisspace(ls.current)) { if (currIsNewline(ls)) inclinenumber(ls); else next(ls); } will = 'no_save'; break; } default: { - esccheck(ls, ljstype.lisdigit(ls.current), defs.to_luastring("invalid escape sequence", true)); + esccheck(ls, lisdigit(ls.current), to_luastring("invalid escape sequence", true)); c = readdecesc(ls); /* digital escape '\ddd' */ will = 'only_save'; break; } @@ -441,7 +502,7 @@ const read_string = function(ls, del, seminfo) { next(ls); if (will === 'read_save' || will === 'only_save') { - lzio.luaZ_buffremove(ls.buff, 1); /* remove '\\' */ + luaZ_buffremove(ls.buff, 1); /* remove '\\' */ save(ls, c); } @@ -457,120 +518,125 @@ const read_string = function(ls, del, seminfo) { }; const token_to_index = Object.create(null); /* don't want to return true for e.g. 'hasOwnProperty' */ -luaX_tokens.forEach((e, i)=>token_to_index[lstring.luaS_hash(defs.to_luastring(e))] = i); +luaX_tokens.forEach((e, i)=>token_to_index[luaS_hash(e)] = i); const isreserved = function(w) { - let kidx = token_to_index[lstring.luaS_hashlongstr(w)]; + let kidx = token_to_index[luaS_hashlongstr(w)]; return kidx !== void 0 && kidx <= 22; }; const llex = function(ls, seminfo) { - lzio.luaZ_resetbuffer(ls.buff); + luaZ_resetbuffer(ls.buff); for (;;) { - assert(typeof ls.current == "number"); + lua_assert(typeof ls.current == "number"); /* fengari addition */ switch (ls.current) { - case char['\n']: case char['\r']: { /* line breaks */ + case 10 /* ('\n').charCodeAt(0) */: + case 13 /* ('\r').charCodeAt(0) */: { /* line breaks */ inclinenumber(ls); break; } - case char[' ']: case char['\f']: case char['\t']: case char['\v']: { /* spaces */ + case 32 /* (' ').charCodeAt(0) */: + case 12 /* ('\f').charCodeAt(0) */: + case 9 /* ('\t').charCodeAt(0) */: + case 11 /* ('\v').charCodeAt(0) */: { /* spaces */ next(ls); break; } - case char['-']: { /* '-' or '--' (comment) */ + case 45 /* ('-').charCodeAt(0) */: { /* '-' or '--' (comment) */ next(ls); - if (ls.current !== char['-']) return char['-']; + if (ls.current !== 45 /* ('-').charCodeAt(0) */) return 45 /* ('-').charCodeAt(0) */; /* else is a comment */ next(ls); - if (ls.current === char['[']) { /* long comment? */ + if (ls.current === 91 /* ('[').charCodeAt(0) */) { /* long comment? */ let sep = skip_sep(ls); - lzio.luaZ_resetbuffer(ls.buff); /* 'skip_sep' may dirty the buffer */ + luaZ_resetbuffer(ls.buff); /* 'skip_sep' may dirty the buffer */ if (sep >= 0) { read_long_string(ls, null, sep); /* skip long comment */ - lzio.luaZ_resetbuffer(ls.buff); /* previous call may dirty the buff. */ + luaZ_resetbuffer(ls.buff); /* previous call may dirty the buff. */ break; } } /* else short comment */ - while (!currIsNewline(ls) && ls.current !== lzio.EOZ) + while (!currIsNewline(ls) && ls.current !== EOZ) next(ls); /* skip until end of line (or end of file) */ break; } - case char['[']: { /* long string or simply '[' */ + case 91 /* ('[').charCodeAt(0) */: { /* long string or simply '[' */ let sep = skip_sep(ls); if (sep >= 0) { read_long_string(ls, seminfo, sep); - return R.TK_STRING; + return TK_STRING; } else if (sep !== -1) /* '[=...' missing second bracket */ - lexerror(ls, defs.to_luastring("invalid long string delimiter", true), R.TK_STRING); - return char['[']; + lexerror(ls, to_luastring("invalid long string delimiter", true), TK_STRING); + return 91 /* ('[').charCodeAt(0) */; } - case char['=']: { + case 61 /* ('=').charCodeAt(0) */: { next(ls); - if (check_next1(ls, '=')) return R.TK_EQ; - else return char['=']; + if (check_next1(ls, 61 /* ('=').charCodeAt(0) */)) return TK_EQ; + else return 61 /* ('=').charCodeAt(0) */; } - case char['<']: { + case 60 /* ('<').charCodeAt(0) */: { next(ls); - if (check_next1(ls, '=')) return R.TK_LE; - else if (check_next1(ls, '<')) return R.TK_SHL; - else return char['<']; + if (check_next1(ls, 61 /* ('=').charCodeAt(0) */)) return TK_LE; + else if (check_next1(ls, 60 /* ('<').charCodeAt(0) */)) return TK_SHL; + else return 60 /* ('<').charCodeAt(0) */; } - case char['>']: { + case 62 /* ('>').charCodeAt(0) */: { next(ls); - if (check_next1(ls, '=')) return R.TK_GE; - else if (check_next1(ls, '>')) return R.TK_SHR; - else return char['>']; + if (check_next1(ls, 61 /* ('=').charCodeAt(0) */)) return TK_GE; + else if (check_next1(ls, 62 /* ('>').charCodeAt(0) */)) return TK_SHR; + else return 62 /* ('>').charCodeAt(0) */; } - case char['/']: { + case 47 /* ('/').charCodeAt(0) */: { next(ls); - if (check_next1(ls, '/')) return R.TK_IDIV; - else return char['/']; + if (check_next1(ls, 47 /* ('/').charCodeAt(0) */)) return TK_IDIV; + else return 47 /* ('/').charCodeAt(0) */; } - case char['~']: { + case 126 /* ('~').charCodeAt(0) */: { next(ls); - if (check_next1(ls, '=')) return R.TK_NE; - else return char['~']; + if (check_next1(ls, 61 /* ('=').charCodeAt(0) */)) return TK_NE; + else return 126 /* ('~').charCodeAt(0) */; } - case char[':']: { + case 58 /* (':').charCodeAt(0) */: { next(ls); - if (check_next1(ls, ':')) return R.TK_DBCOLON; - else return char[':']; + if (check_next1(ls, 58 /* (':').charCodeAt(0) */)) return TK_DBCOLON; + else return 58 /* (':').charCodeAt(0) */; } - case char['"']: case char['\'']: { /* short literal strings */ + case 34 /* ('"').charCodeAt(0) */: + case 39 /* ('\'').charCodeAt(0) */: { /* short literal strings */ read_string(ls, ls.current, seminfo); - return R.TK_STRING; + return TK_STRING; } - case char['.']: { /* '.', '..', '...', or number */ + case 46 /* ('.').charCodeAt(0) */: { /* '.', '..', '...', or number */ save_and_next(ls); - if (check_next1(ls, '.')) { - if (check_next1(ls, '.')) - return R.TK_DOTS; /* '...' */ - else return R.TK_CONCAT; /* '..' */ + if (check_next1(ls, 46 /* ('.').charCodeAt(0) */)) { + if (check_next1(ls, 46 /* ('.').charCodeAt(0) */)) + return TK_DOTS; /* '...' */ + else return TK_CONCAT; /* '..' */ } - else if (!ljstype.lisdigit(ls.current)) return char['.']; + else if (!lisdigit(ls.current)) return 46 /* ('.').charCodeAt(0) */; else return read_numeral(ls, seminfo); } - case char['0']: case char['1']: case char['2']: case char['3']: case char['4']: - case char['5']: case char['6']: case char['7']: case char['8']: case char['9']: { + case 48 /* ('0').charCodeAt(0) */: case 49 /* ('1').charCodeAt(0) */: case 50 /* ('2').charCodeAt(0) */: case 51 /* ('3').charCodeAt(0) */: case 52 /* ('4').charCodeAt(0) */: + case 53 /* ('5').charCodeAt(0) */: case 54 /* ('6').charCodeAt(0) */: case 55 /* ('7').charCodeAt(0) */: case 56 /* ('8').charCodeAt(0) */: case 57 /* ('9').charCodeAt(0) */: { return read_numeral(ls, seminfo); } - case lzio.EOZ: { - return R.TK_EOS; + case EOZ: { + return TK_EOS; } default: { - if (ljstype.lislalpha(ls.current)) { /* identifier or reserved word? */ + if (lislalpha(ls.current)) { /* identifier or reserved word? */ do { save_and_next(ls); - } while (ljstype.lislalnum(ls.current)); - let ts = luaX_newstring(ls, lzio.luaZ_buffer(ls.buff)); + } while (lislalnum(ls.current)); + let ts = luaX_newstring(ls, luaZ_buffer(ls.buff)); seminfo.ts = ts; - let kidx = token_to_index[lstring.luaS_hashlongstr(ts)]; + let kidx = token_to_index[luaS_hashlongstr(ts)]; if (kidx !== void 0 && kidx <= 22) /* reserved word? */ return kidx + FIRST_RESERVED; else - return R.TK_NAME; + return TK_NAME; } else { /* single-char tokens (+ - / ...) */ let c = ls.current; next(ls); @@ -583,18 +649,18 @@ const llex = function(ls, seminfo) { const luaX_next = function(ls) { ls.lastline = ls.linenumber; - if (ls.lookahead.token !== R.TK_EOS) { /* is there a look-ahead token? */ + if (ls.lookahead.token !== TK_EOS) { /* is there a look-ahead token? */ ls.t.token = ls.lookahead.token; /* use this one */ ls.t.seminfo.i = ls.lookahead.seminfo.i; ls.t.seminfo.r = ls.lookahead.seminfo.r; - ls.t.seminfo.ts = ls.lookahead.seminfo.ts; // TODO ? - ls.lookahead.token = R.TK_EOS; /* and discharge it */ + ls.t.seminfo.ts = ls.lookahead.seminfo.ts; + ls.lookahead.token = TK_EOS; /* and discharge it */ } else ls.t.token = llex(ls, ls.t.seminfo); /* read next token */ }; const luaX_lookahead = function(ls) { - assert(ls.lookahead.token === R.TK_EOS); + lua_assert(ls.lookahead.token === TK_EOS); ls.lookahead.token = llex(ls, ls.lookahead.seminfo); return ls.lookahead.token; }; |