Class Grammar

java.lang.Object
quickparse.grammar.Grammar
All Implemented Interfaces:
java.lang.Iterable<Rule>

public class Grammar
extends java.lang.Object
implements java.lang.Iterable<Rule>
Represents a BNF (Backus-Naur Form) grammar as a set of syntactical rules. To create a new instance of this class, the method create() must be used. A grammar is an iterable object whose elements are the grammar rules.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  Grammar.Builder
    Allows the construction of a grammar, rule by rule
  • Method Summary

    Modifier and Type Method Description
    static Grammar.Builder create()
    Creates a new Grammar.Builder that allows the construction of a grammar, rule by rule.
    ConstructSymbol getAxiom()
    Gets the axiom of the grammar (i.e., the first symbol from which a parser should start to interpret the grammar)
    ConstructSymbol getConstructSymbol​(java.lang.String name)
    Gets the ConstructSymbol associated to the given construct name, if any.
    java.util.Collection<ConstructSymbol> getConstructSymbols()
    Gets all the construct symbols that are specified in this grammar.
    java.util.Set<java.util.regex.Pattern> getIgnoredPatterns()
    Returns the list of patterns to ignore.
    java.util.Map<ConstructSymbol,​java.util.List<Rule>> getRules()
    Returns the rules contained in this grammar.
    java.util.List<Rule> getRules​(java.lang.String symbol)
    Gets all rules used to produce the specified construct (i.e., rules whose head is the specified symbol)
    java.util.List<Rule> getRules​(ConstructSymbol symbol)
    Gets all rules used to produce the specified construct (i.e., rules whose head is the specified symbol)
    TokenSymbol getTokenSymbol​(java.lang.String name)
    Gets the TokenSymbol associated to the given token name, if any.
    java.util.Collection<TokenSymbol> getTokenSymbols()
    Gets all the token symbols that are specified in this grammar.
    java.util.Iterator<Rule> iterator()  
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Method Details

    • getIgnoredPatterns

      public java.util.Set<java.util.regex.Pattern> getIgnoredPatterns()
      Returns the list of patterns to ignore. The ignored patterns usually are spaces and comment patterns (e.g. '//...\n' or '#...\n')
      Returns:
      a list of patterns
    • getRules

      public java.util.Map<ConstructSymbol,​java.util.List<Rule>> getRules()
      Returns the rules contained in this grammar.
      Returns:
      A map that associates a ConstructSymbol to the rules contained in this grammar that produce it.
    • getRules

      public java.util.List<Rule> getRules​(java.lang.String symbol)
      Gets all rules used to produce the specified construct (i.e., rules whose head is the specified symbol)
      Parameters:
      symbol - the head of the rules
      Returns:
      the list of rules that produce the specified construct
      See Also:
      getRules(ConstructSymbol)
    • getRules

      public java.util.List<Rule> getRules​(ConstructSymbol symbol)
      Gets all rules used to produce the specified construct (i.e., rules whose head is the specified symbol)
      Parameters:
      symbol - the head of the rules
      Returns:
      the list of rules that produce the specified construct
      See Also:
      getRules(String)
    • getTokenSymbols

      public java.util.Collection<TokenSymbol> getTokenSymbols()
      Gets all the token symbols that are specified in this grammar.
      Returns:
      an unmodifiable collection of TokenSymbol
    • getConstructSymbols

      public java.util.Collection<ConstructSymbol> getConstructSymbols()
      Gets all the construct symbols that are specified in this grammar.
      Returns:
      an unmodifiable collection of ConstructSymbol
    • getTokenSymbol

      public TokenSymbol getTokenSymbol​(java.lang.String name)
      Gets the TokenSymbol associated to the given token name, if any.
      Parameters:
      name - the name of the token
      Returns:
      a TokenSymbol object
    • getConstructSymbol

      public ConstructSymbol getConstructSymbol​(java.lang.String name)
      Gets the ConstructSymbol associated to the given construct name, if any.
      Parameters:
      name - the name of the construct
      Returns:
      a ConstructSymbol object
    • getAxiom

      public ConstructSymbol getAxiom()
      Gets the axiom of the grammar (i.e., the first symbol from which a parser should start to interpret the grammar)
      Returns:
      a ConstructSymbol object representing the axiom
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • create

      public static Grammar.Builder create()
      Creates a new Grammar.Builder that allows the construction of a grammar, rule by rule.
      Returns:
      a Grammar.Builder instance
    • iterator

      public java.util.Iterator<Rule> iterator()
      Specified by:
      iterator in interface java.lang.Iterable<Rule>