FruitsConfits
A well typed and sugared parser combinator framework for TypeScript/JavaScript.
Install
npm install fruitsconfits
NOTICE:
Use withwebpack >= 5
If you get the error:
Module not found: Error: Can't resolve '(importing/path/to/filename)' in '(path/to/node_modules/path/to/dirname)' Did you mean '(filename).js'?`
Add following setting to your
webpack.config.js
.test: /\.m?js/resolve:fullySpecified: falseOn
webpack >= 5
, the extension in the request is mandatory for it to be fully specified if the origin is a '.mjs' file or a '.js' file where the package.json contains '"type": "module"'.
Breakikng changes
See CHANGELOG.md.
Features
- Build a lexer or parser for a string or object list by parser combinator.
- The parser can receive user data context and acts as a reducer.
- Sugar sweet API syntaxes.
High level APIs
getStringParsers(params)
Get the string parser generators.
;
params
rawToToken
: function to convert string token to AST element.concatTokens
: function to merge two AST elements into one AST element.
returns
returns an object that containing the parsers.
seq(needle: string)
- parser to match the sequence
needle
- parser to match the sequence
cls(...needles: string[])
- parser to match the sequence classes
needles
- parser to match the sequence classes
notCls(...needles: string[])
- parser to match the negation of sequence classes
needles
- parser to match the negation of sequence classes
clsFn(needle: (src: string) => number)
- parser to match the sequence class
needle
- the class is defined by the lambda function
- parser to match the sequence class
- classes
alpha
- parser to match the sequence of us-ascii alphabetic characters
upper
- parser to match the sequence of us-ascii upper alphabetic characters
lower
- parser to match the sequence of us-ascii lower alphabetic characters
num
- parser to match the sequence of us-ascii decimal numeric characters
nonzero
- parser to match the sequence of us-ascii decimal numeric characters except
0
- parser to match the sequence of us-ascii decimal numeric characters except
bin
- parser to match the sequence of us-ascii binary numeric characters
oct
- parser to match the sequence of us-ascii octal numeric characters
hex
- parser to match the sequence of us-ascii hexadecimal numeric characters
alnum
- parser to match the sequence of us-ascii alphabetic and numeric characters
space
- parser to match the sequence of whitespace characters except newline (CR/LF) characters
- the whitespace characters definition conform to javascript regexp
- parser to match the sequence of whitespace characters except newline (CR/LF) characters
spaceWithinSingleLine
- parser to match the sequence of whitespace characters
- the whitespace characters definition conform to javascript regexp
- parser to match the sequence of whitespace characters
ctrl
- parser to match the sequence of control characters
- the control characters definition conform to javascript regexp
- parser to match the sequence of control characters
newline
- parser to match the sequence of newline (CR/LF) characters
word
- parser to match the negation of the sequence of whitespaces and control characters
- the whitespace and control characters definition conform to javascript regexp
- parser to match the negation of the sequence of whitespaces and control characters
any
- parser to match the sequence class that matches to any token
- numbers
bin(...prefixes: StringParserFnWithCtx<C, R>[])
- parse a binary number
oct(...prefixes: StringParserFnWithCtx<C, R>[])
- parse a octal number
hex(...prefixes: StringParserFnWithCtx<C, R>[])
- parse a hex number
int
- parse a javascript style integer number
bigint
- parse a javascript style bigint number
float
- parse a javascript style floating point number
isParam(criteria: (o: any) => boolean, conv?: (o: any) => any)
- parser to match a ES6 template strings parameter value
cat(...parsers: StringParserFnWithCtx<C, R>[])
- parser that combine and concatenate the parsing results of
parsers
- parser that combine and concatenate the parsing results of
once(parser: StringParserFnWithCtx<C, R>)
- parser to match once to the parsing results of
parser
- parser to match once to the parsing results of
repeat(parser: StringParserFnWithCtx<C, R>)
- parser to match zero or more times to the parsing results of
parser
- parser to match zero or more times to the parsing results of
qty(min?: number, max?: number) => (parser: StringParserFnWithCtx<C, R>)
- parser to match min to max times to the parsing results of
parser
- if min and max are ommitted, it is same as
repeat
parser - if max is ommitted, it matches min or more times
- if min and max are ommitted, it is same as
- parser to match min to max times to the parsing results of
zeroWidth(helper?: () => R)
- parser to match any zero width and return result that is provided by
helper
- parser to match any zero width and return result that is provided by
err(message: string)
- parser to match any zero width and raise the error that has
message
- parser to match any zero width and raise the error that has
beginning(helper?: () => R)
- parser to match zero width beginning of input and return result that is provided by
helper
- parser to match zero width beginning of input and return result that is provided by
end(helper?: () => R)
- parser to match zero width end of input and return result that is provided by
helper
- parser to match zero width end of input and return result that is provided by
first(...parsers: StringParserFnWithCtx<C, R>[])
- parser to match the first matched parser in the
parsers
- parser to match the first matched parser in the
or(...parsers: StringParserFnWithCtx<C, R>[])
- parser to match the most long matched parser in the
parsers
- parser to match the most long matched parser in the
combine(...parsers: StringParserFnWithCtx<C, R>[])
- parser that combine parsers
parsers
- parser that combine parsers
erase(...parsers: StringParserFnWithCtx<C, R>[])
- parser that combine parsers
parsers
and return empty result[]
- parser that combine parsers
trans(fn: (tokens: R[]) => R[]) => (...parsers: StringParserFnWithCtx<C, R>[])
- parser that combine parsers
parsers
and transform the result byfn
- parser that combine parsers
ahead(...parsers: StringParserFnWithCtx<C, R>[])
- parser to match zero width by reading ahead and matching with
parsers
- parser to match zero width by reading ahead and matching with
behind(n: number, helper?: () => R)(...parsers: StringParserFnWithCtx<C, R>[])
- parser to match zero width by reading behind and matching with
parsers
and return result that is provided byhelper
- parser to match zero width by reading behind and matching with
rules(args: ApplyProductionRulesArg<string, C, R>) => (lexer: StringParserFnWithCtx<C, R>)
- parser to match the production rules
args
- args.rules : production rules
- args.maxApply : maximum number of production rules applied
- args.check : end condition of production rules
- parser to match the production rules
makeProgram
- parser to enclose most outer
- converts the internal
ParseError
thrown to a return value
- converts the internal
- parser to enclose most outer
getObjectParsers(params)
Get the object list parser generators.
;
params
rawToToken
: function to convert the input object list item to AST element.concatTokens
: function to merge two AST elements into one AST element.comparator
: function to compare two input object list items.
returns
returns an object that containing the parsers.
seq(needle: T)
- parser to match the sequence
needle
- parser to match the sequence
cls(...needles: T[number][])
- parser to match the sequence classes
needles
- parser to match the sequence classes
notCls(...needles: T[number][])
- parser to match the negation of sequence classes
needles
- parser to match the negation of sequence classes
clsFn(needle: (src: T[number]) => boolean)
- parser to match the sequence class
needle
- the class is defined by the lambda function
- parser to match the sequence class
- classes
any
- parser to match the sequence class that matches to any token
cat(...parsers: ParserFnWithCtx<T, C, R>[])
- parser that combine and concatenate the parsing results of
parsers
- parser that combine and concatenate the parsing results of
once(parser: ParserFnWithCtx<T, C, R>)
- parser to match once to the parsing results of
parser
- parser to match once to the parsing results of
repeat(parser: ParserFnWithCtx<T, C, R>)
- parser to match zero or more times to the parsing results of
parser
- parser to match zero or more times to the parsing results of
qty(min?: number, max?: number) => (parser: ParserFnWithCtx<T, C, R>)
- parser to match min to max times to the parsing results of
parser
- if min and max are ommitted, it is same as
repeat
parser - if max is ommitted, it matches min or more times
- if min and max are ommitted, it is same as
- parser to match min to max times to the parsing results of
zeroWidth(helper?: () => R)
- parser to match any zero width and return result that is provided by
helper
- parser to match any zero width and return result that is provided by
err(message: string)
- parser to match any zero width and raise the error that has
message
- parser to match any zero width and raise the error that has
beginning(helper?: () => R)
- parser to match zero width beginning of input and return result that is provided by
helper
- parser to match zero width beginning of input and return result that is provided by
end(helper?: () => R)
- parser to match zero width end of input and return result that is provided by
helper
- parser to match zero width end of input and return result that is provided by
first(...parsers: ParserFnWithCtx<T, C, R>[])
- parser to match the first matched parser in the
parsers
- parser to match the first matched parser in the
or(...parsers: ParserFnWithCtx<T, C, R>[])
- parser to match the most long matched parser in the
parsers
- parser to match the most long matched parser in the
combine(...parsers: ParserFnWithCtx<T, C, R>[])
- parser that combine parsers
parsers
- parser that combine parsers
erase(...parsers: ParserFnWithCtx<T, C, R>[])
- parser that combine parsers
parsers
and return empty result[]
- parser that combine parsers
trans(fn: (tokens: R[]) => R[]) => (...parsers: ParserFnWithCtx<T, C, R>[])
- parser that combine parsers
parsers
and transform the result byfn
- parser that combine parsers
ahead(...parsers: ParserFnWithCtx<T, C, R>[])
- parser to match zero width by reading ahead and matching with
parsers
- parser to match zero width by reading ahead and matching with
behind(n: number, helper?: () => R)(...parsers: ParserFnWithCtx<T, C, R>[])
- parser to match zero width by reading behind and matching with
parsers
and return result that is provided byhelper
- parser to match zero width by reading behind and matching with
rules(args: ApplyProductionRulesArg<T, C, R>) => (lexer: ParserFnWithCtx<T, C, R>)
- parser to match the production rules
args
- args.rules : production rules
- args.maxApply : maximum number of production rules applied
- args.check : end condition of production rules
- parser to match the production rules
makeProgram
- parser to enclose most outer
- converts the internal
ParseError
thrown to a return value
- converts the internal
- parser to enclose most outer
parserInput(src: T, context?: C)
Build a parser input.
Example:
... ;
templateStringsParserInput(strings, values, context?: C)
Build a parser input from ES6 template strings.
Example:
;
Examples
Real world examples
- Tynder
- TypeScript friendly Data validator for JavaScript.
- Code
- kanban-board-app
- Kanban style task management board app
- Code
- Open SOQL
License
ISC
Copyright (c) 2019 Shellyl_N and Authors.