@toml-tools/parser
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

npm

TOML-Tools/parser

A Toml Parser implemented in JavaScript using the Chevrotain Parsing Toolkit. This parser outputs a Chevrotain Concrete Syntax Tree Which is a low level data structure that represents the whole syntax tree.

  • including comments and newline information.
  • Including full position information on every Token.

This means that this parser could be re-used to construct many different kinds of Toml related tooling:

  • Toml to JSON compilers.
  • Toml to Yaml compilers.
    • comments information is needed.
  • Toml source code beautifiers.
    • Once again comments information is needed.
  • Toml Schema Validator
    • Full position information is required for useful error messages.
  • and more...

Installation

yarn add @toml-tools/parser
# or if you prefer npm
npm install @toml-tools/parser

APIs

This package's APIs are exported as a TypeScript definition file.

Usage

const {
  parse,
  BaseTomCstVisitor,
  BaseTomlCstVisitorWithDefaults,
} = require("@toml-tools/parser");

const input = `
# This is a TOML document.
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
`;
const tomlCst = parse(input);

// Read the manual for using the Chevrotain CST visitor:
// - https://sap.github.io/chevrotain/docs/guide/concrete_syntax_tree.html#cst-visitor
// - The full APIs for the CST visitor are defined in api.d.ts linked above
class KeyNamesCollector extends BaseTomlCstVisitorWithDefaults {
  constructor() {
    super();
    this.tableKeyNames = [];
  }

  stdTable(ctx) {
    this.tableKeyNames.push(this.visit(ctx.key));
  }

  key(ctx) {
    const keyImages = ctx.IKey.map((keyTok) => keyTok.image);
    const newKey = keyImages.join(".");
    return newKey;
  }
}

const myVisitor = new KeyNamesCollector();
myVisitor.visit(cst);

console.log(myVisitor.tableKeyNames.length); // 1
console.log(myVisitor.tableKeyNames[0]); // "database"

Readme

Keywords

Package Sidebar

Install

npm i @toml-tools/parser

Weekly Downloads

3,888

Version

1.0.0

License

MIT

Unpacked Size

223 kB

Total Files

18

Last publish

Collaborators

  • jounqin
  • bd82