Skip to content

A Typst package for rendering Extended Backus-Naur Form (EBNF) grammars with customizable fonts and color schemes

License

Notifications You must be signed in to change notification settings

nutthead/nutthead-ebnf.typ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

nutthead-ebnf

A Typst package for rendering Extended Backus-Naur Form (EBNF) grammars.

Faithful to the ISO 14977 (PDF) standard, albeit with a few judicious departures where deemed necessary.

Rust Function Example

Rust function example

Usage

#import "@preview/nutthead-ebnf:0.3.1": *

#context [
  #ebnf[
    #[
      #syntax-rule(
        meta-id: [CHAR],
        definition-list: (
          [
            #special-sequence[a Unicode scalar value]
          ],
        )
      )

      #syntax-rule(
        meta-id: [NUL],
        definition-list: (
          [
            #terminal[U+0000]
          ],
        )
      )

      #syntax-rule(
        meta-id: [Function],
        rule-comment: [some comment],
        rule-example: [```rust fn main() { }```],
        definition-list: (
          [
            #single-definition(illumination: "dimmed")[FunctionQualifiers]
            #terminal[fn]
            #single-definition[IDENTIFIER]
            #single-definition(illumination: "dimmed", qualifier: "opt")[GenericParams]
          ],
          (indent: 2),
          [
            #terminal[(]
            #single-definition(qualifier: "opt")[FunctionParameters]
            #terminal[)]
          ],
          [
            #single-definition(illumination: "dimmed", qualifier: "opt")[FunctionReturnType]
            #single-definition(illumination: "dimmed", qualifier: "opt")[WhereClause]
          ],
          [
            #grouped-sequence(
              single-definition[BlockExpression],
              terminal(illumination: "dimmed")[;]
            )
          ],
        ),
      )
    ]
  ]
]

API Reference

ebnf()

Container function that configures the EBNF rendering context.

Parameter Type Default Description
body content β€” Content to render
definition-separator-symbol str "|" Symbol for separating alternatives
delimiter-symbol str "`" Delimiter for terminal symbols
default-font dict Libertinus Serif 1em Default text font
monospaced-font dict DejaVu Sans Mono 0.887em Monospace font for terminals
#context [
  #ebnf[
    #syntax-rule(...)
  ]
]

syntax-rule()

Defines a production rule with a meta-identifier, optional comment, optional code example, and definition list.

Parameter Type Default Description
meta-id content none Left-hand side identifier
rule-comment content none Optional comment for the rule
rule-example content none Optional code example (typically raw content)
definition-list array none Array of definitions and indent controls
#syntax-rule(
  meta-id: [Function],
  rule-comment: [some comment],
  rule-example: [```rust fn main() { }```],
  definition-list: (
    (indent: 1),
    [#terminal[fn] #single-definition[IDENTIFIER]],
    (indent: 2),
    [#terminal[(] #single-definition(qualifier: "opt")[Params] #terminal[)]],
  ),
)

single-definition()

Renders one or more non-terminal references or terminals as a sequence in italic. Accepts variadic positional arguments.

Parameter Type Default Description
..body content β€” One or more elements to render as a sequence
illumination str none "dimmed" or "highlighted"
qualifier str none "opt" (?), "some" (+), or "any" (*)
#single-definition[IDENTIFIER]
#single-definition(illumination: "dimmed")[FunctionQualifiers]
#single-definition(qualifier: "opt")[GenericParams]
// Multiple elements in a sequence
#single-definition(
  terminal[fn],
  [IDENTIFIER],
  terminal[(]
)

terminal()

Renders a terminal symbol in monospace font with delimiters.

Parameter Type Default Description
body content β€” The terminal text
illumination str none "dimmed" or "highlighted"
qualifier str none "opt" (?), "some" (+), or "any" (*)
#terminal[fn]
#terminal(illumination: "dimmed")[;]
#terminal(qualifier: "opt")[const]

meta-identifier()

Renders a meta-identifier (left-hand side of a production) in bold with a production arrow.

#meta-identifier[Function]

comment()

Renders a comment enclosed in comment delimiters (* ... *).

Parameter Type Default Description
body content β€” Comment content
#comment[This is a comment about the grammar rule]

code-example()

Renders a code example enclosed in comment delimiters (* ... *). Typically used with raw content blocks.

Parameter Type Default Description
raw content β€” Raw code example
#code-example[```rust fn main() { }```]

grouped-sequence()

Renders content in parentheses ( ... ) for grouping alternatives. Accepts variadic positional arguments separated by the definition separator symbol.

Parameter Type Default Description
..body content β€” One or more alternatives to group
illumination str none "dimmed" or "highlighted"
qualifier str none "opt" (?), "some" (+), or "any" (*)
#grouped-sequence(
  single-definition[BlockExpression],
  terminal(illumination: "dimmed")[;]
)

optional-sequence()

Renders content in square brackets [ ... ] for optional elements. Accepts variadic positional arguments separated by the definition separator symbol.

Parameter Type Default Description
..body content β€” One or more optional alternatives
illumination str none "dimmed" or "highlighted"
qualifier str none "opt" (?), "some" (+), or "any" (*)
#optional-sequence(
  terminal[else],
  single-definition[Block]
)

repeated-sequence()

Renders content in curly braces { ... } for repetition (zero or more). Accepts variadic positional arguments separated by the definition separator symbol.

Parameter Type Default Description
..body content β€” One or more alternatives to repeat
illumination str none "dimmed" or "highlighted"
qualifier str none "opt" (?), "some" (+), or "any" (*)
#repeated-sequence(single-definition[Statement])

special-sequence()

Renders content in question marks ? ... ? for special sequences. Accepts variadic positional arguments separated by the definition separator symbol.

Parameter Type Default Description
..body content β€” One or more special sequence descriptions
illumination str none "dimmed" or "highlighted"
qualifier str none "opt" (?), "some" (+), or "any" (*)
#special-sequence[any Unicode character]
#special-sequence[a Unicode scalar value]

License

MIT

About

A Typst package for rendering Extended Backus-Naur Form (EBNF) grammars with customizable fonts and color schemes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published