
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
character-parser
Advanced tools
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.
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.
npm install character-parser
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);
Find all the contents of a bracketed expression:
var section = parser.parseMax('foo="(", bar="}") bing bong');
assert(section.start === 0);
assert(section.end === 16);//exclusive end of string
assert(section.src = 'foo="(", bar="}"');
var section = parser.parseMax('{foo="(", bar="}"} bing bong', {start: 1});
assert(section.start === 1);
assert(section.end === 17);//exclusive end of string
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.
Find code up to a custom delimiter:
var section = parser.parseUntil('foo.bar("%>").baz%> bing bong', '%>');
assert(section.start === 0);
assert(section.end === 17);//exclusive end of string
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);//exclusive end of string
assert(section.src = 'foo.bar("%>").baz');
Delimiters are ignored if they are inside strings or comments.
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.
Returns a State object.
Parses the source until the first unmatched close bracket (any of ), }, ]). It returns an object with the structure:
{
start: 0,//index of first character of string
end: 13,//index of first character after the end of string
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,//index of first character of string
end: 13,//index of first character after the end of string
src: 'source string'
}
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.
Get a default starting state.
Returns true if character represents punctuation in JavaScript.
Returns true if name is a keyword in JavaScript.
A state is an object with the following structure
{
lineComment: false, //true if inside a line comment
blockComment: false, //true if inside a block comment
singleQuote: false, //true if inside a single quoted string
doubleQuote: false, //true if inside a double quoted string
regexp: false, //true if inside a regular expression
escaped: false, //true if in a string and the last character was an escape character
roundDepth: 0, //number of un-closed open `(` brackets
curlyDepth: 0, //number of un-closed open `{` brackets
squareDepth: 0 //number of un-closed open `[` brackets
}
It also has the following useful methods:
.isString() returns true if the current location is inside a string..isComment() returns true if the current location is inside a comment.isNesting() returns true if the current location is anything but at the top level, i.e. with no nesting.MIT
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 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 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).
FAQs
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.
The npm package character-parser receives a total of 1,747,524 weekly downloads. As such, character-parser popularity was classified as popular.
We found that character-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

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.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.