UNPKG

myna-parser-mnast

Version:

Myna parser with Unist compatibility

174 lines (173 loc) 4.56 kB
import { u as A } from "unist-builder"; import { convert as C, is as M } from "unist-util-is"; function x(t, e) { let n = 1, i = 1; for (let f = 0; f < e; f++) { const r = t[f]; r === ` ` ? (n++, i = 1) : r === "\r" ? t[f + 1] === ` ` ? (f++, n++, i = 1) : (n++, i = 1) : i++; } return { line: n, column: i, offset: e }; } function N(t, e, n) { return { start: x(t, e), end: x(t, n) }; } function j(t, e) { const n = []; let i = 0, f = !0; for (let r = 0; r < e.start.offset; r++) { const s = t[r]; s === ` ` || s === "\r" ? (s === "\r" && t[r + 1] === ` ` && r++, i = 0, f = !0) : f && (s === " " || s === " " ? i++ : (n.push(i), f = !1)); } return n; } const P = [], k = !0, b = !1, B = "skip"; function D(t, e, n, i) { let f; typeof e == "function" && typeof n != "function" ? (i = n, n = e) : f = e; const r = C(f), s = i ? -1 : 1; c(t, void 0, [])(); function c(l, u, a) { const p = ( /** @type {Record<string, unknown>} */ l && typeof l == "object" ? l : {} ); if (typeof p.type == "string") { const o = ( // `hast` typeof p.tagName == "string" ? p.tagName : ( // `xast` typeof p.name == "string" ? p.name : void 0 ) ); Object.defineProperty(T, "name", { value: "node (" + (l.type + (o ? "<" + o + ">" : "")) + ")" }); } return T; function T() { let o = P, m, y, d; if ((!e || r(l, u, a[a.length - 1] || void 0)) && (o = E(n(l, a)), o[0] === b)) return o; if ("children" in l && l.children) { const h = ( /** @type {UnistParent} */ l ); if (h.children && o[0] !== B) for (y = (i ? h.children.length : -1) + s, d = a.concat(h); y > -1 && y < h.children.length; ) { const S = h.children[y]; if (m = c(S, y, d)(), m[0] === b) return m; y = typeof m[1] == "number" ? m[1] : y + s; } } return o; } } } function E(t) { return Array.isArray(t) ? t : typeof t == "number" ? [k, t] : t == null ? P : [t]; } function I(t, e, n, i) { let f, r, s; typeof e == "function" && typeof n != "function" ? (r = void 0, s = e, f = n) : (r = e, s = n, f = i), D(t, r, c, f); function c(l, u) { const a = u[u.length - 1], p = a ? a.children.indexOf(l) : void 0; return s(l, p, a); } } function g(t) { if (t) { if (t.className === "AstRule" && t.r) return g(t.r); if (t.className === "Delay" && t.fn) return g(t.fn()); } return t; } function L(t, e = {}) { const { includePosition: n = !0, includeMynaData: i = !0, typeMapper: f, isCST: r = !1, preserveOriginalText: s = !0 } = e; let c; const l = t.rule; if (f) c = f(t); else if (l != null && l.name) c = l.name; else { const a = g(l); a != null && a.type ? c = a.type : t.isLeaf ? c = "text" : c = "node"; } const u = A(c, { ...n && t.input && typeof t.start == "number" && typeof t.end == "number" ? { position: { ...N(t.input, t.start, t.end), indent: j( t.input, N(t.input, t.start, t.end) ) } } : {}, ...i ? { data: { rule: t.rule, fullName: t.fullName, isCST: r, isAST: !r, ...r && s && t.allText ? { originalText: t.allText } : {} } } : {} }); return t.isLeaf && t.allText && (u.value = t.allText), t.children && t.children.length > 0 && (u.children = t.children.map( (a) => L(a, e) )), u; } function w(t) { var i, f, r, s, c; const e = ((i = t.data) == null ? void 0 : i.isCST) ?? !1, n = { rule: ((f = t.data) == null ? void 0 : f.rule) || { name: t.type }, input: t.position ? t.position.start.offset.toString() : "", start: ((r = t.position) == null ? void 0 : r.start.offset) || 0, end: ((s = t.position) == null ? void 0 : s.end.offset) || 0, children: t.children ? t.children.map(w) : null }; return t.value ? n.allText = t.value : e && ((c = t.data) != null && c.originalText) && (n.allText = t.data.originalText), t.data && Object.assign(n, t.data), n; } function U(t, e) { I(t, e); } function X(t, e) { return M(t, e); } function q(t, e) { const n = []; return I(t, (i) => { M(i, e) && n.push(i); }), n; } export { j as calculateIndent, N as calculateNodePosition, x as calculatePosition, w as fromMnast, q as getMnastNodesByType, X as isMnastNode, L as toMnast, U as visitMnast };