index.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. import { Parser, NodeSet, NodeType, DefaultBufferLength, NodeProp, Tree, IterMode } from '@lezer/common';
  2. /// A parse stack. These are used internally by the parser to track
  3. /// parsing progress. They also provide some properties and methods
  4. /// that external code such as a tokenizer can use to get information
  5. /// about the parse state.
  6. class Stack {
  7. /// @internal
  8. constructor(
  9. /// The parse that this stack is part of @internal
  10. p,
  11. /// Holds state, input pos, buffer index triplets for all but the
  12. /// top state @internal
  13. stack,
  14. /// The current parse state @internal
  15. state,
  16. // The position at which the next reduce should take place. This
  17. // can be less than `this.pos` when skipped expressions have been
  18. // added to the stack (which should be moved outside of the next
  19. // reduction)
  20. /// @internal
  21. reducePos,
  22. /// The input position up to which this stack has parsed.
  23. pos,
  24. /// The dynamic score of the stack, including dynamic precedence
  25. /// and error-recovery penalties
  26. /// @internal
  27. score,
  28. // The output buffer. Holds (type, start, end, size) quads
  29. // representing nodes created by the parser, where `size` is
  30. // amount of buffer array entries covered by this node.
  31. /// @internal
  32. buffer,
  33. // The base offset of the buffer. When stacks are split, the split
  34. // instance shared the buffer history with its parent up to
  35. // `bufferBase`, which is the absolute offset (including the
  36. // offset of previous splits) into the buffer at which this stack
  37. // starts writing.
  38. /// @internal
  39. bufferBase,
  40. /// @internal
  41. curContext,
  42. /// @internal
  43. lookAhead = 0,
  44. // A parent stack from which this was split off, if any. This is
  45. // set up so that it always points to a stack that has some
  46. // additional buffer content, never to a stack with an equal
  47. // `bufferBase`.
  48. /// @internal
  49. parent) {
  50. this.p = p;
  51. this.stack = stack;
  52. this.state = state;
  53. this.reducePos = reducePos;
  54. this.pos = pos;
  55. this.score = score;
  56. this.buffer = buffer;
  57. this.bufferBase = bufferBase;
  58. this.curContext = curContext;
  59. this.lookAhead = lookAhead;
  60. this.parent = parent;
  61. }
  62. /// @internal
  63. toString() {
  64. return `[${this.stack.filter((_, i) => i % 3 == 0).concat(this.state)}]@${this.pos}${this.score ? "!" + this.score : ""}`;
  65. }
  66. // Start an empty stack
  67. /// @internal
  68. static start(p, state, pos = 0) {
  69. let cx = p.parser.context;
  70. return new Stack(p, [], state, pos, pos, 0, [], 0, cx ? new StackContext(cx, cx.start) : null, 0, null);
  71. }
  72. /// The stack's current [context](#lr.ContextTracker) value, if
  73. /// any. Its type will depend on the context tracker's type
  74. /// parameter, or it will be `null` if there is no context
  75. /// tracker.
  76. get context() { return this.curContext ? this.curContext.context : null; }
  77. // Push a state onto the stack, tracking its start position as well
  78. // as the buffer base at that point.
  79. /// @internal
  80. pushState(state, start) {
  81. this.stack.push(this.state, start, this.bufferBase + this.buffer.length);
  82. this.state = state;
  83. }
  84. // Apply a reduce action
  85. /// @internal
  86. reduce(action) {
  87. let depth = action >> 19 /* ReduceDepthShift */, type = action & 65535 /* ValueMask */;
  88. let { parser } = this.p;
  89. let dPrec = parser.dynamicPrecedence(type);
  90. if (dPrec)
  91. this.score += dPrec;
  92. if (depth == 0) {
  93. this.pushState(parser.getGoto(this.state, type, true), this.reducePos);
  94. // Zero-depth reductions are a special case—they add stuff to
  95. // the stack without popping anything off.
  96. if (type < parser.minRepeatTerm)
  97. this.storeNode(type, this.reducePos, this.reducePos, 4, true);
  98. this.reduceContext(type, this.reducePos);
  99. return;
  100. }
  101. // Find the base index into `this.stack`, content after which will
  102. // be dropped. Note that with `StayFlag` reductions we need to
  103. // consume two extra frames (the dummy parent node for the skipped
  104. // expression and the state that we'll be staying in, which should
  105. // be moved to `this.state`).
  106. let base = this.stack.length - ((depth - 1) * 3) - (action & 262144 /* StayFlag */ ? 6 : 0);
  107. let start = this.stack[base - 2];
  108. let bufferBase = this.stack[base - 1], count = this.bufferBase + this.buffer.length - bufferBase;
  109. // Store normal terms or `R -> R R` repeat reductions
  110. if (type < parser.minRepeatTerm || (action & 131072 /* RepeatFlag */)) {
  111. let pos = parser.stateFlag(this.state, 1 /* Skipped */) ? this.pos : this.reducePos;
  112. this.storeNode(type, start, pos, count + 4, true);
  113. }
  114. if (action & 262144 /* StayFlag */) {
  115. this.state = this.stack[base];
  116. }
  117. else {
  118. let baseStateID = this.stack[base - 3];
  119. this.state = parser.getGoto(baseStateID, type, true);
  120. }
  121. while (this.stack.length > base)
  122. this.stack.pop();
  123. this.reduceContext(type, start);
  124. }
  125. // Shift a value into the buffer
  126. /// @internal
  127. storeNode(term, start, end, size = 4, isReduce = false) {
  128. if (term == 0 /* Err */ &&
  129. (!this.stack.length || this.stack[this.stack.length - 1] < this.buffer.length + this.bufferBase)) {
  130. // Try to omit/merge adjacent error nodes
  131. let cur = this, top = this.buffer.length;
  132. if (top == 0 && cur.parent) {
  133. top = cur.bufferBase - cur.parent.bufferBase;
  134. cur = cur.parent;
  135. }
  136. if (top > 0 && cur.buffer[top - 4] == 0 /* Err */ && cur.buffer[top - 1] > -1) {
  137. if (start == end)
  138. return;
  139. if (cur.buffer[top - 2] >= start) {
  140. cur.buffer[top - 2] = end;
  141. return;
  142. }
  143. }
  144. }
  145. if (!isReduce || this.pos == end) { // Simple case, just append
  146. this.buffer.push(term, start, end, size);
  147. }
  148. else { // There may be skipped nodes that have to be moved forward
  149. let index = this.buffer.length;
  150. if (index > 0 && this.buffer[index - 4] != 0 /* Err */)
  151. while (index > 0 && this.buffer[index - 2] > end) {
  152. // Move this record forward
  153. this.buffer[index] = this.buffer[index - 4];
  154. this.buffer[index + 1] = this.buffer[index - 3];
  155. this.buffer[index + 2] = this.buffer[index - 2];
  156. this.buffer[index + 3] = this.buffer[index - 1];
  157. index -= 4;
  158. if (size > 4)
  159. size -= 4;
  160. }
  161. this.buffer[index] = term;
  162. this.buffer[index + 1] = start;
  163. this.buffer[index + 2] = end;
  164. this.buffer[index + 3] = size;
  165. }
  166. }
  167. // Apply a shift action
  168. /// @internal
  169. shift(action, next, nextEnd) {
  170. let start = this.pos;
  171. if (action & 131072 /* GotoFlag */) {
  172. this.pushState(action & 65535 /* ValueMask */, this.pos);
  173. }
  174. else if ((action & 262144 /* StayFlag */) == 0) { // Regular shift
  175. let nextState = action, { parser } = this.p;
  176. if (nextEnd > this.pos || next <= parser.maxNode) {
  177. this.pos = nextEnd;
  178. if (!parser.stateFlag(nextState, 1 /* Skipped */))
  179. this.reducePos = nextEnd;
  180. }
  181. this.pushState(nextState, start);
  182. this.shiftContext(next, start);
  183. if (next <= parser.maxNode)
  184. this.buffer.push(next, start, nextEnd, 4);
  185. }
  186. else { // Shift-and-stay, which means this is a skipped token
  187. this.pos = nextEnd;
  188. this.shiftContext(next, start);
  189. if (next <= this.p.parser.maxNode)
  190. this.buffer.push(next, start, nextEnd, 4);
  191. }
  192. }
  193. // Apply an action
  194. /// @internal
  195. apply(action, next, nextEnd) {
  196. if (action & 65536 /* ReduceFlag */)
  197. this.reduce(action);
  198. else
  199. this.shift(action, next, nextEnd);
  200. }
  201. // Add a prebuilt (reused) node into the buffer.
  202. /// @internal
  203. useNode(value, next) {
  204. let index = this.p.reused.length - 1;
  205. if (index < 0 || this.p.reused[index] != value) {
  206. this.p.reused.push(value);
  207. index++;
  208. }
  209. let start = this.pos;
  210. this.reducePos = this.pos = start + value.length;
  211. this.pushState(next, start);
  212. this.buffer.push(index, start, this.reducePos, -1 /* size == -1 means this is a reused value */);
  213. if (this.curContext)
  214. this.updateContext(this.curContext.tracker.reuse(this.curContext.context, value, this, this.p.stream.reset(this.pos - value.length)));
  215. }
  216. // Split the stack. Due to the buffer sharing and the fact
  217. // that `this.stack` tends to stay quite shallow, this isn't very
  218. // expensive.
  219. /// @internal
  220. split() {
  221. let parent = this;
  222. let off = parent.buffer.length;
  223. // Because the top of the buffer (after this.pos) may be mutated
  224. // to reorder reductions and skipped tokens, and shared buffers
  225. // should be immutable, this copies any outstanding skipped tokens
  226. // to the new buffer, and puts the base pointer before them.
  227. while (off > 0 && parent.buffer[off - 2] > parent.reducePos)
  228. off -= 4;
  229. let buffer = parent.buffer.slice(off), base = parent.bufferBase + off;
  230. // Make sure parent points to an actual parent with content, if there is such a parent.
  231. while (parent && base == parent.bufferBase)
  232. parent = parent.parent;
  233. return new Stack(this.p, this.stack.slice(), this.state, this.reducePos, this.pos, this.score, buffer, base, this.curContext, this.lookAhead, parent);
  234. }
  235. // Try to recover from an error by 'deleting' (ignoring) one token.
  236. /// @internal
  237. recoverByDelete(next, nextEnd) {
  238. let isNode = next <= this.p.parser.maxNode;
  239. if (isNode)
  240. this.storeNode(next, this.pos, nextEnd, 4);
  241. this.storeNode(0 /* Err */, this.pos, nextEnd, isNode ? 8 : 4);
  242. this.pos = this.reducePos = nextEnd;
  243. this.score -= 190 /* Delete */;
  244. }
  245. /// Check if the given term would be able to be shifted (optionally
  246. /// after some reductions) on this stack. This can be useful for
  247. /// external tokenizers that want to make sure they only provide a
  248. /// given token when it applies.
  249. canShift(term) {
  250. for (let sim = new SimulatedStack(this);;) {
  251. let action = this.p.parser.stateSlot(sim.state, 4 /* DefaultReduce */) || this.p.parser.hasAction(sim.state, term);
  252. if ((action & 65536 /* ReduceFlag */) == 0)
  253. return true;
  254. if (action == 0)
  255. return false;
  256. sim.reduce(action);
  257. }
  258. }
  259. // Apply up to Recover.MaxNext recovery actions that conceptually
  260. // inserts some missing token or rule.
  261. /// @internal
  262. recoverByInsert(next) {
  263. if (this.stack.length >= 300 /* MaxInsertStackDepth */)
  264. return [];
  265. let nextStates = this.p.parser.nextStates(this.state);
  266. if (nextStates.length > 4 /* MaxNext */ << 1 || this.stack.length >= 120 /* DampenInsertStackDepth */) {
  267. let best = [];
  268. for (let i = 0, s; i < nextStates.length; i += 2) {
  269. if ((s = nextStates[i + 1]) != this.state && this.p.parser.hasAction(s, next))
  270. best.push(nextStates[i], s);
  271. }
  272. if (this.stack.length < 120 /* DampenInsertStackDepth */)
  273. for (let i = 0; best.length < 4 /* MaxNext */ << 1 && i < nextStates.length; i += 2) {
  274. let s = nextStates[i + 1];
  275. if (!best.some((v, i) => (i & 1) && v == s))
  276. best.push(nextStates[i], s);
  277. }
  278. nextStates = best;
  279. }
  280. let result = [];
  281. for (let i = 0; i < nextStates.length && result.length < 4 /* MaxNext */; i += 2) {
  282. let s = nextStates[i + 1];
  283. if (s == this.state)
  284. continue;
  285. let stack = this.split();
  286. stack.pushState(s, this.pos);
  287. stack.storeNode(0 /* Err */, stack.pos, stack.pos, 4, true);
  288. stack.shiftContext(nextStates[i], this.pos);
  289. stack.score -= 200 /* Insert */;
  290. result.push(stack);
  291. }
  292. return result;
  293. }
  294. // Force a reduce, if possible. Return false if that can't
  295. // be done.
  296. /// @internal
  297. forceReduce() {
  298. let reduce = this.p.parser.stateSlot(this.state, 5 /* ForcedReduce */);
  299. if ((reduce & 65536 /* ReduceFlag */) == 0)
  300. return false;
  301. let { parser } = this.p;
  302. if (!parser.validAction(this.state, reduce)) {
  303. let depth = reduce >> 19 /* ReduceDepthShift */, term = reduce & 65535 /* ValueMask */;
  304. let target = this.stack.length - depth * 3;
  305. if (target < 0 || parser.getGoto(this.stack[target], term, false) < 0)
  306. return false;
  307. this.storeNode(0 /* Err */, this.reducePos, this.reducePos, 4, true);
  308. this.score -= 100 /* Reduce */;
  309. }
  310. this.reducePos = this.pos;
  311. this.reduce(reduce);
  312. return true;
  313. }
  314. /// @internal
  315. forceAll() {
  316. while (!this.p.parser.stateFlag(this.state, 2 /* Accepting */)) {
  317. if (!this.forceReduce()) {
  318. this.storeNode(0 /* Err */, this.pos, this.pos, 4, true);
  319. break;
  320. }
  321. }
  322. return this;
  323. }
  324. /// Check whether this state has no further actions (assumed to be a direct descendant of the
  325. /// top state, since any other states must be able to continue
  326. /// somehow). @internal
  327. get deadEnd() {
  328. if (this.stack.length != 3)
  329. return false;
  330. let { parser } = this.p;
  331. return parser.data[parser.stateSlot(this.state, 1 /* Actions */)] == 65535 /* End */ &&
  332. !parser.stateSlot(this.state, 4 /* DefaultReduce */);
  333. }
  334. /// Restart the stack (put it back in its start state). Only safe
  335. /// when this.stack.length == 3 (state is directly below the top
  336. /// state). @internal
  337. restart() {
  338. this.state = this.stack[0];
  339. this.stack.length = 0;
  340. }
  341. /// @internal
  342. sameState(other) {
  343. if (this.state != other.state || this.stack.length != other.stack.length)
  344. return false;
  345. for (let i = 0; i < this.stack.length; i += 3)
  346. if (this.stack[i] != other.stack[i])
  347. return false;
  348. return true;
  349. }
  350. /// Get the parser used by this stack.
  351. get parser() { return this.p.parser; }
  352. /// Test whether a given dialect (by numeric ID, as exported from
  353. /// the terms file) is enabled.
  354. dialectEnabled(dialectID) { return this.p.parser.dialect.flags[dialectID]; }
  355. shiftContext(term, start) {
  356. if (this.curContext)
  357. this.updateContext(this.curContext.tracker.shift(this.curContext.context, term, this, this.p.stream.reset(start)));
  358. }
  359. reduceContext(term, start) {
  360. if (this.curContext)
  361. this.updateContext(this.curContext.tracker.reduce(this.curContext.context, term, this, this.p.stream.reset(start)));
  362. }
  363. /// @internal
  364. emitContext() {
  365. let last = this.buffer.length - 1;
  366. if (last < 0 || this.buffer[last] != -3)
  367. this.buffer.push(this.curContext.hash, this.reducePos, this.reducePos, -3);
  368. }
  369. /// @internal
  370. emitLookAhead() {
  371. let last = this.buffer.length - 1;
  372. if (last < 0 || this.buffer[last] != -4)
  373. this.buffer.push(this.lookAhead, this.reducePos, this.reducePos, -4);
  374. }
  375. updateContext(context) {
  376. if (context != this.curContext.context) {
  377. let newCx = new StackContext(this.curContext.tracker, context);
  378. if (newCx.hash != this.curContext.hash)
  379. this.emitContext();
  380. this.curContext = newCx;
  381. }
  382. }
  383. /// @internal
  384. setLookAhead(lookAhead) {
  385. if (lookAhead > this.lookAhead) {
  386. this.emitLookAhead();
  387. this.lookAhead = lookAhead;
  388. }
  389. }
  390. /// @internal
  391. close() {
  392. if (this.curContext && this.curContext.tracker.strict)
  393. this.emitContext();
  394. if (this.lookAhead > 0)
  395. this.emitLookAhead();
  396. }
  397. }
  398. class StackContext {
  399. constructor(tracker, context) {
  400. this.tracker = tracker;
  401. this.context = context;
  402. this.hash = tracker.strict ? tracker.hash(context) : 0;
  403. }
  404. }
  405. var Recover;
  406. (function (Recover) {
  407. Recover[Recover["Insert"] = 200] = "Insert";
  408. Recover[Recover["Delete"] = 190] = "Delete";
  409. Recover[Recover["Reduce"] = 100] = "Reduce";
  410. Recover[Recover["MaxNext"] = 4] = "MaxNext";
  411. Recover[Recover["MaxInsertStackDepth"] = 300] = "MaxInsertStackDepth";
  412. Recover[Recover["DampenInsertStackDepth"] = 120] = "DampenInsertStackDepth";
  413. })(Recover || (Recover = {}));
  414. // Used to cheaply run some reductions to scan ahead without mutating
  415. // an entire stack
  416. class SimulatedStack {
  417. constructor(start) {
  418. this.start = start;
  419. this.state = start.state;
  420. this.stack = start.stack;
  421. this.base = this.stack.length;
  422. }
  423. reduce(action) {
  424. let term = action & 65535 /* ValueMask */, depth = action >> 19 /* ReduceDepthShift */;
  425. if (depth == 0) {
  426. if (this.stack == this.start.stack)
  427. this.stack = this.stack.slice();
  428. this.stack.push(this.state, 0, 0);
  429. this.base += 3;
  430. }
  431. else {
  432. this.base -= (depth - 1) * 3;
  433. }
  434. let goto = this.start.p.parser.getGoto(this.stack[this.base - 3], term, true);
  435. this.state = goto;
  436. }
  437. }
  438. // This is given to `Tree.build` to build a buffer, and encapsulates
  439. // the parent-stack-walking necessary to read the nodes.
  440. class StackBufferCursor {
  441. constructor(stack, pos, index) {
  442. this.stack = stack;
  443. this.pos = pos;
  444. this.index = index;
  445. this.buffer = stack.buffer;
  446. if (this.index == 0)
  447. this.maybeNext();
  448. }
  449. static create(stack, pos = stack.bufferBase + stack.buffer.length) {
  450. return new StackBufferCursor(stack, pos, pos - stack.bufferBase);
  451. }
  452. maybeNext() {
  453. let next = this.stack.parent;
  454. if (next != null) {
  455. this.index = this.stack.bufferBase - next.bufferBase;
  456. this.stack = next;
  457. this.buffer = next.buffer;
  458. }
  459. }
  460. get id() { return this.buffer[this.index - 4]; }
  461. get start() { return this.buffer[this.index - 3]; }
  462. get end() { return this.buffer[this.index - 2]; }
  463. get size() { return this.buffer[this.index - 1]; }
  464. next() {
  465. this.index -= 4;
  466. this.pos -= 4;
  467. if (this.index == 0)
  468. this.maybeNext();
  469. }
  470. fork() {
  471. return new StackBufferCursor(this.stack, this.pos, this.index);
  472. }
  473. }
  474. class CachedToken {
  475. constructor() {
  476. this.start = -1;
  477. this.value = -1;
  478. this.end = -1;
  479. this.extended = -1;
  480. this.lookAhead = 0;
  481. this.mask = 0;
  482. this.context = 0;
  483. }
  484. }
  485. const nullToken = new CachedToken;
  486. /// [Tokenizers](#lr.ExternalTokenizer) interact with the input
  487. /// through this interface. It presents the input as a stream of
  488. /// characters, tracking lookahead and hiding the complexity of
  489. /// [ranges](#common.Parser.parse^ranges) from tokenizer code.
  490. class InputStream {
  491. /// @internal
  492. constructor(
  493. /// @internal
  494. input,
  495. /// @internal
  496. ranges) {
  497. this.input = input;
  498. this.ranges = ranges;
  499. /// @internal
  500. this.chunk = "";
  501. /// @internal
  502. this.chunkOff = 0;
  503. /// Backup chunk
  504. this.chunk2 = "";
  505. this.chunk2Pos = 0;
  506. /// The character code of the next code unit in the input, or -1
  507. /// when the stream is at the end of the input.
  508. this.next = -1;
  509. /// @internal
  510. this.token = nullToken;
  511. this.rangeIndex = 0;
  512. this.pos = this.chunkPos = ranges[0].from;
  513. this.range = ranges[0];
  514. this.end = ranges[ranges.length - 1].to;
  515. this.readNext();
  516. }
  517. /// @internal
  518. resolveOffset(offset, assoc) {
  519. let range = this.range, index = this.rangeIndex;
  520. let pos = this.pos + offset;
  521. while (pos < range.from) {
  522. if (!index)
  523. return null;
  524. let next = this.ranges[--index];
  525. pos -= range.from - next.to;
  526. range = next;
  527. }
  528. while (assoc < 0 ? pos > range.to : pos >= range.to) {
  529. if (index == this.ranges.length - 1)
  530. return null;
  531. let next = this.ranges[++index];
  532. pos += next.from - range.to;
  533. range = next;
  534. }
  535. return pos;
  536. }
  537. /// Look at a code unit near the stream position. `.peek(0)` equals
  538. /// `.next`, `.peek(-1)` gives you the previous character, and so
  539. /// on.
  540. ///
  541. /// Note that looking around during tokenizing creates dependencies
  542. /// on potentially far-away content, which may reduce the
  543. /// effectiveness incremental parsing—when looking forward—or even
  544. /// cause invalid reparses when looking backward more than 25 code
  545. /// units, since the library does not track lookbehind.
  546. peek(offset) {
  547. let idx = this.chunkOff + offset, pos, result;
  548. if (idx >= 0 && idx < this.chunk.length) {
  549. pos = this.pos + offset;
  550. result = this.chunk.charCodeAt(idx);
  551. }
  552. else {
  553. let resolved = this.resolveOffset(offset, 1);
  554. if (resolved == null)
  555. return -1;
  556. pos = resolved;
  557. if (pos >= this.chunk2Pos && pos < this.chunk2Pos + this.chunk2.length) {
  558. result = this.chunk2.charCodeAt(pos - this.chunk2Pos);
  559. }
  560. else {
  561. let i = this.rangeIndex, range = this.range;
  562. while (range.to <= pos)
  563. range = this.ranges[++i];
  564. this.chunk2 = this.input.chunk(this.chunk2Pos = pos);
  565. if (pos + this.chunk2.length > range.to)
  566. this.chunk2 = this.chunk2.slice(0, range.to - pos);
  567. result = this.chunk2.charCodeAt(0);
  568. }
  569. }
  570. if (pos >= this.token.lookAhead)
  571. this.token.lookAhead = pos + 1;
  572. return result;
  573. }
  574. /// Accept a token. By default, the end of the token is set to the
  575. /// current stream position, but you can pass an offset (relative to
  576. /// the stream position) to change that.
  577. acceptToken(token, endOffset = 0) {
  578. let end = endOffset ? this.resolveOffset(endOffset, -1) : this.pos;
  579. if (end == null || end < this.token.start)
  580. throw new RangeError("Token end out of bounds");
  581. this.token.value = token;
  582. this.token.end = end;
  583. }
  584. getChunk() {
  585. if (this.pos >= this.chunk2Pos && this.pos < this.chunk2Pos + this.chunk2.length) {
  586. let { chunk, chunkPos } = this;
  587. this.chunk = this.chunk2;
  588. this.chunkPos = this.chunk2Pos;
  589. this.chunk2 = chunk;
  590. this.chunk2Pos = chunkPos;
  591. this.chunkOff = this.pos - this.chunkPos;
  592. }
  593. else {
  594. this.chunk2 = this.chunk;
  595. this.chunk2Pos = this.chunkPos;
  596. let nextChunk = this.input.chunk(this.pos);
  597. let end = this.pos + nextChunk.length;
  598. this.chunk = end > this.range.to ? nextChunk.slice(0, this.range.to - this.pos) : nextChunk;
  599. this.chunkPos = this.pos;
  600. this.chunkOff = 0;
  601. }
  602. }
  603. readNext() {
  604. if (this.chunkOff >= this.chunk.length) {
  605. this.getChunk();
  606. if (this.chunkOff == this.chunk.length)
  607. return this.next = -1;
  608. }
  609. return this.next = this.chunk.charCodeAt(this.chunkOff);
  610. }
  611. /// Move the stream forward N (defaults to 1) code units. Returns
  612. /// the new value of [`next`](#lr.InputStream.next).
  613. advance(n = 1) {
  614. this.chunkOff += n;
  615. while (this.pos + n >= this.range.to) {
  616. if (this.rangeIndex == this.ranges.length - 1)
  617. return this.setDone();
  618. n -= this.range.to - this.pos;
  619. this.range = this.ranges[++this.rangeIndex];
  620. this.pos = this.range.from;
  621. }
  622. this.pos += n;
  623. if (this.pos >= this.token.lookAhead)
  624. this.token.lookAhead = this.pos + 1;
  625. return this.readNext();
  626. }
  627. setDone() {
  628. this.pos = this.chunkPos = this.end;
  629. this.range = this.ranges[this.rangeIndex = this.ranges.length - 1];
  630. this.chunk = "";
  631. return this.next = -1;
  632. }
  633. /// @internal
  634. reset(pos, token) {
  635. if (token) {
  636. this.token = token;
  637. token.start = pos;
  638. token.lookAhead = pos + 1;
  639. token.value = token.extended = -1;
  640. }
  641. else {
  642. this.token = nullToken;
  643. }
  644. if (this.pos != pos) {
  645. this.pos = pos;
  646. if (pos == this.end) {
  647. this.setDone();
  648. return this;
  649. }
  650. while (pos < this.range.from)
  651. this.range = this.ranges[--this.rangeIndex];
  652. while (pos >= this.range.to)
  653. this.range = this.ranges[++this.rangeIndex];
  654. if (pos >= this.chunkPos && pos < this.chunkPos + this.chunk.length) {
  655. this.chunkOff = pos - this.chunkPos;
  656. }
  657. else {
  658. this.chunk = "";
  659. this.chunkOff = 0;
  660. }
  661. this.readNext();
  662. }
  663. return this;
  664. }
  665. /// @internal
  666. read(from, to) {
  667. if (from >= this.chunkPos && to <= this.chunkPos + this.chunk.length)
  668. return this.chunk.slice(from - this.chunkPos, to - this.chunkPos);
  669. if (from >= this.chunk2Pos && to <= this.chunk2Pos + this.chunk2.length)
  670. return this.chunk2.slice(from - this.chunk2Pos, to - this.chunk2Pos);
  671. if (from >= this.range.from && to <= this.range.to)
  672. return this.input.read(from, to);
  673. let result = "";
  674. for (let r of this.ranges) {
  675. if (r.from >= to)
  676. break;
  677. if (r.to > from)
  678. result += this.input.read(Math.max(r.from, from), Math.min(r.to, to));
  679. }
  680. return result;
  681. }
  682. }
  683. /// @internal
  684. class TokenGroup {
  685. constructor(data, id) {
  686. this.data = data;
  687. this.id = id;
  688. }
  689. token(input, stack) { readToken(this.data, input, stack, this.id); }
  690. }
  691. TokenGroup.prototype.contextual = TokenGroup.prototype.fallback = TokenGroup.prototype.extend = false;
  692. /// `@external tokens` declarations in the grammar should resolve to
  693. /// an instance of this class.
  694. class ExternalTokenizer {
  695. /// Create a tokenizer. The first argument is the function that,
  696. /// given an input stream, scans for the types of tokens it
  697. /// recognizes at the stream's position, and calls
  698. /// [`acceptToken`](#lr.InputStream.acceptToken) when it finds
  699. /// one.
  700. constructor(
  701. /// @internal
  702. token, options = {}) {
  703. this.token = token;
  704. this.contextual = !!options.contextual;
  705. this.fallback = !!options.fallback;
  706. this.extend = !!options.extend;
  707. }
  708. }
  709. // Tokenizer data is stored a big uint16 array containing, for each
  710. // state:
  711. //
  712. // - A group bitmask, indicating what token groups are reachable from
  713. // this state, so that paths that can only lead to tokens not in
  714. // any of the current groups can be cut off early.
  715. //
  716. // - The position of the end of the state's sequence of accepting
  717. // tokens
  718. //
  719. // - The number of outgoing edges for the state
  720. //
  721. // - The accepting tokens, as (token id, group mask) pairs
  722. //
  723. // - The outgoing edges, as (start character, end character, state
  724. // index) triples, with end character being exclusive
  725. //
  726. // This function interprets that data, running through a stream as
  727. // long as new states with the a matching group mask can be reached,
  728. // and updating `input.token` when it matches a token.
  729. function readToken(data, input, stack, group) {
  730. let state = 0, groupMask = 1 << group, { parser } = stack.p, { dialect } = parser;
  731. scan: for (;;) {
  732. if ((groupMask & data[state]) == 0)
  733. break;
  734. let accEnd = data[state + 1];
  735. // Check whether this state can lead to a token in the current group
  736. // Accept tokens in this state, possibly overwriting
  737. // lower-precedence / shorter tokens
  738. for (let i = state + 3; i < accEnd; i += 2)
  739. if ((data[i + 1] & groupMask) > 0) {
  740. let term = data[i];
  741. if (dialect.allows(term) &&
  742. (input.token.value == -1 || input.token.value == term || parser.overrides(term, input.token.value))) {
  743. input.acceptToken(term);
  744. break;
  745. }
  746. }
  747. let next = input.next, low = 0, high = data[state + 2];
  748. // Special case for EOF
  749. if (input.next < 0 && high > low && data[accEnd + high * 3 - 3] == 65535 /* End */) {
  750. state = data[accEnd + high * 3 - 1];
  751. continue scan;
  752. }
  753. // Do a binary search on the state's edges
  754. for (; low < high;) {
  755. let mid = (low + high) >> 1;
  756. let index = accEnd + mid + (mid << 1);
  757. let from = data[index], to = data[index + 1];
  758. if (next < from)
  759. high = mid;
  760. else if (next >= to)
  761. low = mid + 1;
  762. else {
  763. state = data[index + 2];
  764. input.advance();
  765. continue scan;
  766. }
  767. }
  768. break;
  769. }
  770. }
  771. // See lezer-generator/src/encode.ts for comments about the encoding
  772. // used here
  773. function decodeArray(input, Type = Uint16Array) {
  774. if (typeof input != "string")
  775. return input;
  776. let array = null;
  777. for (let pos = 0, out = 0; pos < input.length;) {
  778. let value = 0;
  779. for (;;) {
  780. let next = input.charCodeAt(pos++), stop = false;
  781. if (next == 126 /* BigValCode */) {
  782. value = 65535 /* BigVal */;
  783. break;
  784. }
  785. if (next >= 92 /* Gap2 */)
  786. next--;
  787. if (next >= 34 /* Gap1 */)
  788. next--;
  789. let digit = next - 32 /* Start */;
  790. if (digit >= 46 /* Base */) {
  791. digit -= 46 /* Base */;
  792. stop = true;
  793. }
  794. value += digit;
  795. if (stop)
  796. break;
  797. value *= 46 /* Base */;
  798. }
  799. if (array)
  800. array[out++] = value;
  801. else
  802. array = new Type(value);
  803. }
  804. return array;
  805. }
  806. // Environment variable used to control console output
  807. const verbose = typeof process != "undefined" && process.env && /\bparse\b/.test(process.env.LOG);
  808. let stackIDs = null;
  809. var Safety;
  810. (function (Safety) {
  811. Safety[Safety["Margin"] = 25] = "Margin";
  812. })(Safety || (Safety = {}));
  813. function cutAt(tree, pos, side) {
  814. let cursor = tree.cursor(IterMode.IncludeAnonymous);
  815. cursor.moveTo(pos);
  816. for (;;) {
  817. if (!(side < 0 ? cursor.childBefore(pos) : cursor.childAfter(pos)))
  818. for (;;) {
  819. if ((side < 0 ? cursor.to < pos : cursor.from > pos) && !cursor.type.isError)
  820. return side < 0 ? Math.max(0, Math.min(cursor.to - 1, pos - 25 /* Margin */))
  821. : Math.min(tree.length, Math.max(cursor.from + 1, pos + 25 /* Margin */));
  822. if (side < 0 ? cursor.prevSibling() : cursor.nextSibling())
  823. break;
  824. if (!cursor.parent())
  825. return side < 0 ? 0 : tree.length;
  826. }
  827. }
  828. }
  829. class FragmentCursor {
  830. constructor(fragments, nodeSet) {
  831. this.fragments = fragments;
  832. this.nodeSet = nodeSet;
  833. this.i = 0;
  834. this.fragment = null;
  835. this.safeFrom = -1;
  836. this.safeTo = -1;
  837. this.trees = [];
  838. this.start = [];
  839. this.index = [];
  840. this.nextFragment();
  841. }
  842. nextFragment() {
  843. let fr = this.fragment = this.i == this.fragments.length ? null : this.fragments[this.i++];
  844. if (fr) {
  845. this.safeFrom = fr.openStart ? cutAt(fr.tree, fr.from + fr.offset, 1) - fr.offset : fr.from;
  846. this.safeTo = fr.openEnd ? cutAt(fr.tree, fr.to + fr.offset, -1) - fr.offset : fr.to;
  847. while (this.trees.length) {
  848. this.trees.pop();
  849. this.start.pop();
  850. this.index.pop();
  851. }
  852. this.trees.push(fr.tree);
  853. this.start.push(-fr.offset);
  854. this.index.push(0);
  855. this.nextStart = this.safeFrom;
  856. }
  857. else {
  858. this.nextStart = 1e9;
  859. }
  860. }
  861. // `pos` must be >= any previously given `pos` for this cursor
  862. nodeAt(pos) {
  863. if (pos < this.nextStart)
  864. return null;
  865. while (this.fragment && this.safeTo <= pos)
  866. this.nextFragment();
  867. if (!this.fragment)
  868. return null;
  869. for (;;) {
  870. let last = this.trees.length - 1;
  871. if (last < 0) { // End of tree
  872. this.nextFragment();
  873. return null;
  874. }
  875. let top = this.trees[last], index = this.index[last];
  876. if (index == top.children.length) {
  877. this.trees.pop();
  878. this.start.pop();
  879. this.index.pop();
  880. continue;
  881. }
  882. let next = top.children[index];
  883. let start = this.start[last] + top.positions[index];
  884. if (start > pos) {
  885. this.nextStart = start;
  886. return null;
  887. }
  888. if (next instanceof Tree) {
  889. if (start == pos) {
  890. if (start < this.safeFrom)
  891. return null;
  892. let end = start + next.length;
  893. if (end <= this.safeTo) {
  894. let lookAhead = next.prop(NodeProp.lookAhead);
  895. if (!lookAhead || end + lookAhead < this.fragment.to)
  896. return next;
  897. }
  898. }
  899. this.index[last]++;
  900. if (start + next.length >= Math.max(this.safeFrom, pos)) { // Enter this node
  901. this.trees.push(next);
  902. this.start.push(start);
  903. this.index.push(0);
  904. }
  905. }
  906. else {
  907. this.index[last]++;
  908. this.nextStart = start + next.length;
  909. }
  910. }
  911. }
  912. }
  913. class TokenCache {
  914. constructor(parser, stream) {
  915. this.stream = stream;
  916. this.tokens = [];
  917. this.mainToken = null;
  918. this.actions = [];
  919. this.tokens = parser.tokenizers.map(_ => new CachedToken);
  920. }
  921. getActions(stack) {
  922. let actionIndex = 0;
  923. let main = null;
  924. let { parser } = stack.p, { tokenizers } = parser;
  925. let mask = parser.stateSlot(stack.state, 3 /* TokenizerMask */);
  926. let context = stack.curContext ? stack.curContext.hash : 0;
  927. let lookAhead = 0;
  928. for (let i = 0; i < tokenizers.length; i++) {
  929. if (((1 << i) & mask) == 0)
  930. continue;
  931. let tokenizer = tokenizers[i], token = this.tokens[i];
  932. if (main && !tokenizer.fallback)
  933. continue;
  934. if (tokenizer.contextual || token.start != stack.pos || token.mask != mask || token.context != context) {
  935. this.updateCachedToken(token, tokenizer, stack);
  936. token.mask = mask;
  937. token.context = context;
  938. }
  939. if (token.lookAhead > token.end + 25 /* Margin */)
  940. lookAhead = Math.max(token.lookAhead, lookAhead);
  941. if (token.value != 0 /* Err */) {
  942. let startIndex = actionIndex;
  943. if (token.extended > -1)
  944. actionIndex = this.addActions(stack, token.extended, token.end, actionIndex);
  945. actionIndex = this.addActions(stack, token.value, token.end, actionIndex);
  946. if (!tokenizer.extend) {
  947. main = token;
  948. if (actionIndex > startIndex)
  949. break;
  950. }
  951. }
  952. }
  953. while (this.actions.length > actionIndex)
  954. this.actions.pop();
  955. if (lookAhead)
  956. stack.setLookAhead(lookAhead);
  957. if (!main && stack.pos == this.stream.end) {
  958. main = new CachedToken;
  959. main.value = stack.p.parser.eofTerm;
  960. main.start = main.end = stack.pos;
  961. actionIndex = this.addActions(stack, main.value, main.end, actionIndex);
  962. }
  963. this.mainToken = main;
  964. return this.actions;
  965. }
  966. getMainToken(stack) {
  967. if (this.mainToken)
  968. return this.mainToken;
  969. let main = new CachedToken, { pos, p } = stack;
  970. main.start = pos;
  971. main.end = Math.min(pos + 1, p.stream.end);
  972. main.value = pos == p.stream.end ? p.parser.eofTerm : 0 /* Err */;
  973. return main;
  974. }
  975. updateCachedToken(token, tokenizer, stack) {
  976. tokenizer.token(this.stream.reset(stack.pos, token), stack);
  977. if (token.value > -1) {
  978. let { parser } = stack.p;
  979. for (let i = 0; i < parser.specialized.length; i++)
  980. if (parser.specialized[i] == token.value) {
  981. let result = parser.specializers[i](this.stream.read(token.start, token.end), stack);
  982. if (result >= 0 && stack.p.parser.dialect.allows(result >> 1)) {
  983. if ((result & 1) == 0 /* Specialize */)
  984. token.value = result >> 1;
  985. else
  986. token.extended = result >> 1;
  987. break;
  988. }
  989. }
  990. }
  991. else {
  992. token.value = 0 /* Err */;
  993. token.end = Math.min(stack.p.stream.end, stack.pos + 1);
  994. }
  995. }
  996. putAction(action, token, end, index) {
  997. // Don't add duplicate actions
  998. for (let i = 0; i < index; i += 3)
  999. if (this.actions[i] == action)
  1000. return index;
  1001. this.actions[index++] = action;
  1002. this.actions[index++] = token;
  1003. this.actions[index++] = end;
  1004. return index;
  1005. }
  1006. addActions(stack, token, end, index) {
  1007. let { state } = stack, { parser } = stack.p, { data } = parser;
  1008. for (let set = 0; set < 2; set++) {
  1009. for (let i = parser.stateSlot(state, set ? 2 /* Skip */ : 1 /* Actions */);; i += 3) {
  1010. if (data[i] == 65535 /* End */) {
  1011. if (data[i + 1] == 1 /* Next */) {
  1012. i = pair(data, i + 2);
  1013. }
  1014. else {
  1015. if (index == 0 && data[i + 1] == 2 /* Other */)
  1016. index = this.putAction(pair(data, i + 2), token, end, index);
  1017. break;
  1018. }
  1019. }
  1020. if (data[i] == token)
  1021. index = this.putAction(pair(data, i + 1), token, end, index);
  1022. }
  1023. }
  1024. return index;
  1025. }
  1026. }
  1027. var Rec;
  1028. (function (Rec) {
  1029. Rec[Rec["Distance"] = 5] = "Distance";
  1030. Rec[Rec["MaxRemainingPerStep"] = 3] = "MaxRemainingPerStep";
  1031. // When two stacks have been running independently long enough to
  1032. // add this many elements to their buffers, prune one.
  1033. Rec[Rec["MinBufferLengthPrune"] = 500] = "MinBufferLengthPrune";
  1034. Rec[Rec["ForceReduceLimit"] = 10] = "ForceReduceLimit";
  1035. // Once a stack reaches this depth (in .stack.length) force-reduce
  1036. // it back to CutTo to avoid creating trees that overflow the stack
  1037. // on recursive traversal.
  1038. Rec[Rec["CutDepth"] = 15000] = "CutDepth";
  1039. Rec[Rec["CutTo"] = 9000] = "CutTo";
  1040. })(Rec || (Rec = {}));
  1041. class Parse {
  1042. constructor(parser, input, fragments, ranges) {
  1043. this.parser = parser;
  1044. this.input = input;
  1045. this.ranges = ranges;
  1046. this.recovering = 0;
  1047. this.nextStackID = 0x2654; // ♔, ♕, ♖, ♗, ♘, ♙, ♠, ♡, ♢, ♣, ♤, ♥, ♦, ♧
  1048. this.minStackPos = 0;
  1049. this.reused = [];
  1050. this.stoppedAt = null;
  1051. this.stream = new InputStream(input, ranges);
  1052. this.tokens = new TokenCache(parser, this.stream);
  1053. this.topTerm = parser.top[1];
  1054. let { from } = ranges[0];
  1055. this.stacks = [Stack.start(this, parser.top[0], from)];
  1056. this.fragments = fragments.length && this.stream.end - from > parser.bufferLength * 4
  1057. ? new FragmentCursor(fragments, parser.nodeSet) : null;
  1058. }
  1059. get parsedPos() {
  1060. return this.minStackPos;
  1061. }
  1062. // Move the parser forward. This will process all parse stacks at
  1063. // `this.pos` and try to advance them to a further position. If no
  1064. // stack for such a position is found, it'll start error-recovery.
  1065. //
  1066. // When the parse is finished, this will return a syntax tree. When
  1067. // not, it returns `null`.
  1068. advance() {
  1069. let stacks = this.stacks, pos = this.minStackPos;
  1070. // This will hold stacks beyond `pos`.
  1071. let newStacks = this.stacks = [];
  1072. let stopped, stoppedTokens;
  1073. // Keep advancing any stacks at `pos` until they either move
  1074. // forward or can't be advanced. Gather stacks that can't be
  1075. // advanced further in `stopped`.
  1076. for (let i = 0; i < stacks.length; i++) {
  1077. let stack = stacks[i];
  1078. for (;;) {
  1079. this.tokens.mainToken = null;
  1080. if (stack.pos > pos) {
  1081. newStacks.push(stack);
  1082. }
  1083. else if (this.advanceStack(stack, newStacks, stacks)) {
  1084. continue;
  1085. }
  1086. else {
  1087. if (!stopped) {
  1088. stopped = [];
  1089. stoppedTokens = [];
  1090. }
  1091. stopped.push(stack);
  1092. let tok = this.tokens.getMainToken(stack);
  1093. stoppedTokens.push(tok.value, tok.end);
  1094. }
  1095. break;
  1096. }
  1097. }
  1098. if (!newStacks.length) {
  1099. let finished = stopped && findFinished(stopped);
  1100. if (finished)
  1101. return this.stackToTree(finished);
  1102. if (this.parser.strict) {
  1103. if (verbose && stopped)
  1104. console.log("Stuck with token " + (this.tokens.mainToken ? this.parser.getName(this.tokens.mainToken.value) : "none"));
  1105. throw new SyntaxError("No parse at " + pos);
  1106. }
  1107. if (!this.recovering)
  1108. this.recovering = 5 /* Distance */;
  1109. }
  1110. if (this.recovering && stopped) {
  1111. let finished = this.stoppedAt != null && stopped[0].pos > this.stoppedAt ? stopped[0]
  1112. : this.runRecovery(stopped, stoppedTokens, newStacks);
  1113. if (finished)
  1114. return this.stackToTree(finished.forceAll());
  1115. }
  1116. if (this.recovering) {
  1117. let maxRemaining = this.recovering == 1 ? 1 : this.recovering * 3 /* MaxRemainingPerStep */;
  1118. if (newStacks.length > maxRemaining) {
  1119. newStacks.sort((a, b) => b.score - a.score);
  1120. while (newStacks.length > maxRemaining)
  1121. newStacks.pop();
  1122. }
  1123. if (newStacks.some(s => s.reducePos > pos))
  1124. this.recovering--;
  1125. }
  1126. else if (newStacks.length > 1) {
  1127. // Prune stacks that are in the same state, or that have been
  1128. // running without splitting for a while, to avoid getting stuck
  1129. // with multiple successful stacks running endlessly on.
  1130. outer: for (let i = 0; i < newStacks.length - 1; i++) {
  1131. let stack = newStacks[i];
  1132. for (let j = i + 1; j < newStacks.length; j++) {
  1133. let other = newStacks[j];
  1134. if (stack.sameState(other) ||
  1135. stack.buffer.length > 500 /* MinBufferLengthPrune */ && other.buffer.length > 500 /* MinBufferLengthPrune */) {
  1136. if (((stack.score - other.score) || (stack.buffer.length - other.buffer.length)) > 0) {
  1137. newStacks.splice(j--, 1);
  1138. }
  1139. else {
  1140. newStacks.splice(i--, 1);
  1141. continue outer;
  1142. }
  1143. }
  1144. }
  1145. }
  1146. }
  1147. this.minStackPos = newStacks[0].pos;
  1148. for (let i = 1; i < newStacks.length; i++)
  1149. if (newStacks[i].pos < this.minStackPos)
  1150. this.minStackPos = newStacks[i].pos;
  1151. return null;
  1152. }
  1153. stopAt(pos) {
  1154. if (this.stoppedAt != null && this.stoppedAt < pos)
  1155. throw new RangeError("Can't move stoppedAt forward");
  1156. this.stoppedAt = pos;
  1157. }
  1158. // Returns an updated version of the given stack, or null if the
  1159. // stack can't advance normally. When `split` and `stacks` are
  1160. // given, stacks split off by ambiguous operations will be pushed to
  1161. // `split`, or added to `stacks` if they move `pos` forward.
  1162. advanceStack(stack, stacks, split) {
  1163. let start = stack.pos, { parser } = this;
  1164. let base = verbose ? this.stackID(stack) + " -> " : "";
  1165. if (this.stoppedAt != null && start > this.stoppedAt)
  1166. return stack.forceReduce() ? stack : null;
  1167. if (this.fragments) {
  1168. let strictCx = stack.curContext && stack.curContext.tracker.strict, cxHash = strictCx ? stack.curContext.hash : 0;
  1169. for (let cached = this.fragments.nodeAt(start); cached;) {
  1170. let match = this.parser.nodeSet.types[cached.type.id] == cached.type ? parser.getGoto(stack.state, cached.type.id) : -1;
  1171. if (match > -1 && cached.length && (!strictCx || (cached.prop(NodeProp.contextHash) || 0) == cxHash)) {
  1172. stack.useNode(cached, match);
  1173. if (verbose)
  1174. console.log(base + this.stackID(stack) + ` (via reuse of ${parser.getName(cached.type.id)})`);
  1175. return true;
  1176. }
  1177. if (!(cached instanceof Tree) || cached.children.length == 0 || cached.positions[0] > 0)
  1178. break;
  1179. let inner = cached.children[0];
  1180. if (inner instanceof Tree && cached.positions[0] == 0)
  1181. cached = inner;
  1182. else
  1183. break;
  1184. }
  1185. }
  1186. let defaultReduce = parser.stateSlot(stack.state, 4 /* DefaultReduce */);
  1187. if (defaultReduce > 0) {
  1188. stack.reduce(defaultReduce);
  1189. if (verbose)
  1190. console.log(base + this.stackID(stack) + ` (via always-reduce ${parser.getName(defaultReduce & 65535 /* ValueMask */)})`);
  1191. return true;
  1192. }
  1193. if (stack.stack.length >= 15000 /* CutDepth */) {
  1194. while (stack.stack.length > 9000 /* CutTo */ && stack.forceReduce()) { }
  1195. }
  1196. let actions = this.tokens.getActions(stack);
  1197. for (let i = 0; i < actions.length;) {
  1198. let action = actions[i++], term = actions[i++], end = actions[i++];
  1199. let last = i == actions.length || !split;
  1200. let localStack = last ? stack : stack.split();
  1201. localStack.apply(action, term, end);
  1202. if (verbose)
  1203. console.log(base + this.stackID(localStack) + ` (via ${(action & 65536 /* ReduceFlag */) == 0 ? "shift"
  1204. : `reduce of ${parser.getName(action & 65535 /* ValueMask */)}`} for ${parser.getName(term)} @ ${start}${localStack == stack ? "" : ", split"})`);
  1205. if (last)
  1206. return true;
  1207. else if (localStack.pos > start)
  1208. stacks.push(localStack);
  1209. else
  1210. split.push(localStack);
  1211. }
  1212. return false;
  1213. }
  1214. // Advance a given stack forward as far as it will go. Returns the
  1215. // (possibly updated) stack if it got stuck, or null if it moved
  1216. // forward and was given to `pushStackDedup`.
  1217. advanceFully(stack, newStacks) {
  1218. let pos = stack.pos;
  1219. for (;;) {
  1220. if (!this.advanceStack(stack, null, null))
  1221. return false;
  1222. if (stack.pos > pos) {
  1223. pushStackDedup(stack, newStacks);
  1224. return true;
  1225. }
  1226. }
  1227. }
  1228. runRecovery(stacks, tokens, newStacks) {
  1229. let finished = null, restarted = false;
  1230. for (let i = 0; i < stacks.length; i++) {
  1231. let stack = stacks[i], token = tokens[i << 1], tokenEnd = tokens[(i << 1) + 1];
  1232. let base = verbose ? this.stackID(stack) + " -> " : "";
  1233. if (stack.deadEnd) {
  1234. if (restarted)
  1235. continue;
  1236. restarted = true;
  1237. stack.restart();
  1238. if (verbose)
  1239. console.log(base + this.stackID(stack) + " (restarted)");
  1240. let done = this.advanceFully(stack, newStacks);
  1241. if (done)
  1242. continue;
  1243. }
  1244. let force = stack.split(), forceBase = base;
  1245. for (let j = 0; force.forceReduce() && j < 10 /* ForceReduceLimit */; j++) {
  1246. if (verbose)
  1247. console.log(forceBase + this.stackID(force) + " (via force-reduce)");
  1248. let done = this.advanceFully(force, newStacks);
  1249. if (done)
  1250. break;
  1251. if (verbose)
  1252. forceBase = this.stackID(force) + " -> ";
  1253. }
  1254. for (let insert of stack.recoverByInsert(token)) {
  1255. if (verbose)
  1256. console.log(base + this.stackID(insert) + " (via recover-insert)");
  1257. this.advanceFully(insert, newStacks);
  1258. }
  1259. if (this.stream.end > stack.pos) {
  1260. if (tokenEnd == stack.pos) {
  1261. tokenEnd++;
  1262. token = 0 /* Err */;
  1263. }
  1264. stack.recoverByDelete(token, tokenEnd);
  1265. if (verbose)
  1266. console.log(base + this.stackID(stack) + ` (via recover-delete ${this.parser.getName(token)})`);
  1267. pushStackDedup(stack, newStacks);
  1268. }
  1269. else if (!finished || finished.score < stack.score) {
  1270. finished = stack;
  1271. }
  1272. }
  1273. return finished;
  1274. }
  1275. // Convert the stack's buffer to a syntax tree.
  1276. stackToTree(stack) {
  1277. stack.close();
  1278. return Tree.build({ buffer: StackBufferCursor.create(stack),
  1279. nodeSet: this.parser.nodeSet,
  1280. topID: this.topTerm,
  1281. maxBufferLength: this.parser.bufferLength,
  1282. reused: this.reused,
  1283. start: this.ranges[0].from,
  1284. length: stack.pos - this.ranges[0].from,
  1285. minRepeatType: this.parser.minRepeatTerm });
  1286. }
  1287. stackID(stack) {
  1288. let id = (stackIDs || (stackIDs = new WeakMap)).get(stack);
  1289. if (!id)
  1290. stackIDs.set(stack, id = String.fromCodePoint(this.nextStackID++));
  1291. return id + stack;
  1292. }
  1293. }
  1294. function pushStackDedup(stack, newStacks) {
  1295. for (let i = 0; i < newStacks.length; i++) {
  1296. let other = newStacks[i];
  1297. if (other.pos == stack.pos && other.sameState(stack)) {
  1298. if (newStacks[i].score < stack.score)
  1299. newStacks[i] = stack;
  1300. return;
  1301. }
  1302. }
  1303. newStacks.push(stack);
  1304. }
  1305. class Dialect {
  1306. constructor(source, flags, disabled) {
  1307. this.source = source;
  1308. this.flags = flags;
  1309. this.disabled = disabled;
  1310. }
  1311. allows(term) { return !this.disabled || this.disabled[term] == 0; }
  1312. }
  1313. const id = x => x;
  1314. /// Context trackers are used to track stateful context (such as
  1315. /// indentation in the Python grammar, or parent elements in the XML
  1316. /// grammar) needed by external tokenizers. You declare them in a
  1317. /// grammar file as `@context exportName from "module"`.
  1318. ///
  1319. /// Context values should be immutable, and can be updated (replaced)
  1320. /// on shift or reduce actions.
  1321. ///
  1322. /// The export used in a `@context` declaration should be of this
  1323. /// type.
  1324. class ContextTracker {
  1325. /// Define a context tracker.
  1326. constructor(spec) {
  1327. this.start = spec.start;
  1328. this.shift = spec.shift || id;
  1329. this.reduce = spec.reduce || id;
  1330. this.reuse = spec.reuse || id;
  1331. this.hash = spec.hash || (() => 0);
  1332. this.strict = spec.strict !== false;
  1333. }
  1334. }
  1335. /// Holds the parse tables for a given grammar, as generated by
  1336. /// `lezer-generator`, and provides [methods](#common.Parser) to parse
  1337. /// content with.
  1338. class LRParser extends Parser {
  1339. /// @internal
  1340. constructor(spec) {
  1341. super();
  1342. /// @internal
  1343. this.wrappers = [];
  1344. if (spec.version != 14 /* Version */)
  1345. throw new RangeError(`Parser version (${spec.version}) doesn't match runtime version (${14 /* Version */})`);
  1346. let nodeNames = spec.nodeNames.split(" ");
  1347. this.minRepeatTerm = nodeNames.length;
  1348. for (let i = 0; i < spec.repeatNodeCount; i++)
  1349. nodeNames.push("");
  1350. let topTerms = Object.keys(spec.topRules).map(r => spec.topRules[r][1]);
  1351. let nodeProps = [];
  1352. for (let i = 0; i < nodeNames.length; i++)
  1353. nodeProps.push([]);
  1354. function setProp(nodeID, prop, value) {
  1355. nodeProps[nodeID].push([prop, prop.deserialize(String(value))]);
  1356. }
  1357. if (spec.nodeProps)
  1358. for (let propSpec of spec.nodeProps) {
  1359. let prop = propSpec[0];
  1360. if (typeof prop == "string")
  1361. prop = NodeProp[prop];
  1362. for (let i = 1; i < propSpec.length;) {
  1363. let next = propSpec[i++];
  1364. if (next >= 0) {
  1365. setProp(next, prop, propSpec[i++]);
  1366. }
  1367. else {
  1368. let value = propSpec[i + -next];
  1369. for (let j = -next; j > 0; j--)
  1370. setProp(propSpec[i++], prop, value);
  1371. i++;
  1372. }
  1373. }
  1374. }
  1375. this.nodeSet = new NodeSet(nodeNames.map((name, i) => NodeType.define({
  1376. name: i >= this.minRepeatTerm ? undefined : name,
  1377. id: i,
  1378. props: nodeProps[i],
  1379. top: topTerms.indexOf(i) > -1,
  1380. error: i == 0,
  1381. skipped: spec.skippedNodes && spec.skippedNodes.indexOf(i) > -1
  1382. })));
  1383. if (spec.propSources)
  1384. this.nodeSet = this.nodeSet.extend(...spec.propSources);
  1385. this.strict = false;
  1386. this.bufferLength = DefaultBufferLength;
  1387. let tokenArray = decodeArray(spec.tokenData);
  1388. this.context = spec.context;
  1389. this.specialized = new Uint16Array(spec.specialized ? spec.specialized.length : 0);
  1390. this.specializers = [];
  1391. if (spec.specialized)
  1392. for (let i = 0; i < spec.specialized.length; i++) {
  1393. this.specialized[i] = spec.specialized[i].term;
  1394. this.specializers[i] = spec.specialized[i].get;
  1395. }
  1396. this.states = decodeArray(spec.states, Uint32Array);
  1397. this.data = decodeArray(spec.stateData);
  1398. this.goto = decodeArray(spec.goto);
  1399. this.maxTerm = spec.maxTerm;
  1400. this.tokenizers = spec.tokenizers.map(value => typeof value == "number" ? new TokenGroup(tokenArray, value) : value);
  1401. this.topRules = spec.topRules;
  1402. this.dialects = spec.dialects || {};
  1403. this.dynamicPrecedences = spec.dynamicPrecedences || null;
  1404. this.tokenPrecTable = spec.tokenPrec;
  1405. this.termNames = spec.termNames || null;
  1406. this.maxNode = this.nodeSet.types.length - 1;
  1407. this.dialect = this.parseDialect();
  1408. this.top = this.topRules[Object.keys(this.topRules)[0]];
  1409. }
  1410. createParse(input, fragments, ranges) {
  1411. let parse = new Parse(this, input, fragments, ranges);
  1412. for (let w of this.wrappers)
  1413. parse = w(parse, input, fragments, ranges);
  1414. return parse;
  1415. }
  1416. /// Get a goto table entry @internal
  1417. getGoto(state, term, loose = false) {
  1418. let table = this.goto;
  1419. if (term >= table[0])
  1420. return -1;
  1421. for (let pos = table[term + 1];;) {
  1422. let groupTag = table[pos++], last = groupTag & 1;
  1423. let target = table[pos++];
  1424. if (last && loose)
  1425. return target;
  1426. for (let end = pos + (groupTag >> 1); pos < end; pos++)
  1427. if (table[pos] == state)
  1428. return target;
  1429. if (last)
  1430. return -1;
  1431. }
  1432. }
  1433. /// Check if this state has an action for a given terminal @internal
  1434. hasAction(state, terminal) {
  1435. let data = this.data;
  1436. for (let set = 0; set < 2; set++) {
  1437. for (let i = this.stateSlot(state, set ? 2 /* Skip */ : 1 /* Actions */), next;; i += 3) {
  1438. if ((next = data[i]) == 65535 /* End */) {
  1439. if (data[i + 1] == 1 /* Next */)
  1440. next = data[i = pair(data, i + 2)];
  1441. else if (data[i + 1] == 2 /* Other */)
  1442. return pair(data, i + 2);
  1443. else
  1444. break;
  1445. }
  1446. if (next == terminal || next == 0 /* Err */)
  1447. return pair(data, i + 1);
  1448. }
  1449. }
  1450. return 0;
  1451. }
  1452. /// @internal
  1453. stateSlot(state, slot) {
  1454. return this.states[(state * 6 /* Size */) + slot];
  1455. }
  1456. /// @internal
  1457. stateFlag(state, flag) {
  1458. return (this.stateSlot(state, 0 /* Flags */) & flag) > 0;
  1459. }
  1460. /// @internal
  1461. validAction(state, action) {
  1462. if (action == this.stateSlot(state, 4 /* DefaultReduce */))
  1463. return true;
  1464. for (let i = this.stateSlot(state, 1 /* Actions */);; i += 3) {
  1465. if (this.data[i] == 65535 /* End */) {
  1466. if (this.data[i + 1] == 1 /* Next */)
  1467. i = pair(this.data, i + 2);
  1468. else
  1469. return false;
  1470. }
  1471. if (action == pair(this.data, i + 1))
  1472. return true;
  1473. }
  1474. }
  1475. /// Get the states that can follow this one through shift actions or
  1476. /// goto jumps. @internal
  1477. nextStates(state) {
  1478. let result = [];
  1479. for (let i = this.stateSlot(state, 1 /* Actions */);; i += 3) {
  1480. if (this.data[i] == 65535 /* End */) {
  1481. if (this.data[i + 1] == 1 /* Next */)
  1482. i = pair(this.data, i + 2);
  1483. else
  1484. break;
  1485. }
  1486. if ((this.data[i + 2] & (65536 /* ReduceFlag */ >> 16)) == 0) {
  1487. let value = this.data[i + 1];
  1488. if (!result.some((v, i) => (i & 1) && v == value))
  1489. result.push(this.data[i], value);
  1490. }
  1491. }
  1492. return result;
  1493. }
  1494. /// @internal
  1495. overrides(token, prev) {
  1496. let iPrev = findOffset(this.data, this.tokenPrecTable, prev);
  1497. return iPrev < 0 || findOffset(this.data, this.tokenPrecTable, token) < iPrev;
  1498. }
  1499. /// Configure the parser. Returns a new parser instance that has the
  1500. /// given settings modified. Settings not provided in `config` are
  1501. /// kept from the original parser.
  1502. configure(config) {
  1503. // Hideous reflection-based kludge to make it easy to create a
  1504. // slightly modified copy of a parser.
  1505. let copy = Object.assign(Object.create(LRParser.prototype), this);
  1506. if (config.props)
  1507. copy.nodeSet = this.nodeSet.extend(...config.props);
  1508. if (config.top) {
  1509. let info = this.topRules[config.top];
  1510. if (!info)
  1511. throw new RangeError(`Invalid top rule name ${config.top}`);
  1512. copy.top = info;
  1513. }
  1514. if (config.tokenizers)
  1515. copy.tokenizers = this.tokenizers.map(t => {
  1516. let found = config.tokenizers.find(r => r.from == t);
  1517. return found ? found.to : t;
  1518. });
  1519. if (config.specializers)
  1520. copy.specializers = this.specializers.map(s => {
  1521. let found = config.specializers.find(r => r.from == s);
  1522. return found ? found.to : s;
  1523. });
  1524. if (config.contextTracker)
  1525. copy.context = config.contextTracker;
  1526. if (config.dialect)
  1527. copy.dialect = this.parseDialect(config.dialect);
  1528. if (config.strict != null)
  1529. copy.strict = config.strict;
  1530. if (config.wrap)
  1531. copy.wrappers = copy.wrappers.concat(config.wrap);
  1532. if (config.bufferLength != null)
  1533. copy.bufferLength = config.bufferLength;
  1534. return copy;
  1535. }
  1536. /// Tells you whether any [parse wrappers](#lr.ParserConfig.wrap)
  1537. /// are registered for this parser.
  1538. hasWrappers() {
  1539. return this.wrappers.length > 0;
  1540. }
  1541. /// Returns the name associated with a given term. This will only
  1542. /// work for all terms when the parser was generated with the
  1543. /// `--names` option. By default, only the names of tagged terms are
  1544. /// stored.
  1545. getName(term) {
  1546. return this.termNames ? this.termNames[term] : String(term <= this.maxNode && this.nodeSet.types[term].name || term);
  1547. }
  1548. /// The eof term id is always allocated directly after the node
  1549. /// types. @internal
  1550. get eofTerm() { return this.maxNode + 1; }
  1551. /// The type of top node produced by the parser.
  1552. get topNode() { return this.nodeSet.types[this.top[1]]; }
  1553. /// @internal
  1554. dynamicPrecedence(term) {
  1555. let prec = this.dynamicPrecedences;
  1556. return prec == null ? 0 : prec[term] || 0;
  1557. }
  1558. /// @internal
  1559. parseDialect(dialect) {
  1560. let values = Object.keys(this.dialects), flags = values.map(() => false);
  1561. if (dialect)
  1562. for (let part of dialect.split(" ")) {
  1563. let id = values.indexOf(part);
  1564. if (id >= 0)
  1565. flags[id] = true;
  1566. }
  1567. let disabled = null;
  1568. for (let i = 0; i < values.length; i++)
  1569. if (!flags[i]) {
  1570. for (let j = this.dialects[values[i]], id; (id = this.data[j++]) != 65535 /* End */;)
  1571. (disabled || (disabled = new Uint8Array(this.maxTerm + 1)))[id] = 1;
  1572. }
  1573. return new Dialect(dialect, flags, disabled);
  1574. }
  1575. /// (used by the output of the parser generator) @internal
  1576. static deserialize(spec) {
  1577. return new LRParser(spec);
  1578. }
  1579. }
  1580. function pair(data, off) { return data[off] | (data[off + 1] << 16); }
  1581. function findOffset(data, start, term) {
  1582. for (let i = start, next; (next = data[i]) != 65535 /* End */; i++)
  1583. if (next == term)
  1584. return i - start;
  1585. return -1;
  1586. }
  1587. function findFinished(stacks) {
  1588. let best = null;
  1589. for (let stack of stacks) {
  1590. let stopped = stack.p.stoppedAt;
  1591. if ((stack.pos == stack.p.stream.end || stopped != null && stack.pos > stopped) &&
  1592. stack.p.parser.stateFlag(stack.state, 2 /* Accepting */) &&
  1593. (!best || best.score < stack.score))
  1594. best = stack;
  1595. }
  1596. return best;
  1597. }
  1598. export { ContextTracker, ExternalTokenizer, InputStream, LRParser, Stack };