The abnf Reference Manual

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

The abnf Reference Manual

This is the abnf Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Wed Jun 15 03:28:09 2022 GMT+0.

Table of Contents


1 Introduction

ABNF DEFINITION OF ABNF

This Common Lisp librairie implements a parser generator for the ABNF grammar format as described in RFC2234.

The generated parser is a regular expression scanner provided by the cl-ppcre lib, which means that we can't parse recursive grammar definition. One such definition is the ABNF definition as given by the RFC. Fortunately, as you have this lib, you most probably don't need to generate another parser to handle that particular ABNF grammar.

Installation

The system has been made Quicklisp ready.

$ cd ~/quicklisp/local-projects/
$ git clone https://github.com/dimitri/cl-abnf.git
* (ql:quickload "abnf")

Currently the ABNF system is maintained as part of the pgloader tool as a central piece of its syslog message parser facility.

Usage

The parse-abnf-grammar function expects the grammar to be parsed as a string, and also needs the top level rule name of the grammar you're interested into, as a symbol or a string. You can also give a list of rule names that you want to capture, they will be capture in the order in which they are needed to expand the given top-level rule.

The parse-abnf-grammar function returns a cl-ppcre scanner.

(defvar *timestamp-abnf*
  "   TIMESTAMP       = NILVALUE / FULL-DATE \"T\" FULL-TIME
      FULL-DATE       = DATE-FULLYEAR \"-\" DATE-MONTH \"-\" DATE-MDAY
      DATE-FULLYEAR   = 4DIGIT
      DATE-MONTH      = 2DIGIT  ; 01-12
      DATE-MDAY       = 2DIGIT  ; 01-28, 01-29, 01-30, 01-31 based on
                                ; month/year
      FULL-TIME       = PARTIAL-TIME TIME-OFFSET
      PARTIAL-TIME    = TIME-HOUR \":\" TIME-MINUTE \":\" TIME-SECOND
                        [TIME-SECFRAC]
      TIME-HOUR       = 2DIGIT  ; 00-23
      TIME-MINUTE     = 2DIGIT  ; 00-59
      TIME-SECOND     = 2DIGIT  ; 00-59
      TIME-SECFRAC    = \".\" 1*6DIGIT
      TIME-OFFSET     = \"Z\" / TIME-NUMOFFSET
      TIME-NUMOFFSET  = (\"+\" / \"-\") TIME-HOUR \":\" TIME-MINUTE

      NILVALUE        = \"-\" "
  "A timestamp ABNF grammar.")

(let ((scanner (abnf:parse-abnf-grammar *timestamp-abnf*
					:timestamp
					:registering-rules '(:full-date))))
  (cl-ppcre:register-groups-bind (date)
      (scanner "2013-09-08T00:02:03.123456Z+02:00")
    date))

In the previous usage example the let block returns "2013-09-08".

ABNF grammar

This library supports the ABNF grammar as given in RFC 2234, with additional support for plain regular expressions.

Parsed grammar

Here's the RFC syntax:

    rulelist       =  1*( rule / (*c-wsp c-nl) )

    rule           =  rulename defined-as elements c-nl
                           ; continues if next line starts
                           ;  with white space

    rulename       =  ALPHA *(ALPHA / DIGIT / "-")

    defined-as     =  *c-wsp ("=" / "=/") *c-wsp
                           ; basic rules definition and
                           ;  incremental alternatives

    elements       =  alternation *c-wsp

    c-wsp          =  WSP / (c-nl WSP)

    c-nl           =  comment / CRLF
                           ; comment or newline

    comment        =  ";" *(WSP / VCHAR) CRLF

    alternation    =  concatenation
                      *(*c-wsp "/" *c-wsp concatenation)

    concatenation  =  repetition *(1*c-wsp repetition)

    repetition     =  [repeat] element

    repeat         =  1*DIGIT / (*DIGIT "*" *DIGIT)

    element        =  rulename / group / option /
                      char-val / num-val / prose-val / regex
                           ; regex is an addition of this lib, see above

    group          =  "(" *c-wsp alternation *c-wsp ")"

    option         =  "[" *c-wsp alternation *c-wsp "]"

    char-val       =  DQUOTE *(%x20-21 / %x23-7E) DQUOTE
                           ; quoted string of SP and VCHAR
                           ;   without DQUOTE

    num-val        =  "%" (bin-val / dec-val / hex-val)

    bin-val        =  "b" 1*BIT
                      [ 1*("." 1*BIT) / ("-" 1*BIT) ]
                           ; series of concatenated bit values
                           ; or single ONEOF range

    dec-val        =  "d" 1*DIGIT
                      [ 1*("." 1*DIGIT) / ("-" 1*DIGIT) ]

    hex-val        =  "x" 1*HEXDIG
                      [ 1*("." 1*HEXDIG) / ("-" 1*HEXDIG) ]

    prose-val      =  "<" *(%x20-3D / %x3F-7E) ">"
                           ; bracketed string of SP and VCHAR
                           ;   without angles
                           ; prose description, to be used as
                           ;   last resort

Core rules

Those parts of the grammar are always provided, they are the defaults rules of the ABNF definition.

    ALPHA          =  %x41-5A / %x61-7A   ; A-Z / a-z

    BIT            =  "0" / "1"

    CHAR           =  %x01-7F
                           ; any 7-bit US-ASCII character, excluding NUL

    CR             =  %x0D
                           ; carriage return

    CRLF           =  CR LF
                           ; Internet standard newline

    CTL            =  %x00-1F / %x7F
                           ; controls

    DIGIT          =  %x30-39
                           ; 0-9

    DQUOTE         =  %x22
                           ; " (Double Quote)

    HEXDIG         =  DIGIT / "A" / "B" / "C" / "D" / "E" / "F"

    HTAB           =  %x09
                           ; horizontal tab

    LF             =  %x0A
                           ; linefeed

    LWSP           =  *(WSP / CRLF WSP)
                           ; linear white space (past newline)

    OCTET          =  %x00-FF
                           ; 8 bits of data

    SP             =  %x20
                       ; space

    VCHAR          =  %x21-7E
                           ; visible (printing) characters

    WSP            =  SP / HTAB
                           ; white space

Regex Support

We add support for plain regexp in the element rule. A regexp is expected to follow the form:

   regex           = "~" delimiter expression delimiter

The expression shouldn't contain the delimiter of course, and the allowed delimiters are ~//, ~[], ~{}, ~(), ~<>, ~"", ~'', ~|| and ~##. If you have to build a regexp with more than one of those delimiters in it, you can just concatenate multiple parts together like in this example:

 complex-regex  = ~/foo{bar}/ ~{baz/quux}

That will be used in exactly the same way as the following example:

 complex-regex  = ~<foo{bar}baz/quux>

2 Systems

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


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

2.1 abnf

ABNF Parser Generator, per RFC2234

Author

Dimitri Fontaine <dim@tapoueh.org>

License

WTFPL

Dependencies
  • esrap (system).
  • cl-ppcre (system).
Source

abnf.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 abnf/abnf.asd

Source

abnf.asd.

Parent Component

abnf (system).

ASDF Systems

abnf.


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

3.1.2 abnf/package.lisp

Source

abnf.asd.

Parent Component

abnf (system).

Packages

abnf.


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

3.1.3 abnf/abnf.lisp

Dependency

package.lisp (file).

Source

abnf.asd.

Parent Component

abnf (system).

Public Interface
Internals

4 Packages

Packages are listed by definition order.


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

4.1 abnf

Source

package.lisp.

Use List
  • common-lisp.
  • esrap.
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


5.1.1 Special variables

Special Variable: *abnf-rfc-syslog-draft-15*

See http://tools.ietf.org/html/draft-ietf-syslog-protocol-15#page-10

Package

abnf.

Source

abnf.lisp.

Special Variable: *abnf-rfc5424-syslog-protocol*

See http://tools.ietf.org/html/rfc5424#section-6

Package

abnf.

Source

abnf.lisp.

Special Variable: *abnf-rsyslog*

See http://www.rsyslog.com/doc/syslog_protocol.html

Package

abnf.

Source

abnf.lisp.


5.1.2 Ordinary functions

Function: parse-abnf-grammar (abnf-string top-level-rule &key registering-rules junk-allowed)

Parse STRING as an ABNF grammar as defined in RFC 2234. Returns a cl-ppcre scanner that will only match strings conforming to given grammar.

See http://tools.ietf.org/html/rfc2234 for details about the ABNF specs. Added to that grammar is support for regular expression, that are expected in the ELEMENT production and spelled ~/regex/. The allowed delimiters are: ~// ~[] ~{} ~() ~<> ~"" ~” ~|| and ~##.

Package

abnf.

Source

abnf.lisp.


5.2 Internals


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

5.2.1 Special variables

Special Variable: *abnf-default-rules*

An alist of the usual rules needed for ABNF grammars

Package

abnf.

Source

abnf.lisp.

Special Variable: *abnf-rfc2234-abnf-definition*

See http://tools.ietf.org/html/rfc2234#section-4

Package

abnf.

Source

abnf.lisp.


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

5.2.2 Ordinary functions

Function: char-val-char-p (character)
Package

abnf.

Source

abnf.lisp.

Function: expand-rule (rule-name rule-set &optional registering-rules)

Given a rule, expand it completely removing references to other parsed rules

Package

abnf.

Source

abnf.lisp.

Function: expand-rule-definition (definition rule-set registering-rules already-expanded-rules)

Expand given rule DEFINITION within given RULE-SET

Package

abnf.

Source

abnf.lisp.

Function: hexadecimal-char-p (character)
Package

abnf.

Source

abnf.lisp.

Function: process-quoted-regex (pr)

Helper function to process different kinds of quotes for regexps

Package

abnf.

Source

abnf.lisp.

Function: rule-name-character-p (character)
Package

abnf.

Source

abnf.lisp.

Function: rule-name-symbol (rule-name &key find-symbol)

Turn the string we read in the ABNF into internal symbol.

Package

abnf.

Source

abnf.lisp.

Function: test (&key times)

This serves as a test and an example: if you’re going to use the same scanner more than one, be sure to compute it only once.

Package

abnf.

Source

abnf.lisp.

Function: vcharp (character)
Package

abnf.

Source

abnf.lisp.


Appendix A Indexes


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

A.1 Concepts