Package jjparse
Class Parsing<I>
java.lang.Object
jjparse.Parsing<I>
- Type Parameters:
I- The input type or, more specifically, the type of a single element in the underlying input stream.
- Direct Known Subclasses:
StringParsing
An abstract class that provides all basic parsing capabilities.
In order to implement a parser, one must extend this class and specify the corresponding input type. The subclass then has access to the
In order to implement a parser, one must extend this class and specify the corresponding input type. The subclass then has access to the
Parsing.Parser class and the Parsing.Result class, which are specialized for
the respective input type.- Author:
- Björn Lötters
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classRepresents a fatalParsing.Failure.final classRepresents a recoverableParsing.Failure.classRepresents aParsing.Resultwhich indicates a parse failure.classThe abstract base class of aParsing.Parser.classRepresents the result of a parsing operation.final classRepresents a successful result of a parsing operation. -
Constructor Summary
ConstructorsConstructorDescriptionParsing()Creates an instance of thisParsingclass and provides access to allParsing.Parsers andParsing.Parsercombinators in that way. -
Method Summary
Modifier and TypeMethodDescriptionConstructs aParsing.Parserthat always fails with anParsing.Abort.Constructs aParsing.Parserthat attempts to parse the providedParsing.Parsers in the order they are given until one of them succeeds or aborts.Constructs aParsing.Parserthat always fails with anParsing.Error.Constructs a newParsing.Parserwhich is initialized lazy.Lifts the providedFunctioninto aParsing.Parser.Attempts to parse the wholeInputwith the providedParsing.Parser.Constructs a newParsing.Parserthat behaves just like the given one, applying the currentInput.Positionto theFunctionreturned by the providedParsing.Parser.Constructs a newParsing.Parserthat applies the providedParsing.Parsers in sequence.final <T> voidsetSkipParser(Parsing<I>.Parser<? extends T> parser) Skips the prefix of the providedInputusing the current skipParsing.Parser.Constructs aParsing.Parserthat always succeeds with aParsing.Success.
-
Constructor Details
-
Parsing
public Parsing()Creates an instance of thisParsingclass and provides access to allParsing.Parsers andParsing.Parsercombinators in that way.
-
-
Method Details
-
parse
public final <T> Parsing<I>.Result<T> parse(Parsing<I>.Parser<? extends T> parser, Input<? extends I> input) Attempts to parse the wholeInputwith the providedParsing.Parser. This method fails, ifInputthat cannot be skipped remains after applying the providedParsing.Parser.- Type Parameters:
T- The type of theParsing.Result.- Parameters:
parser- TheParsing.Parserthat should be applied to theInput.input- TheInputthat should be parsed using the providedParsing.Parser.- Returns:
- The
Parsing.Resultafter applying the providedParsing.Parserto theInput. - See Also:
-
skip
Skips the prefix of the providedInputusing the current skipParsing.Parser. This method does not fail, even if the skipParsing.Parserwas not successful. -
setSkipParser
public final <T> void setSkipParser(Parsing<I>.Parser<? extends T> parser) throws NullPointerException Sets theParsing.Parserthat is used for skippingInputbefore anotherParsing.Parseris applied. By default, no skipParsing.Parseris set and noInputis skipped.- Type Parameters:
T- The result type of the providedParsing.Parser, which is voided internally.- Parameters:
parser- The newParsing.Parserthat should be used for skipping. ThisParsing.Parsermay fail, in which case noInputis skipped.- Throws:
NullPointerException- If the providedParsing.Parserisnull.- See Also:
-
lift
public final <T> Parsing<I>.Parser<T> lift(Function<? super Input<I>, ? extends Parsing<I>.Result<T>> function) Lifts the providedFunctioninto aParsing.Parser. This allows us to use Java's lambda notation, despite thatParsing.Parsermust be an abstract class.- Type Parameters:
T- The type of theParsing.Result.- Parameters:
function- TheFunctionthat parses anInputand returns aParsing.Result.- Returns:
- A
Parsing.Parserwhich behaves just like the providedFunction. - See Also:
-
error
Constructs aParsing.Parserthat always fails with anParsing.Error.- Type Parameters:
T- The mandatory type of theParsing.Result.- Parameters:
message- The message of theParsing.Error.- Returns:
- A
Parsing.Parserthat always fails with anParsing.Error.
-
abort
Constructs aParsing.Parserthat always fails with anParsing.Abort.- Type Parameters:
T- The mandatory type of theParsing.Result.- Parameters:
message- The message of theParsing.Abort.- Returns:
- A
Parsing.Parserthat always fails with anParsing.Abort.
-
success
Constructs aParsing.Parserthat always succeeds with aParsing.Success.- Type Parameters:
T- The type of the value that should be returned byParsing.Parser.- Parameters:
supplier- ASupplierfor the value that should be returned by theParsing.Parser.- Returns:
- A
Parsing.Parserthat always succeeds with aParsing.Success.
-
choice
@SafeVarargs public final <T> Parsing<I>.Parser<T> choice(Parsing<I>.Parser<? extends T>... parsers) Constructs aParsing.Parserthat attempts to parse the providedParsing.Parsers in the order they are given until one of them succeeds or aborts. The returnedParsing.Parserfails if allParsing.Parsers fail.- Type Parameters:
T- The type of theParsing.Result.- Parameters:
parsers- TheParsing.Parsers that should be used for the newParsing.Parser.- Returns:
- A
Parsing.Parserwhich attempts to parse the providedParsing.Parsers in the order they are given.
-
sequence
@SafeVarargs public final <T> Parsing<I>.Parser<List<T>> sequence(Parsing<I>.Parser<? extends T>... parsers) Constructs a newParsing.Parserthat applies the providedParsing.Parsers in sequence. It fails when one of theParsing.Parsers fails and succeeds only when allParsing.Parsers succeed. In this case, theParsing.Results of allParsing.Parsers are collected in a single, immutableList.- Type Parameters:
T- The type of a singleParsing.Result.- Parameters:
parsers- TheParsing.Parsers that should be used for the newParsing.Parser.- Returns:
- A
Parsing.Parserwhich attempts to parse the providedParsing.Parsers in sequence.
-
position
public final <T> Parsing<I>.Parser<T> position(Parsing<I>.Parser<Function<Input<I>.Position, T>> parser) Constructs a newParsing.Parserthat behaves just like the given one, applying the currentInput.Positionto theFunctionreturned by the providedParsing.Parser.- Type Parameters:
T- The type of theParsing.Result.- Parameters:
parser- AParsing.Parserreturning aFunctionwhich accepts the currentInput.Position.- Returns:
- A new
Parsing.Parserthat provides the given one with aInput.Position.
-
lazy
Constructs a newParsing.Parserwhich is initialized lazy. That is to say, when thisParsing.Parseris applied for the first time, the providedSupplieris called and the returnedParsing.Parseris used for parsing. Moreover, thisParsing.Parseris memorized for future applications. This method is especially useful since Java does not provide us with lazy declarations, which are required for mutually recursive parsers (as they occur very often in practice).- Type Parameters:
T- The type of theParsing.Result.- Parameters:
supplier- TheSupplierthat provides the actualParsing.Parser.- Returns:
- A
Parsing.Parserthat lazily evaluates the providedSupplierand behaves just like theParsing.Parserprovided by theSupplier.
-