The parseltongue Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The parseltongue Reference Manual

This is the parseltongue Reference Manual, version 0.0.1, generated automatically by Declt version 4.0 beta 2 "William Riker" on Wed Jun 15 05:30:56 2022 GMT+0.

Table of Contents


1 Introduction

Copyright 2012, Vincent Toups This program is distributed under the terms of the GNU Lesser General Public License (see license.txt).

Parseltongue

A Parser Combinator Library for CL

This is a parser combinator library for Common Lisp inspired by SMUG, by Drew Crampsie and augmented with improvements inspired by my own usage of this library and other monadic computations in various Lisps.

This library probably has the same functionality as SMUG but migth be more familiar to those used to Haskell's do notation.

Fear not, you needn't understand how monads work in order to make good use of this library. For those uninterested in how the library works, there are only two things you need to know to use it: what parsers are, how to use combinators on them, and how to use the special syntax in the parser and defparser forms.

Parsers

Parsers, in this library, are functions which accept a single parameter, the input, and return either nil, if they cannot parse anything from that input, or a list of parser-pair structs (defined by the library), each of which represents a possible parsing of the input, along with the remainder of the input which was not parsed. For instance, a parser that parses "a" from a string looks like:

(defun =parse-a (input)
 (if (empty? input)
     nil
     (let ((first-char (next input)))
       (if (string= first-char "a")
           (list (parser-pair first-char
                   (rest-of input)))
           nil))))

The function next is a method which fetches the next item from an input. next is defined for lists and strings by default, but can be extended by the user with defmethod. rest-of is its partner, it returns the rest of the input. parser-pair constructs a parser-pair struct instance whose first value is the parsed result, and whose second value is the rest of the input that was not parsed. As indicated above, returning nil means nothing was parsed at all.

Any function which conforms to this type can be treated as a parser by the library. If you want to write you own parsers without ever touching the special syntax in the library, you do so just as we did above. By convention, parsers in the library start with the = character, to distinguish them visually from other functions.

We could have written the above parser with a combinator from the library, =>string, like so:

(defun =parse-a (input) (funcall (=>string "a") input))

=>string is a function which takes a string and returns a parser, which we use to parse the input. Any function which produces a parser begins with => in this library. The simplest such function is the function referred to as the parser return function, =>.

