All Projects → inhabitedtype → Angstrom

inhabitedtype / Angstrom

Licence: other
Parser combinators built for speed and memory efficiency

Programming Languages

ocaml
1615 projects

Projects that are alternatives of or similar to Angstrom

Parjs
JavaScript parser-combinator library
Stars: ✭ 145 (-66.59%)
Mutual labels:  parser-combinators, parsing
Syntax
Write value-driven parsers quickly in Swift with an intuitive SwiftUI-like DSL
Stars: ✭ 134 (-69.12%)
Mutual labels:  parsing, parser-combinators
Combine
A parser combinator library for Elixir projects
Stars: ✭ 174 (-59.91%)
Mutual labels:  parser-combinators, parsing
Parser Combinators From Scratch
Code that accompanies the series
Stars: ✭ 56 (-87.1%)
Mutual labels:  parser-combinators, parsing
parser-lang
A parser combinator library with declarative superpowers
Stars: ✭ 25 (-94.24%)
Mutual labels:  parsing, parser-combinators
Parsing With Haskell Parser Combinators
🔍 A step-by-step guide to parsing using Haskell parser combinators.
Stars: ✭ 72 (-83.41%)
Mutual labels:  parser-combinators, parsing
autumn
A Java parser combinator library written with an unmatched feature set.
Stars: ✭ 112 (-74.19%)
Mutual labels:  parsing, parser-combinators
Scala Parser Combinators
simple combinator-based parsing for Scala. formerly part of the Scala standard library, now a separate community-maintained module
Stars: ✭ 523 (+20.51%)
Mutual labels:  parser-combinators, parsing
Ramble
A R parser based on combinatory parsers.
Stars: ✭ 19 (-95.62%)
Mutual labels:  parsing, parser-combinators
ParsecSharp
The faster monadic parser combinator library for C#
Stars: ✭ 23 (-94.7%)
Mutual labels:  parsing, parser-combinators
Pyparsing
Python library for creating PEG parsers
Stars: ✭ 1,052 (+142.4%)
Mutual labels:  parser-combinators, parsing
parser-combinators
Lightweight package providing commonly useful parser combinators
Stars: ✭ 41 (-90.55%)
Mutual labels:  parsing, parser-combinators
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-89.86%)
Mutual labels:  parser-combinators, parsing
Pegtl
Parsing Expression Grammar Template Library
Stars: ✭ 1,295 (+198.39%)
Mutual labels:  parser-combinators, parsing
Comby
A tool for structural code search and replace that supports ~every language.
Stars: ✭ 912 (+110.14%)
Mutual labels:  parser-combinators, parsing
Funcparserlib
Recursive descent parsing library for Python based on functional combinators
Stars: ✭ 250 (-42.4%)
Mutual labels:  parser-combinators, parsing
Pidgin
C#'s fastest parser combinator library
Stars: ✭ 469 (+8.06%)
Mutual labels:  parser-combinators, parsing
metal
A Java library for parsing binary data formats, using declarative descriptions.
Stars: ✭ 13 (-97%)
Mutual labels:  parsing, parser-combinators
loquat
Monadic parser combinators for JavaScript / TypeScript
Stars: ✭ 47 (-89.17%)
Mutual labels:  parsing, parser-combinators
Pom
PEG parser combinators using operator overloading without macros.
Stars: ✭ 310 (-28.57%)
Mutual labels:  parser-combinators, parsing

Angstrom

Angstrom is a parser-combinator library that makes it easy to write efficient, expressive, and reusable parsers suitable for high-performance applications. It exposes monadic and applicative interfaces for composition, and supports incremental input through buffered and unbuffered interfaces. Both interfaces give the user total control over the blocking behavior of their application, with the unbuffered interface enabling zero-copy IO. Parsers are backtracking by default and support unbounded lookahead.

Build Status

Installation

Install the library and its dependencies via OPAM:

opam install angstrom

Usage

Angstrom is written with network protocols and serialization formats in mind. As such, its source distribution includes implementations of various RFCs that are illustrative of real-world applications of the library. These include an HTTP parser and a JSON parser.

In addition, it is an informal tradition for OCaml parser-combinator libraries to include in their READMEs a parser for a simple arithmetic expression language. The code below implements a parser for such a language and computes the numerical result of the expression as it is being parsed. Because Angstrom is written with network protocols and serialization libraries in mind, it does not include combinators for creating infix expression parsers. Such combinators, e.g., chainl1, are nevertheless simple to define.

open Angstrom

let parens p = char '(' *> p <* char ')'
let add = char '+' *> return (+)
let sub = char '-' *> return (-)
let mul = char '*' *> return ( * )
let div = char '/' *> return (/)
let integer =
  take_while1 (function '0' .. '9' -> true | _ -> false) >>| int_of_string

let chainl1 e op =
  let rec go acc =
    (lift2 (fun f x -> f acc x) op e >>= go) <|> return acc in
  e >>= fun init -> go init

let expr : int t =
  fix (fun expr ->
    let factor = parens expr <|> integer in
    let term   = chainl1 factor (mul <|> div) in
    chainl1 term (add <|> sub))

let eval (str:string) : int =
  match parse_string ~consume:All expr str with
  | Ok v      -> v
  | Error msg -> failwith msg

For an explanation of the infix operators and other combinators used in the implementation of this example, see the documentation in the mli.

Comparison to Other Libraries

There are several other parser-combinator libraries available for OCaml that may suit your needs, and are worth considering. Most of them are derivatives of or inspired by Parsec. As such, they require the use of a try combinator to achieve backtracking, rather than providing it by default. They also all use something akin to a lazy character stream as the underlying input abstraction. While this suits Haskell quite nicely, it requires blocking read calls when the entire input is not immediately available—an approach that is inherently incompatible with monadic concurrency libraries such as Async and Lwt, and writing high-performance, concurrent applications in general. Another consequence of this approach to modeling and retrieving input is that the parsers cannot iterate over sections of input in a tight loop, which adversely affects performance.

Below is a table that compares the features of Angstrom against the those of other parser-combinator libraries.

Feature \ Library Angstrom mparser planck opal
Monadic interface
Backtracking by default
Unbounded lookahead
Reports line numbers in errors
Efficient take_while/skip_while
Unbuffered (zero-copy) interface
Non-blocking incremental interface
Async Support
Lwt Support

Development

To install development dependencies, pin the package from the root of the repository:

opam pin add -n angstrom .
opam install --deps-only angstrom

After this, you may install a development version of the library using the install command as usual.

For building and running the tests during development, you will need to install the alcotest package:

opam install alcotest
make test

Acknowledgements

This library started off as a direct port of the inimitable attoparsec library. While the original approach of continuation-passing still survives in the source code, several modifications have been made in order to adapt the ideas to OCaml, and in the process allow for more efficient memory usage and integration with monadic concurrency libraries. This library will undoubtedly diverge further as time goes on, but its name will stand as an homage to its origin.

License

BSD3, see LICENSE file for its text.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].