astexplorer.app
Version:
https://astexplorer.net with ES Modules support and Hot Reloading
1 lines • 83.5 kB
JavaScript
(window.webpackJsonp=window.webpackJsonp||[]).push([[94],{"./node_modules/luaparse/luaparse.js":function(module,exports,__webpack_require__){eval("/* WEBPACK VAR INJECTION */(function(module, global) {var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/* global exports:true, module:true, require:true, define:true, global:true */\n\n(function (root, name, factory) {\n 'use strict';\n\n // Used to determine if values are of the language type `Object`\n var objectTypes = {\n 'function': true\n , 'object': true\n }\n // Detect free variable `exports`\n , freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports\n // Detect free variable `module`\n , freeModule = objectTypes[typeof module] && module && !module.nodeType && module\n // Detect free variable `global`, from Node.js or Browserified code, and\n // use it as `window`\n , freeGlobal = freeExports && freeModule && typeof global === 'object' && global\n // Detect the popular CommonJS extension `module.exports`\n , moduleExports = freeModule && freeModule.exports === freeExports && freeExports;\n\n /* istanbul ignore else */\n if (freeGlobal && (freeGlobal.global === freeGlobal ||\n /* istanbul ignore next */ freeGlobal.window === freeGlobal ||\n /* istanbul ignore next */ freeGlobal.self === freeGlobal)) {\n root = freeGlobal;\n }\n\n // Some AMD build optimizers, like r.js, check for specific condition\n // patterns like the following:\n /* istanbul ignore if */\n if (true) {\n // defined as an anonymous module.\n !(__WEBPACK_AMD_DEFINE_ARRAY__ = [exports], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),\n\t\t\t\t__WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?\n\t\t\t\t(__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),\n\t\t\t\t__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));\n // In case the source has been processed and wrapped in a define module use\n // the supplied `exports` object.\n if (freeExports && moduleExports) factory(freeModule.exports);\n }\n // check for `exports` after `define` in case a build optimizer adds an\n // `exports` object\n else /* istanbul ignore else */ {}\n}(this, 'luaparse', function (exports) {\n 'use strict';\n\n exports.version = '0.2.1';\n\n var input, options, length, features, encodingMode;\n\n // Options can be set either globally on the parser object through\n // defaultOptions, or during the parse call.\n var defaultOptions = exports.defaultOptions = {\n // Explicitly tell the parser when the input ends.\n wait: false\n // Store comments as an array in the chunk object.\n , comments: true\n // Track identifier scopes by adding an isLocal attribute to each\n // identifier-node.\n , scope: false\n // Store location information on each syntax node as\n // `loc: { start: { line, column }, end: { line, column } }`.\n , locations: false\n // Store the start and end character locations on each syntax node as\n // `range: [start, end]`.\n , ranges: false\n // A callback which will be invoked when a syntax node has been completed.\n // The node which has been created will be passed as the only parameter.\n , onCreateNode: null\n // A callback which will be invoked when a new scope is created.\n , onCreateScope: null\n // A callback which will be invoked when the current scope is destroyed.\n , onDestroyScope: null\n // A callback which will be invoked when a local variable is declared in the current scope.\n // The variable's name will be passed as the only parameter\n , onLocalDeclaration: null\n // The version of Lua targeted by the parser (string; allowed values are\n // '5.1', '5.2', '5.3').\n , luaVersion: '5.1'\n // Encoding mode: how to interpret code units higher than U+007F in input\n , encodingMode: 'none'\n };\n\n function encodeUTF8(codepoint, highMask) {\n highMask = highMask || 0;\n\n if (codepoint < 0x80) {\n return String.fromCharCode(codepoint);\n } else if (codepoint < 0x800) {\n return String.fromCharCode(\n highMask | 0xc0 | (codepoint >> 6) ,\n highMask | 0x80 | ( codepoint & 0x3f)\n );\n } else if (codepoint < 0x10000) {\n return String.fromCharCode(\n highMask | 0xe0 | (codepoint >> 12) ,\n highMask | 0x80 | ((codepoint >> 6) & 0x3f),\n highMask | 0x80 | ( codepoint & 0x3f)\n );\n } else /* istanbul ignore else */ if (codepoint < 0x110000) {\n return String.fromCharCode(\n highMask | 0xf0 | (codepoint >> 18) ,\n highMask | 0x80 | ((codepoint >> 12) & 0x3f),\n highMask | 0x80 | ((codepoint >> 6) & 0x3f),\n highMask | 0x80 | ( codepoint & 0x3f)\n );\n } else {\n // TODO: Lua 5.4 allows up to six-byte sequences, as in UTF-8:1993\n return null;\n }\n }\n\n function toHex(num, digits) {\n var result = num.toString(16);\n while (result.length < digits)\n result = '0' + result;\n return result;\n }\n\n function checkChars(rx) {\n return function (s) {\n var m = rx.exec(s);\n if (!m)\n return s;\n raise(null, errors.invalidCodeUnit, toHex(m[0].charCodeAt(0), 4).toUpperCase());\n };\n }\n\n var encodingModes = {\n // `pseudo-latin1` encoding mode: assume the input was decoded with the latin1 encoding\n // WARNING: latin1 does **NOT** mean cp1252 here like in the bone-headed WHATWG standard;\n // it means true ISO/IEC 8859-1 identity-mapped to Basic Latin and Latin-1 Supplement blocks\n 'pseudo-latin1': {\n fixup: checkChars(/[^\\x00-\\xff]/),\n encodeByte: function (value) {\n if (value === null)\n return '';\n return String.fromCharCode(value);\n },\n encodeUTF8: function (codepoint) {\n return encodeUTF8(codepoint);\n },\n },\n\n // `x-user-defined` encoding mode: assume the input was decoded with the WHATWG `x-user-defined` encoding\n 'x-user-defined': {\n fixup: checkChars(/[^\\x00-\\x7f\\uf780-\\uf7ff]/),\n encodeByte: function (value) {\n if (value === null)\n return '';\n if (value >= 0x80)\n return String.fromCharCode(value | 0xf700);\n return String.fromCharCode(value);\n },\n encodeUTF8: function (codepoint) {\n return encodeUTF8(codepoint, 0xf700);\n }\n },\n\n // `none` encoding mode: disregard intrepretation of string literals, leave identifiers as-is\n 'none': {\n discardStrings: true,\n fixup: function (s) {\n return s;\n },\n encodeByte: function (value) {\n return '';\n },\n encodeUTF8: function (codepoint) {\n return '';\n }\n }\n };\n\n // The available tokens expressed as enum flags so they can be checked with\n // bitwise operations.\n\n var EOF = 1, StringLiteral = 2, Keyword = 4, Identifier = 8\n , NumericLiteral = 16, Punctuator = 32, BooleanLiteral = 64\n , NilLiteral = 128, VarargLiteral = 256;\n\n exports.tokenTypes = { EOF: EOF, StringLiteral: StringLiteral\n , Keyword: Keyword, Identifier: Identifier, NumericLiteral: NumericLiteral\n , Punctuator: Punctuator, BooleanLiteral: BooleanLiteral\n , NilLiteral: NilLiteral, VarargLiteral: VarargLiteral\n };\n\n // As this parser is a bit different from luas own, the error messages\n // will be different in some situations.\n\n var errors = exports.errors = {\n unexpected: 'unexpected %1 \\'%2\\' near \\'%3\\''\n , unexpectedEOF: 'unexpected symbol near \\'<eof>\\''\n , expected: '\\'%1\\' expected near \\'%2\\''\n , expectedToken: '%1 expected near \\'%2\\''\n , unfinishedString: 'unfinished string near \\'%1\\''\n , malformedNumber: 'malformed number near \\'%1\\''\n , decimalEscapeTooLarge: 'decimal escape too large near \\'%1\\''\n , invalidEscape: 'invalid escape sequence near \\'%1\\''\n , hexadecimalDigitExpected: 'hexadecimal digit expected near \\'%1\\''\n , braceExpected: 'missing \\'%1\\' near \\'%2\\''\n , tooLargeCodepoint: 'UTF-8 value too large near \\'%1\\''\n , unfinishedLongString: 'unfinished long string (starting at line %1) near \\'%2\\''\n , unfinishedLongComment: 'unfinished long comment (starting at line %1) near \\'%2\\''\n , ambiguousSyntax: 'ambiguous syntax (function call x new statement) near \\'%1\\''\n , noLoopToBreak: 'no loop to break near \\'%1\\''\n , labelAlreadyDefined: 'label \\'%1\\' already defined on line %2'\n , labelNotVisible: 'no visible label \\'%1\\' for <goto>'\n , gotoJumpInLocalScope: '<goto %1> jumps into the scope of local \\'%2\\''\n , cannotUseVararg: 'cannot use \\'...\\' outside a vararg function near \\'%1\\''\n , invalidCodeUnit: 'code unit U+%1 is not allowed in the current encoding mode'\n };\n\n // ### Abstract Syntax Tree\n //\n // The default AST structure is inspired by the Mozilla Parser API but can\n // easily be customized by overriding these functions.\n\n var ast = exports.ast = {\n labelStatement: function(label) {\n return {\n type: 'LabelStatement'\n , label: label\n };\n }\n\n , breakStatement: function() {\n return {\n type: 'BreakStatement'\n };\n }\n\n , gotoStatement: function(label) {\n return {\n type: 'GotoStatement'\n , label: label\n };\n }\n\n , returnStatement: function(args) {\n return {\n type: 'ReturnStatement'\n , 'arguments': args\n };\n }\n\n , ifStatement: function(clauses) {\n return {\n type: 'IfStatement'\n , clauses: clauses\n };\n }\n , ifClause: function(condition, body) {\n return {\n type: 'IfClause'\n , condition: condition\n , body: body\n };\n }\n , elseifClause: function(condition, body) {\n return {\n type: 'ElseifClause'\n , condition: condition\n , body: body\n };\n }\n , elseClause: function(body) {\n return {\n type: 'ElseClause'\n , body: body\n };\n }\n\n , whileStatement: function(condition, body) {\n return {\n type: 'WhileStatement'\n , condition: condition\n , body: body\n };\n }\n\n , doStatement: function(body) {\n return {\n type: 'DoStatement'\n , body: body\n };\n }\n\n , repeatStatement: function(condition, body) {\n return {\n type: 'RepeatStatement'\n , condition: condition\n , body: body\n };\n }\n\n , localStatement: function(variables, init) {\n return {\n type: 'LocalStatement'\n , variables: variables\n , init: init\n };\n }\n\n , assignmentStatement: function(variables, init) {\n return {\n type: 'AssignmentStatement'\n , variables: variables\n , init: init\n };\n }\n\n , callStatement: function(expression) {\n return {\n type: 'CallStatement'\n , expression: expression\n };\n }\n\n , functionStatement: function(identifier, parameters, isLocal, body) {\n return {\n type: 'FunctionDeclaration'\n , identifier: identifier\n , isLocal: isLocal\n , parameters: parameters\n , body: body\n };\n }\n\n , forNumericStatement: function(variable, start, end, step, body) {\n return {\n type: 'ForNumericStatement'\n , variable: variable\n , start: start\n , end: end\n , step: step\n , body: body\n };\n }\n\n , forGenericStatement: function(variables, iterators, body) {\n return {\n type: 'ForGenericStatement'\n , variables: variables\n , iterators: iterators\n , body: body\n };\n }\n\n , chunk: function(body) {\n return {\n type: 'Chunk'\n , body: body\n };\n }\n\n , identifier: function(name) {\n return {\n type: 'Identifier'\n , name: name\n };\n }\n\n , literal: function(type, value, raw) {\n type = (type === StringLiteral) ? 'StringLiteral'\n : (type === NumericLiteral) ? 'NumericLiteral'\n : (type === BooleanLiteral) ? 'BooleanLiteral'\n : (type === NilLiteral) ? 'NilLiteral'\n : 'VarargLiteral';\n\n return {\n type: type\n , value: value\n , raw: raw\n };\n }\n\n , tableKey: function(key, value) {\n return {\n type: 'TableKey'\n , key: key\n , value: value\n };\n }\n , tableKeyString: function(key, value) {\n return {\n type: 'TableKeyString'\n , key: key\n , value: value\n };\n }\n , tableValue: function(value) {\n return {\n type: 'TableValue'\n , value: value\n };\n }\n\n\n , tableConstructorExpression: function(fields) {\n return {\n type: 'TableConstructorExpression'\n , fields: fields\n };\n }\n , binaryExpression: function(operator, left, right) {\n var type = ('and' === operator || 'or' === operator) ?\n 'LogicalExpression' :\n 'BinaryExpression';\n\n return {\n type: type\n , operator: operator\n , left: left\n , right: right\n };\n }\n , unaryExpression: function(operator, argument) {\n return {\n type: 'UnaryExpression'\n , operator: operator\n , argument: argument\n };\n }\n , memberExpression: function(base, indexer, identifier) {\n return {\n type: 'MemberExpression'\n , indexer: indexer\n , identifier: identifier\n , base: base\n };\n }\n\n , indexExpression: function(base, index) {\n return {\n type: 'IndexExpression'\n , base: base\n , index: index\n };\n }\n\n , callExpression: function(base, args) {\n return {\n type: 'CallExpression'\n , base: base\n , 'arguments': args\n };\n }\n\n , tableCallExpression: function(base, args) {\n return {\n type: 'TableCallExpression'\n , base: base\n , 'arguments': args\n };\n }\n\n , stringCallExpression: function(base, argument) {\n return {\n type: 'StringCallExpression'\n , base: base\n , argument: argument\n };\n }\n\n , comment: function(value, raw) {\n return {\n type: 'Comment'\n , value: value\n , raw: raw\n };\n }\n };\n\n // Wrap up the node object.\n\n function finishNode(node) {\n // Pop a `Marker` off the location-array and attach its location data.\n if (trackLocations) {\n var location = locations.pop();\n location.complete();\n location.bless(node);\n }\n if (options.onCreateNode) options.onCreateNode(node);\n return node;\n }\n\n\n // Helpers\n // -------\n\n var slice = Array.prototype.slice\n , toString = Object.prototype.toString\n ;\n\n var indexOf = /* istanbul ignore next */ function (array, element) {\n for (var i = 0, length = array.length; i < length; ++i) {\n if (array[i] === element) return i;\n }\n return -1;\n };\n\n /* istanbul ignore else */\n if (Array.prototype.indexOf)\n indexOf = function (array, element) {\n return array.indexOf(element);\n };\n\n // Iterate through an array of objects and return the index of an object\n // with a matching property.\n\n function indexOfObject(array, property, element) {\n for (var i = 0, length = array.length; i < length; ++i) {\n if (array[i][property] === element) return i;\n }\n return -1;\n }\n\n // A sprintf implementation using %index (beginning at 1) to input\n // arguments in the format string.\n //\n // Example:\n //\n // // Unexpected function in token\n // sprintf('Unexpected %2 in %1.', 'token', 'function');\n\n function sprintf(format) {\n var args = slice.call(arguments, 1);\n format = format.replace(/%(\\d)/g, function (match, index) {\n return '' + args[index - 1] || /* istanbul ignore next */ '';\n });\n return format;\n }\n\n // Polyfill for `Object.assign`.\n\n var assign = /* istanbul ignore next */ function (dest) {\n var args = slice.call(arguments, 1)\n , src, prop;\n\n for (var i = 0, length = args.length; i < length; ++i) {\n src = args[i];\n for (prop in src)\n /* istanbul ignore else */\n if (Object.prototype.hasOwnProperty.call(src, prop)) {\n dest[prop] = src[prop];\n }\n }\n\n return dest;\n };\n\n /* istanbul ignore else */\n if (Object.assign)\n assign = Object.assign;\n\n // ### Error functions\n\n // XXX: Eliminate this function and change the error type to be different from SyntaxError.\n // This will unfortunately be a breaking change, because some downstream users depend\n // on the error thrown being an instance of SyntaxError. For example, the Ace editor:\n // <https://github.com/ajaxorg/ace/blob/4c7e5eb3f5d5ca9434847be51834a4e41661b852/lib/ace/mode/lua_worker.js#L55>\n\n function fixupError(e) {\n /* istanbul ignore if */\n if (!Object.create)\n return e;\n return Object.create(e, {\n 'line': { 'writable': true, value: e.line },\n 'index': { 'writable': true, value: e.index },\n 'column': { 'writable': true, value: e.column }\n });\n }\n\n // #### Raise an exception.\n //\n // Raise an exception by passing a token, a string format and its paramters.\n //\n // The passed tokens location will automatically be added to the error\n // message if it exists, if not it will default to the lexers current\n // position.\n //\n // Example:\n //\n // // [1:0] expected [ near (\n // raise(token, \"expected %1 near %2\", '[', token.value);\n\n function raise(token) {\n var message = sprintf.apply(null, slice.call(arguments, 1))\n , error, col;\n\n if (token === null || typeof token.line === 'undefined') {\n col = index - lineStart + 1;\n error = fixupError(new SyntaxError(sprintf('[%1:%2] %3', line, col, message)));\n error.index = index;\n error.line = line;\n error.column = col;\n } else {\n col = token.range[0] - token.lineStart;\n error = fixupError(new SyntaxError(sprintf('[%1:%2] %3', token.line, col, message)));\n error.line = token.line;\n error.index = token.range[0];\n error.column = col;\n }\n throw error;\n }\n\n function tokenValue(token) {\n var raw = input.slice(token.range[0], token.range[1]);\n if (raw)\n return raw;\n return token.value;\n }\n\n // #### Raise an unexpected token error.\n //\n // Example:\n //\n // // expected <name> near '0'\n // raiseUnexpectedToken('<name>', token);\n\n function raiseUnexpectedToken(type, token) {\n raise(token, errors.expectedToken, type, tokenValue(token));\n }\n\n // #### Raise a general unexpected error\n //\n // Usage should pass either a token object or a symbol string which was\n // expected. We can also specify a nearby token such as <eof>, this will\n // default to the currently active token.\n //\n // Example:\n //\n // // Unexpected symbol 'end' near '<eof>'\n // unexpected(token);\n //\n // If there's no token in the buffer it means we have reached <eof>.\n\n function unexpected(found) {\n var near = tokenValue(lookahead);\n if ('undefined' !== typeof found.type) {\n var type;\n switch (found.type) {\n case StringLiteral: type = 'string'; break;\n case Keyword: type = 'keyword'; break;\n case Identifier: type = 'identifier'; break;\n case NumericLiteral: type = 'number'; break;\n case Punctuator: type = 'symbol'; break;\n case BooleanLiteral: type = 'boolean'; break;\n case NilLiteral:\n return raise(found, errors.unexpected, 'symbol', 'nil', near);\n case EOF:\n return raise(found, errors.unexpectedEOF);\n }\n return raise(found, errors.unexpected, type, tokenValue(found), near);\n }\n return raise(found, errors.unexpected, 'symbol', found, near);\n }\n\n // Lexer\n // -----\n //\n // The lexer, or the tokenizer reads the input string character by character\n // and derives a token left-right. To be as efficient as possible the lexer\n // prioritizes the common cases such as identifiers. It also works with\n // character codes instead of characters as string comparisons was the\n // biggest bottleneck of the parser.\n //\n // If `options.comments` is enabled, all comments encountered will be stored\n // in an array which later will be appended to the chunk object. If disabled,\n // they will simply be disregarded.\n //\n // When the lexer has derived a valid token, it will be returned as an object\n // containing its value and as well as its position in the input string (this\n // is always enabled to provide proper debug messages).\n //\n // `lex()` starts lexing and returns the following token in the stream.\n\n var index\n , token\n , previousToken\n , lookahead\n , comments\n , tokenStart\n , line\n , lineStart;\n\n exports.lex = lex;\n\n function lex() {\n skipWhiteSpace();\n\n // Skip comments beginning with --\n while (45 === input.charCodeAt(index) &&\n 45 === input.charCodeAt(index + 1)) {\n scanComment();\n skipWhiteSpace();\n }\n if (index >= length) return {\n type : EOF\n , value: '<eof>'\n , line: line\n , lineStart: lineStart\n , range: [index, index]\n };\n\n var charCode = input.charCodeAt(index)\n , next = input.charCodeAt(index + 1);\n\n // Memorize the range index where the token begins.\n tokenStart = index;\n if (isIdentifierStart(charCode)) return scanIdentifierOrKeyword();\n\n switch (charCode) {\n case 39: case 34: // '\"\n return scanStringLiteral();\n\n case 48: case 49: case 50: case 51: case 52: case 53:\n case 54: case 55: case 56: case 57: // 0-9\n return scanNumericLiteral();\n\n case 46: // .\n // If the dot is followed by a digit it's a float.\n if (isDecDigit(next)) return scanNumericLiteral();\n if (46 === next) {\n if (46 === input.charCodeAt(index + 2)) return scanVarargLiteral();\n return scanPunctuator('..');\n }\n return scanPunctuator('.');\n\n case 61: // =\n if (61 === next) return scanPunctuator('==');\n return scanPunctuator('=');\n\n case 62: // >\n if (features.bitwiseOperators)\n if (62 === next) return scanPunctuator('>>');\n if (61 === next) return scanPunctuator('>=');\n return scanPunctuator('>');\n\n case 60: // <\n if (features.bitwiseOperators)\n if (60 === next) return scanPunctuator('<<');\n if (61 === next) return scanPunctuator('<=');\n return scanPunctuator('<');\n\n case 126: // ~\n if (61 === next) return scanPunctuator('~=');\n if (!features.bitwiseOperators)\n break;\n return scanPunctuator('~');\n\n case 58: // :\n if (features.labels)\n if (58 === next) return scanPunctuator('::');\n return scanPunctuator(':');\n\n case 91: // [\n // Check for a multiline string, they begin with [= or [[\n if (91 === next || 61 === next) return scanLongStringLiteral();\n return scanPunctuator('[');\n\n case 47: // /\n // Check for integer division op (//)\n if (features.integerDivision)\n if (47 === next) return scanPunctuator('//');\n return scanPunctuator('/');\n\n case 38: case 124: // & |\n if (!features.bitwiseOperators)\n break;\n\n /* fall through */\n case 42: case 94: case 37: case 44: case 123: case 125:\n case 93: case 40: case 41: case 59: case 35: case 45:\n case 43: // * ^ % , { } ] ( ) ; # - +\n return scanPunctuator(input.charAt(index));\n }\n\n return unexpected(input.charAt(index));\n }\n\n // Whitespace has no semantic meaning in lua so simply skip ahead while\n // tracking the encounted newlines. Any kind of eol sequence is counted as a\n // single line.\n\n function consumeEOL() {\n var charCode = input.charCodeAt(index)\n , peekCharCode = input.charCodeAt(index + 1);\n\n if (isLineTerminator(charCode)) {\n // Count \\n\\r and \\r\\n as one newline.\n if (10 === charCode && 13 === peekCharCode) ++index;\n if (13 === charCode && 10 === peekCharCode) ++index;\n ++line;\n lineStart = ++index;\n\n return true;\n }\n return false;\n }\n\n function skipWhiteSpace() {\n while (index < length) {\n var charCode = input.charCodeAt(index);\n if (isWhiteSpace(charCode)) {\n ++index;\n } else if (!consumeEOL()) {\n break;\n }\n }\n }\n\n // Identifiers, keywords, booleans and nil all look the same syntax wise. We\n // simply go through them one by one and defaulting to an identifier if no\n // previous case matched.\n\n function scanIdentifierOrKeyword() {\n var value, type;\n\n // Slicing the input string is prefered before string concatenation in a\n // loop for performance reasons.\n while (isIdentifierPart(input.charCodeAt(++index)));\n value = encodingMode.fixup(input.slice(tokenStart, index));\n\n // Decide on the token type and possibly cast the value.\n if (isKeyword(value)) {\n type = Keyword;\n } else if ('true' === value || 'false' === value) {\n type = BooleanLiteral;\n value = ('true' === value);\n } else if ('nil' === value) {\n type = NilLiteral;\n value = null;\n } else {\n type = Identifier;\n }\n\n return {\n type: type\n , value: value\n , line: line\n , lineStart: lineStart\n , range: [tokenStart, index]\n };\n }\n\n // Once a punctuator reaches this function it should already have been\n // validated so we simply return it as a token.\n\n function scanPunctuator(value) {\n index += value.length;\n return {\n type: Punctuator\n , value: value\n , line: line\n , lineStart: lineStart\n , range: [tokenStart, index]\n };\n }\n\n // A vararg literal consists of three dots.\n\n function scanVarargLiteral() {\n index += 3;\n return {\n type: VarargLiteral\n , value: '...'\n , line: line\n , lineStart: lineStart\n , range: [tokenStart, index]\n };\n }\n\n // Find the string literal by matching the delimiter marks used.\n\n function scanStringLiteral() {\n var delimiter = input.charCodeAt(index++)\n , beginLine = line\n , beginLineStart = lineStart\n , stringStart = index\n , string = encodingMode.discardStrings ? null : ''\n , charCode;\n\n for (;;) {\n charCode = input.charCodeAt(index++);\n if (delimiter === charCode) break;\n // EOF or `\\n` terminates a string literal. If we haven't found the\n // ending delimiter by now, raise an exception.\n if (index > length || isLineTerminator(charCode)) {\n string += input.slice(stringStart, index - 1);\n raise(null, errors.unfinishedString, input.slice(tokenStart, index - 1));\n }\n if (92 === charCode) { // backslash\n if (!encodingMode.discardStrings) {\n var beforeEscape = input.slice(stringStart, index - 1);\n string += encodingMode.fixup(beforeEscape);\n }\n var escapeValue = readEscapeSequence();\n if (!encodingMode.discardStrings)\n string += escapeValue;\n stringStart = index;\n }\n }\n if (!encodingMode.discardStrings) {\n string += encodingMode.encodeByte(null);\n string += encodingMode.fixup(input.slice(stringStart, index - 1));\n }\n\n return {\n type: StringLiteral\n , value: string\n , line: beginLine\n , lineStart: beginLineStart\n , lastLine: line\n , lastLineStart: lineStart\n , range: [tokenStart, index]\n };\n }\n\n // Expect a multiline string literal and return it as a regular string\n // literal, if it doesn't validate into a valid multiline string, throw an\n // exception.\n\n function scanLongStringLiteral() {\n var beginLine = line\n , beginLineStart = lineStart\n , string = readLongString(false);\n // Fail if it's not a multiline literal.\n if (false === string) raise(token, errors.expected, '[', tokenValue(token));\n\n return {\n type: StringLiteral\n , value: encodingMode.discardStrings ? null : encodingMode.fixup(string)\n , line: beginLine\n , lineStart: beginLineStart\n , lastLine: line\n , lastLineStart: lineStart\n , range: [tokenStart, index]\n };\n }\n\n // Numeric literals will be returned as floating-point numbers instead of\n // strings. The raw value should be retrieved from slicing the input string\n // later on in the process.\n //\n // If a hexadecimal number is encountered, it will be converted.\n\n function scanNumericLiteral() {\n var character = input.charAt(index)\n , next = input.charAt(index + 1);\n\n var value = ('0' === character && 'xX'.indexOf(next || null) >= 0) ?\n readHexLiteral() : readDecLiteral();\n\n return {\n type: NumericLiteral\n , value: value\n , line: line\n , lineStart: lineStart\n , range: [tokenStart, index]\n };\n }\n\n // Lua hexadecimals have an optional fraction part and an optional binary\n // exoponent part. These are not included in JavaScript so we will compute\n // all three parts separately and then sum them up at the end of the function\n // with the following algorithm.\n //\n // Digit := toDec(digit)\n // Fraction := toDec(fraction) / 16 ^ fractionCount\n // BinaryExp := 2 ^ binaryExp\n // Number := ( Digit + Fraction ) * BinaryExp\n\n function readHexLiteral() {\n var fraction = 0 // defaults to 0 as it gets summed\n , binaryExponent = 1 // defaults to 1 as it gets multiplied\n , binarySign = 1 // positive\n , digit, fractionStart, exponentStart, digitStart;\n\n digitStart = index += 2; // Skip 0x part\n\n // A minimum of one hex digit is required.\n if (!isHexDigit(input.charCodeAt(index)))\n raise(null, errors.malformedNumber, input.slice(tokenStart, index));\n\n while (isHexDigit(input.charCodeAt(index))) ++index;\n // Convert the hexadecimal digit to base 10.\n digit = parseInt(input.slice(digitStart, index), 16);\n\n // Fraction part i optional.\n if ('.' === input.charAt(index)) {\n fractionStart = ++index;\n\n while (isHexDigit(input.charCodeAt(index))) ++index;\n fraction = input.slice(fractionStart, index);\n\n // Empty fraction parts should default to 0, others should be converted\n // 0.x form so we can use summation at the end.\n fraction = (fractionStart === index) ? 0\n : parseInt(fraction, 16) / Math.pow(16, index - fractionStart);\n }\n\n // Binary exponents are optional\n if ('pP'.indexOf(input.charAt(index) || null) >= 0) {\n ++index;\n\n // Sign part is optional and defaults to 1 (positive).\n if ('+-'.indexOf(input.charAt(index) || null) >= 0)\n binarySign = ('+' === input.charAt(index++)) ? 1 : -1;\n\n exponentStart = index;\n\n // The binary exponent sign requires a decimal digit.\n if (!isDecDigit(input.charCodeAt(index)))\n raise(null, errors.malformedNumber, input.slice(tokenStart, index));\n\n while (isDecDigit(input.charCodeAt(index))) ++index;\n binaryExponent = input.slice(exponentStart, index);\n\n // Calculate the binary exponent of the number.\n binaryExponent = Math.pow(2, binaryExponent * binarySign);\n }\n\n return (digit + fraction) * binaryExponent;\n }\n\n // Decimal numbers are exactly the same in Lua and in JavaScript, because of\n // this we check where the token ends and then parse it with native\n // functions.\n\n function readDecLiteral() {\n while (isDecDigit(input.charCodeAt(index))) ++index;\n // Fraction part is optional\n if ('.' === input.charAt(index)) {\n ++index;\n // Fraction part defaults to 0\n while (isDecDigit(input.charCodeAt(index))) ++index;\n }\n // Exponent part is optional.\n if ('eE'.indexOf(input.charAt(index) || null) >= 0) {\n ++index;\n // Sign part is optional.\n if ('+-'.indexOf(input.charAt(index) || null) >= 0) ++index;\n // An exponent is required to contain at least one decimal digit.\n if (!isDecDigit(input.charCodeAt(index)))\n raise(null, errors.malformedNumber, input.slice(tokenStart, index));\n\n while (isDecDigit(input.charCodeAt(index))) ++index;\n }\n\n return parseFloat(input.slice(tokenStart, index));\n }\n\n function readUnicodeEscapeSequence() {\n var sequenceStart = index++;\n\n if (input.charAt(index++) !== '{')\n raise(null, errors.braceExpected, '{', '\\\\' + input.slice(sequenceStart, index));\n if (!isHexDigit(input.charCodeAt(index)))\n raise(null, errors.hexadecimalDigitExpected, '\\\\' + input.slice(sequenceStart, index));\n\n while (input.charCodeAt(index) === 0x30) ++index;\n var escStart = index;\n\n while (isHexDigit(input.charCodeAt(index))) {\n ++index;\n if (index - escStart > 6)\n raise(null, errors.tooLargeCodepoint, '\\\\' + input.slice(sequenceStart, index));\n }\n\n var b = input.charAt(index++);\n if (b !== '}') {\n if ((b === '\"') || (b === \"'\"))\n raise(null, errors.braceExpected, '}', '\\\\' + input.slice(sequenceStart, index--));\n else\n raise(null, errors.hexadecimalDigitExpected, '\\\\' + input.slice(sequenceStart, index));\n }\n\n var codepoint = parseInt(input.slice(escStart, index - 1) || '0', 16);\n var frag = '\\\\' + input.slice(sequenceStart, index);\n\n if (codepoint > 0x10ffff) {\n raise(null, errors.tooLargeCodepoint, frag);\n }\n\n return encodingMode.encodeUTF8(codepoint, frag);\n }\n\n // Translate escape sequences to the actual characters.\n function readEscapeSequence() {\n var sequenceStart = index;\n switch (input.charAt(index)) {\n // Lua allow the following escape sequences.\n case 'a': ++index; return '\\x07';\n case 'n': ++index; return '\\n';\n case 'r': ++index; return '\\r';\n case 't': ++index; return '\\t';\n case 'v': ++index; return '\\x0b';\n case 'b': ++index; return '\\b';\n case 'f': ++index; return '\\f';\n\n // Backslash at the end of the line. We treat all line endings as equivalent,\n // and as representing the [LF] character (code 10). Lua 5.1 through 5.3\n // have been verified to behave the same way.\n case '\\r':\n case '\\n':\n consumeEOL();\n return '\\n';\n\n case '0': case '1': case '2': case '3': case '4':\n case '5': case '6': case '7': case '8': case '9':\n // \\ddd, where ddd is a sequence of up to three decimal digits.\n while (isDecDigit(input.charCodeAt(index)) && index - sequenceStart < 3) ++index;\n\n var frag = input.slice(sequenceStart, index);\n var ddd = parseInt(frag, 10);\n if (ddd > 255) {\n raise(null, errors.decimalEscapeTooLarge, '\\\\' + ddd);\n }\n return encodingMode.encodeByte(ddd, '\\\\' + frag);\n\n case 'z':\n if (features.skipWhitespaceEscape) {\n ++index;\n skipWhiteSpace();\n return '';\n }\n break;\n\n case 'x':\n if (features.hexEscapes) {\n // \\xXX, where XX is a sequence of exactly two hexadecimal digits\n if (isHexDigit(input.charCodeAt(index + 1)) &&\n isHexDigit(input.charCodeAt(index + 2))) {\n index += 3;\n return encodingMode.encodeByte(parseInt(input.slice(sequenceStart + 1, index), 16), '\\\\' + input.slice(sequenceStart, index));\n }\n raise(null, errors.hexadecimalDigitExpected, '\\\\' + input.slice(sequenceStart, index + 2));\n }\n break;\n\n case 'u':\n if (features.unicodeEscapes)\n return readUnicodeEscapeSequence();\n break;\n\n case '\\\\': case '\"': case \"'\":\n return input.charAt(index++);\n }\n\n if (features.strictEscapes)\n raise(null, errors.invalidEscape, '\\\\' + input.slice(sequenceStart, index + 1));\n return input.charAt(index++);\n }\n\n // Comments begin with -- after which it will be decided if they are\n // multiline comments or not.\n //\n // The multiline functionality works the exact same way as with string\n // literals so we reuse the functionality.\n\n function scanComment() {\n tokenStart = index;\n index += 2; // --\n\n var character = input.charAt(index)\n , content = ''\n , isLong = false\n , commentStart = index\n , lineStartComment = lineStart\n , lineComment = line;\n\n if ('[' === character) {\n content = readLongString(true);\n // This wasn't a multiline comment after all.\n if (false === content) content = character;\n else isLong = true;\n }\n // Scan until next line as long as it's not a multiline comment.\n if (!isLong) {\n while (index < length) {\n if (isLineTerminator(input.charCodeAt(index))) break;\n ++index;\n }\n if (options.comments) content = input.slice(commentStart, index);\n }\n\n if (options.comments) {\n var node = ast.comment(content, input.slice(tokenStart, index));\n\n // `Marker`s depend on tokens available in the parser and as comments are\n // intercepted in the lexer all location data is set manually.\n if (options.locations) {\n node.loc = {\n start: { line: lineComment, column: tokenStart - lineStartComment }\n , end: { line: line, column: index - lineStart }\n };\n }\n if (options.ranges) {\n node.range = [tokenStart, index];\n }\n if (options.onCreateNode) options.onCreateNode(node);\n comments.push(node);\n }\n }\n\n // Read a multiline string by calculating the depth of `=` characters and\n // then appending until an equal depth is found.\n\n function readLongString(isComment) {\n var level = 0\n , content = ''\n , terminator = false\n , character, stringStart, firstLine = line;\n\n ++index; // [\n\n // Calculate the depth of the comment.\n while ('=' === input.charAt(index + level)) ++level;\n // Exit, this is not a long string afterall.\n if ('[' !== input.charAt(index + level)) return false;\n\n index += level + 1;\n\n // If the first character is a newline, ignore it and begin on next line.\n if (isLineTerminator(input.charCodeAt(index))) consumeEOL();\n\n stringStart = index;\n while (index < length) {\n // To keep track of line numbers run the `consumeEOL()` which increments\n // its counter.\n while (isLineTerminator(input.charCodeAt(index))) consumeEOL();\n\n character = input.charAt(index++);\n\n // Once the delimiter is found, iterate through the depth count and see\n // if it matches.\n if (']' === character) {\n terminator = true;\n for (var i = 0; i < level; ++i) {\n if ('=' !== input.charAt(index + i)) terminator = false;\n }\n if (']' !== input.charAt(index + level)) terminator = false;\n }\n\n // We reached the end of the multiline string. Get out now.\n if (terminator) {\n content += input.slice(stringStart, index - 1);\n index += level + 1;\n return content;\n }\n }\n\n raise(null, isComment ?\n errors.unfinishedLongComment :\n errors.unfinishedLongString,\n firstLine, '<eof>');\n }\n\n // ## Lex functions and helpers.\n\n // Read the next token.\n //\n // This is actually done by setting the current token to the lookahead and\n // reading in the new lookahead token.\n\n function next() {\n previousToken = token;\n token = lookahead;\n lookahead = lex();\n }\n\n // Consume a token if its value matches. Once consumed or not, return the\n // success of the operation.\n\n function consume(value) {\n if (value === token.value) {\n next();\n return true;\n }\n return false;\n }\n\n // Expect the next token value to match. If not, throw an exception.\n\n function expect(value) {\n if (value === token.value) next();\n else raise(token, errors.expected, value, tokenValue(token));\n }\n\n // ### Validation functions\n\n function isWhiteSpace(charCode) {\n return 9 === charCode || 32 === charCode || 0xB === charCode || 0xC === charCode;\n }\n\n function isLineTerminator(charCode) {\n return 10 === charCode || 13 === charCode;\n }\n\n function isDecDigit(charCode) {\n return charCode >= 48 && charCode <= 57;\n }\n\n function isHexDigit(charCode) {\n return (charCode >= 48 && charCode <= 57) || (charCode >= 97 && charCode <= 102) || (charCode >= 65 && charCode <= 70);\n }\n\n // From [Lua 5.2](http://www.lua.org/manual/5.2/manual.html#8.1) onwards\n // identifiers cannot use 'locale-dependent' letters (i.e. dependent on the C locale).\n // On the other hand, LuaJIT allows arbitrary octets ≥ 128 in identifiers.\n\n function isIdentifierStart(charCode) {\n if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || 95 === charCode)\n return true;\n if (features.extendedIdentifiers && charCode >= 128)\n return true;\n return false;\n }\n\n function isIdentifierPart(charCode) {\n if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || 95 === charCode || (charCode >= 48 && charCode <= 57))\n return true;\n if (features.extendedIdentifiers && charCode >= 128)\n return true;\n return false;\n }\n\n // [3.1 Lexical Conventions](http://www.lua.org/manual/5.2/manual.html#3.1)\n //\n // `true`, `false` and `nil` will not be considered keywords, but literals.\n\n function isKeyword(id) {\n switch (id.length) {\n case 2:\n return 'do' === id || 'if' === id || 'in' === id || 'or' === id;\n case 3:\n return 'and' === id || 'end' === id || 'for' === id || 'not' === id;\n case 4:\n if ('else' === id || 'then' === id)\n return true;\n if (features.labels && !features.contextualGoto)\n return ('goto' === id);\n return false;\n case 5:\n return 'break' === id || 'local' === id || 'until' === id || 'while' === id;\n case 6:\n return 'elseif' === id || 'repeat' === id || 'return' === id;\n case 8:\n return 'function' === id;\n }\n return false;\n }\n\n function isUnary(token) {\n if (Punctuator === token.type) return '#-~'.indexOf(token.value) >= 0;\n if (Keyword === token.type) return 'not' === token.value;\n return false;\n }\n\n // Check if the token syntactically closes a block.\n\n function isBlockFollow(token) {\n if (EOF === token.type) return true;\n if (Keyword !== token.type) return false;\n switch (token.value) {\n case 'else': case 'elseif':\n case 'end': case 'until':\n return true;\n default:\n return false;\n }\n }\n\n // Scope\n // -----\n\n // Store each block scope as a an array of identifier names. Each scope is\n // stored in an FILO-array.\n var scopes\n // The current scope index\n , scopeDepth\n // A list of all global identifier nodes.\n , globals;\n\n // Create a new scope inheriting all declarations from the previous scope.\n function createScope() {\n var scope = Array.apply(null, scopes[scopeDepth++]);\n scopes.push(scope);\n if (options.onCreateScope) options.onCreateScope();\n }\n\n // Exit and remove the current scope.\n function destroyScope() {\n var scope = scopes.pop();\n --scopeDepth;\n if (options.onDestroyScope) options.onDestroyScope();\n }\n\n // Add identifier name to the current scope if it doesnt already exist.\n function scopeIdentifierName(name) {\n if (options.onLocalDeclaration) options.onLocalDeclaration(name);\n if (-1 !== indexOf(scopes[scopeDepth], name)) return;\n scopes[scopeDepth].push(name);\n }\n\n // Add identifier to the current scope\n function scopeIdentifier(node) {\n scopeIdentifierName(node.name);\n attachScope(node, true);\n }\n\n // Attach scope information to node. If the node is global, store it in the\n // globals array so we can return the information to the user.\n function attachScope(node, isLocal) {\n if (!isLocal && -1 === indexOfObject(globals, 'name', node.name))\n globals.push(node);\n\n node.isLocal = isLocal;\n }\n\n // Is the identifier name available in this scope.\n function scopeHasName(name) {\n return (-1 !== indexOf(scopes[scopeDepth], name));\n }\n\n // Location tracking\n // -----------------\n //\n // Locations are stored in FILO-array as a `Marker` object consisting of both\n // `loc` and `range` data. Once a `Marker` is popped off the list an end\n // location is added and the data is attached to a syntax node.\n\n var locations = []\n , trackLocations;\n\n function createLocationMarker() {\n return new Marker(token);\n }\n\n function Marker(token) {\n if (options.locations) {\n this.loc = {\n start: {\n line: token.line\n , column: token.range[0] - token.lineStart\n }\n , end: {\n line: 0\n , column: 0\n }\n };\n }\n if (options.ranges) this.range = [token.range[0], 0];\n }\n\n // Complete the location data stored in the `Marker` by adding the location\n // of the *previous token* as an end location.\n Marker.prototype.complete = function() {\n if (options.locations) {\n this.loc.end.line = previousToken.lastLine || previousToken.line;\n this.loc.end.column = previousToken.range[1] - (previousToken.lastLineStart || previousToken.lineStart);\n }\n if (options.ranges) {\n this.range[1] = previousToken.range[1];\n }\n };\n\n Marker.prototype.bless = function (node) {\n if (this.loc) {\n var loc = this.loc;\n node.loc = {\n start: {\n line: loc.start.line,\n column: loc.start.column\n },\n end: {\n line: loc.end.line,\n column: loc.end.column\n }\n };\n }\n if (this.range) {\n node.range = [\n this.range[0],\n this.range[1]\n ];\n }\n };\n\n // Create a new `Marker` and add it to the FILO-array.\n function markLocation() {\n if (trackLocations) locations.push(createLocationMarker());\n }\n\n // Push an arbitrary `Marker` object onto the FILO-array.\n function pushLocation(marker) {\n if (trackLocations) locations.push(marker);\n }\n\n // Control flow tracking\n // ---------------------\n // A context object that validates loop breaks and `goto`-based control flow.\n\n function FullFlowContext() {\n this.scopes = [];\n this.pendingGotos = [];\n }\n\n FullFlowContext.prototype.isInLoop = function () {\n var i = this.scopes.length;\n while (i --\x3e 0) {\n if (this.scopes[i].isLoop)\n return true;\n }\n return false;\n };\n\n FullFlowContext.prototype.pushScope = function (isLoop) {\n var scope = {\n labels: {},\n locals: [],\n deferredGotos: [],\n isLoop: !!isLoop\n };\n this.scopes.push(scope);\n };\n\n FullFlowContext.prototype.popScope = function () {\n for (var i = 0; i < this.pendingGotos.length; ++i) {\n var theGoto = this.pendingGotos[i];\n if (theGoto.maxDepth >= this.scopes.length)\n if (--theGoto.maxDepth <= 0)\n raise(theGoto.token, errors.labelNotVisible, theGoto.target);\n }\n\n this.scopes.pop();\n };\n\n FullFlowContext.prototype.addGoto = function (target, token) {\n var localCounts = [];\n\n for (var i = 0; i < this.scopes.length; ++i) {\n var scope = this.scopes[i];\n localCounts.push(scope.locals.length);\n if (Object.prototype.hasOwnProperty.call(scope.labels, target))\n return;\n }\n\n this.pendingGotos.push({\n maxDepth: this.scopes.length,\n target: target,\n token: token,\n localCounts: localCounts\n });\n };\n\n FullFlowContext.prototype.addLabel = function (name, token) {\n var scope = this.currentScope();\n\n if (Object.prototype.hasOwnProperty.call(scope.labels, name)) {\n raise(token, errors.labelAlreadyDefined, name, scope.labels[name].line);\n } else {\n var newGotos = [];\n\n for (var i = 0; i < this.pendingGotos.length; ++i) {\n var theGoto = this.pendingGotos[i];\n\n if (theGoto.maxDepth >= this.scopes.length && theGoto.target === name) {\n if (theGoto.localCounts[this.scopes.length - 1] < scope.locals.length) {\n scope.deferredGotos.push(theGoto);\n }\n continue;\n }\n\n newGotos.push(theGoto);\n }\n\n this.pendingGotos = newGotos;\n }\n\n scope.labels[name] = {\n localCount: scope.locals.length,\n line: token.line\n