A package for combining parsers and transforming strings into julia types.
Compose parsers parsimoneously within a functional parser combinator paradigm, utilize Julia's type inference for transformations, log conveniently for debugging, and let Julia compile your parser for performance.
The CombinedParsers
design
CombinedParsers.jl
is a registered package.
Install with
] add CombinedParsers
This example demonstrates reading of arithmetical terms for rational numbers.
Reflecting operator precedence, term
are subterm
s, interleaved by */,
and subterm
s are Either
integer numbers
@syntax subterm = Either{Rational{Int}}([NumericParser(Int)]; convert=true)
or a subterm
can also be an additive term
in parentheses
:
@syntax for parentheses in subterm
mult = evaluate |> join(subterm, CharIn("*/"), infix=:prefix )
@syntax term = evaluate |> join(mult, CharIn("+-"), infix=:prefix )
Sequence(2,'(',term,')')
end
This CombinedParser
definition in 5,5 lines registers a @term_string
macro for parsing and evaluating rational arithmetics:
julia> term"4*10+2"
42//1
Is every rational answer ultimately the inverse of a universal question in life?
Details in the full documentation example.
This package leverages Julia's compiler and superior type system to parsing.
I am thankful for contributions and inspiration from many great packages:
A bunch of fast text parsing tools, used in CSV.jl
CombinedParsers
composes with fast
TextParse.jl both ways
because CombinedParser <: TextParse.AbstractToken
and by providing a method for TextParse.tryparsenext
,
(leveraging the supreme Julia compiler, type and package architecture).
parse
methods.Automa
processing of parsed tokens is done with actions).ParserCombinator
, written before 2016 (and fixed for Julia 1.0 in 2018).
CombinedParsers
integrates into the Julia 1.0 Iteration API, small Union{Nothing,T} where T
types instead of using Nullables, compiler optimizations and generated functions.
I want to provide benchmarks comparisons with ParserCombinator.jl
.Contributions and feedback are very welcome, especially regarding brief syntax and constructor dispatch. Please open an issue if you encounter any problems or would just like to ask a question, or contact me at mail@g-kappler.de.