Top Description Inners Constructors Methods
org.jparsec

public abstract Class Parser<T>

extends Object
Class Inheritance
Known Direct Subclasses
org.jparsec.BestParser, org.jparsec.DelimitedParser, org.jparsec.EmptyListParser, org.jparsec.NestableBlockCommentScanner, org.jparsec.ReluctantBetweenParser, org.jparsec.RepeatAtLeastParser, org.jparsec.RepeatTimesParser, org.jparsec.SkipAtLeastParser, org.jparsec.SkipTimesParser
Imports
java.io.IOException, java.nio.CharBuffer, java.util.Collection, .List, .Optional, java.util.concurrent.atomic.AtomicReference, java.util.function.BiFunction, .Function, org.jparsec.error.ParserException, org.jparsec.internal.annotations.Private, org.jparsec.internal.util.Checks

Defines grammar and encapsulates parsing logic. A Parser takes as input a CharSequence source and parses it when the parse(CharSequence) method is called. A value of type T will be returned if parsing succeeds, or a ParserException is thrown to indicate parsing error. For example:
   Parser<String> scanner = Scanners.IDENTIFIER;
  assertEquals("foo", scanner.parse("foo"));

Parsers run either on character level to scan the source, or on token level to parse a list of Token objects returned from another parser. This other parser that returns the list of tokens for token level parsing is hooked up via the from(Parser, Parser) or from(Parser) method.

The following are important naming conventions used throughout the library:

To debug a complex parser that fails in un-obvious way, pass Mode#DEBUG mode to parse(CharSequence, Mode) and inspect the result in ParserException#getParseTree(). All labeled parsers will generate a node in the exception's parse tree, with matched indices in the source.
Author
Ben Yu

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
Parser.Mode

Defines the mode that a parser should be run in.

public static class
Parser.Reference<T>

An atomic mutable reference to Parser used in recursive grammars.

private static class

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract boolean
private static <T> T
applyInfixOperators(T initialValue, List<? extends Function<? super T, ? extends T>> functions)

private static <T> T
applyInfixrOperators(T first, List<Parser.Rhs<T>> rhss)

private static <T> T
applyPostfixOperators(T a, Iterable<? extends Function<? super T, ? extends T>> ms)

private static <T> T
applyPrefixOperators(List<? extends Function<? super T, ? extends T>> ms, T a)

pack-priv final Parser<T>
asDelimiter()

As a delimiter, the parser's error is considered lenient and will only be reported if no other meaningful error is encountered.

public final Parser<Optional<T>>
asOptional()

p.asOptional() is equivalent to p?

public final Parser<List<T>>
atLeast(int min)

A Parser that runs this parser greedily for at least min times.

public final Parser<T>
atomic()

A Parser that undoes any partial match if this fails.

public final Parser<T>
between(Parser<?> before, Parser<?> after)

A Parser that runs this between before and after.

public final <R> Parser<R>
cast()

Casts this to a Parser of type R.

public final Parser<List<T>>
endBy(Parser<?> delim)

A Parser that runs this for 0 or more times delimited and terminated by delim.

public final Parser<List<T>>
endBy1(Parser<?> delim)

A Parser that runs this for 1 or more times delimited and terminated by delim.

public final Parser<Boolean>
fails()

A Parser that returns true if this fails, false otherwise.

public final Parser<T>
followedBy(Parser<?> parser)

A Parser that sequentially executes this and then parser, whose return value is ignored.

public final Parser<T>
from(Parser<? extends Collection<Token>> lexer)

A Parser that takes as input the Token collection returned by lexer, and runs this to parse the tokens.

public final Parser<T>
from(Parser<?> tokenizer, Parser<Void> delim)

A Parser that takes as input the tokens returned by tokenizer delimited by delim, and runs this to parse the tokens.

pack-priv final T
public final <R> Parser<R>
ifelse(Parser<? extends R> consequence, Parser<? extends R> alternative)

A Parser that runs consequence if this succeeds, or alternative otherwise.

public final <R> Parser<R>
ifelse(final Function<? super T, ? extends Parser<? extends R>> consequence, final Parser<? extends R> alternative)

A Parser that runs consequence if this succeeds, or alternative otherwise.

public final Parser<T>
infixl(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> operator)

A Parser for left-associative infix operator.

public final Parser<T>
infixn(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> op)

A Parser that parses non-associative infix operator.

public final Parser<T>
infixr(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> op)

A Parser for right-associative infix operator.

public Parser<T>
label(final String name)

A Parser that reports reports an error about name expected, if this fails with no partial match.

public Parser<List<Token>>
lexer(Parser<?> delim)

A Parser that greedily runs this repeatedly, and ignores the pattern recognized by delim before and after each occurrence.

public final Parser<List<T>>
many()

p.many() is equivalent to p* in EBNF.

public final Parser<List<T>>
many1()

