The abnf Reference Manual

Table of Contents

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

The abnf Reference Manual

This is the abnf Reference Manual, generated automatically by Declt version 2.4 "Will Decker" on Wed Jun 20 10:56:18 2018 GMT+0.


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

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

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>

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

2 Systems

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


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

2.1 abnf

Author

Dimitri Fontaine <dim@tapoueh.org>

License

WTFPL

Description

ABNF Parser Generator, per RFC2234

Dependencies
Source

abnf.asd (file)

Components

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

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 files   [Contents][Index]

3.1.1 abnf.asd

Location

abnf.asd

Systems

abnf (system)


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

3.1.2 abnf/package.lisp

Parent

abnf (system)

Location

package.lisp

Packages

abnf


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

3.1.3 abnf/abnf.lisp

Dependencies
Parent

abnf (system)

Location

abnf.lisp

Exported Definitions
Internal Definitions

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

4 Packages

Packages are listed by definition order.


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

4.1 abnf

Source

package.lisp (file)

Use List
Exported Definitions
Internal Definitions

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

5 Definitions

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


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

5.1 Exported definitions


Next: , Previous: , Up: Exported definitions   [Contents][Index]

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 (file)

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

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

Package

abnf

Source

abnf.lisp (file)

Special Variable: *abnf-rsyslog*

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

Package

abnf

Source

abnf.lisp (file)


Previous: , Up: Exported definitions   [Contents][Index]

5.1.2 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 (file)


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

5.2 Internal definitions


Next: , Previous: , Up: Internal definitions   [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 (file)

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

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

Package

abnf

Source

abnf.lisp (file)


Previous: , Up: Internal definitions   [Contents][Index]

5.2.2 Functions

Function: char-val-char-p CHARACTER
Package

abnf

Source

abnf.lisp (file)

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 (file)

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 (file)

Function: hexadecimal-char-p CHARACTER
Package

abnf

Source

abnf.lisp (file)

Function: process-quoted-regex PR

Helper function to process different kinds of quotes for regexps

Package

abnf

Source

abnf.lisp (file)

Function: rule-name-character-p CHARACTER
Package

abnf

Source

abnf.lisp (file)

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 (file)

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 (file)

Function: vcharp CHARACTER
Package

abnf

Source

abnf.lisp (file)


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

Appendix A Indexes


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

A.1 Concepts

Jump to:   A   F   L  
Index Entry  Section

A
abnf.asd: The abnf<dot>asd file
abnf/abnf.lisp: The abnf/abnf<dot>lisp file
abnf/package.lisp: The abnf/package<dot>lisp file

F
File, Lisp, abnf.asd: The abnf<dot>asd file
File, Lisp, abnf/abnf.lisp: The abnf/abnf<dot>lisp file
File, Lisp, abnf/package.lisp: The abnf/package<dot>lisp file

L
Lisp File, abnf.asd: The abnf<dot>asd file
Lisp File, abnf/abnf.lisp: The abnf/abnf<dot>lisp file
Lisp File, abnf/package.lisp: The abnf/package<dot>lisp file

Jump to:   A   F   L  

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

A.2 Functions

Jump to:   C   E   F   H   P   R   T   V  
Index Entry  Section

C
char-val-char-p: Internal functions

E
expand-rule: Internal functions
expand-rule-definition: Internal functions

F
Function, char-val-char-p: Internal functions
Function, expand-rule: Internal functions
Function, expand-rule-definition: Internal functions
Function, hexadecimal-char-p: Internal functions
Function, parse-abnf-grammar: Exported functions
Function, process-quoted-regex: Internal functions
Function, rule-name-character-p: Internal functions
Function, rule-name-symbol: Internal functions
Function, test: Internal functions
Function, vcharp: Internal functions

H
hexadecimal-char-p: Internal functions

P
parse-abnf-grammar: Exported functions
process-quoted-regex: Internal functions

R
rule-name-character-p: Internal functions
rule-name-symbol: Internal functions

T
test: Internal functions

V
vcharp: Internal functions

Jump to:   C   E   F   H   P   R   T   V  

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

A.3 Variables

Jump to:   *  
S  
Index Entry  Section

*
*abnf-default-rules*: Internal special variables
*abnf-rfc-syslog-draft-15*: Exported special variables
*abnf-rfc2234-abnf-definition*: Internal special variables
*abnf-rfc5424-syslog-protocol*: Exported special variables
*abnf-rsyslog*: Exported special variables

S
Special Variable, *abnf-default-rules*: Internal special variables
Special Variable, *abnf-rfc-syslog-draft-15*: Exported special variables
Special Variable, *abnf-rfc2234-abnf-definition*: Internal special variables
Special Variable, *abnf-rfc5424-syslog-protocol*: Exported special variables
Special Variable, *abnf-rsyslog*: Exported special variables

Jump to:   *  
S  

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

A.4 Data types

Jump to:   A   P   S  
Index Entry  Section

A
abnf: The abnf system
abnf: The abnf package

P
Package, abnf: The abnf package

S
System, abnf: The abnf system

Jump to:   A   P   S