worker-lua.js 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632
  1. "no use strict";
  2. ;(function(window) {
  3. if (typeof window.window != "undefined" && window.document)
  4. return;
  5. if (window.require && window.define)
  6. return;
  7. if (!window.console) {
  8. window.console = function() {
  9. var msgs = Array.prototype.slice.call(arguments, 0);
  10. postMessage({type: "log", data: msgs});
  11. };
  12. window.console.error =
  13. window.console.warn =
  14. window.console.log =
  15. window.console.trace = window.console;
  16. }
  17. window.window = window;
  18. window.ace = window;
  19. window.onerror = function(message, file, line, col, err) {
  20. postMessage({type: "error", data: {
  21. message: message,
  22. data: err.data,
  23. file: file,
  24. line: line,
  25. col: col,
  26. stack: err.stack
  27. }});
  28. };
  29. window.normalizeModule = function(parentId, moduleName) {
  30. // normalize plugin requires
  31. if (moduleName.indexOf("!") !== -1) {
  32. var chunks = moduleName.split("!");
  33. return window.normalizeModule(parentId, chunks[0]) + "!" + window.normalizeModule(parentId, chunks[1]);
  34. }
  35. // normalize relative requires
  36. if (moduleName.charAt(0) == ".") {
  37. var base = parentId.split("/").slice(0, -1).join("/");
  38. moduleName = (base ? base + "/" : "") + moduleName;
  39. while (moduleName.indexOf(".") !== -1 && previous != moduleName) {
  40. var previous = moduleName;
  41. moduleName = moduleName.replace(/^\.\//, "").replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, "");
  42. }
  43. }
  44. return moduleName;
  45. };
  46. window.require = function require(parentId, id) {
  47. if (!id) {
  48. id = parentId;
  49. parentId = null;
  50. }
  51. if (!id.charAt)
  52. throw new Error("worker.js require() accepts only (parentId, id) as arguments");
  53. id = window.normalizeModule(parentId, id);
  54. var module = window.require.modules[id];
  55. if (module) {
  56. if (!module.initialized) {
  57. module.initialized = true;
  58. module.exports = module.factory().exports;
  59. }
  60. return module.exports;
  61. }
  62. if (!window.require.tlns)
  63. return console.log("unable to load " + id);
  64. var path = resolveModuleId(id, window.require.tlns);
  65. if (path.slice(-3) != ".js") path += ".js";
  66. window.require.id = id;
  67. window.require.modules[id] = {}; // prevent infinite loop on broken modules
  68. importScripts(path);
  69. return window.require(parentId, id);
  70. };
  71. function resolveModuleId(id, paths) {
  72. var testPath = id, tail = "";
  73. while (testPath) {
  74. var alias = paths[testPath];
  75. if (typeof alias == "string") {
  76. return alias + tail;
  77. } else if (alias) {
  78. return alias.location.replace(/\/*$/, "/") + (tail || alias.main || alias.name);
  79. } else if (alias === false) {
  80. return "";
  81. }
  82. var i = testPath.lastIndexOf("/");
  83. if (i === -1) break;
  84. tail = testPath.substr(i) + tail;
  85. testPath = testPath.slice(0, i);
  86. }
  87. return id;
  88. }
  89. window.require.modules = {};
  90. window.require.tlns = {};
  91. window.define = function(id, deps, factory) {
  92. if (arguments.length == 2) {
  93. factory = deps;
  94. if (typeof id != "string") {
  95. deps = id;
  96. id = window.require.id;
  97. }
  98. } else if (arguments.length == 1) {
  99. factory = id;
  100. deps = [];
  101. id = window.require.id;
  102. }
  103. if (typeof factory != "function") {
  104. window.require.modules[id] = {
  105. exports: factory,
  106. initialized: true
  107. };
  108. return;
  109. }
  110. if (!deps.length)
  111. // If there is no dependencies, we inject "require", "exports" and
  112. // "module" as dependencies, to provide CommonJS compatibility.
  113. deps = ["require", "exports", "module"];
  114. var req = function(childId) {
  115. return window.require(id, childId);
  116. };
  117. window.require.modules[id] = {
  118. exports: {},
  119. factory: function() {
  120. var module = this;
  121. var returnExports = factory.apply(this, deps.map(function(dep) {
  122. switch (dep) {
  123. // Because "require", "exports" and "module" aren't actual
  124. // dependencies, we must handle them seperately.
  125. case "require": return req;
  126. case "exports": return module.exports;
  127. case "module": return module;
  128. // But for all other dependencies, we can just go ahead and
  129. // require them.
  130. default: return req(dep);
  131. }
  132. }));
  133. if (returnExports)
  134. module.exports = returnExports;
  135. return module;
  136. }
  137. };
  138. };
  139. window.define.amd = {};
  140. require.tlns = {};
  141. window.initBaseUrls = function initBaseUrls(topLevelNamespaces) {
  142. for (var i in topLevelNamespaces)
  143. require.tlns[i] = topLevelNamespaces[i];
  144. };
  145. window.initSender = function initSender() {
  146. var EventEmitter = window.require("ace/lib/event_emitter").EventEmitter;
  147. var oop = window.require("ace/lib/oop");
  148. var Sender = function() {};
  149. (function() {
  150. oop.implement(this, EventEmitter);
  151. this.callback = function(data, callbackId) {
  152. postMessage({
  153. type: "call",
  154. id: callbackId,
  155. data: data
  156. });
  157. };
  158. this.emit = function(name, data) {
  159. postMessage({
  160. type: "event",
  161. name: name,
  162. data: data
  163. });
  164. };
  165. }).call(Sender.prototype);
  166. return new Sender();
  167. };
  168. var main = window.main = null;
  169. var sender = window.sender = null;
  170. window.onmessage = function(e) {
  171. var msg = e.data;
  172. if (msg.event && sender) {
  173. sender._signal(msg.event, msg.data);
  174. }
  175. else if (msg.command) {
  176. if (main[msg.command])
  177. main[msg.command].apply(main, msg.args);
  178. else if (window[msg.command])
  179. window[msg.command].apply(window, msg.args);
  180. else
  181. throw new Error("Unknown command:" + msg.command);
  182. }
  183. else if (msg.init) {
  184. window.initBaseUrls(msg.tlns);
  185. require("ace/lib/es5-shim");
  186. sender = window.sender = window.initSender();
  187. var clazz = require(msg.module)[msg.classname];
  188. main = window.main = new clazz(sender);
  189. }
  190. };
  191. })(this);
  192. ace.define("ace/lib/oop",["require","exports","module"], function(require, exports, module) {
  193. "use strict";
  194. exports.inherits = function(ctor, superCtor) {
  195. ctor.super_ = superCtor;
  196. ctor.prototype = Object.create(superCtor.prototype, {
  197. constructor: {
  198. value: ctor,
  199. enumerable: false,
  200. writable: true,
  201. configurable: true
  202. }
  203. });
  204. };
  205. exports.mixin = function(obj, mixin) {
  206. for (var key in mixin) {
  207. obj[key] = mixin[key];
  208. }
  209. return obj;
  210. };
  211. exports.implement = function(proto, mixin) {
  212. exports.mixin(proto, mixin);
  213. };
  214. });
  215. ace.define("ace/range",["require","exports","module"], function(require, exports, module) {
  216. "use strict";
  217. var comparePoints = function(p1, p2) {
  218. return p1.row - p2.row || p1.column - p2.column;
  219. };
  220. var Range = function(startRow, startColumn, endRow, endColumn) {
  221. this.start = {
  222. row: startRow,
  223. column: startColumn
  224. };
  225. this.end = {
  226. row: endRow,
  227. column: endColumn
  228. };
  229. };
  230. (function() {
  231. this.isEqual = function(range) {
  232. return this.start.row === range.start.row &&
  233. this.end.row === range.end.row &&
  234. this.start.column === range.start.column &&
  235. this.end.column === range.end.column;
  236. };
  237. this.toString = function() {
  238. return ("Range: [" + this.start.row + "/" + this.start.column +
  239. "] -> [" + this.end.row + "/" + this.end.column + "]");
  240. };
  241. this.contains = function(row, column) {
  242. return this.compare(row, column) == 0;
  243. };
  244. this.compareRange = function(range) {
  245. var cmp,
  246. end = range.end,
  247. start = range.start;
  248. cmp = this.compare(end.row, end.column);
  249. if (cmp == 1) {
  250. cmp = this.compare(start.row, start.column);
  251. if (cmp == 1) {
  252. return 2;
  253. } else if (cmp == 0) {
  254. return 1;
  255. } else {
  256. return 0;
  257. }
  258. } else if (cmp == -1) {
  259. return -2;
  260. } else {
  261. cmp = this.compare(start.row, start.column);
  262. if (cmp == -1) {
  263. return -1;
  264. } else if (cmp == 1) {
  265. return 42;
  266. } else {
  267. return 0;
  268. }
  269. }
  270. };
  271. this.comparePoint = function(p) {
  272. return this.compare(p.row, p.column);
  273. };
  274. this.containsRange = function(range) {
  275. return this.comparePoint(range.start) == 0 && this.comparePoint(range.end) == 0;
  276. };
  277. this.intersects = function(range) {
  278. var cmp = this.compareRange(range);
  279. return (cmp == -1 || cmp == 0 || cmp == 1);
  280. };
  281. this.isEnd = function(row, column) {
  282. return this.end.row == row && this.end.column == column;
  283. };
  284. this.isStart = function(row, column) {
  285. return this.start.row == row && this.start.column == column;
  286. };
  287. this.setStart = function(row, column) {
  288. if (typeof row == "object") {
  289. this.start.column = row.column;
  290. this.start.row = row.row;
  291. } else {
  292. this.start.row = row;
  293. this.start.column = column;
  294. }
  295. };
  296. this.setEnd = function(row, column) {
  297. if (typeof row == "object") {
  298. this.end.column = row.column;
  299. this.end.row = row.row;
  300. } else {
  301. this.end.row = row;
  302. this.end.column = column;
  303. }
  304. };
  305. this.inside = function(row, column) {
  306. if (this.compare(row, column) == 0) {
  307. if (this.isEnd(row, column) || this.isStart(row, column)) {
  308. return false;
  309. } else {
  310. return true;
  311. }
  312. }
  313. return false;
  314. };
  315. this.insideStart = function(row, column) {
  316. if (this.compare(row, column) == 0) {
  317. if (this.isEnd(row, column)) {
  318. return false;
  319. } else {
  320. return true;
  321. }
  322. }
  323. return false;
  324. };
  325. this.insideEnd = function(row, column) {
  326. if (this.compare(row, column) == 0) {
  327. if (this.isStart(row, column)) {
  328. return false;
  329. } else {
  330. return true;
  331. }
  332. }
  333. return false;
  334. };
  335. this.compare = function(row, column) {
  336. if (!this.isMultiLine()) {
  337. if (row === this.start.row) {
  338. return column < this.start.column ? -1 : (column > this.end.column ? 1 : 0);
  339. }
  340. }
  341. if (row < this.start.row)
  342. return -1;
  343. if (row > this.end.row)
  344. return 1;
  345. if (this.start.row === row)
  346. return column >= this.start.column ? 0 : -1;
  347. if (this.end.row === row)
  348. return column <= this.end.column ? 0 : 1;
  349. return 0;
  350. };
  351. this.compareStart = function(row, column) {
  352. if (this.start.row == row && this.start.column == column) {
  353. return -1;
  354. } else {
  355. return this.compare(row, column);
  356. }
  357. };
  358. this.compareEnd = function(row, column) {
  359. if (this.end.row == row && this.end.column == column) {
  360. return 1;
  361. } else {
  362. return this.compare(row, column);
  363. }
  364. };
  365. this.compareInside = function(row, column) {
  366. if (this.end.row == row && this.end.column == column) {
  367. return 1;
  368. } else if (this.start.row == row && this.start.column == column) {
  369. return -1;
  370. } else {
  371. return this.compare(row, column);
  372. }
  373. };
  374. this.clipRows = function(firstRow, lastRow) {
  375. if (this.end.row > lastRow)
  376. var end = {row: lastRow + 1, column: 0};
  377. else if (this.end.row < firstRow)
  378. var end = {row: firstRow, column: 0};
  379. if (this.start.row > lastRow)
  380. var start = {row: lastRow + 1, column: 0};
  381. else if (this.start.row < firstRow)
  382. var start = {row: firstRow, column: 0};
  383. return Range.fromPoints(start || this.start, end || this.end);
  384. };
  385. this.extend = function(row, column) {
  386. var cmp = this.compare(row, column);
  387. if (cmp == 0)
  388. return this;
  389. else if (cmp == -1)
  390. var start = {row: row, column: column};
  391. else
  392. var end = {row: row, column: column};
  393. return Range.fromPoints(start || this.start, end || this.end);
  394. };
  395. this.isEmpty = function() {
  396. return (this.start.row === this.end.row && this.start.column === this.end.column);
  397. };
  398. this.isMultiLine = function() {
  399. return (this.start.row !== this.end.row);
  400. };
  401. this.clone = function() {
  402. return Range.fromPoints(this.start, this.end);
  403. };
  404. this.collapseRows = function() {
  405. if (this.end.column == 0)
  406. return new Range(this.start.row, 0, Math.max(this.start.row, this.end.row-1), 0)
  407. else
  408. return new Range(this.start.row, 0, this.end.row, 0)
  409. };
  410. this.toScreenRange = function(session) {
  411. var screenPosStart = session.documentToScreenPosition(this.start);
  412. var screenPosEnd = session.documentToScreenPosition(this.end);
  413. return new Range(
  414. screenPosStart.row, screenPosStart.column,
  415. screenPosEnd.row, screenPosEnd.column
  416. );
  417. };
  418. this.moveBy = function(row, column) {
  419. this.start.row += row;
  420. this.start.column += column;
  421. this.end.row += row;
  422. this.end.column += column;
  423. };
  424. }).call(Range.prototype);
  425. Range.fromPoints = function(start, end) {
  426. return new Range(start.row, start.column, end.row, end.column);
  427. };
  428. Range.comparePoints = comparePoints;
  429. Range.comparePoints = function(p1, p2) {
  430. return p1.row - p2.row || p1.column - p2.column;
  431. };
  432. exports.Range = Range;
  433. });
  434. ace.define("ace/apply_delta",["require","exports","module"], function(require, exports, module) {
  435. "use strict";
  436. function throwDeltaError(delta, errorText){
  437. console.log("Invalid Delta:", delta);
  438. throw "Invalid Delta: " + errorText;
  439. }
  440. function positionInDocument(docLines, position) {
  441. return position.row >= 0 && position.row < docLines.length &&
  442. position.column >= 0 && position.column <= docLines[position.row].length;
  443. }
  444. function validateDelta(docLines, delta) {
  445. if (delta.action != "insert" && delta.action != "remove")
  446. throwDeltaError(delta, "delta.action must be 'insert' or 'remove'");
  447. if (!(delta.lines instanceof Array))
  448. throwDeltaError(delta, "delta.lines must be an Array");
  449. if (!delta.start || !delta.end)
  450. throwDeltaError(delta, "delta.start/end must be an present");
  451. var start = delta.start;
  452. if (!positionInDocument(docLines, delta.start))
  453. throwDeltaError(delta, "delta.start must be contained in document");
  454. var end = delta.end;
  455. if (delta.action == "remove" && !positionInDocument(docLines, end))
  456. throwDeltaError(delta, "delta.end must contained in document for 'remove' actions");
  457. var numRangeRows = end.row - start.row;
  458. var numRangeLastLineChars = (end.column - (numRangeRows == 0 ? start.column : 0));
  459. if (numRangeRows != delta.lines.length - 1 || delta.lines[numRangeRows].length != numRangeLastLineChars)
  460. throwDeltaError(delta, "delta.range must match delta lines");
  461. }
  462. exports.applyDelta = function(docLines, delta, doNotValidate) {
  463. var row = delta.start.row;
  464. var startColumn = delta.start.column;
  465. var line = docLines[row] || "";
  466. switch (delta.action) {
  467. case "insert":
  468. var lines = delta.lines;
  469. if (lines.length === 1) {
  470. docLines[row] = line.substring(0, startColumn) + delta.lines[0] + line.substring(startColumn);
  471. } else {
  472. var args = [row, 1].concat(delta.lines);
  473. docLines.splice.apply(docLines, args);
  474. docLines[row] = line.substring(0, startColumn) + docLines[row];
  475. docLines[row + delta.lines.length - 1] += line.substring(startColumn);
  476. }
  477. break;
  478. case "remove":
  479. var endColumn = delta.end.column;
  480. var endRow = delta.end.row;
  481. if (row === endRow) {
  482. docLines[row] = line.substring(0, startColumn) + line.substring(endColumn);
  483. } else {
  484. docLines.splice(
  485. row, endRow - row + 1,
  486. line.substring(0, startColumn) + docLines[endRow].substring(endColumn)
  487. );
  488. }
  489. break;
  490. }
  491. }
  492. });
  493. ace.define("ace/lib/event_emitter",["require","exports","module"], function(require, exports, module) {
  494. "use strict";
  495. var EventEmitter = {};
  496. var stopPropagation = function() { this.propagationStopped = true; };
  497. var preventDefault = function() { this.defaultPrevented = true; };
  498. EventEmitter._emit =
  499. EventEmitter._dispatchEvent = function(eventName, e) {
  500. this._eventRegistry || (this._eventRegistry = {});
  501. this._defaultHandlers || (this._defaultHandlers = {});
  502. var listeners = this._eventRegistry[eventName] || [];
  503. var defaultHandler = this._defaultHandlers[eventName];
  504. if (!listeners.length && !defaultHandler)
  505. return;
  506. if (typeof e != "object" || !e)
  507. e = {};
  508. if (!e.type)
  509. e.type = eventName;
  510. if (!e.stopPropagation)
  511. e.stopPropagation = stopPropagation;
  512. if (!e.preventDefault)
  513. e.preventDefault = preventDefault;
  514. listeners = listeners.slice();
  515. for (var i=0; i<listeners.length; i++) {
  516. listeners[i](e, this);
  517. if (e.propagationStopped)
  518. break;
  519. }
  520. if (defaultHandler && !e.defaultPrevented)
  521. return defaultHandler(e, this);
  522. };
  523. EventEmitter._signal = function(eventName, e) {
  524. var listeners = (this._eventRegistry || {})[eventName];
  525. if (!listeners)
  526. return;
  527. listeners = listeners.slice();
  528. for (var i=0; i<listeners.length; i++)
  529. listeners[i](e, this);
  530. };
  531. EventEmitter.once = function(eventName, callback) {
  532. var _self = this;
  533. callback && this.addEventListener(eventName, function newCallback() {
  534. _self.removeEventListener(eventName, newCallback);
  535. callback.apply(null, arguments);
  536. });
  537. };
  538. EventEmitter.setDefaultHandler = function(eventName, callback) {
  539. var handlers = this._defaultHandlers
  540. if (!handlers)
  541. handlers = this._defaultHandlers = {_disabled_: {}};
  542. if (handlers[eventName]) {
  543. var old = handlers[eventName];
  544. var disabled = handlers._disabled_[eventName];
  545. if (!disabled)
  546. handlers._disabled_[eventName] = disabled = [];
  547. disabled.push(old);
  548. var i = disabled.indexOf(callback);
  549. if (i != -1)
  550. disabled.splice(i, 1);
  551. }
  552. handlers[eventName] = callback;
  553. };
  554. EventEmitter.removeDefaultHandler = function(eventName, callback) {
  555. var handlers = this._defaultHandlers
  556. if (!handlers)
  557. return;
  558. var disabled = handlers._disabled_[eventName];
  559. if (handlers[eventName] == callback) {
  560. var old = handlers[eventName];
  561. if (disabled)
  562. this.setDefaultHandler(eventName, disabled.pop());
  563. } else if (disabled) {
  564. var i = disabled.indexOf(callback);
  565. if (i != -1)
  566. disabled.splice(i, 1);
  567. }
  568. };
  569. EventEmitter.on =
  570. EventEmitter.addEventListener = function(eventName, callback, capturing) {
  571. this._eventRegistry = this._eventRegistry || {};
  572. var listeners = this._eventRegistry[eventName];
  573. if (!listeners)
  574. listeners = this._eventRegistry[eventName] = [];
  575. if (listeners.indexOf(callback) == -1)
  576. listeners[capturing ? "unshift" : "push"](callback);
  577. return callback;
  578. };
  579. EventEmitter.off =
  580. EventEmitter.removeListener =
  581. EventEmitter.removeEventListener = function(eventName, callback) {
  582. this._eventRegistry = this._eventRegistry || {};
  583. var listeners = this._eventRegistry[eventName];
  584. if (!listeners)
  585. return;
  586. var index = listeners.indexOf(callback);
  587. if (index !== -1)
  588. listeners.splice(index, 1);
  589. };
  590. EventEmitter.removeAllListeners = function(eventName) {
  591. if (this._eventRegistry) this._eventRegistry[eventName] = [];
  592. };
  593. exports.EventEmitter = EventEmitter;
  594. });
  595. ace.define("ace/anchor",["require","exports","module","ace/lib/oop","ace/lib/event_emitter"], function(require, exports, module) {
  596. "use strict";
  597. var oop = require("./lib/oop");
  598. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  599. var Anchor = exports.Anchor = function(doc, row, column) {
  600. this.$onChange = this.onChange.bind(this);
  601. this.attach(doc);
  602. if (typeof column == "undefined")
  603. this.setPosition(row.row, row.column);
  604. else
  605. this.setPosition(row, column);
  606. };
  607. (function() {
  608. oop.implement(this, EventEmitter);
  609. this.getPosition = function() {
  610. return this.$clipPositionToDocument(this.row, this.column);
  611. };
  612. this.getDocument = function() {
  613. return this.document;
  614. };
  615. this.$insertRight = false;
  616. this.onChange = function(delta) {
  617. if (delta.start.row == delta.end.row && delta.start.row != this.row)
  618. return;
  619. if (delta.start.row > this.row)
  620. return;
  621. var point = $getTransformedPoint(delta, {row: this.row, column: this.column}, this.$insertRight);
  622. this.setPosition(point.row, point.column, true);
  623. };
  624. function $pointsInOrder(point1, point2, equalPointsInOrder) {
  625. var bColIsAfter = equalPointsInOrder ? point1.column <= point2.column : point1.column < point2.column;
  626. return (point1.row < point2.row) || (point1.row == point2.row && bColIsAfter);
  627. }
  628. function $getTransformedPoint(delta, point, moveIfEqual) {
  629. var deltaIsInsert = delta.action == "insert";
  630. var deltaRowShift = (deltaIsInsert ? 1 : -1) * (delta.end.row - delta.start.row);
  631. var deltaColShift = (deltaIsInsert ? 1 : -1) * (delta.end.column - delta.start.column);
  632. var deltaStart = delta.start;
  633. var deltaEnd = deltaIsInsert ? deltaStart : delta.end; // Collapse insert range.
  634. if ($pointsInOrder(point, deltaStart, moveIfEqual)) {
  635. return {
  636. row: point.row,
  637. column: point.column
  638. };
  639. }
  640. if ($pointsInOrder(deltaEnd, point, !moveIfEqual)) {
  641. return {
  642. row: point.row + deltaRowShift,
  643. column: point.column + (point.row == deltaEnd.row ? deltaColShift : 0)
  644. };
  645. }
  646. return {
  647. row: deltaStart.row,
  648. column: deltaStart.column
  649. };
  650. }
  651. this.setPosition = function(row, column, noClip) {
  652. var pos;
  653. if (noClip) {
  654. pos = {
  655. row: row,
  656. column: column
  657. };
  658. } else {
  659. pos = this.$clipPositionToDocument(row, column);
  660. }
  661. if (this.row == pos.row && this.column == pos.column)
  662. return;
  663. var old = {
  664. row: this.row,
  665. column: this.column
  666. };
  667. this.row = pos.row;
  668. this.column = pos.column;
  669. this._signal("change", {
  670. old: old,
  671. value: pos
  672. });
  673. };
  674. this.detach = function() {
  675. this.document.removeEventListener("change", this.$onChange);
  676. };
  677. this.attach = function(doc) {
  678. this.document = doc || this.document;
  679. this.document.on("change", this.$onChange);
  680. };
  681. this.$clipPositionToDocument = function(row, column) {
  682. var pos = {};
  683. if (row >= this.document.getLength()) {
  684. pos.row = Math.max(0, this.document.getLength() - 1);
  685. pos.column = this.document.getLine(pos.row).length;
  686. }
  687. else if (row < 0) {
  688. pos.row = 0;
  689. pos.column = 0;
  690. }
  691. else {
  692. pos.row = row;
  693. pos.column = Math.min(this.document.getLine(pos.row).length, Math.max(0, column));
  694. }
  695. if (column < 0)
  696. pos.column = 0;
  697. return pos;
  698. };
  699. }).call(Anchor.prototype);
  700. });
  701. 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) {
  702. "use strict";
  703. var oop = require("./lib/oop");
  704. var applyDelta = require("./apply_delta").applyDelta;
  705. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  706. var Range = require("./range").Range;
  707. var Anchor = require("./anchor").Anchor;
  708. var Document = function(textOrLines) {
  709. this.$lines = [""];
  710. if (textOrLines.length === 0) {
  711. this.$lines = [""];
  712. } else if (Array.isArray(textOrLines)) {
  713. this.insertMergedLines({row: 0, column: 0}, textOrLines);
  714. } else {
  715. this.insert({row: 0, column:0}, textOrLines);
  716. }
  717. };
  718. (function() {
  719. oop.implement(this, EventEmitter);
  720. this.setValue = function(text) {
  721. var len = this.getLength() - 1;
  722. this.remove(new Range(0, 0, len, this.getLine(len).length));
  723. this.insert({row: 0, column: 0}, text);
  724. };
  725. this.getValue = function() {
  726. return this.getAllLines().join(this.getNewLineCharacter());
  727. };
  728. this.createAnchor = function(row, column) {
  729. return new Anchor(this, row, column);
  730. };
  731. if ("aaa".split(/a/).length === 0) {
  732. this.$split = function(text) {
  733. return text.replace(/\r\n|\r/g, "\n").split("\n");
  734. };
  735. } else {
  736. this.$split = function(text) {
  737. return text.split(/\r\n|\r|\n/);
  738. };
  739. }
  740. this.$detectNewLine = function(text) {
  741. var match = text.match(/^.*?(\r\n|\r|\n)/m);
  742. this.$autoNewLine = match ? match[1] : "\n";
  743. this._signal("changeNewLineMode");
  744. };
  745. this.getNewLineCharacter = function() {
  746. switch (this.$newLineMode) {
  747. case "windows":
  748. return "\r\n";
  749. case "unix":
  750. return "\n";
  751. default:
  752. return this.$autoNewLine || "\n";
  753. }
  754. };
  755. this.$autoNewLine = "";
  756. this.$newLineMode = "auto";
  757. this.setNewLineMode = function(newLineMode) {
  758. if (this.$newLineMode === newLineMode)
  759. return;
  760. this.$newLineMode = newLineMode;
  761. this._signal("changeNewLineMode");
  762. };
  763. this.getNewLineMode = function() {
  764. return this.$newLineMode;
  765. };
  766. this.isNewLine = function(text) {
  767. return (text == "\r\n" || text == "\r" || text == "\n");
  768. };
  769. this.getLine = function(row) {
  770. return this.$lines[row] || "";
  771. };
  772. this.getLines = function(firstRow, lastRow) {
  773. return this.$lines.slice(firstRow, lastRow + 1);
  774. };
  775. this.getAllLines = function() {
  776. return this.getLines(0, this.getLength());
  777. };
  778. this.getLength = function() {
  779. return this.$lines.length;
  780. };
  781. this.getTextRange = function(range) {
  782. return this.getLinesForRange(range).join(this.getNewLineCharacter());
  783. };
  784. this.getLinesForRange = function(range) {
  785. var lines;
  786. if (range.start.row === range.end.row) {
  787. lines = [this.getLine(range.start.row).substring(range.start.column, range.end.column)];
  788. } else {
  789. lines = this.getLines(range.start.row, range.end.row);
  790. lines[0] = (lines[0] || "").substring(range.start.column);
  791. var l = lines.length - 1;
  792. if (range.end.row - range.start.row == l)
  793. lines[l] = lines[l].substring(0, range.end.column);
  794. }
  795. return lines;
  796. };
  797. this.insertLines = function(row, lines) {
  798. console.warn("Use of document.insertLines is deprecated. Use the insertFullLines method instead.");
  799. return this.insertFullLines(row, lines);
  800. };
  801. this.removeLines = function(firstRow, lastRow) {
  802. console.warn("Use of document.removeLines is deprecated. Use the removeFullLines method instead.");
  803. return this.removeFullLines(firstRow, lastRow);
  804. };
  805. this.insertNewLine = function(position) {
  806. console.warn("Use of document.insertNewLine is deprecated. Use insertMergedLines(position, ['', '']) instead.");
  807. return this.insertMergedLines(position, ["", ""]);
  808. };
  809. this.insert = function(position, text) {
  810. if (this.getLength() <= 1)
  811. this.$detectNewLine(text);
  812. return this.insertMergedLines(position, this.$split(text));
  813. };
  814. this.insertInLine = function(position, text) {
  815. var start = this.clippedPos(position.row, position.column);
  816. var end = this.pos(position.row, position.column + text.length);
  817. this.applyDelta({
  818. start: start,
  819. end: end,
  820. action: "insert",
  821. lines: [text]
  822. }, true);
  823. return this.clonePos(end);
  824. };
  825. this.clippedPos = function(row, column) {
  826. var length = this.getLength();
  827. if (row === undefined) {
  828. row = length;
  829. } else if (row < 0) {
  830. row = 0;
  831. } else if (row >= length) {
  832. row = length - 1;
  833. column = undefined;
  834. }
  835. var line = this.getLine(row);
  836. if (column == undefined)
  837. column = line.length;
  838. column = Math.min(Math.max(column, 0), line.length);
  839. return {row: row, column: column};
  840. };
  841. this.clonePos = function(pos) {
  842. return {row: pos.row, column: pos.column};
  843. };
  844. this.pos = function(row, column) {
  845. return {row: row, column: column};
  846. };
  847. this.$clipPosition = function(position) {
  848. var length = this.getLength();
  849. if (position.row >= length) {
  850. position.row = Math.max(0, length - 1);
  851. position.column = this.getLine(length - 1).length;
  852. } else {
  853. position.row = Math.max(0, position.row);
  854. position.column = Math.min(Math.max(position.column, 0), this.getLine(position.row).length);
  855. }
  856. return position;
  857. };
  858. this.insertFullLines = function(row, lines) {
  859. row = Math.min(Math.max(row, 0), this.getLength());
  860. var column = 0;
  861. if (row < this.getLength()) {
  862. lines = lines.concat([""]);
  863. column = 0;
  864. } else {
  865. lines = [""].concat(lines);
  866. row--;
  867. column = this.$lines[row].length;
  868. }
  869. this.insertMergedLines({row: row, column: column}, lines);
  870. };
  871. this.insertMergedLines = function(position, lines) {
  872. var start = this.clippedPos(position.row, position.column);
  873. var end = {
  874. row: start.row + lines.length - 1,
  875. column: (lines.length == 1 ? start.column : 0) + lines[lines.length - 1].length
  876. };
  877. this.applyDelta({
  878. start: start,
  879. end: end,
  880. action: "insert",
  881. lines: lines
  882. });
  883. return this.clonePos(end);
  884. };
  885. this.remove = function(range) {
  886. var start = this.clippedPos(range.start.row, range.start.column);
  887. var end = this.clippedPos(range.end.row, range.end.column);
  888. this.applyDelta({
  889. start: start,
  890. end: end,
  891. action: "remove",
  892. lines: this.getLinesForRange({start: start, end: end})
  893. });
  894. return this.clonePos(start);
  895. };
  896. this.removeInLine = function(row, startColumn, endColumn) {
  897. var start = this.clippedPos(row, startColumn);
  898. var end = this.clippedPos(row, endColumn);
  899. this.applyDelta({
  900. start: start,
  901. end: end,
  902. action: "remove",
  903. lines: this.getLinesForRange({start: start, end: end})
  904. }, true);
  905. return this.clonePos(start);
  906. };
  907. this.removeFullLines = function(firstRow, lastRow) {
  908. firstRow = Math.min(Math.max(0, firstRow), this.getLength() - 1);
  909. lastRow = Math.min(Math.max(0, lastRow ), this.getLength() - 1);
  910. var deleteFirstNewLine = lastRow == this.getLength() - 1 && firstRow > 0;
  911. var deleteLastNewLine = lastRow < this.getLength() - 1;
  912. var startRow = ( deleteFirstNewLine ? firstRow - 1 : firstRow );
  913. var startCol = ( deleteFirstNewLine ? this.getLine(startRow).length : 0 );
  914. var endRow = ( deleteLastNewLine ? lastRow + 1 : lastRow );
  915. var endCol = ( deleteLastNewLine ? 0 : this.getLine(endRow).length );
  916. var range = new Range(startRow, startCol, endRow, endCol);
  917. var deletedLines = this.$lines.slice(firstRow, lastRow + 1);
  918. this.applyDelta({
  919. start: range.start,
  920. end: range.end,
  921. action: "remove",
  922. lines: this.getLinesForRange(range)
  923. });
  924. return deletedLines;
  925. };
  926. this.removeNewLine = function(row) {
  927. if (row < this.getLength() - 1 && row >= 0) {
  928. this.applyDelta({
  929. start: this.pos(row, this.getLine(row).length),
  930. end: this.pos(row + 1, 0),
  931. action: "remove",
  932. lines: ["", ""]
  933. });
  934. }
  935. };
  936. this.replace = function(range, text) {
  937. if (!(range instanceof Range))
  938. range = Range.fromPoints(range.start, range.end);
  939. if (text.length === 0 && range.isEmpty())
  940. return range.start;
  941. if (text == this.getTextRange(range))
  942. return range.end;
  943. this.remove(range);
  944. var end;
  945. if (text) {
  946. end = this.insert(range.start, text);
  947. }
  948. else {
  949. end = range.start;
  950. }
  951. return end;
  952. };
  953. this.applyDeltas = function(deltas) {
  954. for (var i=0; i<deltas.length; i++) {
  955. this.applyDelta(deltas[i]);
  956. }
  957. };
  958. this.revertDeltas = function(deltas) {
  959. for (var i=deltas.length-1; i>=0; i--) {
  960. this.revertDelta(deltas[i]);
  961. }
  962. };
  963. this.applyDelta = function(delta, doNotValidate) {
  964. var isInsert = delta.action == "insert";
  965. if (isInsert ? delta.lines.length <= 1 && !delta.lines[0]
  966. : !Range.comparePoints(delta.start, delta.end)) {
  967. return;
  968. }
  969. if (isInsert && delta.lines.length > 20000)
  970. this.$splitAndapplyLargeDelta(delta, 20000);
  971. applyDelta(this.$lines, delta, doNotValidate);
  972. this._signal("change", delta);
  973. };
  974. this.$splitAndapplyLargeDelta = function(delta, MAX) {
  975. var lines = delta.lines;
  976. var l = lines.length;
  977. var row = delta.start.row;
  978. var column = delta.start.column;
  979. var from = 0, to = 0;
  980. do {
  981. from = to;
  982. to += MAX - 1;
  983. var chunk = lines.slice(from, to);
  984. if (to > l) {
  985. delta.lines = chunk;
  986. delta.start.row = row + from;
  987. delta.start.column = column;
  988. break;
  989. }
  990. chunk.push("");
  991. this.applyDelta({
  992. start: this.pos(row + from, column),
  993. end: this.pos(row + to, column = 0),
  994. action: delta.action,
  995. lines: chunk
  996. }, true);
  997. } while(true);
  998. };
  999. this.revertDelta = function(delta) {
  1000. this.applyDelta({
  1001. start: this.clonePos(delta.start),
  1002. end: this.clonePos(delta.end),
  1003. action: (delta.action == "insert" ? "remove" : "insert"),
  1004. lines: delta.lines.slice()
  1005. });
  1006. };
  1007. this.indexToPosition = function(index, startRow) {
  1008. var lines = this.$lines || this.getAllLines();
  1009. var newlineLength = this.getNewLineCharacter().length;
  1010. for (var i = startRow || 0, l = lines.length; i < l; i++) {
  1011. index -= lines[i].length + newlineLength;
  1012. if (index < 0)
  1013. return {row: i, column: index + lines[i].length + newlineLength};
  1014. }
  1015. return {row: l-1, column: lines[l-1].length};
  1016. };
  1017. this.positionToIndex = function(pos, startRow) {
  1018. var lines = this.$lines || this.getAllLines();
  1019. var newlineLength = this.getNewLineCharacter().length;
  1020. var index = 0;
  1021. var row = Math.min(pos.row, lines.length);
  1022. for (var i = startRow || 0; i < row; ++i)
  1023. index += lines[i].length + newlineLength;
  1024. return index + pos.column;
  1025. };
  1026. }).call(Document.prototype);
  1027. exports.Document = Document;
  1028. });
  1029. ace.define("ace/lib/lang",["require","exports","module"], function(require, exports, module) {
  1030. "use strict";
  1031. exports.last = function(a) {
  1032. return a[a.length - 1];
  1033. };
  1034. exports.stringReverse = function(string) {
  1035. return string.split("").reverse().join("");
  1036. };
  1037. exports.stringRepeat = function (string, count) {
  1038. var result = '';
  1039. while (count > 0) {
  1040. if (count & 1)
  1041. result += string;
  1042. if (count >>= 1)
  1043. string += string;
  1044. }
  1045. return result;
  1046. };
  1047. var trimBeginRegexp = /^\s\s*/;
  1048. var trimEndRegexp = /\s\s*$/;
  1049. exports.stringTrimLeft = function (string) {
  1050. return string.replace(trimBeginRegexp, '');
  1051. };
  1052. exports.stringTrimRight = function (string) {
  1053. return string.replace(trimEndRegexp, '');
  1054. };
  1055. exports.copyObject = function(obj) {
  1056. var copy = {};
  1057. for (var key in obj) {
  1058. copy[key] = obj[key];
  1059. }
  1060. return copy;
  1061. };
  1062. exports.copyArray = function(array){
  1063. var copy = [];
  1064. for (var i=0, l=array.length; i<l; i++) {
  1065. if (array[i] && typeof array[i] == "object")
  1066. copy[i] = this.copyObject(array[i]);
  1067. else
  1068. copy[i] = array[i];
  1069. }
  1070. return copy;
  1071. };
  1072. exports.deepCopy = function deepCopy(obj) {
  1073. if (typeof obj !== "object" || !obj)
  1074. return obj;
  1075. var copy;
  1076. if (Array.isArray(obj)) {
  1077. copy = [];
  1078. for (var key = 0; key < obj.length; key++) {
  1079. copy[key] = deepCopy(obj[key]);
  1080. }
  1081. return copy;
  1082. }
  1083. if (Object.prototype.toString.call(obj) !== "[object Object]")
  1084. return obj;
  1085. copy = {};
  1086. for (var key in obj)
  1087. copy[key] = deepCopy(obj[key]);
  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. });