htmlx-parser-svast
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

htmlx-parser-svast

A parser for HTMLx that produces a Svast AST which implements the Unist.

Unlike its older handcrafted sibling svelte-parse, this library is not focused on speed. It is built with Chevrotain so that it may inherit the excellent error recovery properties of that parser generator.

It was developed to help with processing Svelte files during development, where the files may contain errors, or expressions and scripts written in languages that svelte's own parser can't handle (eg Typescript).

Usage

Import and invoke the parser by calling parse.

import { parse } from 'htmlx-parser-svast'
 
const input = '<h1 title={someExpression}>Heading</h1>';
const result = parse(input);

The result returned is as much of the AST as could be generated along with any errors encountered

declare type ParseResult = {
    ast: Root; //Root is a Svast AST node
    errors: ParseError[]
};

With errors having the type

 type ParseError = {
    source: "Lexer" | "Parser"
    name: string
    message: string    
    position: {
        start: Point,
        end: Point
    }
}

Limitations

  • It doesn't implement svast's each block.
  • It is slower and larger than svelte-parse.
  • HTMLx expressions are parsed by looking for the nearest, unbalanced }.
  • The AST builder currently doesn't take full advantage of the error recovery features of chevrotain.
  • Writing more tests is a work in progress.

Readme

Keywords

Package Sidebar

Install

npm i htmlx-parser-svast

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

1.06 MB

Total Files

9

Last publish

Collaborators

  • halfnelson