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
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);
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);
console.log(myVisitor.tableKeyNames[0]);