snapdragon
Fast, pluggable and easy-to-use parser-renderer factory.
Install
Install with npm:
$ npm install --save snapdragonCreated by jonschlinkert and doowb.
Features
Bootstrap your own parser, get sourcemap support for free
All parsing and compiling is handled by simple, reusable middleware functions
History
v0.5.0
Breaking changes
Substantial breaking changes were made in v0.5.0! Most of these changes are part of a larger refactor that will be finished in 0.6.0, including the introduction of a Lexer class.
Renderer was renamed to
Compilerthe
.rendermethod was renamed to.compileMany other smaller changes. A more detailed overview will be provided in 0.6.0. If you don't have to time review code, I recommend you wait for the 0.6.0 release.
Usage examples
Parse
Render
See the examples.
Getting started
Parsers
Parsers are middleware functions used for parsing a string into an ast node.
AST node
When the parser finds a match, pos() is called, pushing a token for that node onto the ast that looks something like:
Renderers
Renderers are named middleware functions that visit over an array of ast nodes to compile a string.
Source maps
If you want source map support, make sure to emit the position as well.
Docs
Parser middleware
A parser middleware is a function that returns an abject called a token. This token is pushed onto the AST as a node.
Example token
Example parser middleware
Match a single . in a string:
Get the starting position by calling
this.position()pass a regex for matching a single dot to the
.matchmethodif no match is found, return
undefinedif a match is found,
pos()is called, which returns a token with:
type: the name of the [compiler] to useval: The actual value captured by the regex. In this case, a.. Note that you can capture and return whatever will be needed by the corresponding [compiler].The ending position: automatically calculated by adding the length of the first capture group to the starting position.
Renderer middleware
Renderers are run when the name of the compiler middleware matches the type defined on an ast node (which is defined in a parser).
Example
Exercise: Parse a dot, then compile it as an escaped dot.
API
Create a new Parser with the given input and options.
Params
input{String}options{Object}
Define a non-enumberable property on the Parser instance.
Example
Params
key{String}: propery nameval{any}: property valuereturns{Object}: Returns the Parser instance for chaining.
Set parser name with the given fn
Params
name{String}fn{Function}
Get parser name
Params
name{String}
Push a token onto the type stack.
Params
type{String}returns{Object}token
Pop a token off of the type stack
Params
type{String}returns{Object}: Returns a token
Return true if inside a stack node. Types are braces, parens or brackets.
Params
type{String}returns{Boolean}
Example
Params
node{Object}type{String}returns{Boolean}
Define a non-enumberable property on the Compiler instance.
Example
Params
key{String}: propery nameval{any}: property valuereturns{Object}: Returns the Compiler instance for chaining.
About
Related projects
expand-brackets: Expand POSIX bracket expressions (character classes) in glob patterns. | homepage
micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Building docs
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
Running tests
Install dev dependencies:
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb-generate-readme, v0.1.31, on October 10, 2016.
Last updated
Was this helpful?