p.many1() is equivalent to p+ in EBNF.

public final <R> Parser<R>
map(final Function<? super T, ? extends R> map)

A Parser that runs this parser and transforms the return value using map.

public static <T> Parser.Reference<T>
newReference()

Creates a new instance of Reference.

public final <R> Parser<R>
next(Parser<R> parser)

A Parser that sequentially executes this and then parser.

public final <To> Parser<To>
next(final Function<? super T, ? extends Parser<? extends To>> map)

A Parser that executes this, maps the result using map to another Parser object to be executed as the next step.

public final Parser<?>
not()

A Parser that fails if this succeeds.

public final Parser<?>
not(String
the name of what we don't expect.
unexpected
)

A Parser that fails if this succeeds.

public final Parser<T>
notFollowedBy(Parser<?> parser)

A Parser that succeeds if this succeeds and the pattern recognized by parser isn't following.

public final Parser<T>
optional()
Deprecated since 3.0. Use optional(null) or asOptional instead.

p.optional() is equivalent to p?

public final Parser<T>
optional(T defaultValue)

A Parser that returns defaultValue if this fails with no partial match.

public final Parser<T>
or(Parser<? extends T>
the alternative parser to run if this fails.
alternative
)

p1.or(p2) is equivalent to p1 | p2 in EBNF.

public final Parser<T>
otherwise(Parser<? extends T>
the parser to run if this matches no input.
fallback
)

a.otherwise(fallback) runs fallback when a matches zero input.

public final T
parse(CharSequence source)

Parses source.

public final T
parse(Readable readable)

Parses source read from readable.

public final T
parse(CharSequence source, Parser.Mode mode)

Parses source under the given mode.

public final T

Returns:

the result
parse
(CharSequence
the source string
source
,
String
the name of the module, this name appears in error message
moduleName
)
Deprecated Please use parse(CharSequence) instead.

Parses source.

public final T

Returns:

the result
parse
(Readable
where the source is read from
readable
,
String
the name of the module, this name appears in error message
moduleName
)
Deprecated Please use parse(Readable) instead.

Parses source read from readable.

public final ParseTree
parseTree(CharSequence source)

Parses source and returns a ParseTree corresponding to the syntactical structure of the input.

public final Parser<T>
peek()

A Parser that runs this and undoes any input consumption if succeeds.

public final Parser<T>
postfix(Parser<? extends Function<? super T, ? extends T>> op)

A Parser that runs this and then runs op for 0 or more times greedily.

public final Parser<T>
prefix(Parser<? extends Function<? super T, ? extends T>> op)

A Parser that runs op for 0 or more times greedily, then runs this.

pack-priv static StringBuilder
read(Readable from)

Copies all content from from to to.

public final Parser<T>
reluctantBetween(Parser<?> before, Parser<?> after)
Deprecated This method probably only works in the simplest cases. And it's a character-level parser only. Use it at your own risk. It may be deleted later when we find a better way.

A Parser that first runs before from the input start, then runs after from the input's end, and only then runs this on what's left from the input.

public final <R> Parser<R>
retn(R value)

A Parser that executes this, and returns value if succeeds.

public final Parser<List<T>>
sepBy(Parser<?> delim)

A Parser that runs this 0 or more times separated by delim.

public final Parser<List<T>>
sepBy1(Parser<?> delim)

A Parser that runs this 1 or more times separated by delim.

public final Parser<List<T>>
sepEndBy(Parser<?> delim)

A Parser that runs this for 0 ore more times separated and optionally terminated by delim.

public final Parser<List<T>>
sepEndBy1(final Parser<?> delim)

A Parser that runs this for 1 ore more times separated and optionally terminated by delim.

public final Parser<Void>
skipAtLeast(int min)

A Parser that runs this parser greedily for at least min times and ignores the return values.

public final Parser<Void>
skipMany()

p.skipMany() is equivalent to p* in EBNF.

public final Parser<Void>
skipMany1()

p.skipMany1() is equivalent to p+ in EBNF.

public final Parser<Void>
skipTimes(int n)

A Parser that sequentially runs this for n times and ignores the return values.

public final Parser<Void>
skipTimes(int min, int max)

A Parser that runs this parser for at least min times and up to max times, with all the return values ignored.

public final Parser<String>
source()

A Parser that returns the matched string in the original source.

public final Parser<Boolean>
succeeds()

A Parser that returns true if this succeeds, false otherwise.

public final Parser<List<T>>
times(int n)

A Parser that runs this for n times and collects the return values in a List.

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

A Parser that runs this parser for at least min times and up to max times.

public final Parser<Token>
token()

A Parser that runs this and wraps the return value in a Token.

public final Parser<List<T>>
until(Parser<?> parser)

A Parser that matches this parser zero or many times until the given parser succeeds.

public final Parser<WithSource<T>>
withSource()

A Parser that returns both parsed object and matched string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait