Skip to content

Quick Start

This page gets you from installation to first useful output. If you haven’t installed yet, start with Installation.

import { parseHtml } from '@lanexio/parser-grammar-html';
const encoder = new TextEncoder();
const tree = parseHtml(encoder.encode('<p>Hello <strong>world</strong></p>'));
console.log(tree.nodeCount); // total nodes in the flat AST
console.log(tree.root.kind); // numeric kind id for the Document root

parseHtml accepts a Uint8Array, not a string. Use TextEncoder to convert. It always returns a LexTree and never throws, even on malformed input.

import { parseMarkdown } from '@lanexio/parser-grammar-markdown';
const encoder = new TextEncoder();
const tree = parseMarkdown(encoder.encode('# Hello\n\nA **bold** paragraph.'));
console.log(tree.nodeCount);
console.log(tree.root.kind);

GFM (GitHub Flavored Markdown) extensions are enabled by default. Pass { gfm: false } to restrict to CommonMark only.

import { parseJson } from '@lanexio/parser-grammar-json';
const encoder = new TextEncoder();
const tree = parseJson(encoder.encode('{"key": [1, true, null]}'));
console.log(tree.nodeCount); // total nodes in the flat AST
console.log(tree.root.kind); // numeric kind id for the Document root

parseJson implements RFC 8259. It accepts a Uint8Array, never throws, and passes the complete JSONTestSuite corpus — 311/311 cases (95 accept, 183 reject, 33 never-throw).

import { parseYaml } from '@lanexio/parser-grammar-yaml';
const encoder = new TextEncoder();
const tree = parseYaml(encoder.encode('key: value\nlist:\n - one\n - two'));
console.log(tree.nodeCount);
console.log(tree.root.kind);

parseYaml implements YAML 1.2.2 including flow collections ({...}, [...]), anchors, aliases, and tags. 267/268 yaml-test-suite valid cases pass.

import { parseHtml } from '@lanexio/parser-grammar-html';
import { HtmlKind } from '@lanexio/parser-grammar-html';
const encoder = new TextEncoder();
const tree = parseHtml(encoder.encode('<ul><li>one</li><li>two</li></ul>'));
const cursor = tree.cursor();
visit: while (true) {
const node = cursor.current;
// node.range is a tuple ([start, end]); node.text decodes it for you.
console.log(node.kind, JSON.stringify(node.text.slice(0, 40)));
if (cursor.gotoFirstChild()) continue;
while (!cursor.gotoNextSibling()) {
if (!cursor.gotoParent()) break visit;
}
}
import { parseHtml } from '@lanexio/parser-grammar-html';
const encoder = new TextEncoder();
const tree = parseHtml(encoder.encode('<p><strong>unclosed'));
for (const node of tree.root.children()) {
if (node.hasError) {
console.log('LexError at', node.range);
}
}

parseHtml never throws. Malformed input produces LexError nodes in the tree. You can detect and report them, or ignore them, depending on your use case.