snapdragon-position

Snapdragon util and plugin for patching the position on an AST node.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install
Install with npm:
$ npm install --save snapdragon-position
What does this do?
When used as a plugin, this adds a .position() method to a snapdragon-lexer instance, for adding position information to tokens.
If you prefer .loc over .position, use [snapdragon-location][] instead.
Example
const position = require('snapdragon-position');
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');
lexer.use(position());
lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);
var token = lexer.advance();
console.log(token);
Adds a .position object to the token, like this:
Token {
type: 'text',
value: 'foo',
match: [ 'foo', index: 0, input: 'foo/*' ],
position: Position {
start: Location { index: 0, column: 1, line: 1 },
end: Location { index: 3, column: 4, line: 1 },
range: [0, 3]
}
}
Usage
The main export is a function that can either be called directly to add a .position to a single token, or used as a plugin function with lexer.use().
Sets the start location and returns a function for setting the end location.
Params
lexer {Object}: Lexer instance
returns {Function}: Returns a function that takes a token as its only argument
Example
const position = require('snapdragon-position');
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');
lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);
var pos = position(lexer);
var token = pos(lexer.advance());
console.log(token);
Use as a plugin to add a .position method to your snapdragon-lexer instance, which automatically adds a position object to tokens when the .handle() method is used.
Example
var Lexer = require('snapdragon-lexer');
var position = require('snapdragon-position');
var lexer = new Lexer();
lexer.use(position());
Get the current cursor location, with index, line and column. This is used in the .position() method to add the "start" and "end" locations to the position object, you can also call it directly when needed.
returns {Object}: Returns an object with the current lexer location, with cursor index, line, and column numbers.
Example
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer();
console.log(lexer.location());
Returns a function for getting the current position.
returns {Function}: Returns a function that takes a token as its only argument
Example
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');
lexer.use(position.plugin());
lexer.set('text', function(tok) {
const pos = this.position();
const match = this.match(/^\w+/);
if (match) {
return pos(this.token(match));
}
});
Position information
Position {
start: Location { index: 0, column: 1, line: 1 },
end: Location { index: 3, column: 4, line: 1 },
range: [getter]
}
About
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:
$ 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:
$ npm install -g verbose/verb
Related projects
You might also be interested in these projects:
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on January 08, 2018.