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

snapdragon

Package Overview
Dependencies
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snapdragon

Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map support.

  • 0.11.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.6M
decreased by-62.94%
Maintainers
3
Weekly downloads
 
Created

What is snapdragon?

The snapdragon npm package is a highly pluggable, low-level compiler for creating compilers and other text processing applications. It provides a robust framework for parsing, compiling, and rendering custom grammars in a structured way.

What are snapdragon's main functionalities?

Parsing

This feature allows you to define custom parsing rules. The code sample demonstrates how to create a simple text parser that recognizes sequences of lowercase letters.

const Snapdragon = require('snapdragon');
const snapdragon = new Snapdragon();
snapdragon.parser.use(function() {
  this.set('text', function() {
    const pos = this.position();
    const m = this.match(/^[a-z]+/);
    if (!m) return;
    return pos(this.node({type: 'text', val: m[0]}));
  });
});
const ast = snapdragon.parse('hello');
console.log(ast);

Compiling

This feature allows you to compile parsed input into a new form. The code sample shows how to compile a parsed abstract syntax tree (AST) into uppercase text.

const Snapdragon = require('snapdragon');
const snapdragon = new Snapdragon();
snapdragon.compiler.use(function(node) {
  if (node.type === 'text') {
    return node.val.toUpperCase();
  }
});
const ast = snapdragon.parse('hello');
const result = snapdragon.compile(ast).output;
console.log(result);

Rendering

This feature involves rendering the compiled output. The code sample illustrates how to render the output of a parsed AST, adding a space after each text node.

const Snapdragon = require('snapdragon');
const snapdragon = new Snapdragon();
snapdragon.renderer.use(function(node) {
  if (node.type === 'text') {
    this.emit(node.val + ' '); // add space after each text node
  }
});
const ast = snapdragon.parse('hello world');
const result = snapdragon.render(ast);
console.log(result);

Other packages similar to snapdragon

Keywords

FAQs

Package last updated on 18 Apr 2018

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