uclp

2026-11-29

An experimental implementation of Parsing Expression Grammers in Common Lisp

Upstream URL

github.com/ravi-delia/uclp

Author

Ravi D'Elia

License

MIT
README

1Unnamed Common Lisp Peg

UCLP is an experimental implementation of PEG parsing in Common Lisp. A parsing expression grammar is a very elegant way of recognizing, parsing, and transforming text- much more powerful than regular expressions without the complexity of a custom-built parser. Note that while it is possible to parse PEGs in guaranteed linear time (at the cost of linear space) with a packrat parser, UCLP does not. Most patterns, unless very pathological, can be rewritten to run in linear time, and for ease of use some necessary departures from the strict definition are included.

UCLP patterns are just made of native data structures, so it's easy to compose patterns and interact with them in code. Unlike regular expressions, PEG syntax is pretty readable even if you aren't familiar with the specifics. The below example is largely copied from the Janet language documentation:

  (defparameter ip-address
    '(grammar
       :dig (range "09")
       :0-4 (range "04")
       :0-5 (range "05")
       :byte (choice
               (sequence "25" :0-5)
               (sequence "2" :0-4 :dig)
               (sequence "1" :dig :dig)
               (between :dig 1 2))
       :main (sequence :byte "." :byte "." :byte "." :byte)))

  ; uclp:match returns two values, a boolean indicating success/failure and a list of captures
  (uclp:match ip-address "0.0.0.0") ; -> t nil
  (uclp:match ip-address "elephant") ; -> nil
  (uclp:match ip-address "256.0.0.0") ; -> nil
  (uclp:match ip-address "0.0.0.0moretext") ; -> t nil

If you follow the link you'll see that the example is almost exactly copied, with the only differences being those forced by the differences in language syntax. UCLP is a very close reproduction of the semantics of Janet's PEG module. After experiencing how pleasant text parsing is in Janet you'll also feel the urge to rewrite it for every language you use.

As of now UCLP is usable. Bugs are to be expected, but almost all of Janet's patterns are supported and there shouldn't be any significant footguns. Unfortunately it has only been tested on SBCL, but I plan on at least expanding that to some other implementations since there isn't much implementation-specific code. Except of course that for it to be performant it needs to be compiled quickly to a quick runtime, ideally machine code.

1.1Usage

The peg syntax used by UCLP is largely a 1:1 reproduction of Janet's. Differences arenoted below, but otherwise you can safely default to using Janet's documentation. Manytasks that might require a particular function when using regex can be accomplished withthe correct pattern. However, for convenience, there are entrypoints for a few commonuse-cases.

1.1.1(match rule str &optional start &rest args)

This is the primary entrypoint to UCLP. Matches str against rule anchored to start.Returns two values on success, a boolean indicating success or failure and a list ofcaptures, and nil otherwise. Additional arguments are accessible while matching using theargument pattern. Note that if using additional arguments you must specify a start.

1.1.2(compile-peg rule &key (quiet? t) debug?)

Compiles rule to source ahead of time. The resulting compiled peg can be used anywhere apattern is taken, such as in match or any other entrypoint here. If there are any holes,instead compiles to a frame which can be filled with fill-holes.

1.1.3(assemble-peg rule &key (quiet? t) debug?)

Compiles rule to a chain of closures. Like compile-peg, if there are any holes, thiswill return a frame. For more information, see Assembling vs Compiling.

1.1.4(fill-holes frame &rest names-and-values)

Takes a frame and a &rest parameter consisting of alternating hole names and values. Formore on frames and holes please see Holes below.

1.1.5(captured rule str &optional start &rest args)

Takes exactly the same arguments as match but returns in the opposite order- first thelist of captures, and success as the second value. Useful to get at captures withoutmultiple-value-bind.

1.1.6(replace-all rule replace str &optional start &rest args)

Returns the string resulting from replacing every instance of rule in str withreplace, which may be a string to substitute or a function taking as an argument thesubstring matched by rule. Returns a boolean indicating if matches were found as asecond value.

1.1.7(replace-one rule replace str &optional start &rest args)

Behaves like replace-all, but replaces only the first match.

1.1.8(find-all rule str &optional start &rest args)

