npm install character-parser
Work out how much depth changes:
var state = ;assert;;assert;
Find code up to a custom delimiter:
// EJS-stylevar section = parser;;; // exclusive end of string;var section = parser;;; // exclusive end of string;// Jade-stylevar section = parser;; // exclusive end of string// Dumb parsing// Stop at first delimiter encountered, doesn't matter if it's nested or not// This is the character-parser@1 default behavior.var section = parser;; // exclusive end of string''
Delimiters are ignored if they are inside strings or comments.
All methods may throw an exception in the case of syntax errors. The exception contains an additional
code property that always starts with
CHARACTER_PARSER: that is unique for the error.
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.
Parses the source until the first occurence of
delimiter which is not in a string or a comment.
true, it will still count if the delimiter occurs in a line comment.
true, it will stop at the first bracket, not taking into account if the bracket part of nesting or not. See example above.
It returns an object with the structure:
start: 0//index of first character of stringend: 13//index of first character after the end of stringsrc: '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.
Objects whose values can be a frame in the
stack property of a State (documented below).
A state is an object with the following structure
stack: // stack of detected brackets; the outermost is regexpStart: false // true if a slash is just encountered and a REGEXP state has just been added to the stackescaped: false // true if in a string and the last character was an escape characterhasDollar: false // true if in a template string and the last character was a dollar signsrc: '' // the concatenated source stringhistory: '' // reversed `src`lastChar: '' // last parsed character
stack property can contain any of the following:
characterParser.BRACKETS(the end bracket, not the starting bracket)
It also has the following useful methods:
.current()returns the innermost bracket (i.e. the last stack frame).
trueif the current location is inside a string.
trueif the current location is inside a comment.
trueif the current location is not at the top level, i.e. if the stack is not empty. If
true, line comments are not counted as a level, so for
// ait will still return false.
All errors thrown by character-parser has a
code property attached to it that allows one to identify what sort of error is thrown. For errors thrown from
parseUntil, an additional
index property is available.
In character-parser@2, we have changed the APIs quite a bit. These are some notes that will help you transition to the new version.
Instead of keeping depths of different brackets, we are now keeping a stack. We also removed some properties:
statelineComment → statecurrent === parserTOKEN_TYPESLINE_COMMENTstateblockComment → statecurrent === parserTOKEN_TYPESBLOCK_COMMENTstatesingleQuote → statecurrent === parserTOKEN_TYPESSINGLE_QUOTEstatedoubleQuote → statecurrent === parserTOKEN_TYPESDOUBLE_QUOTEstateregexp → statecurrent === parserTOKEN_TYPESREGEXP
This function has been removed since the usefulness of this function has been questioned. You should find that
parseUntil is a better choice for your task.
The default behavior when the delimiter is a bracket has been changed so that nesting is taken into account to determine if the end is reached.
To preserve the original behavior, pass
ignoreNesting: true as an option.
To see the difference between the new and old behaviors, see the "Usage" section earlier.
This function has been merged into
parseUntil. You can directly rename the function call without any repercussions.