Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@toml-tools/parser

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@toml-tools/parser

TOML Parser Implemented in JavaScript

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.3K
decreased by-11.7%
Maintainers
2
Weekly downloads
 
Created
Source

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"

Keywords

FAQs

Package last updated on 02 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc