Object-oriented Parser System
v3.6

oops3
Class ParserFactory

Object
  extended by ParserFactory

public class ParserFactory
extends Object

factory to construct a Parser.

Version:
3.5.0

Field Summary
protected  boolean error
          if set, parser() will return null.
protected  Map<String,Parser.Literal> literals
          access to Parser.literals.
protected  ArrayList<BitSet> lookaheads
          mutable version of Parser.lookaheads.
protected  int nextSymbol
          next unique, positive number to represent a Terminal.
protected  Map<String,Integer> nonterminals
          maps Nonterminal name to index in rules.
protected  Parser parser
          parser to be delivered, context for all creations.
protected  ArrayList<Parser.Pattern> patterns
          mutable version of Parser.patterns.
protected  ArrayList<Parser.Rule> rules
          mutable version of Parser.rules.
protected  Map<String,Integer> tokens
          maps Token name to index in patterns.
 
Constructor Summary
ParserFactory()
           
 
Method Summary
 Parser.Node and(int position, Parser.Node... cases)
          factory method.
 Parser.Node andList(int position, Parser.Node delimiter, Parser.Node... cases)
          factory method.
 Parser.Delimit delimit(int position, int min, int max, Parser.Node body, Parser.Node delimiter)
          factory method.
 Parser.Delimit delimitMany(int position, Parser.Node body, Parser.Node delimiter)
          convenience method: delimit zero or more times.
 Parser.Delimit delimitSome(int position, Parser.Node body, Parser.Node delimiter)
          convenience method: delimit zero or more times.
protected  void error(int position, String message)
          should be overwritten to redirect all messages.
protected  Parser init(Parser parser, String prolog, String epilog)
          transfers into Parser.patterns, Parser.rules, and Parser.lookaheads.
 Parser.Literal literal(int position, String value)
          factory method: maintains nextSymbol, literals, lookaheads.
 Parser.Repeat many(int position, Parser.Node... body)
          convenience method: repeat zero or more times.
 Parser.Nonterminal nonterminal(int position, String name)
          factory method.
 Parser.Repeat optional(int position, Parser.Node... body)
          convenience method: repeat at most once.
 Parser.Node or(int position, Parser.Node... cases)
          factory method.
 Parser.Node orList(int position, Parser.Node delimiter, Parser.Node... cases)
          factory method.
 Parser parser(String prolog, String epilog)
          factory method: initializes and returns parser or null if there was an error.
 Parser.Pattern pattern(int position, String name, Parser.Type type, String pattern)
          factory method: maintains nextSymbol, tokens, patterns, lookaheads.
protected  String position(int position)
          returns string with nonzero position, if any.
 Parser.Repeat repeat(int position, int min, int max, Parser.Node... body)
          factory method.
 Parser.Rule rule(int position, String name, Parser.Type type, Parser.Node body)
          factory method: maintains nonterminals, rules, and Parser.start.
 Parser.Sequence sequence(int position, Parser.Node... body)
          factory method.
 Parser.Repeat some(int position, Parser.Node... body)
          convenience method: repeat at least once.
 Parser.Token token(int position, String name)
          factory method: searches tokens.
 Parser.Type type(String name, boolean flatten, String... interfaces)
          factory method.
 Parser.Node xor(int position, Parser.Node... cases)
          factory method.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

error

protected boolean error
if set, parser() will return null.

See Also:
error(int,java.lang.String)

nextSymbol

protected int nextSymbol
next unique, positive number to represent a Terminal.

See Also:
literal(int, java.lang.String), pattern(int, java.lang.String, oops3.Parser.Type, java.lang.String)

parser

protected final Parser parser
parser to be delivered, context for all creations.


literals

protected final Map<String,Parser.Literal> literals
access to Parser.literals.

See Also:
literal(int, java.lang.String)

tokens

protected final Map<String,Integer> tokens
maps Token name to index in patterns.

See Also:
pattern(int, java.lang.String, oops3.Parser.Type, java.lang.String), token(int, java.lang.String), rule(int, java.lang.String, oops3.Parser.Type, oops3.Parser.Node)

patterns

protected final ArrayList<Parser.Pattern> patterns
mutable version of Parser.patterns.

See Also:
pattern(int, java.lang.String, oops3.Parser.Type, java.lang.String)

nonterminals

protected final Map<String,Integer> nonterminals
maps Nonterminal name to index in rules.

See Also:
pattern(int, java.lang.String, oops3.Parser.Type, java.lang.String), rule(int, java.lang.String, oops3.Parser.Type, oops3.Parser.Node)

rules

