Package jjparse
Class Parsing.Parser<T>
java.lang.Object
jjparse.Parsing.Parser<T>
- Type Parameters:
T- The covariant type of the value that is the result of running thisParsing.Parser.
public abstract class Parsing.Parser<T>
extends Object
implements Function<Input<I>,Parsing<I>.Result<T>>
The abstract base class of a
Very similar to recursive descent parsing, a
Note: This class is intended to be immutable and hence covariant in its type parameter.
Implementation Note: Unfortunately, Java does not support instance interfaces which is why this class is not a
Parsing.Parser.
Very similar to recursive descent parsing, a
Parsing.Parser is just a Function that takes an Input
and produces a Parsing.Result. In order to implement a Parsing.Parser it therefore suffices to implement the
apply(jjparse.input.Input<I>) method.
Note: This class is intended to be immutable and hence covariant in its type parameter.
Implementation Note: Unfortunately, Java does not support instance interfaces which is why this class is not a
FunctionalInterface and we cannot use the lambda syntax to implement a Parsing.Parser.- Author:
- Björn Lötters
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConcatenates this and the providedParsing.Parserand returns aProductof the corresponding values onParsing.Success.Concatenates this and the providedParsing.Parserbut returns only the value parsed by thisParsing.Parser.Concatenates this and the providedParsing.Parserbut returns only the value parsed by the provided one.Creates aParsing.Parserwhich parses thisParsing.Parserin between the two providedParsing.Parsers, only returning the value of thisParsing.Parser.Just likeseparate(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser.Just likeseparate1(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser.Just likeseparate(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser.Just likeseparate1(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser.commit()Creates aParsing.Parserwhich behaves like thisParsing.Parserexcept that the createdParsing.Parseraborts with anParsing.Abortwhen thisParsing.Parserfails with anParsing.Error.This method shall return aDescriptionfor thisParsing.Parserwhich provides details about the shape of theInputthisParsing.Parserexpects.Creates aParsing.Parserwhich prints additional debug output to the standard output stream.not()Creates aParsing.Parserwhich attempts to not parse thisParsing.Parser.optional()Creates aParsing.Parserwhich attempts to parse thisParsing.Parserand only fails if thisParsing.Parseraborts with anParsing.Abort.Creates aParsing.Parserwhich first attempts to parse thisParsing.Parserand, if thisParsing.Parserfails with anParsing.Error, attempts to parse the exact sameInputwith the providedParsing.Parser.repeat()Creates aParsing.Parserwhich applies thisParsing.Parserarbitrarily many, including zero, times.repeat1()Creates aParsing.Parserwhich applies thisParsing.Parserarbitrarily many times but at least one time.Creates aParsing.Parserwhich parses thisParsing.Parser(possibly zero times) interleaved with the provided separatorParsing.Parser, only returning the values returned by thisParsing.Parser.Creates aParsing.Parserwhich parses thisParsing.Parser(at least one time) interleaved with the provided separatorParsing.Parser, only returning the values returned by thisParsing.Parser.
-
Constructor Details
-
Parser
public Parser()
-
-
Method Details
-
apply
-
description
This method shall return aDescriptionfor thisParsing.Parserwhich provides details about the shape of theInputthisParsing.Parserexpects. By default, this method returns anEmptyDescriptionand must hence be overwritten if required.- Returns:
- A
Descriptionof thisParsing.Parser. - See Also:
-
map
- Type Parameters:
U- The type of the transformedParsing.Parser.- Parameters:
function- TheFunctionwhich shall be applied to theParsing.Results of this parser.- Returns:
- A
Parsing.Parserthat parses the exact same language as this one, but with a transformedParsing.Result. - See Also:
-
flatMap
Maps thisParsing.Parserby applying the providedFunctionto theParsing.Results it produces. On success, theParsing.Parserreturned by thisFunctionis immediately applied to the rest of the input. In this way, this method also behaves like a concatenation of thisParsing.Parserand the returned one.- Type Parameters:
U- The type of the transformedParsing.Parser.- Parameters:
function- TheFunctionwhich shall be applied to theParsing.Results of this parser.- Returns:
- A
Parsing.Parserthat first applies this and then the one returned by the providedFunction. - See Also:
-
and
Concatenates this and the providedParsing.Parserand returns aProductof the corresponding values onParsing.Success.- Type Parameters:
U- The type of the secondParsing.Parser'sParsing.Result.- Parameters:
right- The secondParsing.Parserwhich shall be appended to thisParsing.Parser.- Returns:
- A
Parsing.Parserwhich first parses this and then the provided one, returning theProductof their returned values onParsing.Success. - See Also:
-
andl
Concatenates this and the providedParsing.Parserbut returns only the value parsed by thisParsing.Parser.- Type Parameters:
U- The type of the secondParsing.Parser'sParsing.Result.- Parameters:
right- The secondParsing.Parserwhich shall be appended to thisParsing.Parser.- Returns:
- A
Parsing.Parserwhich first parses this and then the provided one, returning the value of thisParsing.Parseronly. - See Also:
-
andr
Concatenates this and the providedParsing.Parserbut returns only the value parsed by the provided one.- Type Parameters:
U- The type of the secondParsing.Parser'sParsing.Result.- Parameters:
right- The secondParsing.Parserwhich shall be appended to thisParsing.Parser.- Returns:
- A
Parsing.Parserwhich first parses this and then the provided one, returning the value of the providedParsing.Parseronly. - See Also:
-
or
Creates aParsing.Parserwhich first attempts to parse thisParsing.Parserand, if thisParsing.Parserfails with anParsing.Error, attempts to parse the exact sameInputwith the providedParsing.Parser.- Parameters:
parser- The alternativeParsing.Parser.- Returns:
- A
Parsing.Parserwhich first attempts to parse theInputwith this and only then with the providedParsing.Parser. - See Also:
-
optional
Creates aParsing.Parserwhich attempts to parse thisParsing.Parserand only fails if thisParsing.Parseraborts with anParsing.Abort.- Returns:
- A
Parsing.Parserwhich optionally parses thisParsing.Parser. - See Also:
-
repeat
Creates aParsing.Parserwhich applies thisParsing.Parserarbitrarily many, including zero, times.- Returns:
- A
Parsing.Parserwhich repeats thisParsing.Parserarbitrarily many, including zero, times. - See Also:
-
repeat1
Creates aParsing.Parserwhich applies thisParsing.Parserarbitrarily many times but at least one time.- Returns:
- A
Parsing.Parserwhich repeats thisParsing.Parserarbitrarily many times but at least one time. - See Also:
-
separate1
Creates aParsing.Parserwhich parses thisParsing.Parser(at least one time) interleaved with the provided separatorParsing.Parser, only returning the values returned by thisParsing.Parser. This is especially useful forInputs like comma separated values.- Parameters:
separator- The separatorParsing.Parser.- Returns:
- A
Parsing.Parserwhich parses thisParsing.Parserinterleaved with the provided separatorParsing.Parser. - See Also:
-
separate
Creates aParsing.Parserwhich parses thisParsing.Parser(possibly zero times) interleaved with the provided separatorParsing.Parser, only returning the values returned by thisParsing.Parser. This is especially useful forInputs like comma separated values.- Parameters:
separator- The separatorParsing.Parser.- Returns:
- A
Parsing.Parserwhich parses thisParsing.Parserinterleaved with the provided separatorParsing.Parser. - See Also:
-
between
Creates aParsing.Parserwhich parses thisParsing.Parserin between the two providedParsing.Parsers, only returning the value of thisParsing.Parser.- Parameters:
left- The left of the two enclosingParsing.Parsers.right- The right of the two enclosingParsing.Parsers.- Returns:
- A
Parsing.Parserwhich parses thisParsing.Parserin between the two providedParsing.Parsers. - See Also:
-
commit
Creates aParsing.Parserwhich behaves like thisParsing.Parserexcept that the createdParsing.Parseraborts with anParsing.Abortwhen thisParsing.Parserfails with anParsing.Error. The effect of this is that backtracking does not extend over the createdParsing.Parser. This is especially useful for theParsing.Parserright after a keywordParsing.Parser, to prevent backtracking over the keyword.- Returns:
- A
Parsing.Parserwhich behaves like thisParsing.Parser, but with backtracking disabled. - See Also:
-
not
Creates aParsing.Parserwhich attempts to not parse thisParsing.Parser. That is to say, everyParsing.Failureof thisParsing.Parseris transformed into aParsing.Successand vice versa. The createdParsing.Parsernever consumes anyInput.- Returns:
- A
Parsing.Parserwhich ensures that thisParsing.Parseris not applicable. - See Also:
-
log
Creates aParsing.Parserwhich prints additional debug output to the standard output stream.- Parameters:
name- A human-readable name for thisParsing.Parser.- Returns:
- A
Parsing.Parserwhich behaves like thisParsing.Parserbut prints additional debug output.
-
chainl1
Just likeseparate1(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser. However, instead of returning aListof the parsed elements, the elements are folded from left to right into a single element using theBiFunctions returned by the separatorParsing.Parser. This is especially useful for parsing left-associative chains of operators such as1 + 2 + 3.- Parameters:
separator- The separatorParsing.Parserwhich must return aBiFunctionthat combines any two elements.- Returns:
- A
Parsing.Parserwhich behaves similar toseparate1(jjparse.Parsing.Parser<?>)but combines all elements into a single one. - See Also:
-
chainl
public final Parsing<I>.Parser<T> chainl(Parsing<I>.Parser<BiFunction<T, T, T>> separator, T otherwise) Just likeseparate(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser. However, instead of returning aListof the parsed elements, the elements are folded from left to right into a single element using theBiFunctions returned by the separatorParsing.Parser. This is especially useful for parsing left-associative chains of operators such as1 + 2 + 3.- Parameters:
separator- The separatorParsing.Parserwhich must return aBiFunctionthat combines any two elements.otherwise- The element that should be returned when there is no single element in the chain.- Returns:
- A
Parsing.Parserwhich behaves similar toseparate(jjparse.Parsing.Parser<?>)but combines all elements into a single one. - See Also:
-
chainr1
Just likeseparate1(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser. However, instead of returning aListof the parsed elements, the elements are folded from right to left into a single element using theBiFunctions returned by the separatorParsing.Parser. This is especially useful for parsing right-associative chains of operators such asa = b = c.- Parameters:
separator- The separatorParsing.Parserwhich must return aBiFunctionthat combines any two elements.- Returns:
- A
Parsing.Parserwhich behaves similar toseparate1(jjparse.Parsing.Parser<?>)but combines all elements into a single one. - See Also:
-
chainr
public final Parsing<I>.Parser<T> chainr(Parsing<I>.Parser<BiFunction<T, T, T>> separator, T otherwise) Just likeseparate(jjparse.Parsing.Parser<?>), this method separates thisParsing.Parserusing the separatorParsing.Parser. However, instead of returning aListof the parsed elements, the elements are folded from right to left into a single element using theBiFunctions returned by the separatorParsing.Parser. This is especially useful for parsing right-associative chains of operators such asa = b = c.- Parameters:
separator- The separatorParsing.Parserwhich must return aBiFunctionthat combines any two elements.otherwise- The element that should be returned when there is no single element in the chain.- Returns:
- A
Parsing.Parserwhich behaves similar toseparate(jjparse.Parsing.Parser<?>)but combines all elements into a single one. - See Also:
-