public class SimpleParser
extends java.lang.Object
This parser consumes characters of an in-memory string while tracking the current 0-based position, 1-based line, and 1-based column. Expectations can be made on the parser to require specific characters, and when those expectations are not met, a syntax exception is thrown.
Constructor and Description |
---|
SimpleParser(java.lang.String expression)
Creates a new SimpleParser and sets the expression to parse.
|
SimpleParser(java.lang.String expression,
int maxNestingLevel)
Creates a new SimpleParser and sets the expression to parse.
|
Modifier and Type | Method and Description |
---|---|
void |
br()
Skips spaces then expects that the next character is either the end of
the expression or a line break (\n, \r\n, or \r).
|
int |
column()
Gets the current 1-based column number of the parser.
|
void |
consumeRemainingCharactersOnLine()
Skips over the remaining characters on a line but does not consume
the newline character (\n or \r\n).
|
int |
consumeUntilNoLongerMatches(java.util.function.Predicate<java.lang.Character> predicate)
Reads a lexeme from the expression while the given
predicate
matches each peeked character. |
void |
decreaseNestingLevel()
Decreases the current nesting level of the parser.
|
boolean |
eof()
Checks if the parser has reached the end of the expression.
|
char |
expect(char... tokens)
Expects that the next character is one of a fixed set of possible characters.
|
char |
expect(char token)
Expects that the next character is the given character and consumes it.
|
java.lang.String |
expression()
Gets the expression being parsed.
|
void |
increaseNestingLevel()
Increases the current nesting level of the parser.
|
int |
line()
Gets the current 1-based line number of the parser.
|
int |
nestingLevel()
Gets the current 0-based nesting level of the parser.
|
char |
peek()
Returns the current character of the expression, but does not consume it.
|
char |
peek(int offset)
Returns the current character of the expression +
offset
characters, but does not consume it. |
java.lang.String |
peekSingleCharForMessage()
Peeks the next character and returns [EOF] if the next character is past
the end of the expression.
|
int |
position()
Gets the current 0-based position of the parser.
|
void |
skip()
Skips a single character while tracking lines and columns.
|
java.lang.String |
sliceFrom(int start)
Gets a slice of the expression starting from the given 0-based
character position, read all the way through to the current
position of the parser.
|
void |
sp()
Skip 0 or more spaces (that is, ' ' and '\t').
|
java.lang.RuntimeException |
syntax(java.lang.String message)
Creates a syntax error that adds some context to the given message.
|
void |
ws()
Skip 0 or more whitespace characters (that is, ' ', '\t', '\r', and '\n').
|
public SimpleParser(java.lang.String expression)
expression
- Expression to parser.public SimpleParser(java.lang.String expression, int maxNestingLevel)
By default, no maximum parsing level is enforced. Setting the
maxParsingLevel
to 0 disables the enforcement of a
maximum parsing level.
expression
- Expression to parse that must not be null.maxNestingLevel
- The maximum allowed nesting level of the parser.public final java.lang.String expression()
public final int position()
public final int line()
public final int column()
public final boolean eof()
public final char peek()
public final char peek(int offset)
offset
characters, but does not consume it.
If the end of the expression is reached or if the peeked offset is
calculated to be less than 0, Character.MIN_VALUE
is returned
(that is, '\0').
offset
- The number of characters to peek ahead (positive or negative).public final char expect(char token)
token
- The character to expect.public final java.lang.String peekSingleCharForMessage()
public final char expect(char... tokens)
tokens
- Characters to expect.public java.lang.RuntimeException syntax(java.lang.String message)
message
- Message for why the error occurred.public void ws()
public void sp()
public void br()
java.lang.RuntimeException
- if the next non-space character is not EOF or a line break.public void skip()
public void consumeRemainingCharactersOnLine()
This method will also terminate when the end of the expression is encountered.
This method is useful, for example, for skipping the text of a
commented out line in an expression. If the contents of the skipped
line are required, then store the current position before invoking
this method using position()
, then call this method, then get
the contents of the skipped characters using sliceFrom(int)
.
public final java.lang.String sliceFrom(int start)
start
- Position to slice from, ending at the current position.start
to position
.public final int consumeUntilNoLongerMatches(java.util.function.Predicate<java.lang.Character> predicate)
predicate
matches each peeked character.predicate
- Predicate that filters characters.public final void increaseNestingLevel()
This method can be manually invoked when parsing in order to prevent parsing too deeply using recursive descent parsers.
java.lang.RuntimeException
- if the nesting level is deeper than the max allowed nesting.public final void decreaseNestingLevel()
public int nestingLevel()