public abstract class

Parser

extends Object
java.lang.Object
   ↳ com.google.gdata.util.parser.Parser<T>
Known Direct Subclasses

Class Overview

The com.google.gdata.util.parser package provides a framework for creating recursive descent parsers. A fairly straightforward transformation exists between EBNF (extended Backus-Naur form) grammars and code used to construct a parser using this framework that will match the grammar. The intention of this package is to obviate the need to create small mini-parsers for tasks that don't feel large enough for a standard compiler-compiler (like JavaCC) but still need more formalism than simple string tokenization. The basic approach this framework takes to parsing is to define several types of leaf parsers which know how to parse a particular type of object (character set, string literal, etc.) and to then combine them together in interesting ways. For example, a parser for a comma separated list of integers would look like: Parser Chset.DIGIT.plus().list(new Chset(',')); The EBNF this represents is: sent: [0-9]+ ("," [0-9]+)* The Parser.list() functionality is sometimes represented as the '%' operator in EBNF extensions. It performs the transformation: a % b --> a (b a)* The leaf parsers that are currently defined are:

Summary

Constants
int NO_MATCH
Public Constructors
Parser()
Public Methods
final <U extends T> Parser<U> action(Callback<U> callback)
Creates a Action that will fire and call Callback.handle whenever this matches.
static <T> Parser<T> alternative(Parser<? super T> left, Parser<? super T> right)
Creates an Alternative parser from the left and right sub-parsers.
static <T> Parser<T> difference(Parser<? super T> left, Parser<? super T> right)
Creates a Difference parser from the left and right sub-parsers.
static <T> Parser<T> intersection(Parser<? super T> left, Parser<? super T> right)
Creates an Intersection parser from the left and right sub-parsers.
final Parser<T> list(Parser<? super T> sep)
Creates a Parser that matches a sequence of this parsers separated by sep parsers.
final Parser<T> optional()
Creates a Repeat parser that matches this either 0 or 1 times.
final int parse(String str, T udata)
Convenience routine to parse a string.
abstract int parse(char[] buf, int start, int end, T udata)
The parse interface that subclasses must implement.
final int parse(Reader reader, T udata)
Convenience routine to parse a java.io.Reader.
final int parse(char[] buf, T udata)
Convenience routine to parse a character array.
final Parser<T> plus()
Creates a Repeat parser that matches this 1 or more times.
final Parser<T> repeat(int min, int max)
Creates a Repeat parser that matches this at least min times and not mroe than max times.
final Parser<T> repeat(int count)
Creates a Repeat parser that matches this exactly count times.
static <T> Parser<T> sequence(Parser<? super T> one, Parser<? super T> two, Parser<? super T> three, Parser<? super T> four)
Creates a sequence of four parsers.
static <T> Parser<T> sequence(Parser<? super T> one, Parser<? super T> two, Parser<? super T> three, Parser<? super T> four, Parser<? super T> five)
Creates a sequence of five parsers.
static <T> Parser<T> sequence(Parser<? super T> one, Parser<? super T> two, Parser<? super T> three)
Creates a Sequence parser from parsers one, two and three sub-parsers.
static <T> Parser<T> sequence(Parser<? super T> left, Parser<? super T> right)
Creates a Sequence parser from the left and right sub-parsers.
final Parser<T> star()
Creates a Repeat parser that matches this 0 or more times.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int NO_MATCH

Constant Value: -1 (0xffffffff)

Public Constructors

public Parser ()

Public Methods

public final Parser<U> action (Callback<U> callback)

Creates a Action that will fire and call Callback.handle whenever this matches.

Parameters
callback The Callback to call when this matches.

public static Parser<T> alternative (Parser<? super T> left, Parser<? super T> right)

Creates an Alternative parser from the left and right sub-parsers. left | right

Parameters
left
right

public static Parser<T> difference (Parser<? super T> left, Parser<? super T> right)

Creates a Difference parser from the left and right sub-parsers. left - right

Parameters
left
right

public static Parser<T> intersection (Parser<? super T> left, Parser<? super T> right)

Creates an Intersection parser from the left and right sub-parsers. left & right

Parameters
left
right

public final Parser<T> list (Parser<? super T> sep)

Creates a Parser that matches a sequence of this parsers separated by sep parsers. These sequences occur often: space separated words, comma separated words, etc.

Parameters
sep The parser which separates instances of this.

public final Parser<T> optional ()

Creates a Repeat parser that matches this either 0 or 1 times. this?

public final int parse (String str, T udata)

Convenience routine to parse a string.

Parameters
str
udata

public abstract int parse (char[] buf, int start, int end, T udata)

The parse interface that subclasses must implement.

Parameters
buf The character array to match against.
start The start offset of data within the character array to match against.
end The end offset of data within the character array to match against.
udata User defined object that is passed to Callback.handle when an Action fires.

public final int parse (Reader reader, T udata)

Convenience routine to parse a java.io.Reader.

Parameters
reader
udata

public final int parse (char[] buf, T udata)

Convenience routine to parse a character array.

Parameters
buf
udata

public final Parser<T> plus ()

Creates a Repeat parser that matches this 1 or more times. this+

public final Parser<T> repeat (int min, int max)

Creates a Repeat parser that matches this at least min times and not mroe than max times. this{min,max}

Parameters
min The minimum number of times this must match in sequence.
max The maximum number of times this is allowed to match in sequence.

public final Parser<T> repeat (int count)

Creates a Repeat parser that matches this exactly count times. this{count}

Parameters
count The number of times this must match in sequence.

public static Parser<T> sequence (Parser<? super T> one, Parser<? super T> two, Parser<? super T> three, Parser<? super T> four)

Creates a sequence of four parsers.

Parameters
one
two
three
four

public static Parser<T> sequence (Parser<? super T> one, Parser<? super T> two, Parser<? super T> three, Parser<? super T> four, Parser<? super T> five)

Creates a sequence of five parsers.

Parameters
one
two
three
four
five

public static Parser<T> sequence (Parser<? super T> one, Parser<? super T> two, Parser<? super T> three)

Creates a Sequence parser from parsers one, two and three sub-parsers. Equivalent to calling Parser.sequence(one, Parser.sequence(two, three)).

Parameters
one
two
three

public static Parser<T> sequence (Parser<? super T> left, Parser<? super T> right)

Creates a Sequence parser from the left and right sub-parsers. left right

Parameters
left
right

public final Parser<T> star ()

Creates a Repeat parser that matches this 0 or more times. this*