• Stars
    star
    236
  • Rank 170,480 (Top 4 %)
  • Language Idris
  • License
    Other
  • Created about 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parser combinators for Idris

Lightyear

Lightweight parser combinator library for Idris, inspired by Parsec.

Build Status

Module overview:

  • Lightyear.Core: central definitions + instances
  • Lightyear.Errmsg: error message formatting, mainly internal library
  • Lightyear.Combinators: generic combinators like many or sepBy
  • Lightyear.Char: char-bound parsers like char or space
  • Lightyear.Strings: string-bound parsers like strings or lexeme

Synopsis

This package is used (almost) the same way as Parsec, except for one difference: backtracking.

Commitment

  • Parsec combinators won't backtrack if a branch of <|> has consumed any input, hence Parsec parsers require an explicit try.

  • Lightyear parsers are backtrack-by-default and there is the commitTo combinator that makes the parser commit to that branch.

In other words, the following two pieces of code are equivalent (using illustrative combinator names):

Parsec:

elem :: Parser Int
elem = (try (string "0x") >> hexNumber) <|> decNumber

Lightyear:

elem : Parser Int
elem = (string "0x" $> commitTo hexNumber) <|> decNumber
-- which may be abbreviated as:
--   = (string "0x" >! hexNumber) <|> decNumber

After reading the prefix 0x, both parsers commit to reading a hexadecimal number or nothing at all — Parsec does this automatically, Lightyear uses the commitTo combinator for this purpose. On the other hand, Parsec requires the string "0x" to be wrapped in try because if we are reading 0123, we definitely don't want to commit to the left branch upon seeing the leading 0.

For convenience, commitTo is merged in monadic and applicative operators, yielding the operators >!=, >!, <$!>, <$!, and $!>. The ! in the names is inspired by the notation used for cuts in Prolog.

A parser that uses commitment might look like this (notice the leading char '@' that leads to commitment):

entry : Parser Entry
entry = char '@' >! do
  typ <- pack <@> some (satisfy (/= '{'))
  token "{"
  ident <- pack <@> some (satisfy (/= ','))
  token ","
  items <- item `sepBy` comma
  token "}"
  return $ En typ ident items

Lazy Branching with <|>|

It is worth noting that Idris itself is a strict language, and thus the <|> operator will evaluate both its arguments eagerly by default. In order to lazily evaluate different parsing branches we are required to use a special operator: <|>|. In general, all recursive calls of combinators have to occur in a lazy context. (With mutual recursion, this generalises to the rule that each call cycle has to be broken by laziness in at least one place.)

In the wild, it might look like this:

partial parseExpr : Parser SExpr
parseExpr = parseName <|>| ( MkSExpr <$> parens (many parseExpr) )

In the above example, the whole RHS of <|>| is lazy, and so the recursive occurrence of parseExpr in it will be evaluated only if the LHS of <|>| fails. Using <|> would cause infinite recursion.

For convenience, a version of <*> that lazily evaluates its second argument is included as <*>|. Conversely to <|>|, the RHS of <*>| will be evaluated only if the LHS of <*>| succeeds.

Example

Lightyear is used to parse BibTeX in bibdris.

Build

$ make clean
$ make test
$ make install

More Repositories

1

idris-py

Python backend for Idris (generates Python source, not bytecode).
Haskell
124
star
2

rarcrack

[unofficial fork] RAR bruteforce cracker
C
97
star
3

pmtu

Path MTU discovery tool
Python
34
star
4

itt-idris

ITT: quantified dependent calculus with inference of all modalities, implemented in Idris 2
Idris
23
star
5

idris-ocaml

OCaml back end for Idris
Haskell
22
star
6

bibdris

BibTeX database management in Idris
Idris
18
star
7

idris-data-frame

Data frames for Idris 2
Idris
18
star
8

ttstar

Dependently typed core calculus with erasure
Idris
17
star
9

itt

Tiny dependent calculus with inference of irrelevance and erasure
Haskell
15
star
10

text

Text framework for Idris
Idris
13
star
11

idris-benchmarks

Some benchmarks for Idris
Idris
11
star
12

idris2-mlf

Malfunction backend for Idris 2
Idris
11
star
13

idris-bytes

FFI-based byte buffers for Idris
Idris
11
star
14

keychain-bruteforce

Bruteforce master passwords of the MAC OS X password manager.
Python
8
star
15

idris-scheme

Scheme codegen for Idris
Haskell
6
star
16

scan-eSCL

Scan from eSCL-compatible scanners
Python
4
star
17

deconvolution

A deconvolution algorithm
MATLAB
4
star
18

meshub

A lightweight mesh VPN
Python
3
star
19

simple-prolog

A simple Prolog implementation in Haskell
Haskell
3
star
20

macro-pp

Universal macro preprocessor
Haskell
3
star
21

wireplay

[MIRROR] A minimalist approach to replay pcap-dumped TCP sessions.
C
2
star
22

idris2-perf

Various benchmarks
Idris
2
star
23

hackup

Backup system written in Haskell
Haskell
2
star
24

ever-to-excel

Compile a Scheme-like language into a spreadsheet
Haskell
2
star
25

globusator

Globe stripe generator
Perl
2
star
26

hilite

Terminal output highlighter
Perl
1
star
27

buslittles

Watch subtitles on laptop while film is playing on TV
CoffeeScript
1
star
28

qanachem

Anaglyph Chemistry
C++
1
star
29

stacker

Astrophotography stacker
C++
1
star
30

lamtc

Simple Prolog type checker
Prolog
1
star
31

wlist

iw scan parser
Haskell
1
star
32

jpg-recover

Recover JPEG/CR2 files from raw bytes.
C
1
star
33

idris-ttstar

TTstar backend for Idris
Haskell
1
star
34

excomp

A certified compiler of exceptions from my thesis (Agda)
JavaScript
1
star
35

ecomp

A simple certified expression compiler (Coq)
Verilog
1
star
36

ledger-vim

Feature-free vim plugin for ledger files
Vim Script
1
star
37

anamovie

Create anaglyph movies from stereomovies
C
1
star
38

dictpress

Preprocess wordlists before compression
C
1
star
39

trees

Even-odd trees from plain lists, structurally:
Haskell
1
star
40

ofca

OFCA Free Command Analyzer
C++
1
star
41

leveler

HDR to LDR leveler
C++
1
star
42

pastemath

A math pastebin (pastemath.functor.sk)
JavaScript
1
star