Parsect is a parser combinator library for TypeScript or JavaScript. It provides a easy way to write a readable parser in only TypeScript/JavaScript without any other domain-specific languages like yacc/lex, ANTLR or PEG.js. Parsect can be used from not only TypeScript and JavaScript but also other AltJSs.
I got the idea for Parsect from Parsec parser combinator library in Haskell, however this is not a porting of Parsec. Unfortunately, this library doesn't have underlying Monad or Fanctor and it doesn't deal a string as a list of charactor. However, you can combine parsers in the same manner as Parsec with Parsect.
- Parsec-like API
- Statically typed: The API of Parsect is statically typed like Parsec with TypeScript. However, you can also use it from JavaScript as dynamically typed API.
- do-notation like syntax: Parsect has Haskell's do-notation-like notation style. This notation makes a parser more readable.
- Functional Programming API: Most of functions are referential transparent. It means you don't need to consider states of your parsers.
- Easy debugging: You can set a breakpoint in the middle of your parser and watch that the parser consumes the input step-by-step.
- Exitra parsers: Regular expression parser also is supported. You can combine RegExp parsers with other parsers.
- Token parser builder:
makeTokenParser
function is supported. - Expression parser builder:
buildExpressionParser
function is supported.
Parsect has some functions that creates a parser. string
function is one of them. string
take a string and return new parser. This parser parses the string and return it as a raw result value. For example, if you want to parse a string "apple", use string function as follows:
var parser: Parsect.Parser<string> = Parsect.string("apple");
var state: Parsect.State<void> = Parsect.state("apple, grape, banana", 0);
var reply: Parsect.Reply<string,void> = Parsect.parse(parser, state);
console.log(reply.value); // prints "apple".
Note a difference from string
in Parsec. If the parser recieve a unexpected input string like "application"
, the parser would consume no charactors and fail. The both two words have first four letter "appl"
but the parser don't throw a exception and parsing would continue to search other matchings.
Combinator is a function takes parsers as a parameter and returns new parser. many combinator create a parser parses infinitely repeat of a string pattern. many function takes a parser and return new parser. For example, the following parser comsumes a string like "abcabcabcabc...":
Parsect.many(Parsect.string("abc"))
seq
combinator provides do notaion like notaion for Parsect. seq
function take a other function for parameter. The parameter function is called back when the seq parses a string. The parameter function recieves a other function as a parameter. Typically, seq function is used as:
var parser: Parsect.Parser<number> = Parsect.seq((s: Parsect.Context<void>): number => {
s(string("("));
var v: number = s(number);
s(string(")"));
return v;
});
This parser parses a numeric string between parenses. The argument s
is a function takes a parser and execute it. A return value of s(number)
is not a Parsect.State
object but a raw value of result of parsing. So, the variable v
would be bounded a string
object. When p
parses a string "(100)"
, v
is 100
.
If a parser applied to s
failed, all following parsers would be ignore.
When the parsing succeeded, seq
returns a State object contains the value returned from the parameter function. Otherwise, the value
property of the state object is undefined
, regardless of the parameter function returns any value.
If calculation for the result value is costly, you should call success() function and avoid the calculation as follows:
var parser: Parsect.Parser<number> = Parsect.seq((s: Parsect.Context<void>)=>{
s(string("("));
var v: number = s(number);
s(string(")"));
return s.success && very_very_large_operation(v);
});
or
combinator takes some parsers as parameter and try parsing in sequence. If one of them could parse the input, the State object would be returned. For example, the following parser will consume any combination of "a" or "b":
Parsect.many(Parsect.or(Parsect.string("a"), Parsect.string("b")));
(TODO: Left Recursion)
(TODO: Definition Ordering)
Here are two parsers. pa
parses "[a]"
and pb
parses "[b]"
. A parser or(pa, pb)
fails to parse "[b]"
.
var pa = Parsect.between(Parsect.string('['), Parsect.string('a'), Parsect.string(']'));
var pb = Parsect.between(Parsect.string('['), Parsect.string('b'), Parsect.string(']'));
var parser = Parsect.or(pa, pb);
var state = Parsect.state("(b)", 0);
var reply = Parsect.parse(parser, state);
console.log(reply.success); // prints "false"!
It's because pa
consumes "["
even if whole of pa
failed. Generally, commonalizing "["
of both of parsers is the best way.
var pa = Parsect.series(Parsect.string('a'), Parsect.string(']'));
var pb = Parsect.series(Parsect.string('b'), Parsect.string(']'));
var parser = Parsect.seq(s=>{
s(string('['));
return s(Parsect.or(pa, pb));
});
var state = Parsect.state("(b)", 0);
var reply = Parsect.parse(parser, state);
console.log(reply.success); // prints "true"!
However, if commonalizing is difficult, you can use triable
combinator to solve it. triable
parser can retrieve overconsumed strings.
var pa = Parsect.between(Parsect.string('['), Parsect.string('a'), Parsect.string(']'));
var pb = Parsect.between(Parsect.string('['), Parsect.string('b'), Parsect.string(']'));
var parser = Parsect.or(Parsect.triable(pa), pb);
var state = Parsect.state("(b)", 0);
var reply = Parsect.parse(parser, state);
console.log(reply.success); // prints "true"!
U
User state.
state(source: string, position: number = 0, userState?: U): State<U>
source: string
Source string.position: number
Current position.getRowColumn(): { raw: number; column: number; }
Calculate raw number and column number from current position. Those values are ZERO-BASED.seek(delta: number): State<U>
Creates new State object that has new position.equals(src: State<U>): boolean
Compare two state objects.
class Reply<A,U>
A
Semantic value.U
User state.
ok<A,U>(state: State<U>, value: A): Reply<A,U>
error<A,U>(state: State<U>, expected?: ()=> string): Reply<A,U>
state: State<U>
State after the parsing.success: boolean
Succeed or failed.value: A
The semantic value.expected: ()=> string
equals(st: Reply<A,U>): boolean
U
User state.
<T>(p: Parser<T>): T
userState: U
(read/write) Current user state.peek: string
(For debugging) Current input string.success: boolean
(For debugging) Success or fail.
Invoke parser with the state.
// parser parses "abc" between "[" and "]".
var parser: Parsect.State<string> = Parsect.between(
Parsect.string("["),
Parsect.string("abc"),
Parsect.string("]")
);
var state: Parsect.State<void> = Parsect.state("[abc]", 0);
var reply: Reply<void> = Parsect.parse(parser, state);
console.log(reply.success); // true
console.log(reply.value); // "abc"
console.log(reply.state.source); // "[abc]"
console.log(reply.state.position); // 5
var state2: Parsect.State<void> = Parsect.state("[xyz]", 0);
var reply2: Reply<void> = Parsect.parse(parser, state);
console.log(reply2.success); // false
console.log(reply2.expected); // "abc"
console.log(reply2.state.source); // "[abc]"
console.log(reply2.state.position); // 1 (It's because the parser consume heading "[" then failed)
commentStart: Parser<string>
commentEnd: Parser<string>
commentLine: Parser<string>
nestedComments: boolean
identStart: Parser<string>
identLetter: Parser<string>
opStart: Parser<string>
opLetter: Parser<string>
reservedNames: string[]
reservedOpNames: string[]
caseSensitive: boolean
-
identifier: Parser<string>
-
reserved: (s: string)=>Parser<string>
-
operator: Parser<string>
-
reservedOp: (s: string)=>Parser<string>
-
charLiteral: Parser<string>
-
stringLiteral: Parser<string>
-
natural: Parser<number>
-
integer: Parser<number>
-
float: Parser<number>
-
naturalOrFloat: Parser<number>
-
decimal: Parser<number>
-
hexadecimal: Parser<number>
-
octal: Parser<number>
-
symbol: (s: String)=>Parser<string>
-
lexeme: <A>(p: Parser<A>)=>Parser<A>
-
whiteSpace: Parser<void>
-
parens: <A>(p: Parser<A>)=>Parser<A>
-
braces: <A>(p: Parser<A>)=>Parser<A>
-
angles: <A>(p: Parser<A>)=>Parser<A>
-
brackets: <A>(p: Parser<A>)=>Parser<A>
-
semi: Parser<string>
-
comma: Parser<string>
-
colon: Parser<string>
-
dot: Parser<string>
-
semiSep: <A>(p: Parser<A>)=>Parser<A[]>
-
semiSep1: <A>(p: Parser<A>)=>Parser<A[]>
-
commaSep: <A>(p: Parser<A>)=>Parser<A[]>
-
commaSep1: <A>(p: Parser<A>)=>Parser<A[]>
-
binary: <A>(name: string, fun: (a: A, b: A)=>A, assoc: Assoc) => Operator<A>
-
prefix: <A>(name: string, fun: (a: A)=>A) => Operator<A>
-
postfix: <A>(name: string, fun: (a: A)=>A) => Operator<A>
None
Left
Right
- 2013/09/26 ver. 0.2. All APIs are cleaned up.
makeTokenParser
andbuildExpressionParser
supported. - 2013/01/01 ver. 0.1. Happy New Year!
- Adding more test cases or examples.
- Performance tuning.
- Cleaning up user state API.
Parsect is licensed under the MIT License.
The MIT License
Copyright 2013 Kon (http://phyzkit.net/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Some of the codes of Parsect were ported from Parsec. Parsec is provided under BSD-style license as below:
Copyright 1999-2000, Daan Leijen; 2007, Paolo Martini. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This software is provided by the copyright holders "as is" and any express or
implied warranties, including, but not limited to, the implied warranties of
merchantability and fitness for a particular purpose are disclaimed. In no
event shall the copyright holders be liable for any direct, indirect,
incidental, special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use, data,
or profits; or business interruption) however caused and on any theory of
liability, whether in contract, strict liability, or tort (including
negligence or otherwise) arising in any way out of the use of this software,
even if advised of the possibility of such damage.
Author: Kon ( @KDKTN, http://phyzkit.net/ )
日本語でもおk Japanese also available to contact me.