Class Parser<D,T>
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type
- All Implemented Interfaces:
Action.Continuation<Input<D,,T>, Output<D, T>, SimpleMessage<D>, Input<D, T>> Function<Input<D,T>, Action<Output<D, T>, SimpleMessage<D>, Input<D, T>>>
Every parser building block is essentially a transition step, a function
taking Input to Output, with computational behavior specified
by Action and diagnostic messages of type SimpleMessage.
Subclasses must implement this behavior by overriding the abstract method
Action<Output<D, T>, SimpleMessage<D>, Input<D, T>>
apply(Input<D, T>, Success<?, SimpleMessage<D>, Input<D, T>>).
Configurational and Operational Methods
The methods of this class are divided into a configurational and an operational phase:
- Configurational methods create new parser objects, often by combining existing ones, or query invariant properties. They make invoke other configurational methods on parser objects, but no operational methods. They must not change the observable state of parser objects.
- Operational methods give access to the parsing behavior. They should not to change the observable state of parser objects.
Operational methods are explicitly marked in this documentation; all others are configurational.
Behavioral Equivalence
The results of configurational methods are typically specified only in terms of equivalent behavior. Two parsers are behaviorally equivalent if replacing one by the other in the following procedure does not change resulting search trees.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration of local pragmatic modifiers of parsing behavior. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFollows the behavior of this parser sequentially with another.andThen(Parser<D, T> other, Parser.Pragma... pragmas) Follows the behavior of this parser sequentially with another.andThenCombining(Parser<D, T> other, Action.BiContinuation<Data, Data, Data, SimpleMessage<D>, State> combiner, Parser.Pragma... pragmas) Follows the behavior of this parser sequentially with another, combining the result values.apply(Input<D, T> in, Action.Success<?, SimpleMessage<D>, Input<D, T>> recyclable) Applies this continuation, possibly using a recyclable leaf node.Adds the assignment of the result value to a semantic variable to the behavior of this parser.static <D,T> Parser <D, T> Deprecated.static <D,T> Parser <D, T> Deprecated.UseParserFactory.choice(List)instead.closeAndRepeat(Parser.Pragma... pragmas) Returns a parser that behaves like this parser if consumed input is followed by more input of the same syntax.closeAndTerminate(T endType, Parser.Pragma... pragmas) Returns a parser that behaves like this parser if consumed input is followed by the specified token type.Adds the collecting of the result value in a sequence to the behavior of this parser.static <D,T> Parser <D, T> Deprecated.UseParserFactory.constant(Object)instead.diagnose(SimpleMessage<D>... msgs) Add diagnostic messages to the behavior of this parser.Add diagnostic messages to the behavior of this parser.static <D,T> Parser <D, T> Deprecated.UseParserFactory.startInterval()instead.static <D,T> Parser <D, T> fail()Deprecated.UseParserFactory.fail()instead.static <D,T> Parser <D, T> fix(UnaryOperator<Parser<D, T>> loop, Parser.Pragma... pragmas) Deprecated.UseParserFactory.fix(UnaryOperator, Pragma[])instead.final ParserFactoryReturns the factory responsible for creating this parser.@Opt LocationTree<String, Parser<?, ?>> Returns the location tree for this parser if available.Returns the predictor for this parser.Returns a parser that extends the behavior of this parser with a mandatory check and conditional error diagnostics.static <D,T> Parser <D, T> identity()Deprecated.UseParserFactory.identity()instead.static <D,T> Parser <D, T> ifThenElse(Predicate<Token<D, T>> condition, Parser<D, T> thenBranch, Parser<D, T> elseBranch) Deprecated.UseParserFactory.ifThenElse(Predicate, Parser, Parser)instead.Removes the setting of the result value from the semantic effect of this parser.Creates the predictor for this parser.final booleanChecks whether this parser can only succeed by consuming input.static <D,T> Parser <D, T> lookbehind(Predicate<? super Token<D, T>> match) Deprecated.UseParserFactory.lookbehind(Predicate)instead.static <D,T> Parser <D, T> lookbehind(Set<T> types) Deprecated.UseParserFactory.lookbehind(Set)instead.static <D,T> Parser <D, T> lookbehind(T... types) Deprecated.UseParserFactory.lookbehind(Object[])instead.static <D,T> Parser <D, T> lookup(Map<T, Parser<D, T>> alts, Parser.Pragma... pragmas) Deprecated.UseParserFactory.lookup(Map, Pragma[])instead.static <D,T> Parser <D, T> lookup(Map<T, Parser<D, T>> alts, Parser<D, T> otherwise, Parser.Pragma... pragmas) Deprecated.UseParserFactory.lookup(Map, Parser, Pragma[])instead.map(UnaryOperator<Data> transform) Adds a final transformation of the result value to the behavior of this parser.negate()Returns a parser that succeeds if this parser fails, and vice versa.optional(Parser.Pragma... pragmas) Returns a parser that behaves like this parser, or alternatively does nothing.optionalTagged(Parser.Pragma... pragmas) Returns a parser that behaves like this parser, or alternatively does nothing, setting a corresponding result value.optionalTagged(Parser<D, T> defaultBranch, Parser.Pragma... pragmas) Returns a parser that behaves like this parser, or some alternative without result value.orElse(Parser<D, T> other, Parser.Pragma... pragmas) Alternates the behavior of this parser with another in nondeterministic choice.plus(Parser.Pragma... pragmas) Returns a parser that behaves as the sequential iteration of this parser, one or more times.plusSequence(Parser.Pragma... pragmas) Returns a parser that behaves as the sequential iteration of this parser, one or more times, and collects the result values in a sequence.plusSequence(Parser<D, T> separator, Parser.Pragma... pragmas) Returns a parser that behaves as the sequential iteration of this parser, one or more times, interspersed with a separator parser, and collects the result values in a sequence.Appends an ad-hoc continuation to the semantic effect of this parser.postprocess(Action.Continuation<State, State, SimpleMessage<D>, State> cont, String description) Appends an ad-hoc continuation to the semantic effect of this parser.postprocess(Action.Continuation<State, State, SimpleMessage<D>, State> cont, Supplier<String> description) Appends an ad-hoc continuation to the semantic effect of this parser.postprocess(Update<SimpleMessage<D>> effect) Appends to the semantic effect of this parser.Processes an input stream with this parser and the default initial semantic state.process(Input<D, T> input, Action.Visitor<Output<D, T>, SimpleMessage<D>, Input<D, T>> errorVisitor, State initialState) Processes an input stream with this parser and a given initial semantic state.static <D,T> Parser <D, T> Deprecated.UseParserFactory.sequence(Parser[])instead.static <D,T> Parser <D, T> Deprecated.UseParserFactory.sequence(List)instead.static <D,T> Parser <D, T> sideEffect(Runnable effect) Deprecated.UseParserFactory.sideEffect(Runnable)instead.static <D,T> Parser <D, T> sideEffect(Consumer<Input<D, T>> effect) Deprecated.UseParserFactory.sideEffect(Consumer)instead.star(Parser.Pragma... pragmas) Returns a parser that behaves as the sequential iteration of this parser, zero or more times.starSequence(Parser.Pragma... pragmas) Returns a parser that behaves as the sequential iteration of this parser, zero or more times, and collects the result values in a sequence.starSequence(Parser<D, T> separator, Parser.Pragma... pragmas) Returns a parser that behaves as the sequential iteration of this parser, zero or more times, interspersed with a separator parser, and collects the result values in a sequence.static <D,T> Parser <D, T> Deprecated.UseParserFactory.startCollection()instead.static <D,T> Parser <D, T> Deprecated.UseParserFactory.startInterval()instead.static <D,T> Parser <D, T> succeed(Update<SimpleMessage<D>> effect) Deprecated.UseParserFactory.succeed(Update)instead.static <D,T> Parser <D, T> succeedGivenInput(Function<? super Input<D, T>, ? extends Update<SimpleMessage<D>>> effect) Deprecated.UseParserFactory.succeedGivenInput(Function)instead.static <D,T> Parser <D, T> terminal()Deprecated.UseParserFactory.terminal()instead.static <D,T> Parser <D, T> Deprecated.UseParserFactory.terminal(Predicate)instead.static <D,T> Parser <D, T> terminal(T type) Deprecated.UseParserFactory.terminal(Object)instead.static <D,T> Parser <D, T> Deprecated.UseParserFactory.terminal(Object, Predicate)instead.whenFollowedBy(Predictor<T> following, Rewriting<Parser<D, T>, Predictor<T>, Parser.Pragma[]>.Context context) whenFollowedBy(Predictor<T> following, Parser.Pragma... pragmas) Returns a parser that behaves like this parser if consumed input is followed by the specified token types.withOtherTokenSource(LookaheadTokenMultiplexer<K, D, T, ?> multi, K newKey) Extends the behavior of this parser by switching input token sources.Wraps the behavior of this parser in a local scope for a set of semantic variables bound by grammar fragments.Wraps the behavior of this parser in a local scope for a semantic variable.Wraps the behavior of this parser in a local scope for a set of semantic variables bound by grammar fragments.Wraps the behavior of this parser in a local scope for a set of semantic variables.Adds side effects to this parser to be performed directly before and after, respectively.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface eu.bandm.tools.ramus.ogma.Action.Continuation
apply
-
Constructor Details
-
Parser
Creates a new instance.- Parameters:
factory- the factory that has been responsible for creating this parser.- Throws:
NullPointerException- iffactoryis null
-
-
Method Details
-
apply
public abstract Action<Output<D,T>, applySimpleMessage<D>, Input<D, T>> (Input<D, T> in, Action.Success<?, SimpleMessage<D>, Input<D, T>> recyclable) Applies this continuation, possibly using a recyclable leaf node. Operational method.- Specified by:
applyin interfaceAction.Continuation<Input<D,T>, Output<D, T>, SimpleMessage<D>, Input<D, T>> - Parameters:
in- the input parsing resultrecyclable- a recyclable leaf node, ornullif not available- Returns:
- a leaf node with a newly computed parsing result; either
recyclableif available ornew. - See Also:
-
getPredictor
Returns the predictor for this parser.This method must not be called before the constructor has returned.
- Returns:
- the predictor for this parser
-
initializePredictor
Creates the predictor for this parser.This method should not be called explicitly.
The default implementation returns
Predictor.star(), the predictor that contains no information.- Returns:
- the new predictor for this parser
-
isConsuming
public final boolean isConsuming()Checks whether this parser can only succeed by consuming input.- Returns:
trueif this parser only succeeds after having consumed one or more input tokens (including the case that it fails always);falseif this parser can succeed without consuming input.
-
getFactory
Returns the factory responsible for creating this parser.Mixing parsers created by different factories, and hence with possibly different configuration settings, is possible but not recommended.
- Returns:
- the factory responsible for creating this parser
-
getLocationTree
Returns the location tree for this parser if available.If caller locations are available, this tree identifies the client code that has created each building block of this parser.
- Returns:
- the location tree for this parser, or
nullif not available
-
identity
Deprecated.UseParserFactory.identity()instead.Returns a parser that succeeds with no effect, consuming no tokens.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Returns:
- a parser that succeeds with no effect, consuming no tokens.
-
succeed
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> succeed(Update<SimpleMessage<D>> effect) Deprecated.UseParserFactory.succeed(Update)instead.Returns a parser that succeeds with the given effect, consuming no tokens.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
effect- the desired semantic effect- Returns:
- a parser that succeeds with the given effect, consuming no tokens.
- Throws:
NullPointerException- ifeffectis null
-
succeedGivenInput
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> succeedGivenInput(Function<? super Input<D, T>, ? extends Update<SimpleMessage<D>>> effect) Deprecated.UseParserFactory.succeedGivenInput(Function)instead.Returns a parser that succeeds with the given state-dependent effect, consuming no tokens.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
effect- a function object that computes the desired semantic effect, given the current parser input state- Returns:
- a parser that succeeds with the computed effect, consuming no tokens.
- Throws:
NullPointerException- ifeffectis null
-
constant
Deprecated.UseParserFactory.constant(Object)instead.Returns a parser that succeeds and sets the result to the given value, consuming no tokens.This method is a shorthand for
succeed(Update.setValue(Data.atomic(value))).- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
value- the desired semantic value- Returns:
- a parser that succeeds with the effect of setting the result to the given value, consuming no tokens.
- Throws:
NullPointerException- ifvalueis null- See Also:
-
startCollection
Deprecated.UseParserFactory.startCollection()instead.Returns a parser that succeeds and sets the result to the empty sequence, consuming no tokens.This operation is useful in conjunction with
collecting().- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Returns:
- a parser that succeeds with the effect of setting the result to the empty sequence, consuming no tokens.
- See Also:
-
fail
Deprecated.UseParserFactory.fail()instead.Returns a parser that fails immediately.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Returns:
- a parser that fails with no effect, consuming no tokens.
-
negate
Returns a parser that succeeds if this parser fails, and vice versa.The resulting parser speculatively executes the behavior of this parser. If this leads to one or more (nondeterministic) successes, the resulting parser fails; speculatively applied semantic effects have no bearing further computations. Otherwise, if the speculative execution fails, the resulting parser succeeds with no effect, consuming no tokens.
- Returns:
- a parser that succeeds with no effect if this parser fails and vice versa.
- See Also:
-
startInterval
Deprecated.UseParserFactory.startInterval()instead.Returns a parser that sets the result value to the token location at the current input state.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Returns:
- a parser that sets the result value to the token location at the current input state.
-
endInterval
Deprecated.UseParserFactory.startInterval()instead.Returns a parser that sets the result value to the token location before the current input state.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Returns:
- a parser that sets the result value to the token location before the current input state.
-
lookbehind
@Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D,T> Parser<D,T> lookbehind(T... types) Deprecated.UseParserFactory.lookbehind(Object[])instead.Returns a parser that succeeds if and only if the preceding token is of one of the given types.If there is no preceding token, or its type is not among the given ones, then the parser fails.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
types- an array of token types- Returns:
- a parser that succeeds if and only if the preceding token has a
type contained in
types. - Throws:
NullPointerException- iftypesis null
-
lookbehind
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> lookbehind(Set<T> types) Deprecated.UseParserFactory.lookbehind(Set)instead.Returns a parser that succeeds if and only if the preceding token is of one of the given types.If there is no preceding token, or its type is not among the given ones, then the parser fails.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
types- a set of token types- Returns:
- a parser that succeeds if and only if the preceding token has a
type contained in
types. - Throws:
NullPointerException- iftypesis null
-
lookbehind
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> lookbehind(Predicate<? super Token<D, T>> match) Deprecated.UseParserFactory.lookbehind(Predicate)instead.Returns a parser that succeeds if and only if the preceding token matches a given predicate.If there is no preceding token, or its type is not among the given ones, then the parser fails.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
match- a predicate to match- Returns:
- a parser that succeeds if and only if
matchreturnstruewhen applied to the preceding token. - Throws:
NullPointerException- iftypesis null
-
terminal
Deprecated.UseParserFactory.terminal()instead.Returns a parser that consumes any single token.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Returns:
- a parser that succeeds by consuming the next token and storing it in the accumulator of the semantic state
- See Also:
-
terminal
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> terminal(Predicate<Token<D, T>> match) Deprecated.UseParserFactory.terminal(Predicate)instead.Returns a parser that consumes a single token matching a given predicate.This method is a shorthand for
terminal(match, Predictor.plus(), null); use that method directly if more specific information is available.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
match- a functional object that recognizes acceptable tokens- Returns:
- a parser that succeeds by consuming the next token and storing it
in the accumulator of the semantic state, if the token is accepted by
match; otherwise parsing fails and rejects the token - Throws:
NullPointerException- ifmatchis null- See Also:
-
terminal
Deprecated.UseParserFactory.terminal(Object)instead.Returns a parser that consumes a single token of a given type.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
type- the type of tokens to match- Returns:
- a parser that succeeds by consuming the next token and storing it
in the accumulator of the semantic state, if the token has the type
type; otherwise parsing fails and rejects the token - Throws:
NullPointerException- iftypeis null- See Also:
-
terminal
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> terminal(T type, Predicate<? super String> text) Deprecated.UseParserFactory.terminal(Object, Predicate)instead.Returns a parser that consumes a single token of a given type and text.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
type- the type of tokens to matchtext- a predicate that specifies acceptable text- Returns:
- a parser that succeeds by consuming the next token and storing it
in the accumulator of the semantic state, if the token has the type
typeand text accepted bytext; otherwise parsing fails and rejects the token - Throws:
NullPointerException- iftypeortextis null- See Also:
-
diagnose
Add diagnostic messages to the behavior of this parser.- Parameters:
msgs- the messages to add- Returns:
- a parser that behaves equivalently to this parser, but annotates the transition output with the given messages.
-
diagnose
public final Parser<D,T> diagnose(Function<? super Input<D, T>, ? extends List<? extends SimpleMessage<D>>> msgs) Add diagnostic messages to the behavior of this parser.- Parameters:
msgs- a function that computes the messages to add from the current input- Returns:
- a parser that behaves equivalently to this parser, but annotates the transition output with the computed messages.
-
map
Adds a final transformation of the result value to the behavior of this parser.- Parameters:
transform- the transformation to apply to the result value- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect transforms the result value by the specified function.
- Throws:
NullPointerException- iftransformis null
-
assigning
Adds the assignment of the result value to a semantic variable to the behavior of this parser.- Parameters:
key- the key identifying the semantic variable to assign to- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect also assigns the result value to the specified variable.
-
collecting
Adds the collecting of the result value in a sequence to the behavior of this parser.The resulting parser assumes that the result value of its pre-state is set to a sequence. It then performs the effect of this parser, and appends its result value to the preceding sequence, which becomes the overall result value. If the preceding result value is not a sequence, the result parser fails with a failure message.
- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect then appends the result value to the preceding one, if that is a sequence, or otherwise fails.
- See Also:
-
withScope
Wraps the behavior of this parser in a local scope for a semantic variable.The semantic effect of the returned parser embeds the effect of this parser in a context that inializes the specified variable before, and restores the previous value afterwards.
- Parameters:
key- the key identifying the semantic variable to scopeinitializer- the initial value of the variable in its local scope- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variable is locally scoped.
- Throws:
NullPointerException- ifkeyorinitializeris null
-
withScope
Wraps the behavior of this parser in a local scope for a set of semantic variables.The semantic effect of the returned parser embeds the effect of this parser in a context that inializes the specified variables before, and restores the previous values afterwards.
- Parameters:
initializers- the map of keys to initial value of the variable in its local scope- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variables are locally scoped.
- Throws:
NullPointerException- ifinitializersis or contains null
-
withScope
Wraps the behavior of this parser in a local scope for a set of semantic variables bound by grammar fragments.The semantic effect of the returned parser embeds the effect of this parser in a context that inializes the specified variables before, and restores the previous values afterwards.
This method creates a local scope for the target variables of all given fragments that are not marked as imported, and initializes them with the specified values.
- Parameters:
fragments- the list of grammar fragments- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variables are locally scoped.
- Throws:
NullPointerException- iffragmentsis or contains nullIllegalArgumentException- if the list of fragments contains duplicate target variable keys.- See Also:
-
withScope
Wraps the behavior of this parser in a local scope for a set of semantic variables bound by grammar fragments.The semantic effect of the returned parser embeds the effect of this parser in a context that inializes the specified variables before, and restores the previous values afterwards.
This method creates a local scope for the target variables of all given fragments that are not marked as imported, and initializes them with the specified values.
- Parameters:
fragments- the list of grammar fragments- Returns:
- a parser that behaves equivalently to this parser, but whose semantic effect executes in a context where the specified variables are locally scoped.
- Throws:
NullPointerException- iffragmentsis or contains nullIllegalArgumentException- if the list of fragments contains duplicate target variable keys.- See Also:
-
postprocess
Appends to the semantic effect of this parser.This method is an equivalent shorthand for
andThen(succeed(cons)), but the result of this method may be slightly more efficient.- Parameters:
effect- the semantic effect to append- Returns:
- a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given effect.
- Throws:
NullPointerException- ifeffectis null
-
postprocess
Appends an ad-hoc continuation to the semantic effect of this parser.- Parameters:
cont- the semantic continuation to append- Returns:
- a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given continuation.
-
postprocess
public final Parser<D,T> postprocess(Action.Continuation<State, State, SimpleMessage<D>, State> cont, String description) Appends an ad-hoc continuation to the semantic effect of this parser.- Parameters:
cont- the semantic continuation to appenddescription- a user-friendly description of the continuation- Returns:
- a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given continuation.
-
postprocess
public final Parser<D,T> postprocess(Action.Continuation<State, State, SimpleMessage<D>, State> cont, Supplier<String> description) Appends an ad-hoc continuation to the semantic effect of this parser.- Parameters:
cont- the semantic continuation to appenddescription- generates a user-friendly description of the continuation- Returns:
- a parser that behaves equivalently to this parser, but where the semantic effect of this parser is sequentially followed by the given continuation.
-
ignoringResult
Removes the setting of the result value from the semantic effect of this parser. All other effects, such as diagnostics and assignments to state variables, are unaffected.- Returns:
- a parser that behaves equivalently to this parser, but where the previous result value is restored.
-
whenFollowedBy
Returns a parser that behaves like this parser if consumed input is followed by the specified token types.The resulting parser is equivalent, but potentially more deterministic and hence more efficient, if the tokens consumed by it are followed by a token of the specified kind. Otherwise, behavior is undefined.
Since the correctness of the resulting parser depends on context that cannot be enforced, it is not recommended to reuse it in uncontrolled contexts.
- Parameters:
following- a specification of the possible following tokenpragmas- the pragmas to apply- Returns:
- a parser that is equivalent to
thisfor all inputs where the consumed tokens are immediately followed by a token for whichfollowing.acceptwould returntrue - Throws:
NullPointerException- iffollowingis null- See Also:
-
whenFollowedBy
-
closeAndTerminate
Returns a parser that behaves like this parser if consumed input is followed by the specified token type.The resulting parser is equivalent, but potentially more deterministic and hence more efficient, if the tokens consumed by it are followed by a token of the specified type. Otherwise, behavior is undefined.
Since the correctness of the resulting parser depends on context that cannot be enforced, it is not recommended to reuse it in uncontrolled contexts. Use this operation only on top-level parsers that consume a unit of input terminated by a distinguished end token.
- Parameters:
endType- the type of the token that follows and terminates the consumed inputpragmas- the pragmas to apply- Returns:
- a parser that is equivalent to
thisfor all inputs where the consumed tokens are immediately followed by a token of typeendType - Throws:
NullPointerException- ifendTypeis null- See Also:
-
closeAndRepeat
Returns a parser that behaves like this parser if consumed input is followed by more input of the same syntax.The resulting parser is equivalent, but potentially more deterministic and hence more efficient, if the tokens consumed by it are followed by a token of the expected type. Otherwise, behavior is undefined.
Since the correctness of the resulting parser depends on context that cannot be enforced, it is not recommended to reuse it in uncontrolled contexts. Use this operation only on top-level parsers that lazily consume a unit of input from a potentially infinite stream.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that is equivalent to
thisfor all inputs where the consumed tokens are immediately followed by input of the same syntax - See Also:
-
andThen
Follows the behavior of this parser sequentially with another.- Parameters:
other- the subsequent parser- Returns:
- a parser that behaves equivalently to this parser, sequentially
followed by
other. - Throws:
NullPointerException- ifotheris null
-
andThen
Follows the behavior of this parser sequentially with another.This method supports the following pragmas:
Disjoint– throw an exception if the type of the next token does not determine unambiguously whether it is to be consumed by this or the other parser.
- Parameters:
other- the subsequent parser- Returns:
- a parser that behaves equivalently to this parser, sequentially
followed by
other. - Throws:
NullPointerException- ifotheris nullUnsupportedOperationException- if there is a type of tokens that could both be consumed bythis, or byotherafterthissucceeds without consuming any tokens
-
andThenCombining
public final Parser<D,T> andThenCombining(Parser<D, T> other, Action.BiContinuation<Data, Data, Data, SimpleMessage<D>, State> combiner, Parser.Pragma... pragmas) Follows the behavior of this parser sequentially with another, combining the result values.This method supports the following pragmas:
Disjoint– throw an exception if the type of the next token does not determine unambiguously whether it is to be consumed by this or the other parser.
- Parameters:
other- the subsequent parsercombiner- a binary continuation to combine the result values- Returns:
- a parser that behaves equivalently to this parser, sequentially
followed by
other, followed by the combination of result values. - Throws:
NullPointerException- ifotherorcombineris nullUnsupportedOperationException- if there is a type of tokens that could both be consumed bythis, or byotherafterthissucceeds without consuming any tokens
-
sequence
@Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D,T> Parser<D,T> sequence(Parser<D, T>... elems) Deprecated.UseParserFactory.sequence(Parser[])instead.Combines the behavior of several parsers sequentially.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
elems- the array of parsers to combine sequentially- Returns:
- a parser that behaves equivalently to all of the given parsers
combined sequentially, or to
identity()if the array is empty. - Throws:
NullPointerException- ifelemsis or contains null- See Also:
-
sequence
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> sequence(List<? extends Parser<D, T>> elems) Deprecated.UseParserFactory.sequence(List)instead.Combines the behavior of several parsers sequentially.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
elems- the list of parsers to combine sequentially- Returns:
- a parser that behaves equivalently to all of the given parsers
combined sequentially, or to
identity()if the list is empty. - Throws:
NullPointerException- ifelemsis or contains null- See Also:
-
orElse
Alternates the behavior of this parser with another in nondeterministic choice.This method may exploit available syntactic information to reduce the choice to a case for the deterministic variant
ifThenElse.This method supports the following pragmas:
Productive– throw an exception if both alternatives may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.Disjoint– return a result only if the choice between the two alternatives can be made deterministically by lookahead; throw an exception otherwise.
- Parameters:
other- the alternative parserpragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to this parser,
(non)deterministically branching to
otherif necessary. - Throws:
NullPointerException- ifotheris null
-
ifThenElse
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> ifThenElse(Predicate<Token<D, T>> condition, Parser<D, T> thenBranch, Parser<D, T> elseBranch) Deprecated.UseParserFactory.ifThenElse(Predicate, Parser, Parser)instead.Alternates the behavior of two parsers in deterministic choice, depending on the next input token.- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
condition- a predicate to apply to the next input tokenthenBranch- the branch to choose if the next input token matches the conditionelseBranch- the branch to choose if the next input token does not match the condition- Returns:
- a parser that behaves equivalently to either given branch, depending on whether the next input token matches the condition.
- Throws:
NullPointerException- ifcondition,thenBranchorelseBranchis null- See Also:
-
optional
Returns a parser that behaves like this parser, or alternatively does nothing.This method supports the following pragmas:
Greedy– always perform the behavior of this parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to this parser, or alternatively
to the parser
identity(). - Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.isConsuming()returnsfalse.- See Also:
-
optionalTagged
Returns a parser that behaves like this parser, or alternatively does nothing, setting a corresponding result value.The semantic effect of the returned parser wraps the result value of this parser with
Data.just(eu.bandm.tools.ramus.ogma.Data)if the corresponding branch has been chosen, or sets the result value toData.none()otherwise.This method supports the following pragmas:
Greedy– always perform the behavior of this parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to this parser, or alternatively
to the parser
identity(), setting a corresponding result value. - Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.isConsuming()returnsfalse.- See Also:
-
optionalTagged
Returns a parser that behaves like this parser, or some alternative without result value.The semantic effect of the returned parser wraps the result value of this parser with
Data.just(eu.bandm.tools.ramus.ogma.Data)if the corresponding branch has been chosen, or sets the result value toData.none()otherwise.This method supports the following pragmas:
Greedy– always perform the behavior of this parser if the next token is of an acceptable type. Note that the alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
defaultBranch- the parser that specifies the alternativepragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to this parser, or alternatively
to
defaultBranch, setting a corresponding result value. - Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.isConsuming()returnsfalse.- See Also:
-
choice
@Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D,T> Parser<D,T> choice(Parser<D, T>... alts) Deprecated.UseParserFactory.choice(Parser[])instead.Alternates the behavior of several parsers in nondeterministic choice.This method may exploit available syntactic information to reduce the choice to a case for the deterministic variant
lookup.In contrast to the dual operation
sequence, which can be conceived as the moral equivalent of an iteration of the binary operationandThen(eu.bandm.tools.ramus.ogma.Parser<D, T>), this method- is strongly preferrable to an iteration of the binary operation
orElse, where more than two alternatives are concerned; - but may not preserve the order in which alternative branches are tried.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
alts- the alternative branches- Returns:
- a parser that behaves equivalent to the choice of all alternative branches, tried in some arbitrary given order, nondeterministically if necessary.
- See Also:
- is strongly preferrable to an iteration of the binary operation
-
choice
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> choice(List<? extends Parser<D, T>> alts) Deprecated.UseParserFactory.choice(List)instead.Alternates the behavior of several parsers in nondeterministic choice.This method may exploit available syntactic information to reduce the choice to a case for the deterministic variant
lookup.In contrast to the dual operation
sequence, which can be conceived as the moral equivalent of an iteration of the binary operationandThen(eu.bandm.tools.ramus.ogma.Parser<D, T>), this method- is strongly preferrable to an iteration of the binary operation
orElse, where more than two alternatives are concerned; - but may not preserve the order in which alternative branches are tried.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
alts- the alternative branches- Returns:
- a parser that behaves equivalent to the choice of all alternative branches, tried in some arbitrary given order, nondeterministically if necessary.
- See Also:
- is strongly preferrable to an iteration of the binary operation
-
lookup
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> lookup(Map<T, Parser<D, T>> alts, Parser.Pragma... pragmas) Deprecated.UseParserFactory.lookup(Map, Pragma[])instead.Alternates the behavior of several parsers in deterministic choice, depending on the next input token.This method supports the following pragmas:
Reachable– throw an exception if any of the given parsers is known to never succeed if the next input token is of the associated type.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
alts- a map of token types to parsers, to be chosen if the next token is of the respective typepragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the parser mapped to the type of the next input token, or to a parser that fails with a diagnostic hint if there is none.
- Throws:
UnsupportedOperationException- if a parser is found in the mapaltssuch that is predictor rules out the associated key- See Also:
-
lookup
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> lookup(Map<T, Parser<D, T>> alts, Parser<D, T> otherwise, Parser.Pragma... pragmas) Deprecated.UseParserFactory.lookup(Map, Parser, Pragma[])instead.Alternates the behavior of several parsers in deterministic choice, depending on the next input token.This method supports the following pragmas:
Reachable– throw an exception if any of the given parsers is known to never succeed if the next input token is of the associated type.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
alts- a map of token types to parsers, to be chosen if the next input token is of the respective typeotherwise- the parser to be chosen if the next input token is of an unmapped typepragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the parser mapped to the type
of the next input token, or to
otherwiseif there is none. - Throws:
UnsupportedOperationException- if a parser is found in the mapaltssuch that is predictor rules out the associated key- See Also:
-
fix
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> fix(UnaryOperator<Parser<D, T>> loop, Parser.Pragma... pragmas) Deprecated.UseParserFactory.fix(UnaryOperator, Pragma[])instead.Computes the fixpoint of a recursive parser definition.This method supports the following pragmas:
Productive– throw an exception if the recursion step may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
loop- a function object that returns a parser, assuming that it receives a behaviorally equivalent parser as inputpragmas- the pragmas to apply- Returns:
- a parser
pthat is behaviorally equivalent toloop.apply(p). - Throws:
NullPointerException- ifloopis or returns nullUnsupportedOperationException- optionally if theProductivepragma is specified, andisConsuming()method of the parser returned byloopreturnsfalse.
-
star
Returns a parser that behaves as the sequential iteration of this parser, zero or more times. Decreasing numbers of iterations are tried as nondeterministic alternatives if something fails downstream.This method is a shorthand for
plus().optional().This method supports the following pragmas:
Greedy– always perform another iteration of the behavior of this parser if the next token is of an acceptable type.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the choice of any number of zero or more sequential iterations of this parser.
- Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.mayBeEpsilon()returnstrue.- See Also:
-
plus
Returns a parser that behaves as the sequential iteration of this parser, one or more times. Decreasing numbers of iterations are tried as nondeterministic alternatives if something fails downstream.This method is a shorthand for
fix(p -> this.andThen(p.optional())).This method supports the following pragmas:
Greedy– always perform another iteration of the behavior of this parser if the next token is of an acceptable type.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the choice of any number of one or more sequential iterations of this parser.
- Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.isConsuming()returnsfalse.- See Also:
-
starSequence
Returns a parser that behaves as the sequential iteration of this parser, zero or more times, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.This method is a shorthand for
startCollection().andThen(this.collecting().star(pragmas)).This method supports the following pragmas:
Greedy– always perform another iteration of the behavior of this parser if the next token is of an acceptable type.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the choice of any number of zero or more sequential iterations of this parser.
- Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.isConsuming()returnsfalse.- See Also:
-
plusSequence
Returns a parser that behaves as the sequential iteration of this parser, one or more times, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.This method is a shorthand for
startCollection().andThen(this.collecting().plus(pragmas)).This method supports the following pragmas:
Greedy– always perform another iteration of the behavior of this parser if the next token is of an acceptable type.Productive– throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
pragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the choice of any number of one or more sequential iterations of this parser.
- Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, andthis.isConsuming()returnsfalse.- See Also:
-
starSequence
Returns a parser that behaves as the sequential iteration of this parser, zero or more times, interspersed with a separator parser, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.This method supports the following pragmas:
Greedy– always perform another iteration of the behavior of this parser if the next token is of an acceptable type.Productive– throw an exception if this parser and the separator may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
separator- the parser to invoke between consecutive iterationspragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the choice of any number of
zero or more sequential iterations of this parser, interspersed with
separator. - Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, and boththis.isConsuming()andseparator.isConsuming()returnfalse.- See Also:
-
plusSequence
Returns a parser that behaves as the sequential iteration of this parser, one or more times, interspersed with a separator parser, and collects the result values in a sequence. Increasing numbers of iterations are tried as nondeterministic alternatives in ascending order.This method supports the following pragmas:
Greedy– always perform another iteration of the behavior of this parser if the next token is of an acceptable type.Productive– throw an exception if this parser and the separator may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
- Parameters:
separator- the parser to invoke between consecutive iterationspragmas- the pragmas to apply- Returns:
- a parser that behaves equivalently to the choice of any number of
one or more sequential iterations of this parser, interspersed with
separator. - Throws:
UnsupportedOperationException- optionally if theProductivepragma is specified, and boththis.mayBeEpsilon()and {separator.mayBeEpsilon()} returntrue.- See Also:
-
guard
public final Parser<D,T> guard(Predicate<State> condition, Function<State, List<SimpleMessage<D>>> error) Returns a parser that extends the behavior of this parser with a mandatory check and conditional error diagnostics.- Parameters:
condition- the condition to check on the post stateerror- a function to provide diagnostics for the post state if the condition fails- Returns:
- a parser that behaves equivalently to this parser, but then only
succeeds if
conditionreturnstruefor the post state; otherwise diagnostics created by applyingerrorto the same state are attached, and the state is rejected - Throws:
NullPointerException- ifconditionorerroris null
-
process
public @Opt Action<State,SimpleMessage<D>, processState> (Input<D, T> input, Action.Visitor<Output<D, T>, SimpleMessage<D>, Input<D, T>> errorVisitor) Processes an input stream with this parser and the default initial semantic state.This method either returns a non-null search tree with at least one successful parsing result, or invokes
errorVisitorto collect diagnostics in case of failure, but not both.- Parameters:
input- the input stream to processerrorVisitor- a visitor for processing errors if parsing has failed- Returns:
- a search tree of parsing results if there is at least one success,
or
nullotherwise - Throws:
NullPointerException- ifinputorerrorVisitoris null
-
process
public @Opt Action<State,SimpleMessage<D>, processState> (Input<D, T> input, Action.Visitor<Output<D, T>, SimpleMessage<D>, Input<D, T>> errorVisitor, State initialState) Processes an input stream with this parser and a given initial semantic state.This method either returns a non-null search tree with at least one successful parsing result, or invokes
errorVisitorto collect diagnostics in case of failure, but not both.- Parameters:
input- the input stream to processerrorVisitor- a visitor for processing errors if parsing has failedinitialState- the initial state for semantic analysis- Returns:
- a search tree of parsing results if there is at least one success,
or
nullotherwise - Throws:
NullPointerException- ifinput,errorVisitororinitialStateis null
-
sideEffect
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> sideEffect(Runnable effect) Deprecated.UseParserFactory.sideEffect(Runnable)instead.Returns a parser that always succeeds without semantic effect, and performs the given side effect directly.Side effects caused by the resulting parser cannot be undone via backtracking. Use this operation with care and for monitoring purposes only.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
effect- the side effect to perform- Returns:
- a parser that always succeeds without semantic effect, and invokes
effectdirectly - Throws:
NullPointerException- ifeffectis null
-
sideEffect
@Deprecated(forRemoval=false, since="1.0") public static <D,T> Parser<D,T> sideEffect(Consumer<Input<D, T>> effect) Deprecated.UseParserFactory.sideEffect(Consumer)instead.Returns a parser that always succeeds without semantic effect, and performs the given side effect directly.Side effects caused by the resulting parser cannot be undone via backtracking. Use this operation with care and for monitoring purposes only.
- Type Parameters:
D- the type of source document identifiersT- the type of token types; typically an enum type- Parameters:
effect- the side effect to perform with the current input- Returns:
- a parser that always succeeds without semantic effect, and invokes
effectwith the current input directly - Throws:
NullPointerException- ifeffectis null
-
withSideEffects
Adds side effects to this parser to be performed directly before and after, respectively.Side effects caused by the resulting parser cannot be undone via backtracking. Use this operation with care and for monitoring purposes only.
- Parameters:
pre- the side effect to perform before parsing with the current inputpost- the side effect to perform after parsing with the remaining input- Returns:
- a parser that behaves equivalently to
this, but additionally invokesprewith its current input directly before parsing, andpostwith the same input directly after parsing - Throws:
NullPointerException- ifpreorpostis null
-
withOtherTokenSource
public final <K> Parser<D,T> withOtherTokenSource(LookaheadTokenMultiplexer<K, D, T, ?> multi, K newKey) Extends the behavior of this parser by switching input token sources.Note that token source switching is only supported in situations where no lookahead tokens have been acquired.
IllegalStateExceptionwill be thrown if lookahead tokens are present when the switch is performed.- Type Parameters:
K- the type of token multiplexer keys- Parameters:
multi- the token multiplexer to use for switchingnewKey- the key to switch the token source to during parsing with the extended parser- Returns:
- a parser that behaves equivalently to this parser, but switches to
the token source associated with
newKeybefore parsing, and switches back afterwards, on both success and failure - Throws:
NullPointerException- ifmultiornewKeyis null
-
ParserFactory.choice(Parser[])instead.