Easily parse a string to create an AST.
npm install snapdragon-parser> Easily parse a string to create an AST.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Details
Install with npm:
``sh`
$ npm install --save snapdragon-parser
Create a new Parser with the given input and options.
Params
* input {String}options
* {Object}
Example
`js`
const Parser = require('snapdragon-parser');
const parser = new Parser();
Create a new Node with the given value and type.
Params
* node {Object}: The object to use to create a nodereturns
* {Object}: returns the created Node instance.
Example
`js`
const node = parser.node({type: 'slash', value: '/'});
// sugar for
const Node = require('snapdragon-node');
const node = Node({type: 'slash', value: '/'});
Returns true if the given value is an instance of snapdragon-node.
Params
* node {Object}returns
* {Boolean}
Register handler of the given type.
Params
* type {String}handler
* {Function}
Example
`js`
parser.set('all', function(tok) {
// do stuff to tok
return tok;
});
Get a registered handler function.
Params
* type {String}fn
* {Function}: The handler function to register.
Example
`js`
handlers.set('star', function() {
// do parser, lexer, or compiler stuff
});
const star = handlers.get('star');
Returns true if the parser has a registered handler of the given type.
Params
* {String}: type
* returns {Boolean}
Example
`js`
parser.set('star', function() {});
console.log(parser.has('star')); // true
Capture a node of the given type.
Params
* type {String}: (required)regex
* {RegExp}: (optional)lexerFn
* {Function}: (optional)parserFn
* {Function}: (optional)returns
* {Object}: Returns the Parser instance for chaining.
Returns true if the parser is currently "inside" a node of the given type.
Params
* type {String}returns
* {Boolean}
Returns true if node is a "block" node. Block nodes have a nodes array for keeping child nodes.
Params
* node {Object}returns
* {Boolean}
Example
`js`
parser.isBlock(new Node()); //=> false
parser.isBlock(new Node({ nodes: [] })); //=> true
Returns true if node is a new "block" node, with either no child nodes, or only an open node.
Params
* node {Object}returns
* {Boolean}
Example
`js`
parser.isBlock(new Node()); //=> false
parser.isBlock(new Node({ nodes: [] })); //=> true
Returns true if the given node is an "open" node.
Params
* node {Object}returns
* {Object} parentNode
Returns true if the given node is a "close" node.
Params
* node {Object}returns
* {Object} parentNode
Push a child node onto the node.nodes array of the current node on the stack.
Params
* node {Object}: (required)returns
* {Object} node
Events
* emits: push
Example
`js`
parser.set('default', function(tok) {
return this.push(this.node(tok));
});
Pop the last node from the stack. If a
Params
* node {Object}: (optional)returns
* {Object} node
Events
* emits: pop
Example
`js`
parser.set('default', function(tok) {
return this.push(this.node(tok));
});
Get the next token from the lexer, then calls the registered
parser handler for token.type on the token.
* returns {Any}: Returns whatever value the handler returns.
Expect the given type, or throw an exception.
Params
* type {String}
Accept the given type.
Params
* {String}: type
Parses the given input string and returns an AST object.
Params
* input {String}returns
* {Object}: Returns an AST (abstract syntax tree).
Example
`js`
const ast = parser.parse('foo/bar');
Creates a new Parser instance with the given options, and copy
the handlers from the current instance to the new instance.
Params
* options {Object}parent
* {Object}: Optionally pass a different parser instance to copy handlers from.returns
* {Object}: Returns a new Parser instance
Concat nodes from another AST to node.nodes.
Params
* node {Object}ast
* {Object}returns
* {Object}
Returns true if listeners are registered for even name.
Params
* name {String}returns
* {Boolean}
Params
* fn {Function}returns
* {Parser}: Returns the Parser instance.
Example
`js`
const myParser = new Parser();
const plugin = parser => {
// do stuff to parser instance
};
myParser.use(plugin);
Throw a formatted error message with details including the cursor position.
Params
* msg {String}: Message to use in the Error.node
* {Object}returns
* {Undefined}
Example
`js`
parser.set('foo', function(tok) {
if (tok.value !== 'foo') {
throw this.error('expected token.value to be "foo"', tok);
}
});
Get the part of the input string has has already been parsed.
* returns {String}
Params
* parser {Object}returns
* {Boolean}
Example
`js`
const Parser = require('parser');
const parser = new Parser();
console.log(Parser.isParser(parser)); //=> true
console.log(Parser.isParser({})); //=> false
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
`sh`
$ npm install && npm test
Building docs
_(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)_
To generate the readme, run the following command:
`sh``
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
* snapdragon-lexer: Converts a string into an array of tokens, with useful methods for looking ahead and… more | homepage
* snapdragon-node: Snapdragon utility for creating a new AST node in custom code, such as plugins. | homepage
* snapdragon-position: Snapdragon util and plugin for patching the position on an AST node. | homepage
* snapdragon-token: Create a snapdragon token. Used by the snapdragon lexer, but can also be used by… more | homepage
Jon Schlinkert
* GitHub Profile
* Twitter Profile
* LinkedIn Profile
Copyright © 2018, Jon Schlinkert.
Released under the MIT License.
*
_This file was generated by verb-generate-readme, v0.8.0, on November 24, 2018._