What is character-parser?
The character-parser npm package is designed for parsing through strings to identify and handle different characters or sequences of characters. It is particularly useful in scenarios where you need to process code or text to find specific patterns, such as brackets, quotes, or custom-defined sequences. This package can be utilized in developing compilers, interpreters, or any application that requires detailed analysis of text or code.
What are character-parser's main functionalities?
Bracket Matching
This feature allows you to parse through a string to find matching brackets. It's useful for syntax analysis in code editors or compilers.
const characterParser = require('character-parser');
let content = 'function(a, b) { return a + b; }';
let state = characterParser.defaultState();
for (let i = 0; i < content.length; i++) {
state = characterParser.parseChar(content.charAt(i), state);
if (state.isNesting()) {
console.log('Nesting at index', i);
}
}
String Detection
This feature helps in identifying when the parser is within a string. It's particularly useful for syntax highlighting or escaping strings in code.
const characterParser = require('character-parser');
let content = '"Hello, world!" and 'another string'';
let state = characterParser.defaultState();
for (let i = 0; i < content.length; i++) {
state = characterParser.parseChar(content.charAt(i), state);
if (state.isString()) {
console.log('Inside a string at index', i);
}
}
Other packages similar to character-parser
acorn
Acorn is a robust, full-featured JavaScript parser that can parse ECMAScript code. It provides detailed analysis of script structure, which makes it more comprehensive than character-parser for JavaScript-specific projects but potentially heavier for simple character parsing tasks.
esprima
Esprima is another JavaScript parser that supports ECMAScript 5.1 and newer versions. It's used for static analysis and code manipulation. Compared to character-parser, Esprima offers more detailed parsing capabilities specific to JavaScript syntax but might be overkill for basic character parsing needs.
chevrotain
Chevrotain is a fast and feature-rich parser building toolkit for JavaScript. Unlike character-parser, which is focused on character-level parsing, Chevrotain provides tools for creating complex parsers and interpreters, making it suitable for building full programming languages or complex domain-specific languages (DSLs).
character-parser
Parse JavaScript one character at a time to look for snippets in Templates. This is not a validator, it's just designed to allow you to have sections of JavaScript delimited by brackets robustly.
Installation
npm install character-parser
Usage
Work out how much depth changes:
var state = parse('foo(arg1, arg2, {\n foo: [a, b\n');
assert(state.roundDepth === 1);
assert(state.curlyDepth === 1);
assert(state.squareDepth === 1);
parse(' c, d]\n })', state);
assert(state.squareDepth === 0);
assert(state.curlyDepth === 0);
assert(state.roundDepth === 0);
Bracketed Expressions
Find all the contents of a bracketed expression:
var section = parser.parseMax('foo="(", bar="}") bing bong');
assert(section.start === 0);
assert(section.end === 16);
assert(section.src = 'foo="(", bar="}"');
var section = parser.parseMax('{foo="(", bar="}"} bing bong', {start: 1});
assert(section.start === 1);
assert(section.end === 17);
assert(section.src = 'foo="(", bar="}"');
The bracketed expression parsing simply parses up to but excluding the first unmatched closed bracket ()
, }
, ]
). It is clever enough to ignore brackets in comments or strings.
Custom Delimited Expressions
Find code up to a custom delimiter:
var section = parser.parseUntil('foo.bar("%>").baz%> bing bong', '%>');
assert(section.start === 0);
assert(section.end === 17);
assert(section.src = 'foo.bar("%>").baz');
var section = parser.parseUntil('<%foo.bar("%>").baz%> bing bong', '%>', {start: 2});
assert(section.start === 2);
assert(section.end === 19);
assert(section.src = 'foo.bar("%>").baz');
Delimiters are ignored if they are inside strings or comments.
API
parse(str, state = defaultState(), options = {start: 0, end: src.length})
Parse a string starting at the index start, and return the state after parsing that string.
If you want to parse one string in multiple sections you should keep passing the resulting state to the next parse operation.
The resulting object has the structure:
{
lineComment: false,
blockComment: false,
singleQuote: false,
doubleQuote: false,
escaped: false,
roundDepth: 0,
curlyDepth: 0,
squareDepth: 0
}
parseMax(src, options = {start: 0})
Parses the source until the first unmatched close bracket (any of )
, }
, ]
). It returns an object with the structure:
{
start: 0,
end: 13,
src: 'source string'
}
Parses the source until the first occurence of delimiter
which is not in a string or a comment. If includeLineComment
is true
, it will still count if the delimiter occurs in a line comment, but not in a block comment. It returns an object with the structure:
{
start: 0,
end: 13,
src: 'source string'
}
parseChar(character, state = defaultState())
Parses the single character and returns the state. See parse
for the structure of the returned state object. N.B. character must be a single character not a multi character string.
defaultState()
Get a default starting state. See parse
for the structure of the returned state object.
License
MIT