index.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. // FIXME profile adding a per-Tree TreeNode cache, validating it by
  2. // parent pointer
  3. /// The default maximum length of a `TreeBuffer` node.
  4. const DefaultBufferLength = 1024;
  5. let nextPropID = 0;
  6. class Range {
  7. constructor(from, to) {
  8. this.from = from;
  9. this.to = to;
  10. }
  11. }
  12. /// Each [node type](#common.NodeType) or [individual tree](#common.Tree)
  13. /// can have metadata associated with it in props. Instances of this
  14. /// class represent prop names.
  15. class NodeProp {
  16. /// Create a new node prop type.
  17. constructor(config = {}) {
  18. this.id = nextPropID++;
  19. this.perNode = !!config.perNode;
  20. this.deserialize = config.deserialize || (() => {
  21. throw new Error("This node type doesn't define a deserialize function");
  22. });
  23. }
  24. /// This is meant to be used with
  25. /// [`NodeSet.extend`](#common.NodeSet.extend) or
  26. /// [`LRParser.configure`](#lr.ParserConfig.props) to compute
  27. /// prop values for each node type in the set. Takes a [match
  28. /// object](#common.NodeType^match) or function that returns undefined
  29. /// if the node type doesn't get this prop, and the prop's value if
  30. /// it does.
  31. add(match) {
  32. if (this.perNode)
  33. throw new RangeError("Can't add per-node props to node types");
  34. if (typeof match != "function")
  35. match = NodeType.match(match);
  36. return (type) => {
  37. let result = match(type);
  38. return result === undefined ? null : [this, result];
  39. };
  40. }
  41. }
  42. /// Prop that is used to describe matching delimiters. For opening
  43. /// delimiters, this holds an array of node names (written as a
  44. /// space-separated string when declaring this prop in a grammar)
  45. /// for the node types of closing delimiters that match it.
  46. NodeProp.closedBy = new NodeProp({ deserialize: str => str.split(" ") });
  47. /// The inverse of [`closedBy`](#common.NodeProp^closedBy). This is
  48. /// attached to closing delimiters, holding an array of node names
  49. /// of types of matching opening delimiters.
  50. NodeProp.openedBy = new NodeProp({ deserialize: str => str.split(" ") });
  51. /// Used to assign node types to groups (for example, all node
  52. /// types that represent an expression could be tagged with an
  53. /// `"Expression"` group).
  54. NodeProp.group = new NodeProp({ deserialize: str => str.split(" ") });
  55. /// The hash of the [context](#lr.ContextTracker.constructor)
  56. /// that the node was parsed in, if any. Used to limit reuse of
  57. /// contextual nodes.
  58. NodeProp.contextHash = new NodeProp({ perNode: true });
  59. /// The distance beyond the end of the node that the tokenizer
  60. /// looked ahead for any of the tokens inside the node. (The LR
  61. /// parser only stores this when it is larger than 25, for
  62. /// efficiency reasons.)
  63. NodeProp.lookAhead = new NodeProp({ perNode: true });
  64. /// This per-node prop is used to replace a given node, or part of a
  65. /// node, with another tree. This is useful to include trees from
  66. /// different languages in mixed-language parsers.
  67. NodeProp.mounted = new NodeProp({ perNode: true });
  68. /// A mounted tree, which can be [stored](#common.NodeProp^mounted) on
  69. /// a tree node to indicate that parts of its content are
  70. /// represented by another tree.
  71. class MountedTree {
  72. constructor(
  73. /// The inner tree.
  74. tree,
  75. /// If this is null, this tree replaces the entire node (it will
  76. /// be included in the regular iteration instead of its host
  77. /// node). If not, only the given ranges are considered to be
  78. /// covered by this tree. This is used for trees that are mixed in
  79. /// a way that isn't strictly hierarchical. Such mounted trees are
  80. /// only entered by [`resolveInner`](#common.Tree.resolveInner)
  81. /// and [`enter`](#common.SyntaxNode.enter).
  82. overlay,
  83. /// The parser used to create this subtree.
  84. parser) {
  85. this.tree = tree;
  86. this.overlay = overlay;
  87. this.parser = parser;
  88. }
  89. }
  90. const noProps = Object.create(null);
  91. /// Each node in a syntax tree has a node type associated with it.
  92. class NodeType {
  93. /// @internal
  94. constructor(
  95. /// The name of the node type. Not necessarily unique, but if the
  96. /// grammar was written properly, different node types with the
  97. /// same name within a node set should play the same semantic
  98. /// role.
  99. name,
  100. /// @internal
  101. props,
  102. /// The id of this node in its set. Corresponds to the term ids
  103. /// used in the parser.
  104. id,
  105. /// @internal
  106. flags = 0) {
  107. this.name = name;
  108. this.props = props;
  109. this.id = id;
  110. this.flags = flags;
  111. }
  112. /// Define a node type.
  113. static define(spec) {
  114. let props = spec.props && spec.props.length ? Object.create(null) : noProps;
  115. let flags = (spec.top ? 1 /* Top */ : 0) | (spec.skipped ? 2 /* Skipped */ : 0) |
  116. (spec.error ? 4 /* Error */ : 0) | (spec.name == null ? 8 /* Anonymous */ : 0);
  117. let type = new NodeType(spec.name || "", props, spec.id, flags);
  118. if (spec.props)
  119. for (let src of spec.props) {
  120. if (!Array.isArray(src))
  121. src = src(type);
  122. if (src) {
  123. if (src[0].perNode)
  124. throw new RangeError("Can't store a per-node prop on a node type");
  125. props[src[0].id] = src[1];
  126. }
  127. }
  128. return type;
  129. }
  130. /// Retrieves a node prop for this type. Will return `undefined` if
  131. /// the prop isn't present on this node.
  132. prop(prop) { return this.props[prop.id]; }
  133. /// True when this is the top node of a grammar.
  134. get isTop() { return (this.flags & 1 /* Top */) > 0; }
  135. /// True when this node is produced by a skip rule.
  136. get isSkipped() { return (this.flags & 2 /* Skipped */) > 0; }
  137. /// Indicates whether this is an error node.
  138. get isError() { return (this.flags & 4 /* Error */) > 0; }
  139. /// When true, this node type doesn't correspond to a user-declared
  140. /// named node, for example because it is used to cache repetition.
  141. get isAnonymous() { return (this.flags & 8 /* Anonymous */) > 0; }
  142. /// Returns true when this node's name or one of its
  143. /// [groups](#common.NodeProp^group) matches the given string.
  144. is(name) {
  145. if (typeof name == 'string') {
  146. if (this.name == name)
  147. return true;
  148. let group = this.prop(NodeProp.group);
  149. return group ? group.indexOf(name) > -1 : false;
  150. }
  151. return this.id == name;
  152. }
  153. /// Create a function from node types to arbitrary values by
  154. /// specifying an object whose property names are node or
  155. /// [group](#common.NodeProp^group) names. Often useful with
  156. /// [`NodeProp.add`](#common.NodeProp.add). You can put multiple
  157. /// names, separated by spaces, in a single property name to map
  158. /// multiple node names to a single value.
  159. static match(map) {
  160. let direct = Object.create(null);
  161. for (let prop in map)
  162. for (let name of prop.split(" "))
  163. direct[name] = map[prop];
  164. return (node) => {
  165. for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
  166. let found = direct[i < 0 ? node.name : groups[i]];
  167. if (found)
  168. return found;
  169. }
  170. };
  171. }
  172. }
  173. /// An empty dummy node type to use when no actual type is available.
  174. NodeType.none = new NodeType("", Object.create(null), 0, 8 /* Anonymous */);
  175. /// A node set holds a collection of node types. It is used to
  176. /// compactly represent trees by storing their type ids, rather than a
  177. /// full pointer to the type object, in a numeric array. Each parser
  178. /// [has](#lr.LRParser.nodeSet) a node set, and [tree
  179. /// buffers](#common.TreeBuffer) can only store collections of nodes
  180. /// from the same set. A set can have a maximum of 2**16 (65536) node
  181. /// types in it, so that the ids fit into 16-bit typed array slots.
  182. class NodeSet {
  183. /// Create a set with the given types. The `id` property of each
  184. /// type should correspond to its position within the array.
  185. constructor(
  186. /// The node types in this set, by id.
  187. types) {
  188. this.types = types;
  189. for (let i = 0; i < types.length; i++)
  190. if (types[i].id != i)
  191. throw new RangeError("Node type ids should correspond to array positions when creating a node set");
  192. }
  193. /// Create a copy of this set with some node properties added. The
  194. /// arguments to this method can be created with
  195. /// [`NodeProp.add`](#common.NodeProp.add).
  196. extend(...props) {
  197. let newTypes = [];
  198. for (let type of this.types) {
  199. let newProps = null;
  200. for (let source of props) {
  201. let add = source(type);
  202. if (add) {
  203. if (!newProps)
  204. newProps = Object.assign({}, type.props);
  205. newProps[add[0].id] = add[1];
  206. }
  207. }
  208. newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type);
  209. }
  210. return new NodeSet(newTypes);
  211. }
  212. }
  213. const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
  214. /// Options that control iteration. Can be combined with the `|`
  215. /// operator to enable multiple ones.
  216. var IterMode;
  217. (function (IterMode) {
  218. /// When enabled, iteration will only visit [`Tree`](#common.Tree)
  219. /// objects, not nodes packed into
  220. /// [`TreeBuffer`](#common.TreeBuffer)s.
  221. IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers";
  222. /// Enable this to make iteration include anonymous nodes (such as
  223. /// the nodes that wrap repeated grammar constructs into a balanced
  224. /// tree).
  225. IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous";
  226. /// By default, regular [mounted](#common.NodeProp^mounted) nodes
  227. /// replace their base node in iteration. Enable this to ignore them
  228. /// instead.
  229. IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts";
  230. /// This option only applies in
  231. /// [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
  232. /// library to not enter mounted overlays if one covers the given
  233. /// position.
  234. IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays";
  235. })(IterMode || (IterMode = {}));
  236. /// A piece of syntax tree. There are two ways to approach these
  237. /// trees: the way they are actually stored in memory, and the
  238. /// convenient way.
  239. ///
  240. /// Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
  241. /// objects. By packing detail information into `TreeBuffer` leaf
  242. /// nodes, the representation is made a lot more memory-efficient.
  243. ///
  244. /// However, when you want to actually work with tree nodes, this
  245. /// representation is very awkward, so most client code will want to
  246. /// use the [`TreeCursor`](#common.TreeCursor) or
  247. /// [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
  248. /// a view on some part of this data structure, and can be used to
  249. /// move around to adjacent nodes.
  250. class Tree {
  251. /// Construct a new tree. See also [`Tree.build`](#common.Tree^build).
  252. constructor(
  253. /// The type of the top node.
  254. type,
  255. /// This node's child nodes.
  256. children,
  257. /// The positions (offsets relative to the start of this tree) of
  258. /// the children.
  259. positions,
  260. /// The total length of this tree
  261. length,
  262. /// Per-node [node props](#common.NodeProp) to associate with this node.
  263. props) {
  264. this.type = type;
  265. this.children = children;
  266. this.positions = positions;
  267. this.length = length;
  268. /// @internal
  269. this.props = null;
  270. if (props && props.length) {
  271. this.props = Object.create(null);
  272. for (let [prop, value] of props)
  273. this.props[typeof prop == "number" ? prop : prop.id] = value;
  274. }
  275. }
  276. /// @internal
  277. toString() {
  278. let mounted = this.prop(NodeProp.mounted);
  279. if (mounted && !mounted.overlay)
  280. return mounted.tree.toString();
  281. let children = "";
  282. for (let ch of this.children) {
  283. let str = ch.toString();
  284. if (str) {
  285. if (children)
  286. children += ",";
  287. children += str;
  288. }
  289. }
  290. return !this.type.name ? children :
  291. (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) +
  292. (children.length ? "(" + children + ")" : "");
  293. }
  294. /// Get a [tree cursor](#common.TreeCursor) positioned at the top of
  295. /// the tree. Mode can be used to [control](#common.IterMode) which
  296. /// nodes the cursor visits.
  297. cursor(mode = 0) {
  298. return new TreeCursor(this.topNode, mode);
  299. }
  300. /// Get a [tree cursor](#common.TreeCursor) pointing into this tree
  301. /// at the given position and side (see
  302. /// [`moveTo`](#common.TreeCursor.moveTo).
  303. cursorAt(pos, side = 0, mode = 0) {
  304. let scope = CachedNode.get(this) || this.topNode;
  305. let cursor = new TreeCursor(scope);
  306. cursor.moveTo(pos, side);
  307. CachedNode.set(this, cursor._tree);
  308. return cursor;
  309. }
  310. /// Get a [syntax node](#common.SyntaxNode) object for the top of the
  311. /// tree.
  312. get topNode() {
  313. return new TreeNode(this, 0, 0, null);
  314. }
  315. /// Get the [syntax node](#common.SyntaxNode) at the given position.
  316. /// If `side` is -1, this will move into nodes that end at the
  317. /// position. If 1, it'll move into nodes that start at the
  318. /// position. With 0, it'll only enter nodes that cover the position
  319. /// from both sides.
  320. resolve(pos, side = 0) {
  321. let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
  322. CachedNode.set(this, node);
  323. return node;
  324. }
  325. /// Like [`resolve`](#common.Tree.resolve), but will enter
  326. /// [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
  327. /// pointing into the innermost overlaid tree at the given position
  328. /// (with parent links going through all parent structure, including
  329. /// the host trees).
  330. resolveInner(pos, side = 0) {
  331. let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
  332. CachedInnerNode.set(this, node);
  333. return node;
  334. }
  335. /// Iterate over the tree and its children, calling `enter` for any
  336. /// node that touches the `from`/`to` region (if given) before
  337. /// running over such a node's children, and `leave` (if given) when
  338. /// leaving the node. When `enter` returns `false`, that node will
  339. /// not have its children iterated over (or `leave` called).
  340. iterate(spec) {
  341. let { enter, leave, from = 0, to = this.length } = spec;
  342. for (let c = this.cursor((spec.mode || 0) | IterMode.IncludeAnonymous);;) {
  343. let entered = false;
  344. if (c.from <= to && c.to >= from && (c.type.isAnonymous || enter(c) !== false)) {
  345. if (c.firstChild())
  346. continue;
  347. entered = true;
  348. }
  349. for (;;) {
  350. if (entered && leave && !c.type.isAnonymous)
  351. leave(c);
  352. if (c.nextSibling())
  353. break;
  354. if (!c.parent())
  355. return;
  356. entered = true;
  357. }
  358. }
  359. }
  360. /// Get the value of the given [node prop](#common.NodeProp) for this
  361. /// node. Works with both per-node and per-type props.
  362. prop(prop) {
  363. return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined;
  364. }
  365. /// Returns the node's [per-node props](#common.NodeProp.perNode) in a
  366. /// format that can be passed to the [`Tree`](#common.Tree)
  367. /// constructor.
  368. get propValues() {
  369. let result = [];
  370. if (this.props)
  371. for (let id in this.props)
  372. result.push([+id, this.props[id]]);
  373. return result;
  374. }
  375. /// Balance the direct children of this tree, producing a copy of
  376. /// which may have children grouped into subtrees with type
  377. /// [`NodeType.none`](#common.NodeType^none).
  378. balance(config = {}) {
  379. return this.children.length <= 8 /* BranchFactor */ ? this :
  380. balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
  381. }
  382. /// Build a tree from a postfix-ordered buffer of node information,
  383. /// or a cursor over such a buffer.
  384. static build(data) { return buildTree(data); }
  385. }
  386. /// The empty tree
  387. Tree.empty = new Tree(NodeType.none, [], [], 0);
  388. class FlatBufferCursor {
  389. constructor(buffer, index) {
  390. this.buffer = buffer;
  391. this.index = index;
  392. }
  393. get id() { return this.buffer[this.index - 4]; }
  394. get start() { return this.buffer[this.index - 3]; }
  395. get end() { return this.buffer[this.index - 2]; }
  396. get size() { return this.buffer[this.index - 1]; }
  397. get pos() { return this.index; }
  398. next() { this.index -= 4; }
  399. fork() { return new FlatBufferCursor(this.buffer, this.index); }
  400. }
  401. /// Tree buffers contain (type, start, end, endIndex) quads for each
  402. /// node. In such a buffer, nodes are stored in prefix order (parents
  403. /// before children, with the endIndex of the parent indicating which
  404. /// children belong to it).
  405. class TreeBuffer {
  406. /// Create a tree buffer.
  407. constructor(
  408. /// The buffer's content.
  409. buffer,
  410. /// The total length of the group of nodes in the buffer.
  411. length,
  412. /// The node set used in this buffer.
  413. set) {
  414. this.buffer = buffer;
  415. this.length = length;
  416. this.set = set;
  417. }
  418. /// @internal
  419. get type() { return NodeType.none; }
  420. /// @internal
  421. toString() {
  422. let result = [];
  423. for (let index = 0; index < this.buffer.length;) {
  424. result.push(this.childString(index));
  425. index = this.buffer[index + 3];
  426. }
  427. return result.join(",");
  428. }
  429. /// @internal
  430. childString(index) {
  431. let id = this.buffer[index], endIndex = this.buffer[index + 3];
  432. let type = this.set.types[id], result = type.name;
  433. if (/\W/.test(result) && !type.isError)
  434. result = JSON.stringify(result);
  435. index += 4;
  436. if (endIndex == index)
  437. return result;
  438. let children = [];
  439. while (index < endIndex) {
  440. children.push(this.childString(index));
  441. index = this.buffer[index + 3];
  442. }
  443. return result + "(" + children.join(",") + ")";
  444. }
  445. /// @internal
  446. findChild(startIndex, endIndex, dir, pos, side) {
  447. let { buffer } = this, pick = -1;
  448. for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
  449. if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
  450. pick = i;
  451. if (dir > 0)
  452. break;
  453. }
  454. }
  455. return pick;
  456. }
  457. /// @internal
  458. slice(startI, endI, from, to) {
  459. let b = this.buffer;
  460. let copy = new Uint16Array(endI - startI);
  461. for (let i = startI, j = 0; i < endI;) {
  462. copy[j++] = b[i++];
  463. copy[j++] = b[i++] - from;
  464. copy[j++] = b[i++] - from;
  465. copy[j++] = b[i++] - startI;
  466. }
  467. return new TreeBuffer(copy, to - from, this.set);
  468. }
  469. }
  470. function checkSide(side, pos, from, to) {
  471. switch (side) {
  472. case -2 /* Before */: return from < pos;
  473. case -1 /* AtOrBefore */: return to >= pos && from < pos;
  474. case 0 /* Around */: return from < pos && to > pos;
  475. case 1 /* AtOrAfter */: return from <= pos && to > pos;
  476. case 2 /* After */: return to > pos;
  477. case 4 /* DontCare */: return true;
  478. }
  479. }
  480. function enterUnfinishedNodesBefore(node, pos) {
  481. let scan = node.childBefore(pos);
  482. while (scan) {
  483. let last = scan.lastChild;
  484. if (!last || last.to != scan.to)
  485. break;
  486. if (last.type.isError && last.from == last.to) {
  487. node = scan;
  488. scan = last.prevSibling;
  489. }
  490. else {
  491. scan = last;
  492. }
  493. }
  494. return node;
  495. }
  496. function resolveNode(node, pos, side, overlays) {
  497. var _a;
  498. // Move up to a node that actually holds the position, if possible
  499. while (node.from == node.to ||
  500. (side < 1 ? node.from >= pos : node.from > pos) ||
  501. (side > -1 ? node.to <= pos : node.to < pos)) {
  502. let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
  503. if (!parent)
  504. return node;
  505. node = parent;
  506. }
  507. let mode = overlays ? 0 : IterMode.IgnoreOverlays;
  508. // Must go up out of overlays when those do not overlap with pos
  509. if (overlays)
  510. for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
  511. if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
  512. node = parent;
  513. }
  514. for (;;) {
  515. let inner = node.enter(pos, side, mode);
  516. if (!inner)
  517. return node;
  518. node = inner;
  519. }
  520. }
  521. class TreeNode {
  522. constructor(_tree, from,
  523. // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
  524. index, _parent) {
  525. this._tree = _tree;
  526. this.from = from;
  527. this.index = index;
  528. this._parent = _parent;
  529. }
  530. get type() { return this._tree.type; }
  531. get name() { return this._tree.type.name; }
  532. get to() { return this.from + this._tree.length; }
  533. nextChild(i, dir, pos, side, mode = 0) {
  534. for (let parent = this;;) {
  535. for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
  536. let next = children[i], start = positions[i] + parent.from;
  537. if (!checkSide(side, pos, start, start + next.length))
  538. continue;
  539. if (next instanceof TreeBuffer) {
  540. if (mode & IterMode.ExcludeBuffers)
  541. continue;
  542. let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
  543. if (index > -1)
  544. return new BufferNode(new BufferContext(parent, next, i, start), null, index);
  545. }
  546. else if ((mode & IterMode.IncludeAnonymous) || (!next.type.isAnonymous || hasChild(next))) {
  547. let mounted;
  548. if (!(mode & IterMode.IgnoreMounts) &&
  549. next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay)
  550. return new TreeNode(mounted.tree, start, i, parent);
  551. let inner = new TreeNode(next, start, i, parent);
  552. return (mode & IterMode.IncludeAnonymous) || !inner.type.isAnonymous ? inner
  553. : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side);
  554. }
  555. }
  556. if ((mode & IterMode.IncludeAnonymous) || !parent.type.isAnonymous)
  557. return null;
  558. if (parent.index >= 0)
  559. i = parent.index + dir;
  560. else
  561. i = dir < 0 ? -1 : parent._parent._tree.children.length;
  562. parent = parent._parent;
  563. if (!parent)
  564. return null;
  565. }
  566. }
  567. get firstChild() { return this.nextChild(0, 1, 0, 4 /* DontCare */); }
  568. get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* DontCare */); }
  569. childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* After */); }
  570. childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Before */); }
  571. enter(pos, side, mode = 0) {
  572. let mounted;
  573. if (!(mode & IterMode.IgnoreOverlays) && (mounted = this._tree.prop(NodeProp.mounted)) && mounted.overlay) {
  574. let rPos = pos - this.from;
  575. for (let { from, to } of mounted.overlay) {
  576. if ((side > 0 ? from <= rPos : from < rPos) &&
  577. (side < 0 ? to >= rPos : to > rPos))
  578. return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
  579. }
  580. }
  581. return this.nextChild(0, 1, pos, side, mode);
  582. }
  583. nextSignificantParent() {
  584. let val = this;
  585. while (val.type.isAnonymous && val._parent)
  586. val = val._parent;
  587. return val;
  588. }
  589. get parent() {
  590. return this._parent ? this._parent.nextSignificantParent() : null;
  591. }
  592. get nextSibling() {
  593. return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* DontCare */) : null;
  594. }
  595. get prevSibling() {
  596. return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* DontCare */) : null;
  597. }
  598. cursor(mode = 0) { return new TreeCursor(this, mode); }
  599. get tree() { return this._tree; }
  600. toTree() { return this._tree; }
  601. resolve(pos, side = 0) {
  602. return resolveNode(this, pos, side, false);
  603. }
  604. resolveInner(pos, side = 0) {
  605. return resolveNode(this, pos, side, true);
  606. }
  607. enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
  608. getChild(type, before = null, after = null) {
  609. let r = getChildren(this, type, before, after);
  610. return r.length ? r[0] : null;
  611. }
  612. getChildren(type, before = null, after = null) {
  613. return getChildren(this, type, before, after);
  614. }
  615. /// @internal
  616. toString() { return this._tree.toString(); }
  617. get node() { return this; }
  618. matchContext(context) { return matchNodeContext(this, context); }
  619. }
  620. function getChildren(node, type, before, after) {
  621. let cur = node.cursor(), result = [];
  622. if (!cur.firstChild())
  623. return result;
  624. if (before != null)
  625. while (!cur.type.is(before))
  626. if (!cur.nextSibling())
  627. return result;
  628. for (;;) {
  629. if (after != null && cur.type.is(after))
  630. return result;
  631. if (cur.type.is(type))
  632. result.push(cur.node);
  633. if (!cur.nextSibling())
  634. return after == null ? result : [];
  635. }
  636. }
  637. function matchNodeContext(node, context, i = context.length - 1) {
  638. for (let p = node.parent; i >= 0; p = p.parent) {
  639. if (!p)
  640. return false;
  641. if (!p.type.isAnonymous) {
  642. if (context[i] && context[i] != p.name)
  643. return false;
  644. i--;
  645. }
  646. }
  647. return true;
  648. }
  649. class BufferContext {
  650. constructor(parent, buffer, index, start) {
  651. this.parent = parent;
  652. this.buffer = buffer;
  653. this.index = index;
  654. this.start = start;
  655. }
  656. }
  657. class BufferNode {
  658. constructor(context, _parent, index) {
  659. this.context = context;
  660. this._parent = _parent;
  661. this.index = index;
  662. this.type = context.buffer.set.types[context.buffer.buffer[index]];
  663. }
  664. get name() { return this.type.name; }
  665. get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
  666. get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
  667. child(dir, pos, side) {
  668. let { buffer } = this.context;
  669. let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
  670. return index < 0 ? null : new BufferNode(this.context, this, index);
  671. }
  672. get firstChild() { return this.child(1, 0, 4 /* DontCare */); }
  673. get lastChild() { return this.child(-1, 0, 4 /* DontCare */); }
  674. childAfter(pos) { return this.child(1, pos, 2 /* After */); }
  675. childBefore(pos) { return this.child(-1, pos, -2 /* Before */); }
  676. enter(pos, side, mode = 0) {
  677. if (mode & IterMode.ExcludeBuffers)
  678. return null;
  679. let { buffer } = this.context;
  680. let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
  681. return index < 0 ? null : new BufferNode(this.context, this, index);
  682. }
  683. get parent() {
  684. return this._parent || this.context.parent.nextSignificantParent();
  685. }
  686. externalSibling(dir) {
  687. return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* DontCare */);
  688. }
  689. get nextSibling() {
  690. let { buffer } = this.context;
  691. let after = buffer.buffer[this.index + 3];
  692. if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
  693. return new BufferNode(this.context, this._parent, after);
  694. return this.externalSibling(1);
  695. }
  696. get prevSibling() {
  697. let { buffer } = this.context;
  698. let parentStart = this._parent ? this._parent.index + 4 : 0;
  699. if (this.index == parentStart)
  700. return this.externalSibling(-1);
  701. return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* DontCare */));
  702. }
  703. cursor(mode = 0) { return new TreeCursor(this, mode); }
  704. get tree() { return null; }
  705. toTree() {
  706. let children = [], positions = [];
  707. let { buffer } = this.context;
  708. let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
  709. if (endI > startI) {
  710. let from = buffer.buffer[this.index + 1], to = buffer.buffer[this.index + 2];
  711. children.push(buffer.slice(startI, endI, from, to));
  712. positions.push(0);
  713. }
  714. return new Tree(this.type, children, positions, this.to - this.from);
  715. }
  716. resolve(pos, side = 0) {
  717. return resolveNode(this, pos, side, false);
  718. }
  719. resolveInner(pos, side = 0) {
  720. return resolveNode(this, pos, side, true);
  721. }
  722. enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
  723. /// @internal
  724. toString() { return this.context.buffer.childString(this.index); }
  725. getChild(type, before = null, after = null) {
  726. let r = getChildren(this, type, before, after);
  727. return r.length ? r[0] : null;
  728. }
  729. getChildren(type, before = null, after = null) {
  730. return getChildren(this, type, before, after);
  731. }
  732. get node() { return this; }
  733. matchContext(context) { return matchNodeContext(this, context); }
  734. }
  735. /// A tree cursor object focuses on a given node in a syntax tree, and
  736. /// allows you to move to adjacent nodes.
  737. class TreeCursor {
  738. /// @internal
  739. constructor(node,
  740. /// @internal
  741. mode = 0) {
  742. this.mode = mode;
  743. /// @internal
  744. this.buffer = null;
  745. this.stack = [];
  746. /// @internal
  747. this.index = 0;
  748. this.bufferNode = null;
  749. if (node instanceof TreeNode) {
  750. this.yieldNode(node);
  751. }
  752. else {
  753. this._tree = node.context.parent;
  754. this.buffer = node.context;
  755. for (let n = node._parent; n; n = n._parent)
  756. this.stack.unshift(n.index);
  757. this.bufferNode = node;
  758. this.yieldBuf(node.index);
  759. }
  760. }
  761. /// Shorthand for `.type.name`.
  762. get name() { return this.type.name; }
  763. yieldNode(node) {
  764. if (!node)
  765. return false;
  766. this._tree = node;
  767. this.type = node.type;
  768. this.from = node.from;
  769. this.to = node.to;
  770. return true;
  771. }
  772. yieldBuf(index, type) {
  773. this.index = index;
  774. let { start, buffer } = this.buffer;
  775. this.type = type || buffer.set.types[buffer.buffer[index]];
  776. this.from = start + buffer.buffer[index + 1];
  777. this.to = start + buffer.buffer[index + 2];
  778. return true;
  779. }
  780. yield(node) {
  781. if (!node)
  782. return false;
  783. if (node instanceof TreeNode) {
  784. this.buffer = null;
  785. return this.yieldNode(node);
  786. }
  787. this.buffer = node.context;
  788. return this.yieldBuf(node.index, node.type);
  789. }
  790. /// @internal
  791. toString() {
  792. return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
  793. }
  794. /// @internal
  795. enterChild(dir, pos, side) {
  796. if (!this.buffer)
  797. return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
  798. let { buffer } = this.buffer;
  799. let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
  800. if (index < 0)
  801. return false;
  802. this.stack.push(this.index);
  803. return this.yieldBuf(index);
  804. }
  805. /// Move the cursor to this node's first child. When this returns
  806. /// false, the node has no child, and the cursor has not been moved.
  807. firstChild() { return this.enterChild(1, 0, 4 /* DontCare */); }
  808. /// Move the cursor to this node's last child.
  809. lastChild() { return this.enterChild(-1, 0, 4 /* DontCare */); }
  810. /// Move the cursor to the first child that ends after `pos`.
  811. childAfter(pos) { return this.enterChild(1, pos, 2 /* After */); }
  812. /// Move to the last child that starts before `pos`.
  813. childBefore(pos) { return this.enterChild(-1, pos, -2 /* Before */); }
  814. /// Move the cursor to the child around `pos`. If side is -1 the
  815. /// child may end at that position, when 1 it may start there. This
  816. /// will also enter [overlaid](#common.MountedTree.overlay)
  817. /// [mounted](#common.NodeProp^mounted) trees unless `overlays` is
  818. /// set to false.
  819. enter(pos, side, mode = this.mode) {
  820. if (!this.buffer)
  821. return this.yield(this._tree.enter(pos, side, mode));
  822. return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
  823. }
  824. /// Move to the node's parent node, if this isn't the top node.
  825. parent() {
  826. if (!this.buffer)
  827. return this.yieldNode((this.mode & IterMode.IncludeAnonymous) ? this._tree._parent : this._tree.parent);
  828. if (this.stack.length)
  829. return this.yieldBuf(this.stack.pop());
  830. let parent = (this.mode & IterMode.IncludeAnonymous) ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
  831. this.buffer = null;
  832. return this.yieldNode(parent);
  833. }
  834. /// @internal
  835. sibling(dir) {
  836. if (!this.buffer)
  837. return !this._tree._parent ? false
  838. : this.yield(this._tree.index < 0 ? null
  839. : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* DontCare */, this.mode));
  840. let { buffer } = this.buffer, d = this.stack.length - 1;
  841. if (dir < 0) {
  842. let parentStart = d < 0 ? 0 : this.stack[d] + 4;
  843. if (this.index != parentStart)
  844. return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* DontCare */));
  845. }
  846. else {
  847. let after = buffer.buffer[this.index + 3];
  848. if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
  849. return this.yieldBuf(after);
  850. }
  851. return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* DontCare */, this.mode)) : false;
  852. }
  853. /// Move to this node's next sibling, if any.
  854. nextSibling() { return this.sibling(1); }
  855. /// Move to this node's previous sibling, if any.
  856. prevSibling() { return this.sibling(-1); }
  857. atLastNode(dir) {
  858. let index, parent, { buffer } = this;
  859. if (buffer) {
  860. if (dir > 0) {
  861. if (this.index < buffer.buffer.buffer.length)
  862. return false;
  863. }
  864. else {
  865. for (let i = 0; i < this.index; i++)
  866. if (buffer.buffer.buffer[i + 3] < this.index)
  867. return false;
  868. }
  869. ({ index, parent } = buffer);
  870. }
  871. else {
  872. ({ index, _parent: parent } = this._tree);
  873. }
  874. for (; parent; { index, _parent: parent } = parent) {
  875. if (index > -1)
  876. for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
  877. let child = parent._tree.children[i];
  878. if ((this.mode & IterMode.IncludeAnonymous) ||
  879. child instanceof TreeBuffer ||
  880. !child.type.isAnonymous ||
  881. hasChild(child))
  882. return false;
  883. }
  884. }
  885. return true;
  886. }
  887. move(dir, enter) {
  888. if (enter && this.enterChild(dir, 0, 4 /* DontCare */))
  889. return true;
  890. for (;;) {
  891. if (this.sibling(dir))
  892. return true;
  893. if (this.atLastNode(dir) || !this.parent())
  894. return false;
  895. }
  896. }
  897. /// Move to the next node in a
  898. /// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR))
  899. /// traversal, going from a node to its first child or, if the
  900. /// current node is empty or `enter` is false, its next sibling or
  901. /// the next sibling of the first parent node that has one.
  902. next(enter = true) { return this.move(1, enter); }
  903. /// Move to the next node in a last-to-first pre-order traveral. A
  904. /// node is followed by its last child or, if it has none, its
  905. /// previous sibling or the previous sibling of the first parent
  906. /// node that has one.
  907. prev(enter = true) { return this.move(-1, enter); }
  908. /// Move the cursor to the innermost node that covers `pos`. If
  909. /// `side` is -1, it will enter nodes that end at `pos`. If it is 1,
  910. /// it will enter nodes that start at `pos`.
  911. moveTo(pos, side = 0) {
  912. // Move up to a node that actually holds the position, if possible
  913. while (this.from == this.to ||
  914. (side < 1 ? this.from >= pos : this.from > pos) ||
  915. (side > -1 ? this.to <= pos : this.to < pos))
  916. if (!this.parent())
  917. break;
  918. // Then scan down into child nodes as far as possible
  919. while (this.enterChild(1, pos, side)) { }
  920. return this;
  921. }
  922. /// Get a [syntax node](#common.SyntaxNode) at the cursor's current
  923. /// position.
  924. get node() {
  925. if (!this.buffer)
  926. return this._tree;
  927. let cache = this.bufferNode, result = null, depth = 0;
  928. if (cache && cache.context == this.buffer) {
  929. scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
  930. for (let c = cache; c; c = c._parent)
  931. if (c.index == index) {
  932. if (index == this.index)
  933. return c;
  934. result = c;
  935. depth = d + 1;
  936. break scan;
  937. }
  938. index = this.stack[--d];
  939. }
  940. }
  941. for (let i = depth; i < this.stack.length; i++)
  942. result = new BufferNode(this.buffer, result, this.stack[i]);
  943. return this.bufferNode = new BufferNode(this.buffer, result, this.index);
  944. }
  945. /// Get the [tree](#common.Tree) that represents the current node, if
  946. /// any. Will return null when the node is in a [tree
  947. /// buffer](#common.TreeBuffer).
  948. get tree() {
  949. return this.buffer ? null : this._tree._tree;
  950. }
  951. /// Iterate over the current node and all its descendants, calling
  952. /// `enter` when entering a node and `leave`, if given, when leaving
  953. /// one. When `enter` returns `false`, any children of that node are
  954. /// skipped, and `leave` isn't called for it.
  955. iterate(enter, leave) {
  956. for (let depth = 0;;) {
  957. let mustLeave = false;
  958. if (this.type.isAnonymous || enter(this) !== false) {
  959. if (this.firstChild()) {
  960. depth++;
  961. continue;
  962. }
  963. if (!this.type.isAnonymous)
  964. mustLeave = true;
  965. }
  966. for (;;) {
  967. if (mustLeave && leave)
  968. leave(this);
  969. mustLeave = this.type.isAnonymous;
  970. if (this.nextSibling())
  971. break;
  972. if (!depth)
  973. return;
  974. this.parent();
  975. depth--;
  976. mustLeave = true;
  977. }
  978. }
  979. }
  980. /// Test whether the current node matches a given context—a sequence
  981. /// of direct parent node names. Empty strings in the context array
  982. /// are treated as wildcards.
  983. matchContext(context) {
  984. if (!this.buffer)
  985. return matchNodeContext(this.node, context);
  986. let { buffer } = this.buffer, { types } = buffer.set;
  987. for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
  988. if (d < 0)
  989. return matchNodeContext(this.node, context, i);
  990. let type = types[buffer.buffer[this.stack[d]]];
  991. if (!type.isAnonymous) {
  992. if (context[i] && context[i] != type.name)
  993. return false;
  994. i--;
  995. }
  996. }
  997. return true;
  998. }
  999. }
  1000. function hasChild(tree) {
  1001. return tree.children.some(ch => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
  1002. }
  1003. function buildTree(data) {
  1004. var _a;
  1005. let { buffer, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
  1006. let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
  1007. let types = nodeSet.types;
  1008. let contextHash = 0, lookAhead = 0;
  1009. function takeNode(parentStart, minPos, children, positions, inRepeat) {
  1010. let { id, start, end, size } = cursor;
  1011. let lookAheadAtStart = lookAhead;
  1012. while (size < 0) {
  1013. cursor.next();
  1014. if (size == -1 /* Reuse */) {
  1015. let node = reused[id];
  1016. children.push(node);
  1017. positions.push(start - parentStart);
  1018. return;
  1019. }
  1020. else if (size == -3 /* ContextChange */) { // Context change
  1021. contextHash = id;
  1022. return;
  1023. }
  1024. else if (size == -4 /* LookAhead */) {
  1025. lookAhead = id;
  1026. return;
  1027. }
  1028. else {
  1029. throw new RangeError(`Unrecognized record size: ${size}`);
  1030. }
  1031. }
  1032. let type = types[id], node, buffer;
  1033. let startPos = start - parentStart;
  1034. if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
  1035. // Small enough for a buffer, and no reused nodes inside
  1036. let data = new Uint16Array(buffer.size - buffer.skip);
  1037. let endPos = cursor.pos - buffer.size, index = data.length;
  1038. while (cursor.pos > endPos)
  1039. index = copyToBuffer(buffer.start, data, index);
  1040. node = new TreeBuffer(data, end - buffer.start, nodeSet);
  1041. startPos = buffer.start - parentStart;
  1042. }
  1043. else { // Make it a node
  1044. let endPos = cursor.pos - size;
  1045. cursor.next();
  1046. let localChildren = [], localPositions = [];
  1047. let localInRepeat = id >= minRepeatType ? id : -1;
  1048. let lastGroup = 0, lastEnd = end;
  1049. while (cursor.pos > endPos) {
  1050. if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
  1051. if (cursor.end <= lastEnd - maxBufferLength) {
  1052. makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
  1053. lastGroup = localChildren.length;
  1054. lastEnd = cursor.end;
  1055. }
  1056. cursor.next();
  1057. }
  1058. else {
  1059. takeNode(start, endPos, localChildren, localPositions, localInRepeat);
  1060. }
  1061. }
  1062. if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
  1063. makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
  1064. localChildren.reverse();
  1065. localPositions.reverse();
  1066. if (localInRepeat > -1 && lastGroup > 0) {
  1067. let make = makeBalanced(type);
  1068. node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
  1069. }
  1070. else {
  1071. node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
  1072. }
  1073. }
  1074. children.push(node);
  1075. positions.push(startPos);
  1076. }
  1077. function makeBalanced(type) {
  1078. return (children, positions, length) => {
  1079. let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
  1080. if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
  1081. if (!lastI && last.type == type && last.length == length)
  1082. return last;
  1083. if (lookAheadProp = last.prop(NodeProp.lookAhead))
  1084. lookAhead = positions[lastI] + last.length + lookAheadProp;
  1085. }
  1086. return makeTree(type, children, positions, length, lookAhead);
  1087. };
  1088. }
  1089. function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
  1090. let localChildren = [], localPositions = [];
  1091. while (children.length > i) {
  1092. localChildren.push(children.pop());
  1093. localPositions.push(positions.pop() + base - from);
  1094. }
  1095. children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
  1096. positions.push(from - base);
  1097. }
  1098. function makeTree(type, children, positions, length, lookAhead = 0, props) {
  1099. if (contextHash) {
  1100. let pair = [NodeProp.contextHash, contextHash];
  1101. props = props ? [pair].concat(props) : [pair];
  1102. }
  1103. if (lookAhead > 25) {
  1104. let pair = [NodeProp.lookAhead, lookAhead];
  1105. props = props ? [pair].concat(props) : [pair];
  1106. }
  1107. return new Tree(type, children, positions, length, props);
  1108. }
  1109. function findBufferSize(maxSize, inRepeat) {
  1110. // Scan through the buffer to find previous siblings that fit
  1111. // together in a TreeBuffer, and don't contain any reused nodes
  1112. // (which can't be stored in a buffer).
  1113. // If `inRepeat` is > -1, ignore node boundaries of that type for
  1114. // nesting, but make sure the end falls either at the start
  1115. // (`maxSize`) or before such a node.
  1116. let fork = cursor.fork();
  1117. let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
  1118. let result = { size: 0, start: 0, skip: 0 };
  1119. scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
  1120. let nodeSize = fork.size;
  1121. // Pretend nested repeat nodes of the same type don't exist
  1122. if (fork.id == inRepeat && nodeSize >= 0) {
  1123. // Except that we store the current state as a valid return
  1124. // value.
  1125. result.size = size;
  1126. result.start = start;
  1127. result.skip = skip;
  1128. skip += 4;
  1129. size += 4;
  1130. fork.next();
  1131. continue;
  1132. }
  1133. let startPos = fork.pos - nodeSize;
  1134. if (nodeSize < 0 || startPos < minPos || fork.start < minStart)
  1135. break;
  1136. let localSkipped = fork.id >= minRepeatType ? 4 : 0;
  1137. let nodeStart = fork.start;
  1138. fork.next();
  1139. while (fork.pos > startPos) {
  1140. if (fork.size < 0) {
  1141. if (fork.size == -3 /* ContextChange */)
  1142. localSkipped += 4;
  1143. else
  1144. break scan;
  1145. }
  1146. else if (fork.id >= minRepeatType) {
  1147. localSkipped += 4;
  1148. }
  1149. fork.next();
  1150. }
  1151. start = nodeStart;
  1152. size += nodeSize;
  1153. skip += localSkipped;
  1154. }
  1155. if (inRepeat < 0 || size == maxSize) {
  1156. result.size = size;
  1157. result.start = start;
  1158. result.skip = skip;
  1159. }
  1160. return result.size > 4 ? result : undefined;
  1161. }
  1162. function copyToBuffer(bufferStart, buffer, index) {
  1163. let { id, start, end, size } = cursor;
  1164. cursor.next();
  1165. if (size >= 0 && id < minRepeatType) {
  1166. let startIndex = index;
  1167. if (size > 4) {
  1168. let endPos = cursor.pos - (size - 4);
  1169. while (cursor.pos > endPos)
  1170. index = copyToBuffer(bufferStart, buffer, index);
  1171. }
  1172. buffer[--index] = startIndex;
  1173. buffer[--index] = end - bufferStart;
  1174. buffer[--index] = start - bufferStart;
  1175. buffer[--index] = id;
  1176. }
  1177. else if (size == -3 /* ContextChange */) {
  1178. contextHash = id;
  1179. }
  1180. else if (size == -4 /* LookAhead */) {
  1181. lookAhead = id;
  1182. }
  1183. return index;
  1184. }
  1185. let children = [], positions = [];
  1186. while (cursor.pos > 0)
  1187. takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1);
  1188. let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
  1189. return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
  1190. }
  1191. const nodeSizeCache = new WeakMap;
  1192. function nodeSize(balanceType, node) {
  1193. if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType)
  1194. return 1;
  1195. let size = nodeSizeCache.get(node);
  1196. if (size == null) {
  1197. size = 1;
  1198. for (let child of node.children) {
  1199. if (child.type != balanceType || !(child instanceof Tree)) {
  1200. size = 1;
  1201. break;
  1202. }
  1203. size += nodeSize(balanceType, child);
  1204. }
  1205. nodeSizeCache.set(node, size);
  1206. }
  1207. return size;
  1208. }
  1209. function balanceRange(
  1210. // The type the balanced tree's inner nodes.
  1211. balanceType,
  1212. // The direct children and their positions
  1213. children, positions,
  1214. // The index range in children/positions to use
  1215. from, to,
  1216. // The start position of the nodes, relative to their parent.
  1217. start,
  1218. // Length of the outer node
  1219. length,
  1220. // Function to build the top node of the balanced tree
  1221. mkTop,
  1222. // Function to build internal nodes for the balanced tree
  1223. mkTree) {
  1224. let total = 0;
  1225. for (let i = from; i < to; i++)
  1226. total += nodeSize(balanceType, children[i]);
  1227. let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
  1228. let localChildren = [], localPositions = [];
  1229. function divide(children, positions, from, to, offset) {
  1230. for (let i = from; i < to;) {
  1231. let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
  1232. i++;
  1233. for (; i < to; i++) {
  1234. let nextSize = nodeSize(balanceType, children[i]);
  1235. if (groupSize + nextSize >= maxChild)
  1236. break;
  1237. groupSize += nextSize;
  1238. }
  1239. if (i == groupFrom + 1) {
  1240. if (groupSize > maxChild) {
  1241. let only = children[groupFrom]; // Only trees can have a size > 1
  1242. divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
  1243. continue;
  1244. }
  1245. localChildren.push(children[groupFrom]);
  1246. }
  1247. else {
  1248. let length = positions[i - 1] + children[i - 1].length - groupStart;
  1249. localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
  1250. }
  1251. localPositions.push(groupStart + offset - start);
  1252. }
  1253. }
  1254. divide(children, positions, from, to, 0);
  1255. return (mkTop || mkTree)(localChildren, localPositions, length);
  1256. }
  1257. /// Provides a way to associate values with pieces of trees. As long
  1258. /// as that part of the tree is reused, the associated values can be
  1259. /// retrieved from an updated tree.
  1260. class NodeWeakMap {
  1261. constructor() {
  1262. this.map = new WeakMap();
  1263. }
  1264. setBuffer(buffer, index, value) {
  1265. let inner = this.map.get(buffer);
  1266. if (!inner)
  1267. this.map.set(buffer, inner = new Map);
  1268. inner.set(index, value);
  1269. }
  1270. getBuffer(buffer, index) {
  1271. let inner = this.map.get(buffer);
  1272. return inner && inner.get(index);
  1273. }
  1274. /// Set the value for this syntax node.
  1275. set(node, value) {
  1276. if (node instanceof BufferNode)
  1277. this.setBuffer(node.context.buffer, node.index, value);
  1278. else if (node instanceof TreeNode)
  1279. this.map.set(node.tree, value);
  1280. }
  1281. /// Retrieve value for this syntax node, if it exists in the map.
  1282. get(node) {
  1283. return node instanceof BufferNode ? this.getBuffer(node.context.buffer, node.index)
  1284. : node instanceof TreeNode ? this.map.get(node.tree) : undefined;
  1285. }
  1286. /// Set the value for the node that a cursor currently points to.
  1287. cursorSet(cursor, value) {
  1288. if (cursor.buffer)
  1289. this.setBuffer(cursor.buffer.buffer, cursor.index, value);
  1290. else
  1291. this.map.set(cursor.tree, value);
  1292. }
  1293. /// Retrieve the value for the node that a cursor currently points
  1294. /// to.
  1295. cursorGet(cursor) {
  1296. return cursor.buffer ? this.getBuffer(cursor.buffer.buffer, cursor.index) : this.map.get(cursor.tree);
  1297. }
  1298. }
  1299. /// Tree fragments are used during [incremental
  1300. /// parsing](#common.Parser.startParse) to track parts of old trees
  1301. /// that can be reused in a new parse. An array of fragments is used
  1302. /// to track regions of an old tree whose nodes might be reused in new
  1303. /// parses. Use the static
  1304. /// [`applyChanges`](#common.TreeFragment^applyChanges) method to
  1305. /// update fragments for document changes.
  1306. class TreeFragment {
  1307. /// Construct a tree fragment. You'll usually want to use
  1308. /// [`addTree`](#common.TreeFragment^addTree) and
  1309. /// [`applyChanges`](#common.TreeFragment^applyChanges) instead of
  1310. /// calling this directly.
  1311. constructor(
  1312. /// The start of the unchanged range pointed to by this fragment.
  1313. /// This refers to an offset in the _updated_ document (as opposed
  1314. /// to the original tree).
  1315. from,
  1316. /// The end of the unchanged range.
  1317. to,
  1318. /// The tree that this fragment is based on.
  1319. tree,
  1320. /// The offset between the fragment's tree and the document that
  1321. /// this fragment can be used against. Add this when going from
  1322. /// document to tree positions, subtract it to go from tree to
  1323. /// document positions.
  1324. offset, openStart = false, openEnd = false) {
  1325. this.from = from;
  1326. this.to = to;
  1327. this.tree = tree;
  1328. this.offset = offset;
  1329. this.open = (openStart ? 1 /* Start */ : 0) | (openEnd ? 2 /* End */ : 0);
  1330. }
  1331. /// Whether the start of the fragment represents the start of a
  1332. /// parse, or the end of a change. (In the second case, it may not
  1333. /// be safe to reuse some nodes at the start, depending on the
  1334. /// parsing algorithm.)
  1335. get openStart() { return (this.open & 1 /* Start */) > 0; }
  1336. /// Whether the end of the fragment represents the end of a
  1337. /// full-document parse, or the start of a change.
  1338. get openEnd() { return (this.open & 2 /* End */) > 0; }
  1339. /// Create a set of fragments from a freshly parsed tree, or update
  1340. /// an existing set of fragments by replacing the ones that overlap
  1341. /// with a tree with content from the new tree. When `partial` is
  1342. /// true, the parse is treated as incomplete, and the resulting
  1343. /// fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
  1344. /// true.
  1345. static addTree(tree, fragments = [], partial = false) {
  1346. let result = [new TreeFragment(0, tree.length, tree, 0, false, partial)];
  1347. for (let f of fragments)
  1348. if (f.to > tree.length)
  1349. result.push(f);
  1350. return result;
  1351. }
  1352. /// Apply a set of edits to an array of fragments, removing or
  1353. /// splitting fragments as necessary to remove edited ranges, and
  1354. /// adjusting offsets for fragments that moved.
  1355. static applyChanges(fragments, changes, minGap = 128) {
  1356. if (!changes.length)
  1357. return fragments;
  1358. let result = [];
  1359. let fI = 1, nextF = fragments.length ? fragments[0] : null;
  1360. for (let cI = 0, pos = 0, off = 0;; cI++) {
  1361. let nextC = cI < changes.length ? changes[cI] : null;
  1362. let nextPos = nextC ? nextC.fromA : 1e9;
  1363. if (nextPos - pos >= minGap)
  1364. while (nextF && nextF.from < nextPos) {
  1365. let cut = nextF;
  1366. if (pos >= cut.from || nextPos <= cut.to || off) {
  1367. let fFrom = Math.max(cut.from, pos) - off, fTo = Math.min(cut.to, nextPos) - off;
  1368. cut = fFrom >= fTo ? null : new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, cI > 0, !!nextC);
  1369. }
  1370. if (cut)
  1371. result.push(cut);
  1372. if (nextF.to > nextPos)
  1373. break;
  1374. nextF = fI < fragments.length ? fragments[fI++] : null;
  1375. }
  1376. if (!nextC)
  1377. break;
  1378. pos = nextC.toA;
  1379. off = nextC.toA - nextC.toB;
  1380. }
  1381. return result;
  1382. }
  1383. }
  1384. /// A superclass that parsers should extend.
  1385. class Parser {
  1386. /// Start a parse, returning a [partial parse](#common.PartialParse)
  1387. /// object. [`fragments`](#common.TreeFragment) can be passed in to
  1388. /// make the parse incremental.
  1389. ///
  1390. /// By default, the entire input is parsed. You can pass `ranges`,
  1391. /// which should be a sorted array of non-empty, non-overlapping
  1392. /// ranges, to parse only those ranges. The tree returned in that
  1393. /// case will start at `ranges[0].from`.
  1394. startParse(input, fragments, ranges) {
  1395. if (typeof input == "string")
  1396. input = new StringInput(input);
  1397. ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map(r => new Range(r.from, r.to)) : [new Range(0, 0)];
  1398. return this.createParse(input, fragments || [], ranges);
  1399. }
  1400. /// Run a full parse, returning the resulting tree.
  1401. parse(input, fragments, ranges) {
  1402. let parse = this.startParse(input, fragments, ranges);
  1403. for (;;) {
  1404. let done = parse.advance();
  1405. if (done)
  1406. return done;
  1407. }
  1408. }
  1409. }
  1410. class StringInput {
  1411. constructor(string) {
  1412. this.string = string;
  1413. }
  1414. get length() { return this.string.length; }
  1415. chunk(from) { return this.string.slice(from); }
  1416. get lineChunks() { return false; }
  1417. read(from, to) { return this.string.slice(from, to); }
  1418. }
  1419. /// Create a parse wrapper that, after the inner parse completes,
  1420. /// scans its tree for mixed language regions with the `nest`
  1421. /// function, runs the resulting [inner parses](#common.NestedParse),
  1422. /// and then [mounts](#common.NodeProp^mounted) their results onto the
  1423. /// tree.
  1424. function parseMixed(nest) {
  1425. return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
  1426. }
  1427. class InnerParse {
  1428. constructor(parser, parse, overlay, target, ranges) {
  1429. this.parser = parser;
  1430. this.parse = parse;
  1431. this.overlay = overlay;
  1432. this.target = target;
  1433. this.ranges = ranges;
  1434. }
  1435. }
  1436. class ActiveOverlay {
  1437. constructor(parser, predicate, mounts, index, start, target, prev) {
  1438. this.parser = parser;
  1439. this.predicate = predicate;
  1440. this.mounts = mounts;
  1441. this.index = index;
  1442. this.start = start;
  1443. this.target = target;
  1444. this.prev = prev;
  1445. this.depth = 0;
  1446. this.ranges = [];
  1447. }
  1448. }
  1449. const stoppedInner = new NodeProp({ perNode: true });
  1450. class MixedParse {
  1451. constructor(base, nest, input, fragments, ranges) {
  1452. this.nest = nest;
  1453. this.input = input;
  1454. this.fragments = fragments;
  1455. this.ranges = ranges;
  1456. this.inner = [];
  1457. this.innerDone = 0;
  1458. this.baseTree = null;
  1459. this.stoppedAt = null;
  1460. this.baseParse = base;
  1461. }
  1462. advance() {
  1463. if (this.baseParse) {
  1464. let done = this.baseParse.advance();
  1465. if (!done)
  1466. return null;
  1467. this.baseParse = null;
  1468. this.baseTree = done;
  1469. this.startInner();
  1470. if (this.stoppedAt != null)
  1471. for (let inner of this.inner)
  1472. inner.parse.stopAt(this.stoppedAt);
  1473. }
  1474. if (this.innerDone == this.inner.length) {
  1475. let result = this.baseTree;
  1476. if (this.stoppedAt != null)
  1477. result = new Tree(result.type, result.children, result.positions, result.length, result.propValues.concat([[stoppedInner, this.stoppedAt]]));
  1478. return result;
  1479. }
  1480. let inner = this.inner[this.innerDone], done = inner.parse.advance();
  1481. if (done) {
  1482. this.innerDone++;
  1483. // This is a somewhat dodgy but super helpful hack where we
  1484. // patch up nodes created by the inner parse (and thus
  1485. // presumably not aliased anywhere else) to hold the information
  1486. // about the inner parse.
  1487. let props = Object.assign(Object.create(null), inner.target.props);
  1488. props[NodeProp.mounted.id] = new MountedTree(done, inner.overlay, inner.parser);
  1489. inner.target.props = props;
  1490. }
  1491. return null;
  1492. }
  1493. get parsedPos() {
  1494. if (this.baseParse)
  1495. return 0;
  1496. let pos = this.input.length;
  1497. for (let i = this.innerDone; i < this.inner.length; i++) {
  1498. if (this.inner[i].ranges[0].from < pos)
  1499. pos = Math.min(pos, this.inner[i].parse.parsedPos);
  1500. }
  1501. return pos;
  1502. }
  1503. stopAt(pos) {
  1504. this.stoppedAt = pos;
  1505. if (this.baseParse)
  1506. this.baseParse.stopAt(pos);
  1507. else
  1508. for (let i = this.innerDone; i < this.inner.length; i++)
  1509. this.inner[i].parse.stopAt(pos);
  1510. }
  1511. startInner() {
  1512. let fragmentCursor = new FragmentCursor(this.fragments);
  1513. let overlay = null;
  1514. let covered = null;
  1515. let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
  1516. scan: for (let nest, isCovered; this.stoppedAt == null || cursor.from < this.stoppedAt;) {
  1517. let enter = true, range;
  1518. if (fragmentCursor.hasNode(cursor)) {
  1519. if (overlay) {
  1520. let match = overlay.mounts.find(m => m.frag.from <= cursor.from && m.frag.to >= cursor.to && m.mount.overlay);
  1521. if (match)
  1522. for (let r of match.mount.overlay) {
  1523. let from = r.from + match.pos, to = r.to + match.pos;
  1524. if (from >= cursor.from && to <= cursor.to && !overlay.ranges.some(r => r.from < to && r.to > from))
  1525. overlay.ranges.push({ from, to });
  1526. }
  1527. }
  1528. enter = false;
  1529. }
  1530. else if (covered && (isCovered = checkCover(covered.ranges, cursor.from, cursor.to))) {
  1531. enter = isCovered != 2 /* Full */;
  1532. }
  1533. else if (!cursor.type.isAnonymous && cursor.from < cursor.to && (nest = this.nest(cursor, this.input))) {
  1534. if (!cursor.tree)
  1535. materialize(cursor);
  1536. let oldMounts = fragmentCursor.findMounts(cursor.from, nest.parser);
  1537. if (typeof nest.overlay == "function") {
  1538. overlay = new ActiveOverlay(nest.parser, nest.overlay, oldMounts, this.inner.length, cursor.from, cursor.tree, overlay);
  1539. }
  1540. else {
  1541. let ranges = punchRanges(this.ranges, nest.overlay || [new Range(cursor.from, cursor.to)]);
  1542. if (ranges.length)
  1543. this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges));
  1544. if (!nest.overlay)
  1545. enter = false;
  1546. else if (ranges.length)
  1547. covered = { ranges, depth: 0, prev: covered };
  1548. }
  1549. }
  1550. else if (overlay && (range = overlay.predicate(cursor))) {
  1551. if (range === true)
  1552. range = new Range(cursor.from, cursor.to);
  1553. if (range.from < range.to)
  1554. overlay.ranges.push(range);
  1555. }
  1556. if (enter && cursor.firstChild()) {
  1557. if (overlay)
  1558. overlay.depth++;
  1559. if (covered)
  1560. covered.depth++;
  1561. }
  1562. else {
  1563. for (;;) {
  1564. if (cursor.nextSibling())
  1565. break;
  1566. if (!cursor.parent())
  1567. break scan;
  1568. if (overlay && !--overlay.depth) {
  1569. let ranges = punchRanges(this.ranges, overlay.ranges);
  1570. if (ranges.length)
  1571. this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges));
  1572. overlay = overlay.prev;
  1573. }
  1574. if (covered && !--covered.depth)
  1575. covered = covered.prev;
  1576. }
  1577. }
  1578. }
  1579. }
  1580. }
  1581. function checkCover(covered, from, to) {
  1582. for (let range of covered) {
  1583. if (range.from >= to)
  1584. break;
  1585. if (range.to > from)
  1586. return range.from <= from && range.to >= to ? 2 /* Full */ : 1 /* Partial */;
  1587. }
  1588. return 0 /* None */;
  1589. }
  1590. // Take a piece of buffer and convert it into a stand-alone
  1591. // TreeBuffer.
  1592. function sliceBuf(buf, startI, endI, nodes, positions, off) {
  1593. if (startI < endI) {
  1594. let from = buf.buffer[startI + 1], to = buf.buffer[endI - 2];
  1595. nodes.push(buf.slice(startI, endI, from, to));
  1596. positions.push(from - off);
  1597. }
  1598. }
  1599. // This function takes a node that's in a buffer, and converts it, and
  1600. // its parent buffer nodes, into a Tree. This is again acting on the
  1601. // assumption that the trees and buffers have been constructed by the
  1602. // parse that was ran via the mix parser, and thus aren't shared with
  1603. // any other code, making violations of the immutability safe.
  1604. function materialize(cursor) {
  1605. let { node } = cursor, depth = 0;
  1606. // Scan up to the nearest tree
  1607. do {
  1608. cursor.parent();
  1609. depth++;
  1610. } while (!cursor.tree);
  1611. // Find the index of the buffer in that tree
  1612. let i = 0, base = cursor.tree, off = 0;
  1613. for (;; i++) {
  1614. off = base.positions[i] + cursor.from;
  1615. if (off <= node.from && off + base.children[i].length >= node.to)
  1616. break;
  1617. }
  1618. let buf = base.children[i], b = buf.buffer;
  1619. // Split a level in the buffer, putting the nodes before and after
  1620. // the child that contains `node` into new buffers.
  1621. function split(startI, endI, type, innerOffset, length) {
  1622. let i = startI;
  1623. while (b[i + 2] + off <= node.from)
  1624. i = b[i + 3];
  1625. let children = [], positions = [];
  1626. sliceBuf(buf, startI, i, children, positions, innerOffset);
  1627. let from = b[i + 1], to = b[i + 2];
  1628. let isTarget = from + off == node.from && to + off == node.to && b[i] == node.type.id;
  1629. children.push(isTarget ? node.toTree() : split(i + 4, b[i + 3], buf.set.types[b[i]], from, to - from));
  1630. positions.push(from - innerOffset);
  1631. sliceBuf(buf, b[i + 3], endI, children, positions, innerOffset);
  1632. return new Tree(type, children, positions, length);
  1633. }
  1634. base.children[i] = split(0, b.length, NodeType.none, 0, buf.length);
  1635. // Move the cursor back to the target node
  1636. for (let d = 0; d <= depth; d++)
  1637. cursor.childAfter(node.from);
  1638. }
  1639. class StructureCursor {
  1640. constructor(root, offset) {
  1641. this.offset = offset;
  1642. this.done = false;
  1643. this.cursor = root.cursor(IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
  1644. }
  1645. // Move to the first node (in pre-order) that starts at or after `pos`.
  1646. moveTo(pos) {
  1647. let { cursor } = this, p = pos - this.offset;
  1648. while (!this.done && cursor.from < p) {
  1649. if (cursor.to >= pos && cursor.enter(p, 1, IterMode.IgnoreOverlays | IterMode.ExcludeBuffers)) ;
  1650. else if (!cursor.next(false))
  1651. this.done = true;
  1652. }
  1653. }
  1654. hasNode(cursor) {
  1655. this.moveTo(cursor.from);
  1656. if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
  1657. for (let tree = this.cursor.tree;;) {
  1658. if (tree == cursor.tree)
  1659. return true;
  1660. if (tree.children.length && tree.positions[0] == 0 && tree.children[0] instanceof Tree)
  1661. tree = tree.children[0];
  1662. else
  1663. break;
  1664. }
  1665. }
  1666. return false;
  1667. }
  1668. }
  1669. class FragmentCursor {
  1670. constructor(fragments) {
  1671. var _a;
  1672. this.fragments = fragments;
  1673. this.curTo = 0;
  1674. this.fragI = 0;
  1675. if (fragments.length) {
  1676. let first = this.curFrag = fragments[0];
  1677. this.curTo = (_a = first.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : first.to;
  1678. this.inner = new StructureCursor(first.tree, -first.offset);
  1679. }
  1680. else {
  1681. this.curFrag = this.inner = null;
  1682. }
  1683. }
  1684. hasNode(node) {
  1685. while (this.curFrag && node.from >= this.curTo)
  1686. this.nextFrag();
  1687. return this.curFrag && this.curFrag.from <= node.from && this.curTo >= node.to && this.inner.hasNode(node);
  1688. }
  1689. nextFrag() {
  1690. var _a;
  1691. this.fragI++;
  1692. if (this.fragI == this.fragments.length) {
  1693. this.curFrag = this.inner = null;
  1694. }
  1695. else {
  1696. let frag = this.curFrag = this.fragments[this.fragI];
  1697. this.curTo = (_a = frag.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : frag.to;
  1698. this.inner = new StructureCursor(frag.tree, -frag.offset);
  1699. }
  1700. }
  1701. findMounts(pos, parser) {
  1702. var _a;
  1703. let result = [];
  1704. if (this.inner) {
  1705. this.inner.cursor.moveTo(pos, 1);
  1706. for (let pos = this.inner.cursor.node; pos; pos = pos.parent) {
  1707. let mount = (_a = pos.tree) === null || _a === void 0 ? void 0 : _a.prop(NodeProp.mounted);
  1708. if (mount && mount.parser == parser) {
  1709. for (let i = this.fragI; i < this.fragments.length; i++) {
  1710. let frag = this.fragments[i];
  1711. if (frag.from >= pos.to)
  1712. break;
  1713. if (frag.tree == this.curFrag.tree)
  1714. result.push({
  1715. frag,
  1716. pos: pos.from - frag.offset,
  1717. mount
  1718. });
  1719. }
  1720. }
  1721. }
  1722. }
  1723. return result;
  1724. }
  1725. }
  1726. function punchRanges(outer, ranges) {
  1727. let copy = null, current = ranges;
  1728. for (let i = 1, j = 0; i < outer.length; i++) {
  1729. let gapFrom = outer[i - 1].to, gapTo = outer[i].from;
  1730. for (; j < current.length; j++) {
  1731. let r = current[j];
  1732. if (r.from >= gapTo)
  1733. break;
  1734. if (r.to <= gapFrom)
  1735. continue;
  1736. if (!copy)
  1737. current = copy = ranges.slice();
  1738. if (r.from < gapFrom) {
  1739. copy[j] = new Range(r.from, gapFrom);
  1740. if (r.to > gapTo)
  1741. copy.splice(j + 1, 0, new Range(gapTo, r.to));
  1742. }
  1743. else if (r.to > gapTo) {
  1744. copy[j--] = new Range(gapTo, r.to);
  1745. }
  1746. else {
  1747. copy.splice(j--, 1);
  1748. }
  1749. }
  1750. }
  1751. return current;
  1752. }
  1753. function findCoverChanges(a, b, from, to) {
  1754. let iA = 0, iB = 0, inA = false, inB = false, pos = -1e9;
  1755. let result = [];
  1756. for (;;) {
  1757. let nextA = iA == a.length ? 1e9 : inA ? a[iA].to : a[iA].from;
  1758. let nextB = iB == b.length ? 1e9 : inB ? b[iB].to : b[iB].from;
  1759. if (inA != inB) {
  1760. let start = Math.max(pos, from), end = Math.min(nextA, nextB, to);
  1761. if (start < end)
  1762. result.push(new Range(start, end));
  1763. }
  1764. pos = Math.min(nextA, nextB);
  1765. if (pos == 1e9)
  1766. break;
  1767. if (nextA == pos) {
  1768. if (!inA)
  1769. inA = true;
  1770. else {
  1771. inA = false;
  1772. iA++;
  1773. }
  1774. }
  1775. if (nextB == pos) {
  1776. if (!inB)
  1777. inB = true;
  1778. else {
  1779. inB = false;
  1780. iB++;
  1781. }
  1782. }
  1783. }
  1784. return result;
  1785. }
  1786. // Given a number of fragments for the outer tree, and a set of ranges
  1787. // to parse, find fragments for inner trees mounted around those
  1788. // ranges, if any.
  1789. function enterFragments(mounts, ranges) {
  1790. let result = [];
  1791. for (let { pos, mount, frag } of mounts) {
  1792. let startPos = pos + (mount.overlay ? mount.overlay[0].from : 0), endPos = startPos + mount.tree.length;
  1793. let from = Math.max(frag.from, startPos), to = Math.min(frag.to, endPos);
  1794. if (mount.overlay) {
  1795. let overlay = mount.overlay.map(r => new Range(r.from + pos, r.to + pos));
  1796. let changes = findCoverChanges(ranges, overlay, from, to);
  1797. for (let i = 0, pos = from;; i++) {
  1798. let last = i == changes.length, end = last ? to : changes[i].from;
  1799. if (end > pos)
  1800. result.push(new TreeFragment(pos, end, mount.tree, -startPos, frag.from >= pos, frag.to <= end));
  1801. if (last)
  1802. break;
  1803. pos = changes[i].to;
  1804. }
  1805. }
  1806. else {
  1807. result.push(new TreeFragment(from, to, mount.tree, -startPos, frag.from >= startPos, frag.to <= endPos));
  1808. }
  1809. }
  1810. return result;
  1811. }
  1812. export { DefaultBufferLength, IterMode, MountedTree, NodeProp, NodeSet, NodeType, NodeWeakMap, Parser, Tree, TreeBuffer, TreeCursor, TreeFragment, parseMixed };