NAME

Grammar::Formal - Object model to represent formal BNF-like grammars

SYNOPSIS

use Grammar::Formal;

my $g = Grammar::Formal->new;

my $s1 = Grammar::Formal::CaseSensitiveString->new(value => "a");
my $s2 = Grammar::Formal::CaseSensitiveString->new(value => "b");
my $choice = Grammar::Formal::Choice->new(p1 => $s1, p2 => $s2);

$g->set_rule("a-or-b" => $choice);

DESCRIPTION

This module provides packages that can be used to model formal grammars with production rules for non-terminals and terminals with arbitrary operators and operands. The idea is to have a common baseline format to avoid transformations between object models. Currently it has enough features to model IETF ABNF grammars without loss of information (minor details like certain syntax choices notwithstanding). All packages use Moose.

API

Grammar::Formal::Pattern 
  # Base package for all operators and operands
  has rw user_data # Simple extension point
  has ro parent    # parent node if any

  + Grammar::Formal::Binary
    # Base package for operators with 2 children

    has ro p1 # first child
    has ro p2 # second child

    + Grammar::Formal::Group  # concatenation
    + Grammar::Formal::Choice # alternatives
    + Grammar::Formal::OrderedChoice # ... with preference

  + Grammar::Formal::Unary
    # Base package for operators with 1 child

    has ro p # the child pattern

    + Grammar::Formal::ZeroOrMore # zero or more
    + Grammar::Formal::OneOrMore  # one or more
    + Grammar::Formal::SomeOrMore # min-bounded

      has ro min # minimum number of occurences

    + Grammar::Formal::BoundedRepetition
      # bound repetition

      has ro min # minimum number of occurences
      has ro max # maximum number of occurences

    + Grammar::Formal::Rule
      # grammar production rule

      has ro name # name of the non-terminal symbol

  + Grammar::Formal::Reference
    # Named reference to a non-terminal symbol

    has ro ref # name of the referenced non-terminal
    can expand # returns the associated ::Rule or undef

  + Grammar::Formal::Grammar
    # A grammar pattern with named rules

    has ro rules # Hash mapping rule names to ::Rules
    has ro start # optional start symbol
    can set_rule($name, $value) # set rule $name to ::Rule $rule

  + Grammar::Formal::CaseSensitiveString
    # Case-sensitive sequence of characters

    has ro value # Text string this represents

  + Grammar::Formal::AsciiInsensitiveString
    # Sequence of characters that treats [A-Z] like [a-z]

    has ro value # Text string

  + Grammar::Formal::ProseValue
    # Free form text description, as in IETF ABNF grammars

    has ro value # Prose

  + Grammar::Formal::Range
    # Range between two integers (inclusive)

    has ro min # first integer in range
    has ro max # last integer in range

  + Grammar::Formal::CharClass
    # Set of integers

    has ro spans            # a Set::IntSpan object
    can from_numbers(@ints) # static constructor

EXPORTS

None.

TODO

Surely there is a better way to automatically generate better POD?

AUTHOR / COPYRIGHT / LICENSE

Copyright (c) 2014 Bjoern Hoehrmann <bjoern@hoehrmann.de>.
This module is licensed under the same terms as Perl itself.