A very simple and stupid parser, based on a statemachine and regular expressions.

It\textquotesingle{}s not intended for complex languages. It\textquotesingle{}s intended to easily write a simple parser for a simple language.\hypertarget{md_sfcode_node_modules_fastparse_README_autotoc_md10259}{}\doxysection{Usage}\label{md_sfcode_node_modules_fastparse_README_autotoc_md10259}
Pass a description of statemachine to the constructor. The description must be in this form\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{new Parser(description)}
\DoxyCodeLine{}
\DoxyCodeLine{description is \{}
\DoxyCodeLine{    // The key is the name of the state}
\DoxyCodeLine{    // The value is an object containing possible transitions}
\DoxyCodeLine{    "{}state-\/name"{}: \{}
\DoxyCodeLine{        // The key is a regular expression}
\DoxyCodeLine{        // If the regular expression matches the transition is executed}
\DoxyCodeLine{        // The value can be "{}true"{}, a other state name or a function}
\DoxyCodeLine{}
\DoxyCodeLine{        "{}a"{}: true,}
\DoxyCodeLine{        // true will make the parser stay in the current state}
\DoxyCodeLine{        }
\DoxyCodeLine{        "{}b"{}: "{}other-\/state-\/name"{},}
\DoxyCodeLine{        // a string will make the parser transit to a new state}
\DoxyCodeLine{        }
\DoxyCodeLine{        "{}[cde]"{}: function(match, index, matchLength) \{}
\DoxyCodeLine{            // "{}match"{} will be the matched string}
\DoxyCodeLine{            // "{}index"{} will be the position in the complete string}
\DoxyCodeLine{            // "{}matchLength"{} will be "{}match.length"{}}
\DoxyCodeLine{            }
\DoxyCodeLine{            // "{}this"{} will be the "{}context"{} passed to the "{}parse"{} method"{}}
\DoxyCodeLine{            }
\DoxyCodeLine{            // A new state name (string) can be returned}
\DoxyCodeLine{            return "{}other-\/state-\/name"{};}
\DoxyCodeLine{        \},}
\DoxyCodeLine{        }
\DoxyCodeLine{        "{}([0-\/9]+)(\(\backslash\)\(\backslash\).[0-\/9]+)?"{}: function(match, first, second, index, matchLength) \{}
\DoxyCodeLine{            // groups can be used in the regular expression}
\DoxyCodeLine{            // they will match to arguments "{}first"{}, "{}second"{}}
\DoxyCodeLine{        \},}
\DoxyCodeLine{        }
\DoxyCodeLine{        // the parser stops when it cannot match the string anymore}
\DoxyCodeLine{        }
\DoxyCodeLine{        // order of keys is the order in which regular expressions are matched}
\DoxyCodeLine{        // if the javascript runtime preserves the order of keys in an object}
\DoxyCodeLine{        // (this is not standardized, but it's a de-\/facto standard)}
\DoxyCodeLine{    \}}
\DoxyCodeLine{\} }

\end{DoxyCode}


The statemachine is compiled down to a single regular expression per state. So basically the parsing work is delegated to the (native) regular expression logic of the javascript runtime.


\begin{DoxyCode}{0}
\DoxyCodeLine{Parser.prototype.parse(initialState: String, parsedString: String, context: Object) }

\end{DoxyCode}


{\ttfamily initial\+State}\+: state where the parser starts to parse.

{\ttfamily parsed\+String}\+: the string which should be parsed.

{\ttfamily context}\+: an object which can be used to save state and results. Available as {\ttfamily this} in transition functions.

returns {\ttfamily context}\hypertarget{md_sfcode_node_modules_fastparse_README_autotoc_md10260}{}\doxysection{Example}\label{md_sfcode_node_modules_fastparse_README_autotoc_md10260}

\begin{DoxyCode}{0}
\DoxyCodeLine{var Parser = require("{}fastparse"{});}
\DoxyCodeLine{}
\DoxyCodeLine{// A simple parser that extracts @licence ... from comments in a JS file}
\DoxyCodeLine{var parser = new Parser(\{}
\DoxyCodeLine{    // The "{}source"{} state}
\DoxyCodeLine{    "{}source"{}: \{}
\DoxyCodeLine{        // matches comment start}
\DoxyCodeLine{        "{}/\(\backslash\)\(\backslash\)*"{}: "{}comment"{},}
\DoxyCodeLine{        "{}//"{}: "{}linecomment"{},}
\DoxyCodeLine{        }
\DoxyCodeLine{        // this would be necessary for a complex language like JS}
\DoxyCodeLine{        // but omitted here for simplicity}
\DoxyCodeLine{        // "{}\(\backslash\)"{}"{}: "{}string1"{},}
\DoxyCodeLine{        // "{}\(\backslash\)'"{}: "{}string2"{},}
\DoxyCodeLine{        // "{}\(\backslash\)/"{}: "{}regexp"{}}
\DoxyCodeLine{        }
\DoxyCodeLine{    \},}
\DoxyCodeLine{    // The "{}comment"{} state}
\DoxyCodeLine{    "{}comment"{}: \{}
\DoxyCodeLine{        "{}\(\backslash\)\(\backslash\)*/"{}: "{}source"{},}
\DoxyCodeLine{        "{}@licen[cs]e\(\backslash\)\(\backslash\)s((?:[\string^*\(\backslash\)n]|\(\backslash\)\(\backslash\)*+[\string^*/\(\backslash\)n])*)"{}: function(match, licenseText) \{}
\DoxyCodeLine{            this.licences.push(licenseText.trim());}
\DoxyCodeLine{        \}}
\DoxyCodeLine{    \},}
\DoxyCodeLine{    // The "{}linecomment"{} state}
\DoxyCodeLine{    "{}linecomment"{}: \{}
\DoxyCodeLine{        "{}\(\backslash\)n"{}: "{}source"{},}
\DoxyCodeLine{        "{}@licen[cs]e\(\backslash\)\(\backslash\)s(.*)"{}: function(match, licenseText) \{}
\DoxyCodeLine{            this.licences.push(licenseText.trim());}
\DoxyCodeLine{        \}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{var licences = parser.parse("{}source"{}, sourceCode, \{ licences: [] \}).licences;}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(licences); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_fastparse_README_autotoc_md10261}{}\doxysection{License}\label{md_sfcode_node_modules_fastparse_README_autotoc_md10261}
MIT (\href{http://www.opensource.org/licenses/mit-license.php}{\texttt{ http\+://www.\+opensource.\+org/licenses/mit-\/license.\+php}}) 