protected final ArrayList<Parser.Rule> rules
mutable version of Parser.rules.

See Also:
rule(int, java.lang.String, oops3.Parser.Type, oops3.Parser.Node), nonterminal(int, java.lang.String)

lookaheads

protected final ArrayList<BitSet> lookaheads
mutable version of Parser.lookaheads. This is filled with null as placeholders and singletons for Terminal.

See Also:
literal(int, java.lang.String), pattern(int, java.lang.String, oops3.Parser.Type, java.lang.String)
Constructor Detail

ParserFactory

public ParserFactory()
Method Detail

error

protected void error(int position,
                     String message)
should be overwritten to redirect all messages. Sets error.


parser

public Parser parser(String prolog,
                     String epilog)
factory method: initializes and returns parser or null if there was an error.

See Also:
init(Parser,java.lang.String,java.lang.String)

init

protected Parser init(Parser parser,
                      String prolog,
                      String epilog)
transfers into Parser.patterns, Parser.rules, and Parser.lookaheads. Complains about undefined nonterminals, i.e., rules with null body.


literal

public Parser.Literal literal(int position,
                              String value)
factory method: maintains nextSymbol, literals, lookaheads.


pattern

public Parser.Pattern pattern(int position,
                              String name,
                              Parser.Type type,
                              String pattern)
factory method: maintains nextSymbol, tokens, patterns, lookaheads. Complains about duplicate definitions in both, tokens and nonterminals.


token

public Parser.Token token(int position,
                          String name)
factory method: searches tokens. Error if untyped pattern.

Throws:
IllegalArgumentException - if missing definition.

rule

public Parser.Rule rule(int position,
                        String name,
                        Parser.Type type,
                        Parser.Node body)
factory method: maintains nonterminals, rules, and Parser.start. Complains about duplicate definitions in both, tokens and nonterminals.

Parameters:
type - cannot be void but can be null -- eventually collected as ArrayList.
body - can be null -- forward declaration, type is ignored.

type

public Parser.Type type(String name,
                        boolean flatten,
                        String... interfaces)
factory method.


nonterminal

public Parser.Nonterminal nonterminal(int position,
                                      String name)
factory method.


repeat

public Parser.Repeat repeat(int position,
                            int min,
                            int max,
                            Parser.Node... body)
factory method.

Throws:
IllegalArgumentException - if max < min or max is zero or body is null or empty.

many

public Parser.Repeat many(int position,
                          Parser.Node... body)
convenience method: repeat zero or more times.


optional

public Parser.Repeat optional(int position,
                              Parser.Node... body)
convenience method: repeat at most once.


some

public Parser.Repeat some(int position,
                          Parser.Node... body)
convenience method: repeat at least once.


delimit

public Parser.Delimit delimit(int position,
                              int min,
                              int max,
                              Parser.Node body,
                              Parser.Node delimiter)
factory method.

Throws:
IllegalArgumentException - if max < min or max < 2 or body or delimiter are null.

delimitMany

public Parser.Delimit delimitMany(int position,
                                  Parser.Node body,
                                  Parser.Node delimiter)
convenience method: delimit zero or more times.


delimitSome

public Parser.Delimit delimitSome(int position,
                                  Parser.Node body,
                                  Parser.Node delimiter)
convenience method: delimit zero or more times.


and

public Parser.Node and(int position,
                       Parser.Node... cases)
factory method.

Returns:
body if it is a singleton.
Throws:
IllegalArgumentException - if body is null or empty.

or

public Parser.Node or(int position,
                      Parser.Node... cases)
factory method.

Returns:
body if it is a singleton.
Throws:
IllegalArgumentException - if body is null or empty.

andList

public Parser.Node andList(int position,
                           Parser.Node delimiter,
                           Parser.Node... cases)
factory method.

Throws:
IllegalArgumentException - if body is null or has fewer then two cases, or if delimiter is null.

orList

public Parser.Node orList(int position,
                          Parser.Node delimiter,
                          Parser.Node... cases)
factory method.

Throws:
IllegalArgumentException - if body is null or has fewer then two cases, or if delimiter is null.

xor

public Parser.Node xor(int position,
                       Parser.Node... cases)
factory method.

Returns:
body if it is a singleton.
Throws:
IllegalArgumentException - if body is null or empty.

sequence

public Parser.Sequence sequence(int position,
                                Parser.Node... body)
factory method.

Returns:
Sequence even if body is a singleton.
Throws:
IllegalArgumentException - if body is null or empty.

position

protected String position(int position)
returns string with nonzero position, if any.



(c) 2008 Axel T. Schreiner