Package net.fauxpark.stringes
Class StringeReader
- java.lang.Object
-
- net.fauxpark.stringes.StringeReader
-
public class StringeReader extends Object
Represents a reader that can read data from a Stringe.
-
-
Constructor Summary
Constructors Constructor Description StringeReader(String str)
Constructs a new StringeReader using the specified string as input.StringeReader(Stringe stre)
Constructs a new StringeReader using the specified Stringe as input.
-
Method Summary
Modifier and Type Method Description boolean
atEndOfStringe()
Indicates whether the reader position is at the end of the input string.boolean
eat(char c)
Indicates whether the specified character occurs at the reader's current position.boolean
eat(String str)
Indicates whether the specified string occurs at the reader's current position.boolean
eat(Pattern regex)
Indicates whether the specified regular expression matches the input at the reader's current position.boolean
eat(Pattern regex, Stringe result)
Indicates whether the specified regular expression matches the input at the reader's current position.boolean
eatAll(char c)
Indicates whether the specified character occurs at the reader's current position.boolean
eatAll(char... cs)
Indicates whether any of the specified characters occur at the reader's current position.boolean
eatAll(String str)
Indicates whether the specified string occurs at the reader's current position.boolean
eatAny(char... cs)
Indicates whether any of the specified characters occurs at the reader's current position.boolean
eatChareWhile(Function<Chare,Boolean> predicate)
Indicates whether the specified predicate matches the Chare at the reader's current position.boolean
eatExactlyWhere(Function<Character,Boolean> predicate, int count)
Indicates whether the specified predicate matches the specified number of characters at the reader's current position.boolean
eatWhile(Function<Character,Boolean> predicate)
Indicates whether the specified predicate matches the character at the reader's current position.int
getLength()
Returns the total length, in characters, of the Stringe being read.int
getPosition()
Returns the current zero-based position of the reader.boolean
isNext(char c)
Indicates whether the specified character occurs at the reader's current position.boolean
isNext(char... cs)
Indicates whether any of the specified characters occur at the reader's current position.boolean
isNext(String str)
Indicates whether the specified string occurs at the reader's current position.boolean
isNext(String str, boolean ignoreCase)
Indicates whether the specified string occurs at the reader's current position.boolean
isNext(Pattern regex)
Indicates whether the specified regular expression matches the input at the reader's current position.boolean
isNext(Pattern regex, Stringe result)
Indicates whether the specified regular expression matches the input at the reader's current position, and outputs the result.int
peekChar()
Reads the next character in the input, but does not consume it.Chare
peekChare()
Reads the next Chare in the input, but does not consume it.Chare
readChare()
Reads the next Chare from the input and consumes it.Stringe
readStringe(int length)
Reads a Stringe from the input and advances the position by the number of characters read.Stringe
readStringeUntil(char c)
Reads a Stringe from the input and advances the position to the next occurrence of the specified character.Stringe
readStringeUntilAny(char... cs)
Reads a Stringe from the input and advances the position to the next occurrence of any of the specified characters.<T,U>
UreadToken(Lexer<T> rules, BiFunction<Stringe,T,U> tokenEmitter)
Reads the next token from the current position, then advances the position past it.void
setPosition(int newPos)
Sets the position of the reader.boolean
skipWhitespace()
Advances the reader's position past any immediate white space.boolean
wasLast(char c)
Indicates whether the specified character matches the input before the reader's current position.boolean
wasLast(char... cs)
Indicates whether any of the specified characters match the input before the reader's current position.boolean
wasLast(String str)
Indicates whether the specified string matches the input before the reader's current position.boolean
wasLast(String str, boolean ignoreCase)
Indicates whether the specified string matches the input before the reader's current position.
-
-
-
Constructor Detail
-
StringeReader
public StringeReader(String str)
Constructs a new StringeReader using the specified string as input.- Parameters:
str
- The string to use as input. This will be converted to a root-level Stringe.
-
StringeReader
public StringeReader(Stringe stre)
Constructs a new StringeReader using the specified Stringe as input.- Parameters:
stre
- The Stringe to use as input.
-
-
Method Detail
-
atEndOfStringe
public boolean atEndOfStringe()
Indicates whether the reader position is at the end of the input string.- Returns:
- True if no more Chares can be read.
-
readChare
public Chare readChare()
Reads the next Chare from the input and consumes it.- Returns:
- The Chare that was just read.
-
peekChare
public Chare peekChare()
Reads the next Chare in the input, but does not consume it.- Returns:
- The Chare at the next position, or null if no more Chares can be read.
-
peekChar
public int peekChar()
Reads the next character in the input, but does not consume it.- Returns:
- The char at the next position, or -1 if no more characters can be read.
-
readStringe
public Stringe readStringe(int length)
Reads a Stringe from the input and advances the position by the number of characters read.- Parameters:
length
- The number of characters to read.- Returns:
- A Stringe containing the characters that have been read.
-
readStringeUntil
public Stringe readStringeUntil(char c)
Reads a Stringe from the input and advances the position to the next occurrence of the specified character. If no match is found, it reads to the end.- Parameters:
c
- The character to stop at.- Returns:
- A Stringe containing the characters that have been read.
-
readStringeUntilAny
public Stringe readStringeUntilAny(char... cs)
Reads a Stringe from the input and advances the position to the next occurrence of any of the specified characters. If no match is found, it reads to the end.- Parameters:
cs
- The characters to stop at.- Returns:
- A Stringe containing the characters that have been read.
-
eat
public boolean eat(char c)
Indicates whether the specified character occurs at the reader's current position. If a match is found, the reader consumes it.- Parameters:
c
- The character to test for.- Returns:
- True if the character was present.
-
eatExactlyWhere
public boolean eatExactlyWhere(Function<Character,Boolean> predicate, int count)
Indicates whether the specified predicate matches the specified number of characters at the reader's current position. If and only if the function returns true every time, the reader consumes them.- Parameters:
predicate
- The function to read the characters with.count
- The number of times to test.- Returns:
- True if the predicate applied the given number of times.
-
eatAll
public boolean eatAll(char c)
Indicates whether the specified character occurs at the reader's current position. If a match is found, the reader consumes it and any following matches.- Parameters:
c
- The character to test for.- Returns:
- True if the given character was present at least once.
-
eatAny
public boolean eatAny(char... cs)
Indicates whether any of the specified characters occurs at the reader's current position. If a match is found, the reader consumes it.- Parameters:
cs
- The characters to test for.- Returns:
- True if any of the given characters were present.
-
eatAll
public boolean eatAll(char... cs)
Indicates whether any of the specified characters occur at the reader's current position. If a match is found, the reader consumes it and any following matches.- Parameters:
cs
- The characters to test for.- Returns:
- True if any of the given characters were present at least once.
-
eatWhile
public boolean eatWhile(Function<Character,Boolean> predicate)
Indicates whether the specified predicate matches the character at the reader's current position. While the function returns true, the reader consumes it and any following matching characters.- Parameters:
predicate
- The function to read the character with.- Returns:
- True if the predicate applied at least once.
-
eatChareWhile
public boolean eatChareWhile(Function<Chare,Boolean> predicate)
Indicates whether the specified predicate matches the Chare at the reader's current position. While the function returns true, the reader consumes it and any following matching Chares.- Parameters:
predicate
- The function to read the Chare with.- Returns:
- True if the predicate applied at least once.
-
eat
public boolean eat(String str)
Indicates whether the specified string occurs at the reader's current position. If a match is found, the reader consumes it.- Parameters:
str
- The string to test for.- Returns:
- True if the given string was present.
-
eatAll
public boolean eatAll(String str)
Indicates whether the specified string occurs at the reader's current position. If a match is found, the reader consumes it and any following matching strings.- Parameters:
str
- The string to test for.- Returns:
- True if the given string was present at least once.
-
eat
public boolean eat(Pattern regex) throws IllegalArgumentException
Indicates whether the specified regular expression matches the input at the reader's current position. If a match is found, the reader consumes it.- Parameters:
regex
- The regular expression to test for.- Returns:
- True if the given regex matched.
- Throws:
IllegalArgumentException
- If the regex is null.
-
eat
public boolean eat(Pattern regex, Stringe result) throws IllegalArgumentException
Indicates whether the specified regular expression matches the input at the reader's current position. If a match is found, the reader consumes it and outputs the result.- Parameters:
regex
- The regular expression to test for.result
- The Stringe to output the result to.- Returns:
- True if the given regex matched.
- Throws:
IllegalArgumentException
- If the regex is null.
-
isNext
public boolean isNext(char c)
Indicates whether the specified character occurs at the reader's current position.- Parameters:
c
- The character to test for.- Returns:
- True if the given character is next.
-
isNext
public boolean isNext(char... cs)
Indicates whether any of the specified characters occur at the reader's current position.- Parameters:
cs
- The characters to test for.- Returns:
- True if any of the given characters are next.
-
isNext
public boolean isNext(String str)
Indicates whether the specified string occurs at the reader's current position.- Parameters:
str
- The string to test for.- Returns:
- True if the given string is next.
-
isNext
public boolean isNext(String str, boolean ignoreCase)
Indicates whether the specified string occurs at the reader's current position.- Parameters:
str
- The string to test for.ignoreCase
- Whether to ignore case considerations.- Returns:
- True if the given string is next.
-
isNext
public boolean isNext(Pattern regex) throws IllegalArgumentException
Indicates whether the specified regular expression matches the input at the reader's current position.- Parameters:
regex
- The regular expression to test for.- Returns:
- True if the given regex is next.
- Throws:
IllegalArgumentException
- If the regex is null.
-
isNext
public boolean isNext(Pattern regex, Stringe result) throws IllegalArgumentException
Indicates whether the specified regular expression matches the input at the reader's current position, and outputs the result.- Parameters:
regex
- The regular expression to test for.result
- The Stringe to output the result to.- Returns:
- True if the given regex is next.
- Throws:
IllegalArgumentException
- If the regex is null.
-
skipWhitespace
public boolean skipWhitespace()
Advances the reader's position past any immediate white space.- Returns:
- True if the reader advanced.
-
wasLast
public boolean wasLast(char c)
Indicates whether the specified character matches the input before the reader's current position.- Parameters:
c
- The character to test for.- Returns:
- True if the given character was last.
-
wasLast
public boolean wasLast(char... cs)
Indicates whether any of the specified characters match the input before the reader's current position.- Parameters:
cs
- The characters to test for.- Returns:
- True if any of the given characters were last.
-
wasLast
public boolean wasLast(String str)
Indicates whether the specified string matches the input before the reader's current position.- Parameters:
str
- The string to test for.- Returns:
- True if the given string was last.
-
wasLast
public boolean wasLast(String str, boolean ignoreCase)
Indicates whether the specified string matches the input before the reader's current position.- Parameters:
str
- The string to test for.ignoreCase
- Whether to ignore case considerations.- Returns:
- True if the given string was last.
-
readToken
public <T,U> U readToken(Lexer<T> rules, BiFunction<Stringe,T,U> tokenEmitter) throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException
Reads the next token from the current position, then advances the position past it.- Type Parameters:
T
- The token identifier type to use.U
- The token class to use.- Parameters:
rules
- The lexer to use.tokenEmitter
- The callback that creates the returned token.- Returns:
- The token that has been read.
- Throws:
IllegalArgumentException
- If the token emitter is null.IllegalStateException
- If the end of the Stringe is reached earlier than expected.UnsupportedOperationException
- If there is no undefined capture rule and an invalid token is found.
-
getPosition
public int getPosition()
Returns the current zero-based position of the reader.- Returns:
- The reader's current position.
-
setPosition
public void setPosition(int newPos) throws StringIndexOutOfBoundsException
Sets the position of the reader.- Parameters:
newPos
- The new position of the reader.- Throws:
StringIndexOutOfBoundsException
- If the new position is negative or greater than the stringe's length.
-
getLength
public int getLength()
Returns the total length, in characters, of the Stringe being read.- Returns:
- The length of the Stringe being read.
-
-