worker-lua.js 103 KB

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