gkappler / CombinedParsers.jl

Compiled parser combinators and regular expressions in pure julia
MIT License
77 stars 10 forks source link
julia-types parser-combinators

CombinedParsers in pure Julia

Dev Build Status Codecov 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

Getting started

CombinedParsers.jl is a registered package. Install with

] add CombinedParsers

Example: rational numbers arithmetics

This example demonstrates reading of arithmetical terms for rational numbers. Reflecting operator precedence, term are subterms, interleaved by */, and subterms 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.

Acknowledgements

This package leverages Julia's compiler and superior type system to parsing.

I am thankful for contributions and inspiration from many great packages:

TextParse.jl

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).

Inspirations

Next Steps

Contributing and Questions

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.