(=> 'some-value)

=> returns a parser which does nothing to the input, and returns a single parser-pair (in a list), whose return value is some-value.

The whole idea of this library is to construct parsers from simpler parsers.

Special Syntax for Parser Construction and Definition

The library provides syntax to make writing parsers easier. We could have defined =parse-a above like so, for instance:

(defparser =parse-a 
  (x <- =item)
  (if (string= x "a") (=> x) =nil))

Since we often treat parsers as both functions and regular variables, defparser establishes both a variable =parse-a and a function by the same name. This function, as defined above, is equivalent to the previous definitions, but how do we read it?

Within the body of a defparser (or parser form, which is the anonymous version), each expression must be either a parser itself or a "binding expression" of the form

`(variable <- parser-expression)` 

When an expression is just a parser, that parser must succeed or the entire parser being defined will return nil. Subsequent parsers executed in the body will then see only the input not parsed by previous parsers.

When a binding expression is encountered, the parser on the right hand side is to the current input, which may have been parsed down by previous expressions in the body, and, if the parse succeeds, then in the rest of the body, the variable in the left-hand-side is bound to the value in the value-slot of the parser-pairs returned by the parser. Hence, in the above expression, the first form:

(x <- =item)

Sees =item applied to the input of the parser. =item always pulls one item off of the input and only fails when the input is empty. If the input is empty, then the parse fails, and no further forms are executed. If =item succeeds, then we look to the next line. The next line is not a binding form. It is an if statement, which is fine, subject to the constraint that each branch must return a parser. If the item we've parsed from the input is "a", then we use => to create a parser which inserts "a" as its parser-pair's value. If not, we return the =nil parser, which always fails.

If we wanted to parse "a" and then "b", we'd write:

(defparser =parse-ab
 (=>string "a")
 (=>string "b"))

Important Combinators

The =>or combinator produces a parser if any of its input parsers succeed, returning the value of the first success from left to right. For instance, to parser "a" or "b":

(defparser =a-or-b 
 (=>or (=>string "a")
       (=>string "b")))

Then:

(=a-or-b "abc") -> (list (parser-pair "a" "bc"))
(=a-or-b "bbc") -> (list (parser-pair "b" "bc"))

The combinator =>and succeeds only when all of its input parsers succeed in turn, finally returning the result of the last parser:

(funcall (=>and (=>string "a") (=>string "b")) "ab") ->
 (list (parser-pair "b" ""))

The combinator =>items parses n or fewer items from the input, regardless of what they are:

(funcall (=>items 3) "abcd") -> 
 (list (parser-pair (list "a" "b" "c") "d"))

The combinator =>zero-plus-more parsers as many of its input parser as possible and returns them in a list, possibly an empty one:

(funcall (=>zero-plus-more
          (=>string "a"))
         "aaaab") ->
(list (parser-pair (list "a" "a" "a" "a")
                   "b"))

The combinator =>one-plus-more does the same except it fails if there is not at least one parsable object.

Non-determinism

Parseltongue, like SMUG, is actually a non-deterministic library - parsers can parse in multiple ways simultaneously. I'll write some documentation about that later, but if you are using it for regular deterministic parser, you are usually interested in only the first parser-result. Hence, the function parse/first-result is a handy thing:

(parser/first-result (=>string "a") "abc") -> "a"

It returns the first parse result and leaves off the leftover input.

Thanks

I'd like to thank Drew for writing up SMUG, which was critical in developing an understanding of monads in Lisp and obviously in inspiring this library. He also provided some correspondence when I didn't understand aspects of his code.

Other Notes:

If you like this library, it is almost a line for line port of an Elisp parser combinator library I also wrote, available in my emacs-utils repository here on github.



2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 parseltongue

Parseltongue

Maintainer

Vincent Toups

Author

Vincent Toups

License

LGPL

Long Description

A monadic parser combinator library with Haskell do-like notation.

Version

0.0.1

Dependency

lisp-unit (system).

Source

parseltongue.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 parseltongue/parseltongue.asd

Source

parseltongue.asd.

Parent Component

parseltongue (system).

ASDF Systems

parseltongue.


3.1.2 parseltongue/package.lisp

Source

parseltongue.asd.

Parent Component

parseltongue (system).

Packages

parseltongue.


3.1.3 parseltongue/parseltongue.lisp

Dependency

package.lisp (file).

Source

parseltongue.asd.

Parent Component

parseltongue (system).

Public Interface
Internals

3.1.4 parseltongue/tests.lisp

Dependency

parseltongue.lisp (file).

Source

parseltongue.asd.

Parent Component

parseltongue (system).


4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 parseltongue

Source

package.lisp.

Use List
  • common-lisp.
  • lisp-unit.
Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


Next: , Previous: , Up: Public Interface   [Contents][Index]

5.1.1 Special variables

Special Variable: =item

A parser

Package

parseltongue.

Source

parseltongue.lisp.

Special Variable: =rest

A parser

Package

parseltongue.

Source

parseltongue.lisp.


5.1.2 Macros

Macro: defparser (name/args maybe-doc &rest body)
Package

parseltongue.

Source

parseltongue.lisp.

Macro: parser (&rest forms)
Package

parseltongue.

Source

parseltongue.lisp.


5.1.3 Ordinary functions

Function: =>and (&rest ps)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>eq (to)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>equal (to)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>items (n)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>items->string (n)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>maybe (=p)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>maybe-alternative (=p alt)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>one-plus-more (=p)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>or (&rest ps)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>reduce-concat (=p)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>satisfies (fun)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>string (s)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>zero-plus-more (p)

Produce a parser which parses P zero or more times and monadically returns the results in a list.

Package

parseltongue.

Source

parseltongue.lisp.

Function: =item (input)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =rest (input)
Package

parseltongue.

Source

parseltongue.lisp.

Function: parse/first-result (=p input)
Package

parseltongue.

Source

parseltongue.lisp.

Function: parser-bind (=p =>p)
Package

parseltongue.

Source

parseltongue.lisp.

Function: parser-pair (value input)
Package

parseltongue.

Source

parseltongue.lisp.

Function: parser-return (&rest items)
Package

parseltongue.

Source

parseltongue.lisp.


5.1.4 Generic functions

Generic Function: empty? (list)
Package

parseltongue.

Methods
Method: empty? ((string string))
Source

parseltongue.lisp.

Method: empty? ((list list))
Source

parseltongue.lisp.

Generic Function: next (list)
Package

parseltongue.

Methods
Method: next ((string string))
Source

parseltongue.lisp.

Method: next ((list list))
Source

parseltongue.lisp.

Generic Function: rest-of (list)
Package

parseltongue.

Methods
Method: rest-of ((string string))
Source

parseltongue.lisp.

Method: rest-of ((list list))
Source

parseltongue.lisp.


5.1.5 Structures

Structure: parser-pair
Package

parseltongue.

Source

parseltongue.lisp.

Direct superclasses

structure-object.

Direct slots
Slot: value
Readers

parser-pair-value.

Writers

(setf parser-pair-value).

Slot: input
Readers

parser-pair-input.

Writers

(setf parser-pair-input).


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Special variables

Special Variable: =nil

A parser

Package

parseltongue.

Source

parseltongue.lisp.


5.2.2 Macros

Macro: defun/var (name arg-list &rest body)
Package

parseltongue.

Source

parseltongue.lisp.

Macro: named-let (name bindings &rest body)
Package

parseltongue.

Source

parseltongue.lisp.

Macro: parser-helper (&rest forms)
Package

parseltongue.

Source

parseltongue.lisp.


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.3 Ordinary functions

Function: => (&rest items)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>and2 (=p1 =p2)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>list (=p)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =>or2 (=p1 =p2)
Package

parseltongue.

Source

parseltongue.lisp.

Function: =nil (input)
Package

parseltongue.

Source

parseltongue.lisp.

Function: alist (a key)

Return the value at KEY or NIL.

Package

parseltongue.

Source

parseltongue.lisp.

Function: alist-cons (a key val)

CONS VAL onto the LIST held at KEY in the ALIST A.

Package

parseltongue.

Source

parseltongue.lisp.

Function: bind-form? (form)
Package

parseltongue.

Source

parseltongue.lisp.

Function: copy-parser-pair (instance)
Package

parseltongue.

Source

parseltongue.lisp.

Function: make-parser-pair (&key value input)
Package

parseltongue.

Source

parseltongue.lisp.

Function: mapcar/deal (fun lst)

Map FUN over LST. FUN returns a list of two items, the first of which is a key the second of which is a value. The VALUES are accumulated at the KEYS in an ALIST which is returned.

Package

parseltongue.

Source

parseltongue.lisp.

Reader: parser-pair-input (instance)
Writer: (setf parser-pair-input) (instance)
Package

parseltongue.

Source

parseltongue.lisp.

Target Slot

input.

Function: parser-pair-p (object)
Package

parseltongue.

Source

parseltongue.lisp.

Reader: parser-pair-value (instance)
Writer: (setf parser-pair-value) (instance)
Package

parseltongue.

Source

parseltongue.lisp.

Target Slot

value.

Function: parser-plus (&rest ps)
Package

parseltongue.

Source

parseltongue.lisp.

Function: reverse-alist-keys (a)

Reverse the lists held at each key in A.

Package

parseltongue.

Source

parseltongue.lisp.

Function: strcat (&rest s)
Package

parseltongue.

Source

parseltongue.lisp.

Function: zero-plus-more-step (substate parser)

Apply PARSER to the CDR of substate. If it succeeds, cons the result onto the list in the CAR of substate and indicate CONTINUE for MAPCAR/DEAL. If PARSER on CDR of substate FAILS, then reverse the CAR of SUBSTATE and return this value consed with the last INPUT state.

Package

parseltongue.

Source

parseltongue.lisp.


5.2.4 Generic functions

Generic Function: empty-of (list)
Package

parseltongue.

Methods
Method: empty-of ((string string))
Source

parseltongue.lisp.

Method: empty-of ((list list))
Source

parseltongue.lisp.

Generic Function: prefix (o string)
Package

parseltongue.

Methods
Method: prefix (o (list list))
Source

parseltongue.lisp.

Method: prefix (o (string string))
Source

parseltongue.lisp.


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   (   =  
A   B   C   D   E   F   G   M   N   P   R   S   Z  
Index Entry  Section

(
(setf parser-pair-input): Private ordinary functions
(setf parser-pair-value): Private ordinary functions

=
=>: Private ordinary functions
=>and: Public ordinary functions
=>and2: Private ordinary functions
=>eq: Public ordinary functions
=>equal: Public ordinary functions
=>items: Public ordinary functions
=>items->string: Public ordinary functions
=>list: Private ordinary functions
=>maybe: Public ordinary functions
=>maybe-alternative: Public ordinary functions
=>one-plus-more: Public ordinary functions
=>or: Public ordinary functions
=>or2: Private ordinary functions
=>reduce-concat: Public ordinary functions
=>satisfies: Public ordinary functions
=>string: Public ordinary functions
=>zero-plus-more: Public ordinary functions
=item: Public ordinary functions
=nil: Private ordinary functions
=rest: Public ordinary functions

A
alist: Private ordinary functions
alist-cons: Private ordinary functions

B
bind-form?: Private ordinary functions

C
copy-parser-pair: Private ordinary functions

D
defparser: Public macros
defun/var: Private macros

E
empty-of: Private generic functions
empty-of: Private generic functions
empty-of: Private generic functions
empty?: Public generic functions
empty?: Public generic functions
empty?: Public generic functions

F
Function, (setf parser-pair-input): Private ordinary functions
Function, (setf parser-pair-value): Private ordinary functions
Function, =>: Private ordinary functions
Function, =>and: Public ordinary functions
Function, =>and2: Private ordinary functions
Function, =>eq: Public ordinary functions
Function, =>equal: Public ordinary functions
Function, =>items: Public ordinary functions
Function, =>items->string: Public ordinary functions
Function, =>list: Private ordinary functions
Function, =>maybe: Public ordinary functions
Function, =>maybe-alternative: Public ordinary functions
Function, =>one-plus-more: Public ordinary functions
Function, =>or: Public ordinary functions
Function, =>or2: Private ordinary functions
Function, =>reduce-concat: Public ordinary functions
Function, =>satisfies: Public ordinary functions
Function, =>string: Public ordinary functions
Function, =>zero-plus-more: Public ordinary functions
Function, =item: Public ordinary functions
Function, =nil: Private ordinary functions
Function, =rest: Public ordinary functions
Function, alist: Private ordinary functions
Function, alist-cons: Private ordinary functions
Function, bind-form?: Private ordinary functions
Function, copy-parser-pair: Private ordinary functions
Function, make-parser-pair: Private ordinary functions
Function, mapcar/deal: Private ordinary functions
Function, parse/first-result: Public ordinary functions
Function, parser-bind: Public ordinary functions
Function, parser-pair: Public ordinary functions
Function, parser-pair-input: Private ordinary functions
Function, parser-pair-p: Private ordinary functions
Function, parser-pair-value: Private ordinary functions
Function, parser-plus: Private ordinary functions
Function, parser-return: Public ordinary functions
Function, reverse-alist-keys: Private ordinary functions
Function, strcat: Private ordinary functions
Function, zero-plus-more-step: Private ordinary functions

G
Generic Function, empty-of: Private generic functions
Generic Function, empty?: Public generic functions
Generic Function, next: Public generic functions
Generic Function, prefix: Private generic functions
Generic Function, rest-of: Public generic functions

M
Macro, defparser: Public macros
Macro, defun/var: Private macros
Macro, named-let: Private macros
Macro, parser: Public macros
Macro, parser-helper: Private macros
make-parser-pair: Private ordinary functions
mapcar/deal: Private ordinary functions
Method, empty-of: Private generic functions
Method, empty-of: Private generic functions
Method, empty?: Public generic functions
Method, empty?: Public generic functions
Method, next: Public generic functions
Method, next: Public generic functions
Method, prefix: Private generic functions
Method, prefix: Private generic functions
Method, rest-of: Public generic functions
Method, rest-of: Public generic functions

N
named-let: Private macros
next: Public generic functions
next: Public generic functions
next: Public generic functions

P
parse/first-result: Public ordinary functions
parser: Public macros
parser-bind: Public ordinary functions
parser-helper: Private macros
parser-pair: Public ordinary functions
parser-pair-input: Private ordinary functions
parser-pair-p: Private ordinary functions
parser-pair-value: Private ordinary functions
parser-plus: Private ordinary functions
parser-return: Public ordinary functions
prefix: Private generic functions
prefix: Private generic functions
prefix: Private generic functions

R
rest-of: Public generic functions
rest-of: Public generic functions
rest-of: Public generic functions
reverse-alist-keys: Private ordinary functions

S
strcat: Private ordinary functions

Z
zero-plus-more-step: Private ordinary functions

Jump to:   (   =  
A   B   C   D   E   F   G   M   N   P   R   S   Z