worker-lua.js 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608
  1. "no use strict";
  2. !(function(window) {
  3. if (typeof window.window != "undefined" && window.document)
  4. return;
  5. if (window.require && window.define)
  6. return;
  7. if (!window.console) {
  8. window.console = function() {
  9. var msgs = Array.prototype.slice.call(arguments, 0);
  10. postMessage({type: "log", data: msgs});
  11. };
  12. window.console.error =
  13. window.console.warn =
  14. window.console.log =
  15. window.console.trace = window.console;
  16. }
  17. window.window = window;
  18. window.ace = window;
  19. window.onerror = function(message, file, line, col, err) {
  20. postMessage({type: "error", data: {
  21. message: message,
  22. data: err.data,
  23. file: file,
  24. line: line,
  25. col: col,
  26. stack: err.stack
  27. }});
  28. };
  29. window.normalizeModule = function(parentId, moduleName) {
  30. // normalize plugin requires
  31. if (moduleName.indexOf("!") !== -1) {
  32. var chunks = moduleName.split("!");
  33. return window.normalizeModule(parentId, chunks[0]) + "!" + window.normalizeModule(parentId, chunks[1]);
  34. }
  35. // normalize relative requires
  36. if (moduleName.charAt(0) == ".") {
  37. var base = parentId.split("/").slice(0, -1).join("/");
  38. moduleName = (base ? base + "/" : "") + moduleName;
  39. while (moduleName.indexOf(".") !== -1 && previous != moduleName) {
  40. var previous = moduleName;
  41. moduleName = moduleName.replace(/^\.\//, "").replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, "");
  42. }
  43. }
  44. return moduleName;
  45. };
  46. window.require = function require(parentId, id) {
  47. if (!id) {
  48. id = parentId;
  49. parentId = null;
  50. }
  51. if (!id.charAt)
  52. throw new Error("worker.js require() accepts only (parentId, id) as arguments");
  53. id = window.normalizeModule(parentId, id);
  54. var module = window.require.modules[id];
  55. if (module) {
  56. if (!module.initialized) {
  57. module.initialized = true;
  58. module.exports = module.factory().exports;
  59. }
  60. return module.exports;
  61. }
  62. if (!window.require.tlns)
  63. return console.log("unable to load " + id);
  64. var path = resolveModuleId(id, window.require.tlns);
  65. if (path.slice(-3) != ".js") path += ".js";
  66. window.require.id = id;
  67. window.require.modules[id] = {}; // prevent infinite loop on broken modules
  68. importScripts(path);
  69. return window.require(parentId, id);
  70. };
  71. function resolveModuleId(id, paths) {
  72. var testPath = id, tail = "";
  73. while (testPath) {
  74. var alias = paths[testPath];
  75. if (typeof alias == "string") {
  76. return alias + tail;
  77. } else if (alias) {
  78. return alias.location.replace(/\/*$/, "/") + (tail || alias.main || alias.name);
  79. } else if (alias === false) {
  80. return "";
  81. }
  82. var i = testPath.lastIndexOf("/");
  83. if (i === -1) break;
  84. tail = testPath.substr(i) + tail;
  85. testPath = testPath.slice(0, i);
  86. }
  87. return id;
  88. }
  89. window.require.modules = {};
  90. window.require.tlns = {};
  91. window.define = function(id, deps, factory) {
  92. if (arguments.length == 2) {
  93. factory = deps;
  94. if (typeof id != "string") {
  95. deps = id;
  96. id = window.require.id;
  97. }
  98. } else if (arguments.length == 1) {
  99. factory = id;
  100. deps = [];
  101. id = window.require.id;
  102. }
  103. if (typeof factory != "function") {
  104. window.require.modules[id] = {
  105. exports: factory,
  106. initialized: true
  107. };
  108. return;
  109. }
  110. if (!deps.length)
  111. // If there is no dependencies, we inject "require", "exports" and
  112. // "module" as dependencies, to provide CommonJS compatibility.
  113. deps = ["require", "exports", "module"];
  114. var req = function(childId) {
  115. return window.require(id, childId);
  116. };
  117. window.require.modules[id] = {
  118. exports: {},
  119. factory: function() {
  120. var module = this;
  121. var returnExports = factory.apply(this, deps.slice(0, factory.length).map(function(dep) {
  122. switch (dep) {
  123. // Because "require", "exports" and "module" aren't actual
  124. // dependencies, we must handle them seperately.
  125. case "require": return req;
  126. case "exports": return module.exports;
  127. case "module": return module;
  128. // But for all other dependencies, we can just go ahead and
  129. // require them.
  130. default: return req(dep);
  131. }
  132. }));
  133. if (returnExports)
  134. module.exports = returnExports;
  135. return module;
  136. }
  137. };
  138. };
  139. window.define.amd = {};
  140. require.tlns = {};
  141. window.initBaseUrls = function initBaseUrls(topLevelNamespaces) {
  142. for (var i in topLevelNamespaces)
  143. require.tlns[i] = topLevelNamespaces[i];
  144. };
  145. window.initSender = function initSender() {
  146. var EventEmitter = window.require("ace/lib/event_emitter").EventEmitter;
  147. var oop = window.require("ace/lib/oop");
  148. var Sender = function() {};
  149. (function() {
  150. oop.implement(this, EventEmitter);
  151. this.callback = function(data, callbackId) {
  152. postMessage({
  153. type: "call",
  154. id: callbackId,
  155. data: data
  156. });
  157. };
  158. this.emit = function(name, data) {
  159. postMessage({
  160. type: "event",
  161. name: name,
  162. data: data
  163. });
  164. };
  165. }).call(Sender.prototype);
  166. return new Sender();
  167. };
  168. var main = window.main = null;
  169. var sender = window.sender = null;
  170. window.onmessage = function(e) {
  171. var msg = e.data;
  172. if (msg.event && sender) {
  173. sender._signal(msg.event, msg.data);
  174. }
  175. else if (msg.command) {
  176. if (main[msg.command])
  177. main[msg.command].apply(main, msg.args);
  178. else if (window[msg.command])
  179. window[msg.command].apply(window, msg.args);
  180. else
  181. throw new Error("Unknown command:" + msg.command);
  182. }
  183. else if (msg.init) {
  184. window.initBaseUrls(msg.tlns);
  185. sender = window.sender = window.initSender();
  186. var clazz = require(msg.module)[msg.classname];
  187. main = window.main = new clazz(sender);
  188. }
  189. };
  190. })(this);
  191. define("ace/lib/oop",[], function(require, exports, module) {
  192. "use strict";
  193. exports.inherits = function(ctor, superCtor) {
  194. ctor.super_ = superCtor;
  195. ctor.prototype = Object.create(superCtor.prototype, {
  196. constructor: {
  197. value: ctor,
  198. enumerable: false,
  199. writable: true,
  200. configurable: true
  201. }
  202. });
  203. };
  204. exports.mixin = function(obj, mixin) {
  205. for (var key in mixin) {
  206. obj[key] = mixin[key];
  207. }
  208. return obj;
  209. };
  210. exports.implement = function(proto, mixin) {
  211. exports.mixin(proto, mixin);
  212. };
  213. });
  214. define("ace/range",[], function(require, exports, module) {
  215. "use strict";
  216. var comparePoints = function(p1, p2) {
  217. return p1.row - p2.row || p1.column - p2.column;
  218. };
  219. var Range = function(startRow, startColumn, endRow, endColumn) {
  220. this.start = {
  221. row: startRow,
  222. column: startColumn
  223. };
  224. this.end = {
  225. row: endRow,
  226. column: endColumn
  227. };
  228. };
  229. (function() {
  230. this.isEqual = function(range) {
  231. return this.start.row === range.start.row &&
  232. this.end.row === range.end.row &&
  233. this.start.column === range.start.column &&
  234. this.end.column === range.end.column;
  235. };
  236. this.toString = function() {
  237. return ("Range: [" + this.start.row + "/" + this.start.column +
  238. "] -> [" + this.end.row + "/" + this.end.column + "]");
  239. };
  240. this.contains = function(row, column) {
  241. return this.compare(row, column) == 0;
  242. };
  243. this.compareRange = function(range) {
  244. var cmp,
  245. end = range.end,
  246. start = range.start;
  247. cmp = this.compare(end.row, end.column);
  248. if (cmp == 1) {
  249. cmp = this.compare(start.row, start.column);
  250. if (cmp == 1) {
  251. return 2;
  252. } else if (cmp == 0) {
  253. return 1;
  254. } else {
  255. return 0;
  256. }
  257. } else if (cmp == -1) {
  258. return -2;
  259. } else {
  260. cmp = this.compare(start.row, start.column);
  261. if (cmp == -1) {
  262. return -1;
  263. } else if (cmp == 1) {
  264. return 42;
  265. } else {
  266. return 0;
  267. }
  268. }
  269. };
  270. this.comparePoint = function(p) {
  271. return this.compare(p.row, p.column);
  272. };
  273. this.containsRange = function(range) {
  274. return this.comparePoint(range.start) == 0 && this.comparePoint(range.end) == 0;
  275. };
  276. this.intersects = function(range) {
  277. var cmp = this.compareRange(range);
  278. return (cmp == -1 || cmp == 0 || cmp == 1);
  279. };
  280. this.isEnd = function(row, column) {
  281. return this.end.row == row && this.end.column == column;
  282. };
  283. this.isStart = function(row, column) {
  284. return this.start.row == row && this.start.column == column;
  285. };
  286. this.setStart = function(row, column) {
  287. if (typeof row == "object") {
  288. this.start.column = row.column;
  289. this.start.row = row.row;
  290. } else {
  291. this.start.row = row;
  292. this.start.column = column;
  293. }
  294. };
  295. this.setEnd = function(row, column) {
  296. if (typeof row == "object") {
  297. this.end.column = row.column;
  298. this.end.row = row.row;
  299. } else {
  300. this.end.row = row;
  301. this.end.column = column;
  302. }
  303. };
  304. this.inside = function(row, column) {
  305. if (this.compare(row, column) == 0) {
  306. if (this.isEnd(row, column) || this.isStart(row, column)) {
  307. return false;
  308. } else {
  309. return true;
  310. }
  311. }
  312. return false;
  313. };
  314. this.insideStart = function(row, column) {
  315. if (this.compare(row, column) == 0) {
  316. if (this.isEnd(row, column)) {
  317. return false;
  318. } else {
  319. return true;
  320. }
  321. }
  322. return false;
  323. };
  324. this.insideEnd = function(row, column) {
  325. if (this.compare(row, column) == 0) {
  326. if (this.isStart(row, column)) {
  327. return false;
  328. } else {
  329. return true;
  330. }
  331. }
  332. return false;
  333. };
  334. this.compare = function(row, column) {
  335. if (!this.isMultiLine()) {
  336. if (row === this.start.row) {
  337. return column < this.start.column ? -1 : (column > this.end.column ? 1 : 0);
  338. }
  339. }
  340. if (row < this.start.row)
  341. return -1;
  342. if (row > this.end.row)
  343. return 1;
  344. if (this.start.row === row)
  345. return column >= this.start.column ? 0 : -1;
  346. if (this.end.row === row)
  347. return column <= this.end.column ? 0 : 1;
  348. return 0;
  349. };
  350. this.compareStart = function(row, column) {
  351. if (this.start.row == row && this.start.column == column) {
  352. return -1;
  353. } else {
  354. return this.compare(row, column);
  355. }
  356. };
  357. this.compareEnd = function(row, column) {
  358. if (this.end.row == row && this.end.column == column) {
  359. return 1;
  360. } else {
  361. return this.compare(row, column);
  362. }
  363. };
  364. this.compareInside = function(row, column) {
  365. if (this.end.row == row && this.end.column == column) {
  366. return 1;
  367. } else if (this.start.row == row && this.start.column == column) {
  368. return -1;
  369. } else {
  370. return this.compare(row, column);
  371. }
  372. };
  373. this.clipRows = function(firstRow, lastRow) {
  374. if (this.end.row > lastRow)
  375. var end = {row: lastRow + 1, column: 0};
  376. else if (this.end.row < firstRow)
  377. var end = {row: firstRow, column: 0};
  378. if (this.start.row > lastRow)
  379. var start = {row: lastRow + 1, column: 0};
  380. else if (this.start.row < firstRow)
  381. var start = {row: firstRow, column: 0};
  382. return Range.fromPoints(start || this.start, end || this.end);
  383. };
  384. this.extend = function(row, column) {
  385. var cmp = this.compare(row, column);
  386. if (cmp == 0)
  387. return this;
  388. else if (cmp == -1)
  389. var start = {row: row, column: column};
  390. else
  391. var end = {row: row, column: column};
  392. return Range.fromPoints(start || this.start, end || this.end);
  393. };
  394. this.isEmpty = function() {
  395. return (this.start.row === this.end.row && this.start.column === this.end.column);
  396. };
  397. this.isMultiLine = function() {
  398. return (this.start.row !== this.end.row);
  399. };
  400. this.clone = function() {
  401. return Range.fromPoints(this.start, this.end);
  402. };
  403. this.collapseRows = function() {
  404. if (this.end.column == 0)
  405. return new Range(this.start.row, 0, Math.max(this.start.row, this.end.row-1), 0);
  406. else
  407. return new Range(this.start.row, 0, this.end.row, 0);
  408. };
  409. this.toScreenRange = function(session) {
  410. var screenPosStart = session.documentToScreenPosition(this.start);
  411. var screenPosEnd = session.documentToScreenPosition(this.end);
  412. return new Range(
  413. screenPosStart.row, screenPosStart.column,
  414. screenPosEnd.row, screenPosEnd.column
  415. );
  416. };
  417. this.moveBy = function(row, column) {
  418. this.start.row += row;
  419. this.start.column += column;
  420. this.end.row += row;
  421. this.end.column += column;
  422. };
  423. }).call(Range.prototype);
  424. Range.fromPoints = function(start, end) {
  425. return new Range(start.row, start.column, end.row, end.column);
  426. };
  427. Range.comparePoints = comparePoints;
  428. Range.comparePoints = function(p1, p2) {
  429. return p1.row - p2.row || p1.column - p2.column;
  430. };
  431. exports.Range = Range;
  432. });
  433. define("ace/apply_delta",[], function(require, exports, module) {
  434. "use strict";
  435. function throwDeltaError(delta, errorText){
  436. console.log("Invalid Delta:", delta);
  437. throw "Invalid Delta: " + errorText;
  438. }
  439. function positionInDocument(docLines, position) {
  440. return position.row >= 0 && position.row < docLines.length &&
  441. position.column >= 0 && position.column <= docLines[position.row].length;
  442. }
  443. function validateDelta(docLines, delta) {
  444. if (delta.action != "insert" && delta.action != "remove")
  445. throwDeltaError(delta, "delta.action must be 'insert' or 'remove'");
  446. if (!(delta.lines instanceof Array))
  447. throwDeltaError(delta, "delta.lines must be an Array");
  448. if (!delta.start || !delta.end)
  449. throwDeltaError(delta, "delta.start/end must be an present");
  450. var start = delta.start;
  451. if (!positionInDocument(docLines, delta.start))
  452. throwDeltaError(delta, "delta.start must be contained in document");
  453. var end = delta.end;
  454. if (delta.action == "remove" && !positionInDocument(docLines, end))
  455. throwDeltaError(delta, "delta.end must contained in document for 'remove' actions");
  456. var numRangeRows = end.row - start.row;
  457. var numRangeLastLineChars = (end.column - (numRangeRows == 0 ? start.column : 0));
  458. if (numRangeRows != delta.lines.length - 1 || delta.lines[numRangeRows].length != numRangeLastLineChars)
  459. throwDeltaError(delta, "delta.range must match delta lines");
  460. }
  461. exports.applyDelta = function(docLines, delta, doNotValidate) {
  462. var row = delta.start.row;
  463. var startColumn = delta.start.column;
  464. var line = docLines[row] || "";
  465. switch (delta.action) {
  466. case "insert":
  467. var lines = delta.lines;
  468. if (lines.length === 1) {
  469. docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn);
  470. } else {
  471. var args = [row, 1].concat(delta.lines);
  472. docLines.splice.apply(docLines, args);
  473. docLines[row] = line.substring(0, startColumn) + docLines[row];
  474. docLines[row + delta.lines.length - 1] += line.substring(startColumn);
  475. }
  476. break;
  477. case "remove":
  478. var endColumn = delta.end.column;
  479. var endRow = delta.end.row;
  480. if (row === endRow) {
  481. docLines[row] = line.substring(0, startColumn) + line.substring(endColumn);
  482. } else {
  483. docLines.splice(
  484. row, endRow - row + 1,
  485. line.substring(0, startColumn) + docLines[endRow].substring(endColumn)
  486. );
  487. }
  488. break;
  489. }
  490. };
  491. });
  492. define("ace/lib/event_emitter",[], function(require, exports, module) {
  493. "use strict";
  494. var EventEmitter = {};
  495. var stopPropagation = function() { this.propagationStopped = true; };
  496. var preventDefault = function() { this.defaultPrevented = true; };
  497. EventEmitter._emit =
  498. EventEmitter._dispatchEvent = function(eventName, e) {
  499. this._eventRegistry || (this._eventRegistry = {});
  500. this._defaultHandlers || (this._defaultHandlers = {});
  501. var listeners = this._eventRegistry[eventName] || [];
  502. var defaultHandler = this._defaultHandlers[eventName];
  503. if (!listeners.length && !defaultHandler)
  504. return;
  505. if (typeof e != "object" || !e)
  506. e = {};
  507. if (!e.type)
  508. e.type = eventName;
  509. if (!e.stopPropagation)
  510. e.stopPropagation = stopPropagation;
  511. if (!e.preventDefault)
  512. e.preventDefault = preventDefault;
  513. listeners = listeners.slice();
  514. for (var i=0; i<listeners.length; i++) {
  515. listeners[i](e, this);
  516. if (e.propagationStopped)
  517. break;
  518. }
  519. if (defaultHandler && !e.defaultPrevented)
  520. return defaultHandler(e, this);
  521. };
  522. EventEmitter._signal = function(eventName, e) {
  523. var listeners = (this._eventRegistry || {})[eventName];
  524. if (!listeners)
  525. return;
  526. listeners = listeners.slice();
  527. for (var i=0; i<listeners.length; i++)
  528. listeners[i](e, this);
  529. };
  530. EventEmitter.once = function(eventName, callback) {
  531. var _self = this;
  532. this.on(eventName, function newCallback() {
  533. _self.off(eventName, newCallback);
  534. callback.apply(null, arguments);
  535. });
  536. if (!callback) {
  537. return new Promise(function(resolve) {
  538. callback = resolve;
  539. });
  540. }
  541. };
  542. EventEmitter.setDefaultHandler = function(eventName, callback) {
  543. var handlers = this._defaultHandlers;
  544. if (!handlers)
  545. handlers = this._defaultHandlers = {_disabled_: {}};
  546. if (handlers[eventName]) {
  547. var old = handlers[eventName];
  548. var disabled = handlers._disabled_[eventName];
  549. if (!disabled)
  550. handlers._disabled_[eventName] = disabled = [];
  551. disabled.push(old);
  552. var i = disabled.indexOf(callback);
  553. if (i != -1)
  554. disabled.splice(i, 1);
  555. }
  556. handlers[eventName] = callback;
  557. };
  558. EventEmitter.removeDefaultHandler = function(eventName, callback) {
  559. var handlers = this._defaultHandlers;
  560. if (!handlers)
  561. return;
  562. var disabled = handlers._disabled_[eventName];
  563. if (handlers[eventName] == callback) {
  564. if (disabled)
  565. this.setDefaultHandler(eventName, disabled.pop());
  566. } else if (disabled) {
  567. var i = disabled.indexOf(callback);
  568. if (i != -1)
  569. disabled.splice(i, 1);
  570. }
  571. };
  572. EventEmitter.on =
  573. EventEmitter.addEventListener = function(eventName, callback, capturing) {
  574. this._eventRegistry = this._eventRegistry || {};
  575. var listeners = this._eventRegistry[eventName];
  576. if (!listeners)
  577. listeners = this._eventRegistry[eventName] = [];
  578. if (listeners.indexOf(callback) == -1)
  579. listeners[capturing ? "unshift" : "push"](callback);
  580. return callback;
  581. };
  582. EventEmitter.off =
  583. EventEmitter.removeListener =
  584. EventEmitter.removeEventListener = function(eventName, callback) {
  585. this._eventRegistry = this._eventRegistry || {};
  586. var listeners = this._eventRegistry[eventName];
  587. if (!listeners)
  588. return;
  589. var index = listeners.indexOf(callback);
  590. if (index !== -1)
  591. listeners.splice(index, 1);
  592. };
  593. EventEmitter.removeAllListeners = function(eventName) {
  594. if (!eventName) this._eventRegistry = this._defaultHandlers = undefined;
  595. if (this._eventRegistry) this._eventRegistry[eventName] = undefined;
  596. if (this._defaultHandlers) this._defaultHandlers[eventName] = undefined;
  597. };
  598. exports.EventEmitter = EventEmitter;
  599. });
  600. define("ace/anchor",[], function(require, exports, module) {
  601. "use strict";
  602. var oop = require("./lib/oop");
  603. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  604. var Anchor = exports.Anchor = function(doc, row, column) {
  605. this.$onChange = this.onChange.bind(this);
  606. this.attach(doc);
  607. if (typeof column == "undefined")
  608. this.setPosition(row.row, row.column);
  609. else
  610. this.setPosition(row, column);
  611. };
  612. (function() {
  613. oop.implement(this, EventEmitter);
  614. this.getPosition = function() {
  615. return this.$clipPositionToDocument(this.row, this.column);
  616. };
  617. this.getDocument = function() {
  618. return this.document;
  619. };
  620. this.$insertRight = false;
  621. this.onChange = function(delta) {
  622. if (delta.start.row == delta.end.row && delta.start.row != this.row)
  623. return;
  624. if (delta.start.row > this.row)
  625. return;
  626. var point = $getTransformedPoint(delta, {row: this.row, column: this.column}, this.$insertRight);
  627. this.setPosition(point.row, point.column, true);
  628. };
  629. function $pointsInOrder(point1, point2, equalPointsInOrder) {
  630. var bColIsAfter = equalPointsInOrder ? point1.column <= point2.column : point1.column < point2.column;
  631. return (point1.row < point2.row) || (point1.row == point2.row && bColIsAfter);
  632. }
  633. function $getTransformedPoint(delta, point, moveIfEqual) {
  634. var deltaIsInsert = delta.action == "insert";
  635. var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.end.row - delta.start.row);
  636. var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.end.column - delta.start.column);
  637. var deltaStart = delta.start;
  638. var deltaEnd = deltaIsInsert ? deltaStart : delta.end; // Collapse insert range.
  639. if ($pointsInOrder(point, deltaStart, moveIfEqual)) {
  640. return {
  641. row: point.row,
  642. column: point.column
  643. };
  644. }
  645. if ($pointsInOrder(deltaEnd, point, !moveIfEqual)) {
  646. return {
  647. row: point.row + deltaRowShift,
  648. column: point.column + (point.row == deltaEnd.row ? deltaColShift : 0)
  649. };
  650. }
  651. return {
  652. row: deltaStart.row,
  653. column: deltaStart.column
  654. };
  655. }
  656. this.setPosition = function(row, column, noClip) {
  657. var pos;
  658. if (noClip) {
  659. pos = {
  660. row: row,
  661. column: column
  662. };
  663. } else {
  664. pos = this.$clipPositionToDocument(row, column);
  665. }
  666. if (this.row == pos.row && this.column == pos.column)
  667. return;
  668. var old = {
  669. row: this.row,
  670. column: this.column
  671. };
  672. this.row = pos.row;
  673. this.column = pos.column;
  674. this._signal("change", {
  675. old: old,
  676. value: pos
  677. });
  678. };
  679. this.detach = function() {
  680. this.document.off("change", this.$onChange);
  681. };
  682. this.attach = function(doc) {
  683. this.document = doc || this.document;
  684. this.document.on("change", this.$onChange);
  685. };
  686. this.$clipPositionToDocument = function(row, column) {
  687. var pos = {};
  688. if (row >= this.document.getLength()) {
  689. pos.row = Math.max(0, this.document.getLength() - 1);
  690. pos.column = this.document.getLine(pos.row).length;
  691. }
  692. else if (row < 0) {
  693. pos.row = 0;
  694. pos.column = 0;
  695. }
  696. else {
  697. pos.row = row;
  698. pos.column = Math.min(this.document.getLine(pos.row).length, Math.max(0, column));
  699. }
  700. if (column < 0)
  701. pos.column = 0;
  702. return pos;
  703. };
  704. }).call(Anchor.prototype);
  705. });
  706. define("ace/document",[], function(require, exports, module) {
  707. "use strict";
  708. var oop = require("./lib/oop");
  709. var applyDelta = require("./apply_delta").applyDelta;
  710. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  711. var Range = require("./range").Range;
  712. var Anchor = require("./anchor").Anchor;
  713. var Document = function(textOrLines) {
  714. this.$lines = [""];
  715. if (textOrLines.length === 0) {
  716. this.$lines = [""];
  717. } else if (Array.isArray(textOrLines)) {
  718. this.insertMergedLines({row: 0, column: 0}, textOrLines);
  719. } else {
  720. this.insert({row: 0, column:0}, textOrLines);
  721. }
  722. };
  723. (function() {
  724. oop.implement(this, EventEmitter);
  725. this.setValue = function(text) {
  726. var len = this.getLength() - 1;
  727. this.remove(new Range(0, 0, len, this.getLine(len).length));
  728. this.insert({row: 0, column: 0}, text);
  729. };
  730. this.getValue = function() {
  731. return this.getAllLines().join(this.getNewLineCharacter());
  732. };
  733. this.createAnchor = function(row, column) {
  734. return new Anchor(this, row, column);
  735. };
  736. if ("aaa".split(/a/).length === 0) {
  737. this.$split = function(text) {
  738. return text.replace(/\r\n|\r/g, "\n").split("\n");
  739. };
  740. } else {
  741. this.$split = function(text) {
  742. return text.split(/\r\n|\r|\n/);
  743. };
  744. }
  745. this.$detectNewLine = function(text) {
  746. var match = text.match(/^.*?(\r\n|\r|\n)/m);
  747. this.$autoNewLine = match ? match[1] : "\n";
  748. this._signal("changeNewLineMode");
  749. };
  750. this.getNewLineCharacter = function() {
  751. switch (this.$newLineMode) {
  752. case "windows":
  753. return "\r\n";
  754. case "unix":
  755. return "\n";
  756. default:
  757. return this.$autoNewLine || "\n";
  758. }
  759. };
  760. this.$autoNewLine = "";
  761. this.$newLineMode = "auto";
  762. this.setNewLineMode = function(newLineMode) {
  763. if (this.$newLineMode === newLineMode)
  764. return;
  765. this.$newLineMode = newLineMode;
  766. this._signal("changeNewLineMode");
  767. };
  768. this.getNewLineMode = function() {
  769. return this.$newLineMode;
  770. };
  771. this.isNewLine = function(text) {
  772. return (text == "\r\n" || text == "\r" || text == "\n");
  773. };
  774. this.getLine = function(row) {
  775. return this.$lines[row] || "";
  776. };
  777. this.getLines = function(firstRow, lastRow) {
  778. return this.$lines.slice(firstRow, lastRow + 1);
  779. };
  780. this.getAllLines = function() {
  781. return this.getLines(0, this.getLength());
  782. };
  783. this.getLength = function() {
  784. return this.$lines.length;
  785. };
  786. this.getTextRange = function(range) {
  787. return this.getLinesForRange(range).join(this.getNewLineCharacter());
  788. };
  789. this.getLinesForRange = function(range) {
  790. var lines;
  791. if (range.start.row === range.end.row) {
  792. lines = [this.getLine(range.start.row).substring(range.start.column, range.end.column)];
  793. } else {
  794. lines = this.getLines(range.start.row, range.end.row);
  795. lines[0] = (lines[0] || "").substring(range.start.column);
  796. var l = lines.length - 1;
  797. if (range.end.row - range.start.row == l)
  798. lines[l] = lines[l].substring(0, range.end.column);
  799. }
  800. return lines;
  801. };
  802. this.insertLines = function(row, lines) {
  803. console.warn("Use of document.insertLines is deprecated. Use the insertFullLines method instead.");
  804. return this.insertFullLines(row, lines);
  805. };
  806. this.removeLines = function(firstRow, lastRow) {
  807. console.warn("Use of document.removeLines is deprecated. Use the removeFullLines method instead.");
  808. return this.removeFullLines(firstRow, lastRow);
  809. };
  810. this.insertNewLine = function(position) {
  811. console.warn("Use of document.insertNewLine is deprecated. Use insertMergedLines(position, ['', '']) instead.");
  812. return this.insertMergedLines(position, ["", ""]);
  813. };
  814. this.insert = function(position, text) {
  815. if (this.getLength() <= 1)
  816. this.$detectNewLine(text);
  817. return this.insertMergedLines(position, this.$split(text));
  818. };
  819. this.insertInLine = function(position, text) {
  820. var start = this.clippedPos(position.row, position.column);
  821. var end = this.pos(position.row, position.column + text.length);
  822. this.applyDelta({
  823. start: start,
  824. end: end,
  825. action: "insert",
  826. lines: [text]
  827. }, true);
  828. return this.clonePos(end);
  829. };
  830. this.clippedPos = function(row, column) {
  831. var length = this.getLength();
  832. if (row === undefined) {
  833. row = length;
  834. } else if (row < 0) {
  835. row = 0;
  836. } else if (row >= length) {
  837. row = length - 1;
  838. column = undefined;
  839. }
  840. var line = this.getLine(row);
  841. if (column == undefined)
  842. column = line.length;
  843. column = Math.min(Math.max(column, 0), line.length);
  844. return {row: row, column: column};
  845. };
  846. this.clonePos = function(pos) {
  847. return {row: pos.row, column: pos.column};
  848. };
  849. this.pos = function(row, column) {
  850. return {row: row, column: column};
  851. };
  852. this.$clipPosition = function(position) {
  853. var length = this.getLength();
  854. if (position.row >= length) {
  855. position.row = Math.max(0, length - 1);
  856. position.column = this.getLine(length - 1).length;
  857. } else {
  858. position.row = Math.max(0, position.row);
  859. position.column = Math.min(Math.max(position.column, 0), this.getLine(position.row).length);
  860. }
  861. return position;
  862. };
  863. this.insertFullLines = function(row, lines) {
  864. row = Math.min(Math.max(row, 0), this.getLength());
  865. var column = 0;
  866. if (row < this.getLength()) {
  867. lines = lines.concat([""]);
  868. column = 0;
  869. } else {
  870. lines = [""].concat(lines);
  871. row--;
  872. column = this.$lines[row].length;
  873. }
  874. this.insertMergedLines({row: row, column: column}, lines);
  875. };
  876. this.insertMergedLines = function(position, lines) {
  877. var start = this.clippedPos(position.row, position.column);
  878. var end = {
  879. row: start.row + lines.length - 1,
  880. column: (lines.length == 1 ? start.column : 0) + lines[lines.length - 1].length
  881. };
  882. this.applyDelta({
  883. start: start,
  884. end: end,
  885. action: "insert",
  886. lines: lines
  887. });
  888. return this.clonePos(end);
  889. };
  890. this.remove = function(range) {
  891. var start = this.clippedPos(range.start.row, range.start.column);
  892. var end = this.clippedPos(range.end.row, range.end.column);
  893. this.applyDelta({
  894. start: start,
  895. end: end,
  896. action: "remove",
  897. lines: this.getLinesForRange({start: start, end: end})
  898. });
  899. return this.clonePos(start);
  900. };
  901. this.removeInLine = function(row, startColumn, endColumn) {
  902. var start = this.clippedPos(row, startColumn);
  903. var end = this.clippedPos(row, endColumn);
  904. this.applyDelta({
  905. start: start,
  906. end: end,
  907. action: "remove",
  908. lines: this.getLinesForRange({start: start, end: end})
  909. }, true);
  910. return this.clonePos(start);
  911. };
  912. this.removeFullLines = function(firstRow, lastRow) {
  913. firstRow = Math.min(Math.max(0, firstRow), this.getLength() - 1);
  914. lastRow = Math.min(Math.max(0, lastRow ), this.getLength() - 1);
  915. var deleteFirstNewLine = lastRow == this.getLength() - 1 && firstRow > 0;
  916. var deleteLastNewLine = lastRow < this.getLength() - 1;
  917. var startRow = ( deleteFirstNewLine ? firstRow - 1 : firstRow );
  918. var startCol = ( deleteFirstNewLine ? this.getLine(startRow).length : 0 );
  919. var endRow = ( deleteLastNewLine ? lastRow + 1 : lastRow );
  920. var endCol = ( deleteLastNewLine ? 0 : this.getLine(endRow).length );
  921. var range = new Range(startRow, startCol, endRow, endCol);
  922. var deletedLines = this.$lines.slice(firstRow, lastRow + 1);
  923. this.applyDelta({
  924. start: range.start,
  925. end: range.end,
  926. action: "remove",
  927. lines: this.getLinesForRange(range)
  928. });
  929. return deletedLines;
  930. };
  931. this.removeNewLine = function(row) {
  932. if (row < this.getLength() - 1 && row >= 0) {
  933. this.applyDelta({
  934. start: this.pos(row, this.getLine(row).length),
  935. end: this.pos(row + 1, 0),
  936. action: "remove",
  937. lines: ["", ""]
  938. });
  939. }
  940. };
  941. this.replace = function(range, text) {
  942. if (!(range instanceof Range))
  943. range = Range.fromPoints(range.start, range.end);
  944. if (text.length === 0 && range.isEmpty())
  945. return range.start;
  946. if (text == this.getTextRange(range))
  947. return range.end;
  948. this.remove(range);
  949. var end;
  950. if (text) {
  951. end = this.insert(range.start, text);
  952. }
  953. else {
  954. end = range.start;
  955. }
  956. return end;
  957. };
  958. this.applyDeltas = function(deltas) {
  959. for (var i=0; i<deltas.length; i++) {
  960. this.applyDelta(deltas[i]);
  961. }
  962. };
  963. this.revertDeltas = function(deltas) {
  964. for (var i=deltas.length-1; i>=0; i--) {
  965. this.revertDelta(deltas[i]);
  966. }
  967. };
  968. this.applyDelta = function(delta, doNotValidate) {
  969. var isInsert = delta.action == "insert";
  970. if (isInsert ? delta.lines.length <= 1 && !delta.lines[0]
  971. : !Range.comparePoints(delta.start, delta.end)) {
  972. return;
  973. }
  974. if (isInsert && delta.lines.length > 20000) {
  975. this.$splitAndapplyLargeDelta(delta, 20000);
  976. }
  977. else {
  978. applyDelta(this.$lines, delta, doNotValidate);
  979. this._signal("change", delta);
  980. }
  981. };
  982. this.$safeApplyDelta = function(delta) {
  983. var docLength = this.$lines.length;
  984. if (
  985. delta.action == "remove" && delta.start.row < docLength && delta.end.row < docLength
  986. || delta.action == "insert" && delta.start.row <= docLength
  987. ) {
  988. this.applyDelta(delta);
  989. }
  990. };
  991. this.$splitAndapplyLargeDelta = function(delta, MAX) {
  992. var lines = delta.lines;
  993. var l = lines.length - MAX + 1;
  994. var row = delta.start.row;
  995. var column = delta.start.column;
  996. for (var from = 0, to = 0; from < l; from = to) {
  997. to += MAX - 1;
  998. var chunk = lines.slice(from, to);
  999. chunk.push("");
  1000. this.applyDelta({
  1001. start: this.pos(row + from, column),
  1002. end: this.pos(row + to, column = 0),
  1003. action: delta.action,
  1004. lines: chunk
  1005. }, true);
  1006. }
  1007. delta.lines = lines.slice(from);
  1008. delta.start.row = row + from;
  1009. delta.start.column = column;
  1010. this.applyDelta(delta, true);
  1011. };
  1012. this.revertDelta = function(delta) {
  1013. this.$safeApplyDelta({
  1014. start: this.clonePos(delta.start),
  1015. end: this.clonePos(delta.end),
  1016. action: (delta.action == "insert" ? "remove" : "insert"),
  1017. lines: delta.lines.slice()
  1018. });
  1019. };
  1020. this.indexToPosition = function(index, startRow) {
  1021. var lines = this.$lines || this.getAllLines();
  1022. var newlineLength = this.getNewLineCharacter().length;
  1023. for (var i = startRow || 0, l = lines.length; i < l; i++) {
  1024. index -= lines[i].length + newlineLength;
  1025. if (index < 0)
  1026. return {row: i, column: index + lines[i].length + newlineLength};
  1027. }
  1028. return {row: l-1, column: index + lines[l-1].length + newlineLength};
  1029. };
  1030. this.positionToIndex = function(pos, startRow) {
  1031. var lines = this.$lines || this.getAllLines();
  1032. var newlineLength = this.getNewLineCharacter().length;
  1033. var index = 0;
  1034. var row = Math.min(pos.row, lines.length);
  1035. for (var i = startRow || 0; i < row; ++i)
  1036. index += lines[i].length + newlineLength;
  1037. return index + pos.column;
  1038. };
  1039. }).call(Document.prototype);
  1040. exports.Document = Document;
  1041. });
  1042. define("ace/lib/lang",[], function(require, exports, module) {
  1043. "use strict";
  1044. exports.last = function(a) {
  1045. return a[a.length - 1];
  1046. };
  1047. exports.stringReverse = function(string) {
  1048. return string.split("").reverse().join("");
  1049. };
  1050. exports.stringRepeat = function (string, count) {
  1051. var result = '';
  1052. while (count > 0) {
  1053. if (count & 1)
  1054. result += string;
  1055. if (count >>= 1)
  1056. string += string;
  1057. }
  1058. return result;
  1059. };
  1060. var trimBeginRegexp = /^\s\s*/;
  1061. var trimEndRegexp = /\s\s*$/;
  1062. exports.stringTrimLeft = function (string) {
  1063. return string.replace(trimBeginRegexp, '');
  1064. };
  1065. exports.stringTrimRight = function (string) {
  1066. return string.replace(trimEndRegexp, '');
  1067. };
  1068. exports.copyObject = function(obj) {
  1069. var copy = {};
  1070. for (var key in obj) {
  1071. copy[key] = obj[key];
  1072. }
  1073. return copy;
  1074. };
  1075. exports.copyArray = function(array){
  1076. var copy = [];
  1077. for (var i=0, l=array.length; i<l; i++) {
  1078. if (array[i] && typeof array[i] == "object")
  1079. copy[i] = this.copyObject(array[i]);
  1080. else
  1081. copy[i] = array[i];
  1082. }
  1083. return copy;
  1084. };
  1085. exports.deepCopy = function deepCopy(obj) {
  1086. if (typeof obj !== "object" || !obj)
  1087. return obj;
  1088. var copy;
  1089. if (Array.isArray(obj)) {
  1090. copy = [];
  1091. for (var key = 0; key < obj.length; key++) {
  1092. copy[key] = deepCopy(obj[key]);
  1093. }
  1094. return copy;
  1095. }
  1096. if (Object.prototype.toString.call(obj) !== "[object Object]")
  1097. return obj;
  1098. copy = {};
  1099. for (var key in obj)
  1100. copy[key] = deepCopy(obj[key]);
  1101. return copy;
  1102. };
  1103. exports.arrayToMap = function(arr) {
  1104. var map = {};
  1105. for (var i=0; i<arr.length; i++) {
  1106. map[arr[i]] = 1;
  1107. }
  1108. return map;
  1109. };
  1110. exports.createMap = function(props) {
  1111. var map = Object.create(null);
  1112. for (var i in props) {
  1113. map[i] = props[i];
  1114. }
  1115. return map;
  1116. };
  1117. exports.arrayRemove = function(array, value) {
  1118. for (var i = 0; i <= array.length; i++) {
  1119. if (value === array[i]) {
  1120. array.splice(i, 1);
  1121. }
  1122. }
  1123. };
  1124. exports.escapeRegExp = function(str) {
  1125. return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  1126. };
  1127. exports.escapeHTML = function(str) {
  1128. return ("" + str).replace(/&/g, "&#38;").replace(/"/g, "&#34;").replace(/'/g, "&#39;").replace(/</g, "&#60;");
  1129. };
  1130. exports.getMatchOffsets = function(string, regExp) {
  1131. var matches = [];
  1132. string.replace(regExp, function(str) {
  1133. matches.push({
  1134. offset: arguments[arguments.length-2],
  1135. length: str.length
  1136. });
  1137. });
  1138. return matches;
  1139. };
  1140. exports.deferredCall = function(fcn) {
  1141. var timer = null;
  1142. var callback = function() {
  1143. timer = null;
  1144. fcn();
  1145. };
  1146. var deferred = function(timeout) {
  1147. deferred.cancel();
  1148. timer = setTimeout(callback, timeout || 0);
  1149. return deferred;
  1150. };
  1151. deferred.schedule = deferred;
  1152. deferred.call = function() {
  1153. this.cancel();
  1154. fcn();
  1155. return deferred;
  1156. };
  1157. deferred.cancel = function() {
  1158. clearTimeout(timer);
  1159. timer = null;
  1160. return deferred;
  1161. };
  1162. deferred.isPending = function() {
  1163. return timer;
  1164. };
  1165. return deferred;
  1166. };
  1167. exports.delayedCall = function(fcn, defaultTimeout) {
  1168. var timer = null;
  1169. var callback = function() {
  1170. timer = null;
  1171. fcn();
  1172. };
  1173. var _self = function(timeout) {
  1174. if (timer == null)
  1175. timer = setTimeout(callback, timeout || defaultTimeout);
  1176. };
  1177. _self.delay = function(timeout) {
  1178. timer && clearTimeout(timer);
  1179. timer = setTimeout(callback, timeout || defaultTimeout);
  1180. };
  1181. _self.schedule = _self;
  1182. _self.call = function() {
  1183. this.cancel();
  1184. fcn();
  1185. };
  1186. _self.cancel = function() {
  1187. timer && clearTimeout(timer);
  1188. timer = null;
  1189. };
  1190. _self.isPending = function() {
  1191. return timer;
  1192. };
  1193. return _self;
  1194. };
  1195. });
  1196. define("ace/worker/mirror",[], function(require, exports, module) {
  1197. "use strict";
  1198. var Range = require("../range").Range;
  1199. var Document = require("../document").Document;
  1200. var lang = require("../lib/lang");
  1201. var Mirror = exports.Mirror = function(sender) {
  1202. this.sender = sender;
  1203. var doc = this.doc = new Document("");
  1204. var deferredUpdate = this.deferredUpdate = lang.delayedCall(this.onUpdate.bind(this));
  1205. var _self = this;
  1206. sender.on("change", function(e) {
  1207. var data = e.data;
  1208. if (data[0].start) {
  1209. doc.applyDeltas(data);
  1210. } else {
  1211. for (var i = 0; i < data.length; i += 2) {
  1212. if (Array.isArray(data[i+1])) {
  1213. var d = {action: "insert", start: data[i], lines: data[i+1]};
  1214. } else {
  1215. var d = {action: "remove", start: data[i], end: data[i+1]};
  1216. }
  1217. doc.applyDelta(d, true);
  1218. }
  1219. }
  1220. if (_self.$timeout)
  1221. return deferredUpdate.schedule(_self.$timeout);
  1222. _self.onUpdate();
  1223. });
  1224. };
  1225. (function() {
  1226. this.$timeout = 500;
  1227. this.setTimeout = function(timeout) {
  1228. this.$timeout = timeout;
  1229. };
  1230. this.setValue = function(value) {
  1231. this.doc.setValue(value);
  1232. this.deferredUpdate.schedule(this.$timeout);
  1233. };
  1234. this.getValue = function(callbackId) {
  1235. this.sender.callback(this.doc.getValue(), callbackId);
  1236. };
  1237. this.onUpdate = function() {
  1238. };
  1239. this.isPending = function() {
  1240. return this.deferredUpdate.isPending();
  1241. };
  1242. }).call(Mirror.prototype);
  1243. });
  1244. define("ace/mode/lua/luaparse",[], function(require, exports, module) {
  1245. (function (root, name, factory) {
  1246. factory(exports)
  1247. }(this, 'luaparse', function (exports) {
  1248. 'use strict';
  1249. exports.version = "0.3.1";
  1250. var input, options, length, features, encodingMode;
  1251. var defaultOptions = exports.defaultOptions = {
  1252. wait: false
  1253. , comments: true
  1254. , scope: false
  1255. , locations: false
  1256. , ranges: false
  1257. , onCreateNode: null
  1258. , onCreateScope: null
  1259. , onDestroyScope: null
  1260. , onLocalDeclaration: null
  1261. , luaVersion: '5.1'
  1262. , encodingMode: 'none'
  1263. };
  1264. function encodeUTF8(codepoint, highMask) {
  1265. highMask = highMask || 0;
  1266. if (codepoint < 0x80) {
  1267. return String.fromCharCode(codepoint);
  1268. } else if (codepoint < 0x800) {
  1269. return String.fromCharCode(
  1270. highMask | 0xc0 | (codepoint >> 6) ,
  1271. highMask | 0x80 | ( codepoint & 0x3f)
  1272. );
  1273. } else if (codepoint < 0x10000) {
  1274. return String.fromCharCode(
  1275. highMask | 0xe0 | (codepoint >> 12) ,
  1276. highMask | 0x80 | ((codepoint >> 6) & 0x3f),
  1277. highMask | 0x80 | ( codepoint & 0x3f)
  1278. );
  1279. } else /* istanbul ignore else */ if (codepoint < 0x110000) {
  1280. return String.fromCharCode(
  1281. highMask | 0xf0 | (codepoint >> 18) ,
  1282. highMask | 0x80 | ((codepoint >> 12) & 0x3f),
  1283. highMask | 0x80 | ((codepoint >> 6) & 0x3f),
  1284. highMask | 0x80 | ( codepoint & 0x3f)
  1285. );
  1286. } else {
  1287. return null;
  1288. }
  1289. }
  1290. function toHex(num, digits) {
  1291. var result = num.toString(16);
  1292. while (result.length < digits)
  1293. result = '0' + result;
  1294. return result;
  1295. }
  1296. function checkChars(rx) {
  1297. return function (s) {
  1298. var m = rx.exec(s);
  1299. if (!m)
  1300. return s;
  1301. raise(null, errors.invalidCodeUnit, toHex(m[0].charCodeAt(0), 4).toUpperCase());
  1302. };
  1303. }
  1304. var encodingModes = {
  1305. 'pseudo-latin1': {
  1306. fixup: checkChars(/[^\x00-\xff]/),
  1307. encodeByte: function (value) {
  1308. if (value === null)
  1309. return '';
  1310. return String.fromCharCode(value);
  1311. },
  1312. encodeUTF8: function (codepoint) {
  1313. return encodeUTF8(codepoint);
  1314. },
  1315. },
  1316. 'x-user-defined': {
  1317. fixup: checkChars(/[^\x00-\x7f\uf780-\uf7ff]/),
  1318. encodeByte: function (value) {
  1319. if (value === null)
  1320. return '';
  1321. if (value >= 0x80)
  1322. return String.fromCharCode(value | 0xf700);
  1323. return String.fromCharCode(value);
  1324. },
  1325. encodeUTF8: function (codepoint) {
  1326. return encodeUTF8(codepoint, 0xf700);
  1327. }
  1328. },
  1329. 'none': {
  1330. discardStrings: true,
  1331. fixup: function (s) {
  1332. return s;
  1333. },
  1334. encodeByte: function (value) {
  1335. return '';
  1336. },
  1337. encodeUTF8: function (codepoint) {
  1338. return '';
  1339. }
  1340. }
  1341. };
  1342. var EOF = 1, StringLiteral = 2, Keyword = 4, Identifier = 8
  1343. , NumericLiteral = 16, Punctuator = 32, BooleanLiteral = 64
  1344. , NilLiteral = 128, VarargLiteral = 256;
  1345. exports.tokenTypes = { EOF: EOF, StringLiteral: StringLiteral
  1346. , Keyword: Keyword, Identifier: Identifier, NumericLiteral: NumericLiteral
  1347. , Punctuator: Punctuator, BooleanLiteral: BooleanLiteral
  1348. , NilLiteral: NilLiteral, VarargLiteral: VarargLiteral
  1349. };
  1350. var errors = exports.errors = {
  1351. unexpected: 'unexpected %1 \'%2\' near \'%3\''
  1352. , unexpectedEOF: 'unexpected symbol near \'<eof>\''
  1353. , expected: '\'%1\' expected near \'%2\''
  1354. , expectedToken: '%1 expected near \'%2\''
  1355. , unfinishedString: 'unfinished string near \'%1\''
  1356. , malformedNumber: 'malformed number near \'%1\''
  1357. , decimalEscapeTooLarge: 'decimal escape too large near \'%1\''
  1358. , invalidEscape: 'invalid escape sequence near \'%1\''
  1359. , hexadecimalDigitExpected: 'hexadecimal digit expected near \'%1\''
  1360. , braceExpected: 'missing \'%1\' near \'%2\''
  1361. , tooLargeCodepoint: 'UTF-8 value too large near \'%1\''
  1362. , unfinishedLongString: 'unfinished long string (starting at line %1) near \'%2\''
  1363. , unfinishedLongComment: 'unfinished long comment (starting at line %1) near \'%2\''
  1364. , ambiguousSyntax: 'ambiguous syntax (function call x new statement) near \'%1\''
  1365. , noLoopToBreak: 'no loop to break near \'%1\''
  1366. , labelAlreadyDefined: 'label \'%1\' already defined on line %2'
  1367. , labelNotVisible: 'no visible label \'%1\' for <goto>'
  1368. , gotoJumpInLocalScope: '<goto %1> jumps into the scope of local \'%2\''
  1369. , cannotUseVararg: 'cannot use \'...\' outside a vararg function near \'%1\''
  1370. , invalidCodeUnit: 'code unit U+%1 is not allowed in the current encoding mode'
  1371. };
  1372. var ast = exports.ast = {
  1373. labelStatement: function(label) {
  1374. return {
  1375. type: 'LabelStatement'
  1376. , label: label
  1377. };
  1378. }
  1379. , breakStatement: function() {
  1380. return {
  1381. type: 'BreakStatement'
  1382. };
  1383. }
  1384. , gotoStatement: function(label) {
  1385. return {
  1386. type: 'GotoStatement'
  1387. , label: label
  1388. };
  1389. }
  1390. , returnStatement: function(args) {
  1391. return {
  1392. type: 'ReturnStatement'
  1393. , 'arguments': args
  1394. };
  1395. }
  1396. , ifStatement: function(clauses) {
  1397. return {
  1398. type: 'IfStatement'
  1399. , clauses: clauses
  1400. };
  1401. }
  1402. , ifClause: function(condition, body) {
  1403. return {
  1404. type: 'IfClause'
  1405. , condition: condition
  1406. , body: body
  1407. };
  1408. }
  1409. , elseifClause: function(condition, body) {
  1410. return {
  1411. type: 'ElseifClause'
  1412. , condition: condition
  1413. , body: body
  1414. };
  1415. }
  1416. , elseClause: function(body) {
  1417. return {
  1418. type: 'ElseClause'
  1419. , body: body
  1420. };
  1421. }
  1422. , whileStatement: function(condition, body) {
  1423. return {
  1424. type: 'WhileStatement'
  1425. , condition: condition
  1426. , body: body
  1427. };
  1428. }
  1429. , doStatement: function(body) {
  1430. return {
  1431. type: 'DoStatement'
  1432. , body: body
  1433. };
  1434. }
  1435. , repeatStatement: function(condition, body) {
  1436. return {
  1437. type: 'RepeatStatement'
  1438. , condition: condition
  1439. , body: body
  1440. };
  1441. }
  1442. , localStatement: function(variables, init) {
  1443. return {
  1444. type: 'LocalStatement'
  1445. , variables: variables
  1446. , init: init
  1447. };
  1448. }
  1449. , assignmentStatement: function(variables, init) {
  1450. return {
  1451. type: 'AssignmentStatement'
  1452. , variables: variables
  1453. , init: init
  1454. };
  1455. }
  1456. , callStatement: function(expression) {
  1457. return {
  1458. type: 'CallStatement'
  1459. , expression: expression
  1460. };
  1461. }
  1462. , functionStatement: function(identifier, parameters, isLocal, body) {
  1463. return {
  1464. type: 'FunctionDeclaration'
  1465. , identifier: identifier
  1466. , isLocal: isLocal
  1467. , parameters: parameters
  1468. , body: body
  1469. };
  1470. }
  1471. , forNumericStatement: function(variable, start, end, step, body) {
  1472. return {
  1473. type: 'ForNumericStatement'
  1474. , variable: variable
  1475. , start: start
  1476. , end: end
  1477. , step: step
  1478. , body: body
  1479. };
  1480. }
  1481. , forGenericStatement: function(variables, iterators, body) {
  1482. return {
  1483. type: 'ForGenericStatement'
  1484. , variables: variables
  1485. , iterators: iterators
  1486. , body: body
  1487. };
  1488. }
  1489. , chunk: function(body) {
  1490. return {
  1491. type: 'Chunk'
  1492. , body: body
  1493. };
  1494. }
  1495. , identifier: function(name) {
  1496. return {
  1497. type: 'Identifier'
  1498. , name: name
  1499. };
  1500. }
  1501. , literal: function(type, value, raw) {
  1502. type = (type === StringLiteral) ? 'StringLiteral'
  1503. : (type === NumericLiteral) ? 'NumericLiteral'
  1504. : (type === BooleanLiteral) ? 'BooleanLiteral'
  1505. : (type === NilLiteral) ? 'NilLiteral'
  1506. : 'VarargLiteral';
  1507. return {
  1508. type: type
  1509. , value: value
  1510. , raw: raw
  1511. };
  1512. }
  1513. , tableKey: function(key, value) {
  1514. return {
  1515. type: 'TableKey'
  1516. , key: key
  1517. , value: value
  1518. };
  1519. }
  1520. , tableKeyString: function(key, value) {
  1521. return {
  1522. type: 'TableKeyString'
  1523. , key: key
  1524. , value: value
  1525. };
  1526. }
  1527. , tableValue: function(value) {
  1528. return {
  1529. type: 'TableValue'
  1530. , value: value
  1531. };
  1532. }
  1533. , tableConstructorExpression: function(fields) {
  1534. return {
  1535. type: 'TableConstructorExpression'
  1536. , fields: fields
  1537. };
  1538. }
  1539. , binaryExpression: function(operator, left, right) {
  1540. var type = ('and' === operator || 'or' === operator) ?
  1541. 'LogicalExpression' :
  1542. 'BinaryExpression';
  1543. return {
  1544. type: type
  1545. , operator: operator
  1546. , left: left
  1547. , right: right
  1548. };
  1549. }
  1550. , unaryExpression: function(operator, argument) {
  1551. return {
  1552. type: 'UnaryExpression'
  1553. , operator: operator
  1554. , argument: argument
  1555. };
  1556. }
  1557. , memberExpression: function(base, indexer, identifier) {
  1558. return {
  1559. type: 'MemberExpression'
  1560. , indexer: indexer
  1561. , identifier: identifier
  1562. , base: base
  1563. };
  1564. }
  1565. , indexExpression: function(base, index) {
  1566. return {
  1567. type: 'IndexExpression'
  1568. , base: base
  1569. , index: index
  1570. };
  1571. }
  1572. , callExpression: function(base, args) {
  1573. return {
  1574. type: 'CallExpression'
  1575. , base: base
  1576. , 'arguments': args
  1577. };
  1578. }
  1579. , tableCallExpression: function(base, args) {
  1580. return {
  1581. type: 'TableCallExpression'
  1582. , base: base
  1583. , 'arguments': args
  1584. };
  1585. }
  1586. , stringCallExpression: function(base, argument) {
  1587. return {
  1588. type: 'StringCallExpression'
  1589. , base: base
  1590. , argument: argument
  1591. };
  1592. }
  1593. , comment: function(value, raw) {
  1594. return {
  1595. type: 'Comment'
  1596. , value: value
  1597. , raw: raw
  1598. };
  1599. }
  1600. };
  1601. function finishNode(node) {
  1602. if (trackLocations) {
  1603. var location = locations.pop();
  1604. location.complete();
  1605. location.bless(node);
  1606. }
  1607. if (options.onCreateNode) options.onCreateNode(node);
  1608. return node;
  1609. }
  1610. var slice = Array.prototype.slice
  1611. , toString = Object.prototype.toString
  1612. ;
  1613. var indexOf = /* istanbul ignore next */ function (array, element) {
  1614. for (var i = 0, length = array.length; i < length; ++i) {
  1615. if (array[i] === element) return i;
  1616. }
  1617. return -1;
  1618. };
  1619. if (Array.prototype.indexOf)
  1620. indexOf = function (array, element) {
  1621. return array.indexOf(element);
  1622. };
  1623. function indexOfObject(array, property, element) {
  1624. for (var i = 0, length = array.length; i < length; ++i) {
  1625. if (array[i][property] === element) return i;
  1626. }
  1627. return -1;
  1628. }
  1629. function sprintf(format) {
  1630. var args = slice.call(arguments, 1);
  1631. format = format.replace(/%(\d)/g, function (match, index) {
  1632. return '' + args[index - 1] || /* istanbul ignore next */ '';
  1633. });
  1634. return format;
  1635. }
  1636. var assign = /* istanbul ignore next */ function (dest) {
  1637. var args = slice.call(arguments, 1)
  1638. , src, prop;
  1639. for (var i = 0, length = args.length; i < length; ++i) {
  1640. src = args[i];
  1641. for (prop in src)
  1642. if (Object.prototype.hasOwnProperty.call(src, prop)) {
  1643. dest[prop] = src[prop];
  1644. }
  1645. }
  1646. return dest;
  1647. };
  1648. if (Object.assign)
  1649. assign = Object.assign;
  1650. exports.SyntaxError = SyntaxError;
  1651. function fixupError(e) {
  1652. if (!Object.create)
  1653. return e;
  1654. return Object.create(e, {
  1655. 'line': { 'writable': true, value: e.line },
  1656. 'index': { 'writable': true, value: e.index },
  1657. 'column': { 'writable': true, value: e.column }
  1658. });
  1659. }
  1660. function raise(token) {
  1661. var message = sprintf.apply(null, slice.call(arguments, 1))
  1662. , error, col;
  1663. if (token === null || typeof token.line === 'undefined') {
  1664. col = index - lineStart + 1;
  1665. error = fixupError(new SyntaxError(sprintf('[%1:%2] %3', line, col, message)));
  1666. error.index = index;
  1667. error.line = line;
  1668. error.column = col;
  1669. } else {
  1670. col = token.range[0] - token.lineStart;
  1671. error = fixupError(new SyntaxError(sprintf('[%1:%2] %3', token.line, col, message)));
  1672. error.line = token.line;
  1673. error.index = token.range[0];
  1674. error.column = col;
  1675. }
  1676. throw error;
  1677. }
  1678. function tokenValue(token) {
  1679. var raw = input.slice(token.range[0], token.range[1]);
  1680. if (raw)
  1681. return raw;
  1682. return token.value;
  1683. }
  1684. function raiseUnexpectedToken(type, token) {
  1685. raise(token, errors.expectedToken, type, tokenValue(token));
  1686. }
  1687. function unexpected(found) {
  1688. var near = tokenValue(lookahead);
  1689. if ('undefined' !== typeof found.type) {
  1690. var type;
  1691. switch (found.type) {
  1692. case StringLiteral: type = 'string'; break;
  1693. case Keyword: type = 'keyword'; break;
  1694. case Identifier: type = 'identifier'; break;
  1695. case NumericLiteral: type = 'number'; break;
  1696. case Punctuator: type = 'symbol'; break;
  1697. case BooleanLiteral: type = 'boolean'; break;
  1698. case NilLiteral:
  1699. return raise(found, errors.unexpected, 'symbol', 'nil', near);
  1700. case EOF:
  1701. return raise(found, errors.unexpectedEOF);
  1702. }
  1703. return raise(found, errors.unexpected, type, tokenValue(found), near);
  1704. }
  1705. return raise(found, errors.unexpected, 'symbol', found, near);
  1706. }
  1707. var index
  1708. , token
  1709. , previousToken
  1710. , lookahead
  1711. , comments
  1712. , tokenStart
  1713. , line
  1714. , lineStart;
  1715. exports.lex = lex;
  1716. function lex() {
  1717. skipWhiteSpace();
  1718. while (45 === input.charCodeAt(index) &&
  1719. 45 === input.charCodeAt(index + 1)) {
  1720. scanComment();
  1721. skipWhiteSpace();
  1722. }
  1723. if (index >= length) return {
  1724. type : EOF
  1725. , value: '<eof>'
  1726. , line: line
  1727. , lineStart: lineStart
  1728. , range: [index, index]
  1729. };
  1730. var charCode = input.charCodeAt(index)
  1731. , next = input.charCodeAt(index + 1);
  1732. tokenStart = index;
  1733. if (isIdentifierStart(charCode)) return scanIdentifierOrKeyword();
  1734. switch (charCode) {
  1735. case 39: case 34: // '"
  1736. return scanStringLiteral();
  1737. case 48: case 49: case 50: case 51: case 52: case 53:
  1738. case 54: case 55: case 56: case 57: // 0-9
  1739. return scanNumericLiteral();
  1740. case 46: // .
  1741. if (isDecDigit(next)) return scanNumericLiteral();
  1742. if (46 === next) {
  1743. if (46 === input.charCodeAt(index + 2)) return scanVarargLiteral();
  1744. return scanPunctuator('..');
  1745. }
  1746. return scanPunctuator('.');
  1747. case 61: // =
  1748. if (61 === next) return scanPunctuator('==');
  1749. return scanPunctuator('=');
  1750. case 62: // >
  1751. if (features.bitwiseOperators)
  1752. if (62 === next) return scanPunctuator('>>');
  1753. if (61 === next) return scanPunctuator('>=');
  1754. return scanPunctuator('>');
  1755. case 60: // <
  1756. if (features.bitwiseOperators)
  1757. if (60 === next) return scanPunctuator('<<');
  1758. if (61 === next) return scanPunctuator('<=');
  1759. return scanPunctuator('<');
  1760. case 126: // ~
  1761. if (61 === next) return scanPunctuator('~=');
  1762. if (!features.bitwiseOperators)
  1763. break;
  1764. return scanPunctuator('~');
  1765. case 58: // :
  1766. if (features.labels)
  1767. if (58 === next) return scanPunctuator('::');
  1768. return scanPunctuator(':');
  1769. case 91: // [
  1770. if (91 === next || 61 === next) return scanLongStringLiteral();
  1771. return scanPunctuator('[');
  1772. case 47: // /
  1773. if (features.integerDivision)
  1774. if (47 === next) return scanPunctuator('//');
  1775. return scanPunctuator('/');
  1776. case 38: case 124: // & |
  1777. if (!features.bitwiseOperators)
  1778. break;
  1779. case 42: case 94: case 37: case 44: case 123: case 125:
  1780. case 93: case 40: case 41: case 59: case 35: case 45:
  1781. case 43: // * ^ % , { } ] ( ) ; # - +
  1782. return scanPunctuator(input.charAt(index));
  1783. }
  1784. return unexpected(input.charAt(index));
  1785. }
  1786. function consumeEOL() {
  1787. var charCode = input.charCodeAt(index)
  1788. , peekCharCode = input.charCodeAt(index + 1);
  1789. if (isLineTerminator(charCode)) {
  1790. if (10 === charCode && 13 === peekCharCode) ++index;
  1791. if (13 === charCode && 10 === peekCharCode) ++index;
  1792. ++line;
  1793. lineStart = ++index;
  1794. return true;
  1795. }
  1796. return false;
  1797. }
  1798. function skipWhiteSpace() {
  1799. while (index < length) {
  1800. var charCode = input.charCodeAt(index);
  1801. if (isWhiteSpace(charCode)) {
  1802. ++index;
  1803. } else if (!consumeEOL()) {
  1804. break;
  1805. }
  1806. }
  1807. }
  1808. function scanIdentifierOrKeyword() {
  1809. var value, type;
  1810. while (isIdentifierPart(input.charCodeAt(++index)));
  1811. value = encodingMode.fixup(input.slice(tokenStart, index));
  1812. if (isKeyword(value)) {
  1813. type = Keyword;
  1814. } else if ('true' === value || 'false' === value) {
  1815. type = BooleanLiteral;
  1816. value = ('true' === value);
  1817. } else if ('nil' === value) {
  1818. type = NilLiteral;
  1819. value = null;
  1820. } else {
  1821. type = Identifier;
  1822. }
  1823. return {
  1824. type: type
  1825. , value: value
  1826. , line: line
  1827. , lineStart: lineStart
  1828. , range: [tokenStart, index]
  1829. };
  1830. }
  1831. function scanPunctuator(value) {
  1832. index += value.length;
  1833. return {
  1834. type: Punctuator
  1835. , value: value
  1836. , line: line
  1837. , lineStart: lineStart
  1838. , range: [tokenStart, index]
  1839. };
  1840. }
  1841. function scanVarargLiteral() {
  1842. index += 3;
  1843. return {
  1844. type: VarargLiteral
  1845. , value: '...'
  1846. , line: line
  1847. , lineStart: lineStart
  1848. , range: [tokenStart, index]
  1849. };
  1850. }
  1851. function scanStringLiteral() {
  1852. var delimiter = input.charCodeAt(index++)
  1853. , beginLine = line
  1854. , beginLineStart = lineStart
  1855. , stringStart = index
  1856. , string = encodingMode.discardStrings ? null : ''
  1857. , charCode;
  1858. for (;;) {
  1859. charCode = input.charCodeAt(index++);
  1860. if (delimiter === charCode) break;
  1861. if (index > length || isLineTerminator(charCode)) {
  1862. string += input.slice(stringStart, index - 1);
  1863. raise(null, errors.unfinishedString, input.slice(tokenStart, index - 1));
  1864. }
  1865. if (92 === charCode) { // backslash
  1866. if (!encodingMode.discardStrings) {
  1867. var beforeEscape = input.slice(stringStart, index - 1);
  1868. string += encodingMode.fixup(beforeEscape);
  1869. }
  1870. var escapeValue = readEscapeSequence();
  1871. if (!encodingMode.discardStrings)
  1872. string += escapeValue;
  1873. stringStart = index;
  1874. }
  1875. }
  1876. if (!encodingMode.discardStrings) {
  1877. string += encodingMode.encodeByte(null);
  1878. string += encodingMode.fixup(input.slice(stringStart, index - 1));
  1879. }
  1880. return {
  1881. type: StringLiteral
  1882. , value: string
  1883. , line: beginLine
  1884. , lineStart: beginLineStart
  1885. , lastLine: line
  1886. , lastLineStart: lineStart
  1887. , range: [tokenStart, index]
  1888. };
  1889. }
  1890. function scanLongStringLiteral() {
  1891. var beginLine = line
  1892. , beginLineStart = lineStart
  1893. , string = readLongString(false);
  1894. if (false === string) raise(token, errors.expected, '[', tokenValue(token));
  1895. return {
  1896. type: StringLiteral
  1897. , value: encodingMode.discardStrings ? null : encodingMode.fixup(string)
  1898. , line: beginLine
  1899. , lineStart: beginLineStart
  1900. , lastLine: line
  1901. , lastLineStart: lineStart
  1902. , range: [tokenStart, index]
  1903. };
  1904. }
  1905. function scanNumericLiteral() {
  1906. var character = input.charAt(index)
  1907. , next = input.charAt(index + 1);
  1908. var literal = ('0' === character && 'xX'.indexOf(next || null) >= 0) ?
  1909. readHexLiteral() : readDecLiteral();
  1910. var foundImaginaryUnit = readImaginaryUnitSuffix()
  1911. , foundInt64Suffix = readInt64Suffix();
  1912. if (foundInt64Suffix && (foundImaginaryUnit || literal.hasFractionPart)) {
  1913. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  1914. }
  1915. return {
  1916. type: NumericLiteral
  1917. , value: literal.value
  1918. , line: line
  1919. , lineStart: lineStart
  1920. , range: [tokenStart, index]
  1921. };
  1922. }
  1923. function readImaginaryUnitSuffix() {
  1924. if (!features.imaginaryNumbers) return;
  1925. if ('iI'.indexOf(input.charAt(index) || null) >= 0) {
  1926. ++index;
  1927. return true;
  1928. } else {
  1929. return false;
  1930. }
  1931. }
  1932. function readInt64Suffix() {
  1933. if (!features.integerSuffixes) return;
  1934. if ('uU'.indexOf(input.charAt(index) || null) >= 0) {
  1935. ++index;
  1936. if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
  1937. ++index;
  1938. if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
  1939. ++index;
  1940. return 'ULL';
  1941. } else {
  1942. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  1943. }
  1944. } else {
  1945. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  1946. }
  1947. } else if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
  1948. ++index;
  1949. if ('lL'.indexOf(input.charAt(index) || null) >= 0) {
  1950. ++index;
  1951. return 'LL';
  1952. } else {
  1953. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  1954. }
  1955. }
  1956. }
  1957. function readHexLiteral() {
  1958. var fraction = 0 // defaults to 0 as it gets summed
  1959. , binaryExponent = 1 // defaults to 1 as it gets multiplied
  1960. , binarySign = 1 // positive
  1961. , digit, fractionStart, exponentStart, digitStart;
  1962. digitStart = index += 2; // Skip 0x part
  1963. if (!isHexDigit(input.charCodeAt(index)))
  1964. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  1965. while (isHexDigit(input.charCodeAt(index))) ++index;
  1966. digit = parseInt(input.slice(digitStart, index), 16);
  1967. var foundFraction = false;
  1968. if ('.' === input.charAt(index)) {
  1969. foundFraction = true;
  1970. fractionStart = ++index;
  1971. while (isHexDigit(input.charCodeAt(index))) ++index;
  1972. fraction = input.slice(fractionStart, index);
  1973. fraction = (fractionStart === index) ? 0
  1974. : parseInt(fraction, 16) / Math.pow(16, index - fractionStart);
  1975. }
  1976. var foundBinaryExponent = false;
  1977. if ('pP'.indexOf(input.charAt(index) || null) >= 0) {
  1978. foundBinaryExponent = true;
  1979. ++index;
  1980. if ('+-'.indexOf(input.charAt(index) || null) >= 0)
  1981. binarySign = ('+' === input.charAt(index++)) ? 1 : -1;
  1982. exponentStart = index;
  1983. if (!isDecDigit(input.charCodeAt(index)))
  1984. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  1985. while (isDecDigit(input.charCodeAt(index))) ++index;
  1986. binaryExponent = input.slice(exponentStart, index);
  1987. binaryExponent = Math.pow(2, binaryExponent * binarySign);
  1988. }
  1989. return {
  1990. value: (digit + fraction) * binaryExponent,
  1991. hasFractionPart: foundFraction || foundBinaryExponent
  1992. };
  1993. }
  1994. function readDecLiteral() {
  1995. while (isDecDigit(input.charCodeAt(index))) ++index;
  1996. var foundFraction = false;
  1997. if ('.' === input.charAt(index)) {
  1998. foundFraction = true;
  1999. ++index;
  2000. while (isDecDigit(input.charCodeAt(index))) ++index;
  2001. }
  2002. var foundExponent = false;
  2003. if ('eE'.indexOf(input.charAt(index) || null) >= 0) {
  2004. foundExponent = true;
  2005. ++index;
  2006. if ('+-'.indexOf(input.charAt(index) || null) >= 0) ++index;
  2007. if (!isDecDigit(input.charCodeAt(index)))
  2008. raise(null, errors.malformedNumber, input.slice(tokenStart, index));
  2009. while (isDecDigit(input.charCodeAt(index))) ++index;
  2010. }
  2011. return {
  2012. value: parseFloat(input.slice(tokenStart, index)),
  2013. hasFractionPart: foundFraction || foundExponent
  2014. };
  2015. }
  2016. function readUnicodeEscapeSequence() {
  2017. var sequenceStart = index++;
  2018. if (input.charAt(index++) !== '{')
  2019. raise(null, errors.braceExpected, '{', '\\' + input.slice(sequenceStart, index));
  2020. if (!isHexDigit(input.charCodeAt(index)))
  2021. raise(null, errors.hexadecimalDigitExpected, '\\' + input.slice(sequenceStart, index));
  2022. while (input.charCodeAt(index) === 0x30) ++index;
  2023. var escStart = index;
  2024. while (isHexDigit(input.charCodeAt(index))) {
  2025. ++index;
  2026. if (index - escStart > 6)
  2027. raise(null, errors.tooLargeCodepoint, '\\' + input.slice(sequenceStart, index));
  2028. }
  2029. var b = input.charAt(index++);
  2030. if (b !== '}') {
  2031. if ((b === '"') || (b === "'"))
  2032. raise(null, errors.braceExpected, '}', '\\' + input.slice(sequenceStart, index--));
  2033. else
  2034. raise(null, errors.hexadecimalDigitExpected, '\\' + input.slice(sequenceStart, index));
  2035. }
  2036. var codepoint = parseInt(input.slice(escStart, index - 1) || '0', 16);
  2037. var frag = '\\' + input.slice(sequenceStart, index);
  2038. if (codepoint > 0x10ffff) {
  2039. raise(null, errors.tooLargeCodepoint, frag);
  2040. }
  2041. return encodingMode.encodeUTF8(codepoint, frag);
  2042. }
  2043. function readEscapeSequence() {
  2044. var sequenceStart = index;
  2045. switch (input.charAt(index)) {
  2046. case 'a': ++index; return '\x07';
  2047. case 'n': ++index; return '\n';
  2048. case 'r': ++index; return '\r';
  2049. case 't': ++index; return '\t';
  2050. case 'v': ++index; return '\x0b';
  2051. case 'b': ++index; return '\b';
  2052. case 'f': ++index; return '\f';
  2053. case '\r':
  2054. case '\n':
  2055. consumeEOL();
  2056. return '\n';
  2057. case '0': case '1': case '2': case '3': case '4':
  2058. case '5': case '6': case '7': case '8': case '9':
  2059. while (isDecDigit(input.charCodeAt(index)) && index - sequenceStart < 3) ++index;
  2060. var frag = input.slice(sequenceStart, index);
  2061. var ddd = parseInt(frag, 10);
  2062. if (ddd > 255) {
  2063. raise(null, errors.decimalEscapeTooLarge, '\\' + ddd);
  2064. }
  2065. return encodingMode.encodeByte(ddd, '\\' + frag);
  2066. case 'z':
  2067. if (features.skipWhitespaceEscape) {
  2068. ++index;
  2069. skipWhiteSpace();
  2070. return '';
  2071. }
  2072. break;
  2073. case 'x':
  2074. if (features.hexEscapes) {
  2075. if (isHexDigit(input.charCodeAt(index + 1)) &&
  2076. isHexDigit(input.charCodeAt(index + 2))) {
  2077. index += 3;
  2078. return encodingMode.encodeByte(parseInt(input.slice(sequenceStart + 1, index), 16), '\\' + input.slice(sequenceStart, index));
  2079. }
  2080. raise(null, errors.hexadecimalDigitExpected, '\\' + input.slice(sequenceStart, index + 2));
  2081. }
  2082. break;
  2083. case 'u':
  2084. if (features.unicodeEscapes)
  2085. return readUnicodeEscapeSequence();
  2086. break;
  2087. case '\\': case '"': case "'":
  2088. return input.charAt(index++);
  2089. }
  2090. if (features.strictEscapes)
  2091. raise(null, errors.invalidEscape, '\\' + input.slice(sequenceStart, index + 1));
  2092. return input.charAt(index++);
  2093. }
  2094. function scanComment() {
  2095. tokenStart = index;
  2096. index += 2; // --
  2097. var character = input.charAt(index)
  2098. , content = ''
  2099. , isLong = false
  2100. , commentStart = index
  2101. , lineStartComment = lineStart
  2102. , lineComment = line;
  2103. if ('[' === character) {
  2104. content = readLongString(true);
  2105. if (false === content) content = character;
  2106. else isLong = true;
  2107. }
  2108. if (!isLong) {
  2109. while (index < length) {
  2110. if (isLineTerminator(input.charCodeAt(index))) break;
  2111. ++index;
  2112. }
  2113. if (options.comments) content = input.slice(commentStart, index);
  2114. }
  2115. if (options.comments) {
  2116. var node = ast.comment(content, input.slice(tokenStart, index));
  2117. if (options.locations) {
  2118. node.loc = {
  2119. start: { line: lineComment, column: tokenStart - lineStartComment }
  2120. , end: { line: line, column: index - lineStart }
  2121. };
  2122. }
  2123. if (options.ranges) {
  2124. node.range = [tokenStart, index];
  2125. }
  2126. if (options.onCreateNode) options.onCreateNode(node);
  2127. comments.push(node);
  2128. }
  2129. }
  2130. function readLongString(isComment) {
  2131. var level = 0
  2132. , content = ''
  2133. , terminator = false
  2134. , character, stringStart, firstLine = line;
  2135. ++index; // [
  2136. while ('=' === input.charAt(index + level)) ++level;
  2137. if ('[' !== input.charAt(index + level)) return false;
  2138. index += level + 1;
  2139. if (isLineTerminator(input.charCodeAt(index))) consumeEOL();
  2140. stringStart = index;
  2141. while (index < length) {
  2142. while (isLineTerminator(input.charCodeAt(index))) consumeEOL();
  2143. character = input.charAt(index++);
  2144. if (']' === character) {
  2145. terminator = true;
  2146. for (var i = 0; i < level; ++i) {
  2147. if ('=' !== input.charAt(index + i)) terminator = false;
  2148. }
  2149. if (']' !== input.charAt(index + level)) terminator = false;
  2150. }
  2151. if (terminator) {
  2152. content += input.slice(stringStart, index - 1);
  2153. index += level + 1;
  2154. return content;
  2155. }
  2156. }
  2157. raise(null, isComment ?
  2158. errors.unfinishedLongComment :
  2159. errors.unfinishedLongString,
  2160. firstLine, '<eof>');
  2161. }
  2162. function next() {
  2163. previousToken = token;
  2164. token = lookahead;
  2165. lookahead = lex();
  2166. }
  2167. function consume(value) {
  2168. if (value === token.value) {
  2169. next();
  2170. return true;
  2171. }
  2172. return false;
  2173. }
  2174. function expect(value) {
  2175. if (value === token.value) next();
  2176. else raise(token, errors.expected, value, tokenValue(token));
  2177. }
  2178. function isWhiteSpace(charCode) {
  2179. return 9 === charCode || 32 === charCode || 0xB === charCode || 0xC === charCode;
  2180. }
  2181. function isLineTerminator(charCode) {
  2182. return 10 === charCode || 13 === charCode;
  2183. }
  2184. function isDecDigit(charCode) {
  2185. return charCode >= 48 && charCode <= 57;
  2186. }
  2187. function isHexDigit(charCode) {
  2188. return (charCode >= 48 && charCode <= 57) || (charCode >= 97 && charCode <= 102) || (charCode >= 65 && charCode <= 70);
  2189. }
  2190. function isIdentifierStart(charCode) {
  2191. if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || 95 === charCode)
  2192. return true;
  2193. if (features.extendedIdentifiers && charCode >= 128)
  2194. return true;
  2195. return false;
  2196. }
  2197. function isIdentifierPart(charCode) {
  2198. if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || 95 === charCode || (charCode >= 48 && charCode <= 57))
  2199. return true;
  2200. if (features.extendedIdentifiers && charCode >= 128)
  2201. return true;
  2202. return false;
  2203. }
  2204. function isKeyword(id) {
  2205. switch (id.length) {
  2206. case 2:
  2207. return 'do' === id || 'if' === id || 'in' === id || 'or' === id;
  2208. case 3:
  2209. return 'and' === id || 'end' === id || 'for' === id || 'not' === id;
  2210. case 4:
  2211. if ('else' === id || 'then' === id)
  2212. return true;
  2213. if (features.labels && !features.contextualGoto)
  2214. return ('goto' === id);
  2215. return false;
  2216. case 5:
  2217. return 'break' === id || 'local' === id || 'until' === id || 'while' === id;
  2218. case 6:
  2219. return 'elseif' === id || 'repeat' === id || 'return' === id;
  2220. case 8:
  2221. return 'function' === id;
  2222. }
  2223. return false;
  2224. }
  2225. function isUnary(token) {
  2226. if (Punctuator === token.type) return '#-~'.indexOf(token.value) >= 0;
  2227. if (Keyword === token.type) return 'not' === token.value;
  2228. return false;
  2229. }
  2230. function isBlockFollow(token) {
  2231. if (EOF === token.type) return true;
  2232. if (Keyword !== token.type) return false;
  2233. switch (token.value) {
  2234. case 'else': case 'elseif':
  2235. case 'end': case 'until':
  2236. return true;
  2237. default:
  2238. return false;
  2239. }
  2240. }
  2241. var scopes
  2242. , scopeDepth
  2243. , globals;
  2244. function createScope() {
  2245. var scope = scopes[scopeDepth++].slice();
  2246. scopes.push(scope);
  2247. if (options.onCreateScope) options.onCreateScope();
  2248. }
  2249. function destroyScope() {
  2250. var scope = scopes.pop();
  2251. --scopeDepth;
  2252. if (options.onDestroyScope) options.onDestroyScope();
  2253. }
  2254. function scopeIdentifierName(name) {
  2255. if (options.onLocalDeclaration) options.onLocalDeclaration(name);
  2256. if (-1 !== indexOf(scopes[scopeDepth], name)) return;
  2257. scopes[scopeDepth].push(name);
  2258. }
  2259. function scopeIdentifier(node) {
  2260. scopeIdentifierName(node.name);
  2261. attachScope(node, true);
  2262. }
  2263. function attachScope(node, isLocal) {
  2264. if (!isLocal && -1 === indexOfObject(globals, 'name', node.name))
  2265. globals.push(node);
  2266. node.isLocal = isLocal;
  2267. }
  2268. function scopeHasName(name) {
  2269. return (-1 !== indexOf(scopes[scopeDepth], name));
  2270. }
  2271. var locations = []
  2272. , trackLocations;
  2273. function createLocationMarker() {
  2274. return new Marker(token);
  2275. }
  2276. function Marker(token) {
  2277. if (options.locations) {
  2278. this.loc = {
  2279. start: {
  2280. line: token.line
  2281. , column: token.range[0] - token.lineStart
  2282. }
  2283. , end: {
  2284. line: 0
  2285. , column: 0
  2286. }
  2287. };
  2288. }
  2289. if (options.ranges) this.range = [token.range[0], 0];
  2290. }
  2291. Marker.prototype.complete = function() {
  2292. if (options.locations) {
  2293. this.loc.end.line = previousToken.lastLine || previousToken.line;
  2294. this.loc.end.column = previousToken.range[1] - (previousToken.lastLineStart || previousToken.lineStart);
  2295. }
  2296. if (options.ranges) {
  2297. this.range[1] = previousToken.range[1];
  2298. }
  2299. };
  2300. Marker.prototype.bless = function (node) {
  2301. if (this.loc) {
  2302. var loc = this.loc;
  2303. node.loc = {
  2304. start: {
  2305. line: loc.start.line,
  2306. column: loc.start.column
  2307. },
  2308. end: {
  2309. line: loc.end.line,
  2310. column: loc.end.column
  2311. }
  2312. };
  2313. }
  2314. if (this.range) {
  2315. node.range = [
  2316. this.range[0],
  2317. this.range[1]
  2318. ];
  2319. }
  2320. };
  2321. function markLocation() {
  2322. if (trackLocations) locations.push(createLocationMarker());
  2323. }
  2324. function pushLocation(marker) {
  2325. if (trackLocations) locations.push(marker);
  2326. }
  2327. function FullFlowContext() {
  2328. this.scopes = [];
  2329. this.pendingGotos = [];
  2330. }
  2331. FullFlowContext.prototype.isInLoop = function () {
  2332. var i = this.scopes.length;
  2333. while (i --> 0) {
  2334. if (this.scopes[i].isLoop)
  2335. return true;
  2336. }
  2337. return false;
  2338. };
  2339. FullFlowContext.prototype.pushScope = function (isLoop) {
  2340. var scope = {
  2341. labels: {},
  2342. locals: [],
  2343. deferredGotos: [],
  2344. isLoop: !!isLoop
  2345. };
  2346. this.scopes.push(scope);
  2347. };
  2348. FullFlowContext.prototype.popScope = function () {
  2349. for (var i = 0; i < this.pendingGotos.length; ++i) {
  2350. var theGoto = this.pendingGotos[i];
  2351. if (theGoto.maxDepth >= this.scopes.length)
  2352. if (--theGoto.maxDepth <= 0)
  2353. raise(theGoto.token, errors.labelNotVisible, theGoto.target);
  2354. }
  2355. this.scopes.pop();
  2356. };
  2357. FullFlowContext.prototype.addGoto = function (target, token) {
  2358. var localCounts = [];
  2359. for (var i = 0; i < this.scopes.length; ++i) {
  2360. var scope = this.scopes[i];
  2361. localCounts.push(scope.locals.length);
  2362. if (Object.prototype.hasOwnProperty.call(scope.labels, target))
  2363. return;
  2364. }
  2365. this.pendingGotos.push({
  2366. maxDepth: this.scopes.length,
  2367. target: target,
  2368. token: token,
  2369. localCounts: localCounts
  2370. });
  2371. };
  2372. FullFlowContext.prototype.addLabel = function (name, token) {
  2373. var scope = this.currentScope();
  2374. if (Object.prototype.hasOwnProperty.call(scope.labels, name)) {
  2375. raise(token, errors.labelAlreadyDefined, name, scope.labels[name].line);
  2376. } else {
  2377. var newGotos = [];
  2378. for (var i = 0; i < this.pendingGotos.length; ++i) {
  2379. var theGoto = this.pendingGotos[i];
  2380. if (theGoto.maxDepth >= this.scopes.length && theGoto.target === name) {
  2381. if (theGoto.localCounts[this.scopes.length - 1] < scope.locals.length) {
  2382. scope.deferredGotos.push(theGoto);
  2383. }
  2384. continue;
  2385. }
  2386. newGotos.push(theGoto);
  2387. }
  2388. this.pendingGotos = newGotos;
  2389. }
  2390. scope.labels[name] = {
  2391. localCount: scope.locals.length,
  2392. line: token.line
  2393. };
  2394. };
  2395. FullFlowContext.prototype.addLocal = function (name, token) {
  2396. this.currentScope().locals.push({
  2397. name: name,
  2398. token: token
  2399. });
  2400. };
  2401. FullFlowContext.prototype.currentScope = function () {
  2402. return this.scopes[this.scopes.length - 1];
  2403. };
  2404. FullFlowContext.prototype.raiseDeferredErrors = function () {
  2405. var scope = this.currentScope();
  2406. var bads = scope.deferredGotos;
  2407. for (var i = 0; i < bads.length; ++i) {
  2408. var theGoto = bads[i];
  2409. raise(theGoto.token, errors.gotoJumpInLocalScope, theGoto.target, scope.locals[theGoto.localCounts[this.scopes.length - 1]].name);
  2410. }
  2411. };
  2412. function LoopFlowContext() {
  2413. this.level = 0;
  2414. this.loopLevels = [];
  2415. }
  2416. LoopFlowContext.prototype.isInLoop = function () {
  2417. return !!this.loopLevels.length;
  2418. };
  2419. LoopFlowContext.prototype.pushScope = function (isLoop) {
  2420. ++this.level;
  2421. if (isLoop)
  2422. this.loopLevels.push(this.level);
  2423. };
  2424. LoopFlowContext.prototype.popScope = function () {
  2425. var levels = this.loopLevels;
  2426. var levlen = levels.length;
  2427. if (levlen) {
  2428. if (levels[levlen - 1] === this.level)
  2429. levels.pop();
  2430. }
  2431. --this.level;
  2432. };
  2433. LoopFlowContext.prototype.addGoto =
  2434. LoopFlowContext.prototype.addLabel =
  2435. function () { throw new Error('This should never happen'); };
  2436. LoopFlowContext.prototype.addLocal =
  2437. LoopFlowContext.prototype.raiseDeferredErrors =
  2438. function () {};
  2439. function makeFlowContext() {
  2440. return features.labels ? new FullFlowContext() : new LoopFlowContext();
  2441. }
  2442. function parseChunk() {
  2443. next();
  2444. markLocation();
  2445. if (options.scope) createScope();
  2446. var flowContext = makeFlowContext();
  2447. flowContext.allowVararg = true;
  2448. flowContext.pushScope();
  2449. var body = parseBlock(flowContext);
  2450. flowContext.popScope();
  2451. if (options.scope) destroyScope();
  2452. if (EOF !== token.type) unexpected(token);
  2453. if (trackLocations && !body.length) previousToken = token;
  2454. return finishNode(ast.chunk(body));
  2455. }
  2456. function parseBlock(flowContext) {
  2457. var block = []
  2458. , statement;
  2459. while (!isBlockFollow(token)) {
  2460. if ('return' === token.value || (!features.relaxedBreak && 'break' === token.value)) {
  2461. block.push(parseStatement(flowContext));
  2462. break;
  2463. }
  2464. statement = parseStatement(flowContext);
  2465. consume(';');
  2466. if (statement) block.push(statement);
  2467. }
  2468. return block;
  2469. }
  2470. function parseStatement(flowContext) {
  2471. markLocation();
  2472. if (Punctuator === token.type) {
  2473. if (consume('::')) return parseLabelStatement(flowContext);
  2474. }
  2475. if (features.emptyStatement) {
  2476. if (consume(';')) {
  2477. if (trackLocations) locations.pop();
  2478. return;
  2479. }
  2480. }
  2481. flowContext.raiseDeferredErrors();
  2482. if (Keyword === token.type) {
  2483. switch (token.value) {
  2484. case 'local': next(); return parseLocalStatement(flowContext);
  2485. case 'if': next(); return parseIfStatement(flowContext);
  2486. case 'return': next(); return parseReturnStatement(flowContext);
  2487. case 'function': next();
  2488. var name = parseFunctionName();
  2489. return parseFunctionDeclaration(name);
  2490. case 'while': next(); return parseWhileStatement(flowContext);
  2491. case 'for': next(); return parseForStatement(flowContext);
  2492. case 'repeat': next(); return parseRepeatStatement(flowContext);
  2493. case 'break': next();
  2494. if (!flowContext.isInLoop())
  2495. raise(token, errors.noLoopToBreak, token.value);
  2496. return parseBreakStatement();
  2497. case 'do': next(); return parseDoStatement(flowContext);
  2498. case 'goto': next(); return parseGotoStatement(flowContext);
  2499. }
  2500. }
  2501. if (features.contextualGoto &&
  2502. token.type === Identifier && token.value === 'goto' &&
  2503. lookahead.type === Identifier && lookahead.value !== 'goto') {
  2504. next(); return parseGotoStatement(flowContext);
  2505. }
  2506. if (trackLocations) locations.pop();
  2507. return parseAssignmentOrCallStatement(flowContext);
  2508. }
  2509. function parseLabelStatement(flowContext) {
  2510. var nameToken = token
  2511. , label = parseIdentifier();
  2512. if (options.scope) {
  2513. scopeIdentifierName('::' + nameToken.value + '::');
  2514. attachScope(label, true);
  2515. }
  2516. expect('::');
  2517. flowContext.addLabel(nameToken.value, nameToken);
  2518. return finishNode(ast.labelStatement(label));
  2519. }
  2520. function parseBreakStatement() {
  2521. return finishNode(ast.breakStatement());
  2522. }
  2523. function parseGotoStatement(flowContext) {
  2524. var name = token.value
  2525. , gotoToken = previousToken
  2526. , label = parseIdentifier();
  2527. flowContext.addGoto(name, gotoToken);
  2528. return finishNode(ast.gotoStatement(label));
  2529. }
  2530. function parseDoStatement(flowContext) {
  2531. if (options.scope) createScope();
  2532. flowContext.pushScope();
  2533. var body = parseBlock(flowContext);
  2534. flowContext.popScope();
  2535. if (options.scope) destroyScope();
  2536. expect('end');
  2537. return finishNode(ast.doStatement(body));
  2538. }
  2539. function parseWhileStatement(flowContext) {
  2540. var condition = parseExpectedExpression(flowContext);
  2541. expect('do');
  2542. if (options.scope) createScope();
  2543. flowContext.pushScope(true);
  2544. var body = parseBlock(flowContext);
  2545. flowContext.popScope();
  2546. if (options.scope) destroyScope();
  2547. expect('end');
  2548. return finishNode(ast.whileStatement(condition, body));
  2549. }
  2550. function parseRepeatStatement(flowContext) {
  2551. if (options.scope) createScope();
  2552. flowContext.pushScope(true);
  2553. var body = parseBlock(flowContext);
  2554. expect('until');
  2555. flowContext.raiseDeferredErrors();
  2556. var condition = parseExpectedExpression(flowContext);
  2557. flowContext.popScope();
  2558. if (options.scope) destroyScope();
  2559. return finishNode(ast.repeatStatement(condition, body));
  2560. }
  2561. function parseReturnStatement(flowContext) {
  2562. var expressions = [];
  2563. if ('end' !== token.value) {
  2564. var expression = parseExpression(flowContext);
  2565. if (null != expression) expressions.push(expression);
  2566. while (consume(',')) {
  2567. expression = parseExpectedExpression(flowContext);
  2568. expressions.push(expression);
  2569. }
  2570. consume(';'); // grammar tells us ; is optional here.
  2571. }
  2572. return finishNode(ast.returnStatement(expressions));
  2573. }
  2574. function parseIfStatement(flowContext) {
  2575. var clauses = []
  2576. , condition
  2577. , body
  2578. , marker;
  2579. if (trackLocations) {
  2580. marker = locations[locations.length - 1];
  2581. locations.push(marker);
  2582. }
  2583. condition = parseExpectedExpression(flowContext);
  2584. expect('then');
  2585. if (options.scope) createScope();
  2586. flowContext.pushScope();
  2587. body = parseBlock(flowContext);
  2588. flowContext.popScope();
  2589. if (options.scope) destroyScope();
  2590. clauses.push(finishNode(ast.ifClause(condition, body)));
  2591. if (trackLocations) marker = createLocationMarker();
  2592. while (consume('elseif')) {
  2593. pushLocation(marker);
  2594. condition = parseExpectedExpression(flowContext);
  2595. expect('then');
  2596. if (options.scope) createScope();
  2597. flowContext.pushScope();
  2598. body = parseBlock(flowContext);
  2599. flowContext.popScope();
  2600. if (options.scope) destroyScope();
  2601. clauses.push(finishNode(ast.elseifClause(condition, body)));
  2602. if (trackLocations) marker = createLocationMarker();
  2603. }
  2604. if (consume('else')) {
  2605. if (trackLocations) {
  2606. marker = new Marker(previousToken);
  2607. locations.push(marker);
  2608. }
  2609. if (options.scope) createScope();
  2610. flowContext.pushScope();
  2611. body = parseBlock(flowContext);
  2612. flowContext.popScope();
  2613. if (options.scope) destroyScope();
  2614. clauses.push(finishNode(ast.elseClause(body)));
  2615. }
  2616. expect('end');
  2617. return finishNode(ast.ifStatement(clauses));
  2618. }
  2619. function parseForStatement(flowContext) {
  2620. var variable = parseIdentifier()
  2621. , body;
  2622. if (options.scope) {
  2623. createScope();
  2624. scopeIdentifier(variable);
  2625. }
  2626. if (consume('=')) {
  2627. var start = parseExpectedExpression(flowContext);
  2628. expect(',');
  2629. var end = parseExpectedExpression(flowContext);
  2630. var step = consume(',') ? parseExpectedExpression(flowContext) : null;
  2631. expect('do');
  2632. flowContext.pushScope(true);
  2633. body = parseBlock(flowContext);
  2634. flowContext.popScope();
  2635. expect('end');
  2636. if (options.scope) destroyScope();
  2637. return finishNode(ast.forNumericStatement(variable, start, end, step, body));
  2638. }
  2639. else {
  2640. var variables = [variable];
  2641. while (consume(',')) {
  2642. variable = parseIdentifier();
  2643. if (options.scope) scopeIdentifier(variable);
  2644. variables.push(variable);
  2645. }
  2646. expect('in');
  2647. var iterators = [];
  2648. do {
  2649. var expression = parseExpectedExpression(flowContext);
  2650. iterators.push(expression);
  2651. } while (consume(','));
  2652. expect('do');
  2653. flowContext.pushScope(true);
  2654. body = parseBlock(flowContext);
  2655. flowContext.popScope();
  2656. expect('end');
  2657. if (options.scope) destroyScope();
  2658. return finishNode(ast.forGenericStatement(variables, iterators, body));
  2659. }
  2660. }
  2661. function parseLocalStatement(flowContext) {
  2662. var name
  2663. , declToken = previousToken;
  2664. if (Identifier === token.type) {
  2665. var variables = []
  2666. , init = [];
  2667. do {
  2668. name = parseIdentifier();
  2669. variables.push(name);
  2670. flowContext.addLocal(name.name, declToken);
  2671. } while (consume(','));
  2672. if (consume('=')) {
  2673. do {
  2674. var expression = parseExpectedExpression(flowContext);
  2675. init.push(expression);
  2676. } while (consume(','));
  2677. }
  2678. if (options.scope) {
  2679. for (var i = 0, l = variables.length; i < l; ++i) {
  2680. scopeIdentifier(variables[i]);
  2681. }
  2682. }
  2683. return finishNode(ast.localStatement(variables, init));
  2684. }
  2685. if (consume('function')) {
  2686. name = parseIdentifier();
  2687. flowContext.addLocal(name.name, declToken);
  2688. if (options.scope) {
  2689. scopeIdentifier(name);
  2690. createScope();
  2691. }
  2692. return parseFunctionDeclaration(name, true);
  2693. } else {
  2694. raiseUnexpectedToken('<name>', token);
  2695. }
  2696. }
  2697. function parseAssignmentOrCallStatement(flowContext) {
  2698. var previous = token
  2699. , marker, startMarker;
  2700. var lvalue, base, name;
  2701. var targets = [];
  2702. if (trackLocations) startMarker = createLocationMarker();
  2703. do {
  2704. if (trackLocations) marker = createLocationMarker();
  2705. if (Identifier === token.type) {
  2706. name = token.value;
  2707. base = parseIdentifier();
  2708. if (options.scope) attachScope(base, scopeHasName(name));
  2709. lvalue = true;
  2710. } else if ('(' === token.value) {
  2711. next();
  2712. base = parseExpectedExpression(flowContext);
  2713. expect(')');
  2714. lvalue = false;
  2715. } else {
  2716. return unexpected(token);
  2717. }
  2718. both: for (;;) {
  2719. var newBase;
  2720. switch (StringLiteral === token.type ? '"' : token.value) {
  2721. case '.':
  2722. case '[':
  2723. lvalue = true;
  2724. break;
  2725. case ':':
  2726. case '(':
  2727. case '{':
  2728. case '"':
  2729. lvalue = null;
  2730. break;
  2731. default:
  2732. break both;
  2733. }
  2734. base = parsePrefixExpressionPart(base, marker, flowContext);
  2735. }
  2736. targets.push(base);
  2737. if (',' !== token.value)
  2738. break;
  2739. if (!lvalue) {
  2740. return unexpected(token);
  2741. }
  2742. next();
  2743. } while (true);
  2744. if (targets.length === 1 && lvalue === null) {
  2745. pushLocation(marker);
  2746. return finishNode(ast.callStatement(targets[0]));
  2747. } else if (!lvalue) {
  2748. return unexpected(token);
  2749. }
  2750. expect('=');
  2751. var values = [];
  2752. do {
  2753. values.push(parseExpectedExpression(flowContext));
  2754. } while (consume(','));
  2755. pushLocation(startMarker);
  2756. return finishNode(ast.assignmentStatement(targets, values));
  2757. }
  2758. function parseIdentifier() {
  2759. markLocation();
  2760. var identifier = token.value;
  2761. if (Identifier !== token.type) raiseUnexpectedToken('<name>', token);
  2762. next();
  2763. return finishNode(ast.identifier(identifier));
  2764. }
  2765. function parseFunctionDeclaration(name, isLocal) {
  2766. var flowContext = makeFlowContext();
  2767. flowContext.pushScope();
  2768. var parameters = [];
  2769. expect('(');
  2770. if (!consume(')')) {
  2771. while (true) {
  2772. if (Identifier === token.type) {
  2773. var parameter = parseIdentifier();
  2774. if (options.scope) scopeIdentifier(parameter);
  2775. parameters.push(parameter);
  2776. if (consume(',')) continue;
  2777. }
  2778. else if (VarargLiteral === token.type) {
  2779. flowContext.allowVararg = true;
  2780. parameters.push(parsePrimaryExpression(flowContext));
  2781. } else {
  2782. raiseUnexpectedToken('<name> or \'...\'', token);
  2783. }
  2784. expect(')');
  2785. break;
  2786. }
  2787. }
  2788. var body = parseBlock(flowContext);
  2789. flowContext.popScope();
  2790. expect('end');
  2791. if (options.scope) destroyScope();
  2792. isLocal = isLocal || false;
  2793. return finishNode(ast.functionStatement(name, parameters, isLocal, body));
  2794. }
  2795. function parseFunctionName() {
  2796. var base, name, marker;
  2797. if (trackLocations) marker = createLocationMarker();
  2798. base = parseIdentifier();
  2799. if (options.scope) {
  2800. attachScope(base, scopeHasName(base.name));
  2801. createScope();
  2802. }
  2803. while (consume('.')) {
  2804. pushLocation(marker);
  2805. name = parseIdentifier();
  2806. base = finishNode(ast.memberExpression(base, '.', name));
  2807. }
  2808. if (consume(':')) {
  2809. pushLocation(marker);
  2810. name = parseIdentifier();
  2811. base = finishNode(ast.memberExpression(base, ':', name));
  2812. if (options.scope) scopeIdentifierName('self');
  2813. }
  2814. return base;
  2815. }
  2816. function parseTableConstructor(flowContext) {
  2817. var fields = []
  2818. , key, value;
  2819. while (true) {
  2820. markLocation();
  2821. if (Punctuator === token.type && consume('[')) {
  2822. key = parseExpectedExpression(flowContext);
  2823. expect(']');
  2824. expect('=');
  2825. value = parseExpectedExpression(flowContext);
  2826. fields.push(finishNode(ast.tableKey(key, value)));
  2827. } else if (Identifier === token.type) {
  2828. if ('=' === lookahead.value) {
  2829. key = parseIdentifier();
  2830. next();
  2831. value = parseExpectedExpression(flowContext);
  2832. fields.push(finishNode(ast.tableKeyString(key, value)));
  2833. } else {
  2834. value = parseExpectedExpression(flowContext);
  2835. fields.push(finishNode(ast.tableValue(value)));
  2836. }
  2837. } else {
  2838. if (null == (value = parseExpression(flowContext))) {
  2839. locations.pop();
  2840. break;
  2841. }
  2842. fields.push(finishNode(ast.tableValue(value)));
  2843. }
  2844. if (',;'.indexOf(token.value) >= 0) {
  2845. next();
  2846. continue;
  2847. }
  2848. break;
  2849. }
  2850. expect('}');
  2851. return finishNode(ast.tableConstructorExpression(fields));
  2852. }
  2853. function parseExpression(flowContext) {
  2854. var expression = parseSubExpression(0, flowContext);
  2855. return expression;
  2856. }
  2857. function parseExpectedExpression(flowContext) {
  2858. var expression = parseExpression(flowContext);
  2859. if (null == expression) raiseUnexpectedToken('<expression>', token);
  2860. else return expression;
  2861. }
  2862. function binaryPrecedence(operator) {
  2863. var charCode = operator.charCodeAt(0)
  2864. , length = operator.length;
  2865. if (1 === length) {
  2866. switch (charCode) {
  2867. case 94: return 12; // ^
  2868. case 42: case 47: case 37: return 10; // * / %
  2869. case 43: case 45: return 9; // + -
  2870. case 38: return 6; // &
  2871. case 126: return 5; // ~
  2872. case 124: return 4; // |
  2873. case 60: case 62: return 3; // < >
  2874. }
  2875. } else if (2 === length) {
  2876. switch (charCode) {
  2877. case 47: return 10; // //
  2878. case 46: return 8; // ..
  2879. case 60: case 62:
  2880. if('<<' === operator || '>>' === operator) return 7; // << >>
  2881. return 3; // <= >=
  2882. case 61: case 126: return 3; // == ~=
  2883. case 111: return 1; // or
  2884. }
  2885. } else if (97 === charCode && 'and' === operator) return 2;
  2886. return 0;
  2887. }
  2888. function parseSubExpression(minPrecedence, flowContext) {
  2889. var operator = token.value
  2890. , expression, marker;
  2891. if (trackLocations) marker = createLocationMarker();
  2892. if (isUnary(token)) {
  2893. markLocation();
  2894. next();
  2895. var argument = parseSubExpression(10, flowContext);
  2896. if (argument == null) raiseUnexpectedToken('<expression>', token);
  2897. expression = finishNode(ast.unaryExpression(operator, argument));
  2898. }
  2899. if (null == expression) {
  2900. expression = parsePrimaryExpression(flowContext);
  2901. if (null == expression) {
  2902. expression = parsePrefixExpression(flowContext);
  2903. }
  2904. }
  2905. if (null == expression) return null;
  2906. var precedence;
  2907. while (true) {
  2908. operator = token.value;
  2909. precedence = (Punctuator === token.type || Keyword === token.type) ?
  2910. binaryPrecedence(operator) : 0;
  2911. if (precedence === 0 || precedence <= minPrecedence) break;
  2912. if ('^' === operator || '..' === operator) --precedence;
  2913. next();
  2914. var right = parseSubExpression(precedence, flowContext);
  2915. if (null == right) raiseUnexpectedToken('<expression>', token);
  2916. if (trackLocations) locations.push(marker);
  2917. expression = finishNode(ast.binaryExpression(operator, expression, right));
  2918. }
  2919. return expression;
  2920. }
  2921. function parsePrefixExpressionPart(base, marker, flowContext) {
  2922. var expression, identifier;
  2923. if (Punctuator === token.type) {
  2924. switch (token.value) {
  2925. case '[':
  2926. pushLocation(marker);
  2927. next();
  2928. expression = parseExpectedExpression(flowContext);
  2929. expect(']');
  2930. return finishNode(ast.indexExpression(base, expression));
  2931. case '.':
  2932. pushLocation(marker);
  2933. next();
  2934. identifier = parseIdentifier();
  2935. return finishNode(ast.memberExpression(base, '.', identifier));
  2936. case ':':
  2937. pushLocation(marker);
  2938. next();
  2939. identifier = parseIdentifier();
  2940. base = finishNode(ast.memberExpression(base, ':', identifier));
  2941. pushLocation(marker);
  2942. return parseCallExpression(base, flowContext);
  2943. case '(': case '{': // args
  2944. pushLocation(marker);
  2945. return parseCallExpression(base, flowContext);
  2946. }
  2947. } else if (StringLiteral === token.type) {
  2948. pushLocation(marker);
  2949. return parseCallExpression(base, flowContext);
  2950. }
  2951. return null;
  2952. }
  2953. function parsePrefixExpression(flowContext) {
  2954. var base, name, marker;
  2955. if (trackLocations) marker = createLocationMarker();
  2956. if (Identifier === token.type) {
  2957. name = token.value;
  2958. base = parseIdentifier();
  2959. if (options.scope) attachScope(base, scopeHasName(name));
  2960. } else if (consume('(')) {
  2961. base = parseExpectedExpression(flowContext);
  2962. expect(')');
  2963. } else {
  2964. return null;
  2965. }
  2966. for (;;) {
  2967. var newBase = parsePrefixExpressionPart(base, marker, flowContext);
  2968. if (newBase === null)
  2969. break;
  2970. base = newBase;
  2971. }
  2972. return base;
  2973. }
  2974. function parseCallExpression(base, flowContext) {
  2975. if (Punctuator === token.type) {
  2976. switch (token.value) {
  2977. case '(':
  2978. if (!features.emptyStatement) {
  2979. if (token.line !== previousToken.line)
  2980. raise(null, errors.ambiguousSyntax, token.value);
  2981. }
  2982. next();
  2983. var expressions = [];
  2984. var expression = parseExpression(flowContext);
  2985. if (null != expression) expressions.push(expression);
  2986. while (consume(',')) {
  2987. expression = parseExpectedExpression(flowContext);
  2988. expressions.push(expression);
  2989. }
  2990. expect(')');
  2991. return finishNode(ast.callExpression(base, expressions));
  2992. case '{':
  2993. markLocation();
  2994. next();
  2995. var table = parseTableConstructor(flowContext);
  2996. return finishNode(ast.tableCallExpression(base, table));
  2997. }
  2998. } else if (StringLiteral === token.type) {
  2999. return finishNode(ast.stringCallExpression(base, parsePrimaryExpression(flowContext)));
  3000. }
  3001. raiseUnexpectedToken('function arguments', token);
  3002. }
  3003. function parsePrimaryExpression(flowContext) {
  3004. var literals = StringLiteral | NumericLiteral | BooleanLiteral | NilLiteral | VarargLiteral
  3005. , value = token.value
  3006. , type = token.type
  3007. , marker;
  3008. if (trackLocations) marker = createLocationMarker();
  3009. if (type === VarargLiteral && !flowContext.allowVararg) {
  3010. raise(token, errors.cannotUseVararg, token.value);
  3011. }
  3012. if (type & literals) {
  3013. pushLocation(marker);
  3014. var raw = input.slice(token.range[0], token.range[1]);
  3015. next();
  3016. return finishNode(ast.literal(type, value, raw));
  3017. } else if (Keyword === type && 'function' === value) {
  3018. pushLocation(marker);
  3019. next();
  3020. if (options.scope) createScope();
  3021. return parseFunctionDeclaration(null);
  3022. } else if (consume('{')) {
  3023. pushLocation(marker);
  3024. return parseTableConstructor(flowContext);
  3025. }
  3026. }
  3027. exports.parse = parse;
  3028. var versionFeatures = {
  3029. '5.1': {
  3030. },
  3031. '5.2': {
  3032. labels: true,
  3033. emptyStatement: true,
  3034. hexEscapes: true,
  3035. skipWhitespaceEscape: true,
  3036. strictEscapes: true,
  3037. relaxedBreak: true
  3038. },
  3039. '5.3': {
  3040. labels: true,
  3041. emptyStatement: true,
  3042. hexEscapes: true,
  3043. skipWhitespaceEscape: true,
  3044. strictEscapes: true,
  3045. unicodeEscapes: true,
  3046. bitwiseOperators: true,
  3047. integerDivision: true,
  3048. relaxedBreak: true
  3049. },
  3050. 'LuaJIT': {
  3051. labels: true,
  3052. contextualGoto: true,
  3053. hexEscapes: true,
  3054. skipWhitespaceEscape: true,
  3055. strictEscapes: true,
  3056. unicodeEscapes: true,
  3057. imaginaryNumbers: true,
  3058. integerSuffixes: true
  3059. }
  3060. };
  3061. function parse(_input, _options) {
  3062. if ('undefined' === typeof _options && 'object' === typeof _input) {
  3063. _options = _input;
  3064. _input = undefined;
  3065. }
  3066. if (!_options) _options = {};
  3067. input = _input || '';
  3068. options = assign({}, defaultOptions, _options);
  3069. index = 0;
  3070. line = 1;
  3071. lineStart = 0;
  3072. length = input.length;
  3073. scopes = [[]];
  3074. scopeDepth = 0;
  3075. globals = [];
  3076. locations = [];
  3077. if (!Object.prototype.hasOwnProperty.call(versionFeatures, options.luaVersion)) {
  3078. throw new Error(sprintf("Lua version '%1' not supported", options.luaVersion));
  3079. }
  3080. features = assign({}, versionFeatures[options.luaVersion]);
  3081. if (options.extendedIdentifiers !== void 0)
  3082. features.extendedIdentifiers = !!options.extendedIdentifiers;
  3083. if (!Object.prototype.hasOwnProperty.call(encodingModes, options.encodingMode)) {
  3084. throw new Error(sprintf("Encoding mode '%1' not supported", options.encodingMode));
  3085. }
  3086. encodingMode = encodingModes[options.encodingMode];
  3087. if (options.comments) comments = [];
  3088. if (!options.wait) return end();
  3089. return exports;
  3090. }
  3091. exports.write = write;
  3092. function write(_input) {
  3093. input += String(_input);
  3094. length = input.length;
  3095. return exports;
  3096. }
  3097. exports.end = end;
  3098. function end(_input) {
  3099. if ('undefined' !== typeof _input) write(_input);
  3100. if (input && input.substr(0, 2) === '#!') input = input.replace(/^.*/, function (line) {
  3101. return line.replace(/./g, ' ');
  3102. });
  3103. length = input.length;
  3104. trackLocations = options.locations || options.ranges;
  3105. lookahead = lex();
  3106. var chunk = parseChunk();
  3107. if (options.comments) chunk.comments = comments;
  3108. if (options.scope) chunk.globals = globals;
  3109. if (locations.length > 0)
  3110. throw new Error('Location tracking failed. This is most likely a bug in luaparse');
  3111. return chunk;
  3112. }
  3113. }));
  3114. });
  3115. define("ace/mode/lua_worker",[], function(require, exports, module) {
  3116. "use strict";
  3117. var oop = require("../lib/oop");
  3118. var Mirror = require("../worker/mirror").Mirror;
  3119. var luaparse = require("../mode/lua/luaparse");
  3120. var Worker = exports.Worker = function(sender) {
  3121. Mirror.call(this, sender);
  3122. this.setTimeout(500);
  3123. };
  3124. oop.inherits(Worker, Mirror);
  3125. (function() {
  3126. this.onUpdate = function() {
  3127. var value = this.doc.getValue();
  3128. var errors = [];
  3129. try {
  3130. luaparse.parse(value);
  3131. } catch(e) {
  3132. if (e instanceof luaparse.SyntaxError) {
  3133. errors.push({
  3134. row: e.line - 1,
  3135. column: e.column,
  3136. text: e.message,
  3137. type: "error"
  3138. });
  3139. }
  3140. }
  3141. this.sender.emit("annotate", errors);
  3142. };
  3143. }).call(Worker.prototype);
  3144. });