Gives the position of each match in str as a list, such that
  (defparameter dig '(* "dig:" :d))
  (defparameter str "dig:7, dig:8, dig:9")

  (every (lambda (position)
           (uclp:match dig str position))
         (uclp:find-all dig str)) ; => t

1.1.9(find-one rule str &optional start &rest args)

Returns the position of the first match, or nil if there are no matches.

1.2Differences From Janet

Mostly UCLP adheres to the behavior of Janet pegs, enough so that Janet's documentation isbetter than anything I'll have put together for a little while. However, there are somedifferences which obviously need to be documented somewhere. Some are due to differencesin host language, some are due to taste, and some are just features I haven't implementedyet.

Any difference in behavior from Janet not mentioned below is a bug, and either the behavior or the documentation will need to be changed.

1.2.1Unimplemented

Janet buffers and strings are simply byte strings, and Janet pegs work on arbitrarystrings of bytes. UCLP expects to work with Common Lisp strings, which in general arenot byte strings but vectors of type character. As such the patterns intended to workon bytes are not implemented. These are: uint, uint-be, int, and int-be. Becauseusing PEGs to parse binaries is so nice, I plan on at some point implementing some way ofcompiling PEGs intended to operate on raw bytes. However, specialization will be atcompile-time and the above patterns will likely be available only in a byte PEG.

I'm not yet sure if UCLP will ever support a general number pattern. It's possible I'll bring in parse-float to make a float as well as a general number pattern.

1.2.2Implemented

  • While UCLP does not have number, it does have integer, which takes identicalarguments and parses an integer using parse-integer.
  • To create a template PEG with holes filled in later, UCLP introduces the (hole name)pattern. For more information on holes, see Holes below.

1.2.3Changes

  • Anywhere a string literal can go, including those in range or set, a character orlist of characters and strings can also go. This is because Common Lisp strings do not haveescape codes like Janet strings. So (range ("a" #\Newline)) in UCLP is the same as(range "a\n") in Janet.
  • between, at-least, at-most, and look all have the pattern as the first argument, unlikein Janet where it is the last argument.
  • backmatch requires a tag argument, and will not look up captures on the capture stack
  • replace takes either a string which it captures literally, or a function which it calls.Taking other datatypes literally will probably be in the next version. But unlike Janet,it will never look the matches up.
  • Grammars, represented in Janet by tables or structs, are written in UCLP with thegrammar rule, which is followed by alternating keywords naming rules and patternsimplementing them.
  • Because the reader doesn't distinguish between :s and :S, the complement of a built-inpattern is prefixed with !. So :!s instead of :S.
  • The error pattern has significant differences from Janet's error. See Error below.
  • In addition to the + and * variants of built-in patterns, UCLP has a maybe variantmarked by a ?. So :w? denotes (? :w).
  • UCLP includes any (*), some (+), and maybe (?) variants of complement patterns. So:!d+ is (some (if-not :d 1)). See Aliases below

1.3Compiling vs Assembling

The initial concept for UCLP was that all patterns would be translated to Common Lispsource and compiled at runtime. This provides the fastest matching, but even with SBCL'slightning-fast compile times there is a significant amount of overhead. Therefore,patterns can either be assembled or compiled.

An assembled PEG can be used exactly like a compiled PEG. Both are simply Common Lisp functions wrapped in a struct, which can be transparently passed to match or composed with other patterns. However, unlike compiling a PEG, assembling is fast- fewer than a few milliseconds. The tradeoff is that they're up to 3 times slower at match time.

For one-off patterns created at runtime, it makes the most sense to assemble rather than compile. That's why PEGs passed as raw data are transparently assembled, not compiled. If you know a pattern in advance, you can compile it ahead of time with compile-peg.

To get as much compiled performance as possible without paying compile costs at runtime, see Holes below.

1.4Holes

UCLP introduces the (hole name) pattern, which represents an unknown pattern which will befilled in later. Name is a keyword. If a PEG with a hole is compiled or assembled, theresult will be a frame. A frame can be turned into a pattern by providing a value for eachhole. Two holes with the same name will be filled with the same pattern.
  (defparameter *delimited-frame*
    (compile-peg '(split (hole :div) '(any 1))))
  (defparameter *s* "123,456|789")

  (match (fill-holes *delimited-frame* :div ",") *s*) ; => t ("123" "456|789")
  (match (fill-holes *delimited-frame* :div "|") *s*) ; => t ("123,456" "789")

Holes allow you to compile almost an entire PEG ahead of time, assembling just what's needed on demand to avoid paying the compilation overhead at runtime.

1.5Aliases

UCLP offers aliases, keywords that stand in for larger patterns, similar to the Janet'sbuilt-in patterns. And like built-in patterns, aliases are user extensible. However, thereare a number of differences which are important to be aware of. Aliases are not firstclass citizens of UCLP- rather than full mutually recursive subpatterns, they are simplefind-and-replace macros, inserted literally. You can reference other aliases from insideone, but if you create a cycle it'll just blow out the stack. So just be cautious!

Aliases are stored in the alist *aliases*. You can manipulate *aliases* directly, or call the helper functions register-alias! and register-alias-suite!. Both take the name of the alias as a keyword, and the body as a peg expression, and push the new alias to *aliases*. However, register-alias-suite! will also add the complement, some, maybe, and any variants, like so:

  (uclp:register-alias-suite! :v '(set "vV"))
  (uclp:match '(* :v (<- :!v+)) "v not a V") ; => t (" not a ")

1.6Error

Because Common Lisp conditions are so different from Janet signals, the error patternhas some subtleties in UCLP. It takes arguments of the form(error &optional pat condition). With 0 arguments, it will raise a peg-error. With one argument, it willraise an error only if pat matches. With two arguments, it will raise a condition solong as pat matches. To specify a particular condition a pattern must be given, butsomething like 0 will always match.

UCLP special cases conditions inheriting from peg-error, which is exported. A peg-error has slots pat, matched, and caps. If a pattern is given, these will automatically be filled by the pattern itself, the text that pattern matched, and the captures from the pattern respectively. If no pattern is supplied they will all be nil. These slots can be accessed with error-pat, error-matched, and error-caps. By default peg-error has a report function giving the pattern and, if nonempty, the matching substring. Depending on your choice of pattern, this can be tolerably readable.

Dependencies (2)

  • alexandria
  • fiveam

Dependents (0)

    • GitHub
    • Quicklisp