Package com.google.protobuf
Class TextFormat.Tokenizer
- java.lang.Object
-
- com.google.protobuf.TextFormat.Tokenizer
-
- Enclosing class:
- TextFormat
private static final class TextFormat.Tokenizer extends java.lang.ObjectRepresents a stream of tokens parsed from aString.The Java standard library provides many classes that you might think would be useful for implementing this, but aren't. For example:
java.io.StreamTokenizer: This almost does what we want -- or, at least, something that would get us close to what we want -- except for one fatal flaw: It automatically un-escapes strings using Java escape sequences, which do not include all the escape sequences we need to support (e.g. '\x').java.util.Scanner: This seems like a great way at least to parse regular expressions out of a stream (so we wouldn't have to load the entire input into a single string before parsing). Sadly,Scannerrequires that tokens be delimited with some delimiter. Thus, although the text "foo:" should parse to two tokens ("foo" and ":"),Scannerwould recognize it only as a single token. Furthermore,Scannerprovides no way to inspect the contents of delimiters, making it impossible to keep track of line and column numbers.
Luckily, Java's regular expression support does manage to be useful to us. (Barely: We need
Matcher.usePattern(), which is new in Java 1.5.) So, we can use that, at least. Unfortunately, this implies that we need to have the entire input in one contiguous string.
-
-
Field Summary
Fields Modifier and Type Field Description private intcolumnprivate java.lang.StringcurrentTokenprivate static java.util.regex.PatternDOUBLE_INFINITYprivate static java.util.regex.PatternFLOAT_INFINITYprivate static java.util.regex.PatternFLOAT_NANprivate intlineprivate java.util.regex.Matchermatcherprivate intposprivate intpreviousColumnprivate intpreviousLineprivate java.lang.CharSequencetextprivate static java.util.regex.PatternTOKENprivate static java.util.regex.PatternWHITESPACE
-
Constructor Summary
Constructors Modifier Constructor Description privateTokenizer(java.lang.CharSequence text)Construct a tokenizer that parses tokens from the given text.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanatEnd()Are we at the end of the input?voidconsume(java.lang.String token)If the next token exactly matchestoken, consume it.booleanconsumeBoolean()If the next token is a boolean, consume it and return its value.ByteStringconsumeByteString()If the next token is a string, consume it, unescape it as aByteString, and return it.private voidconsumeByteString(java.util.List<ByteString> list)LikeconsumeByteString()but adds each token of the string to the given list.doubleconsumeDouble()If the next token is a double, consume it and return its value.floatconsumeFloat()If the next token is a float, consume it and return its value.java.lang.StringconsumeIdentifier()If the next token is an identifier, consume it and return its value.intconsumeInt32()If the next token is a 32-bit signed integer, consume it and return its value.longconsumeInt64()If the next token is a 64-bit signed integer, consume it and return its value.java.lang.StringconsumeString()If the next token is a string, consume it and return its (unescaped) value.intconsumeUInt32()If the next token is a 32-bit unsigned integer, consume it and return its value.longconsumeUInt64()If the next token is a 64-bit unsigned integer, consume it and return its value.private TextFormat.ParseExceptionfloatParseException(java.lang.NumberFormatException e)Constructs an appropriateTextFormat.ParseExceptionfor the givenNumberFormatExceptionwhen trying to parse a float or double.(package private) intgetColumn()(package private) intgetLine()(package private) intgetPreviousColumn()(package private) intgetPreviousLine()private TextFormat.ParseExceptionintegerParseException(java.lang.NumberFormatException e)Constructs an appropriateTextFormat.ParseExceptionfor the givenNumberFormatExceptionwhen trying to parse an integer.booleanlookingAt(java.lang.String text)Returnstrueif the current token's text is equal to that specified.booleanlookingAtInteger()Returnstrueif the next token is an integer, but does not consume it.voidnextToken()Advance to the next token.TextFormat.ParseExceptionparseException(java.lang.String description)Returns aTextFormat.ParseExceptionwith the current line and column numbers in the description, suitable for throwing.TextFormat.ParseExceptionparseExceptionPreviousToken(java.lang.String description)Returns aTextFormat.ParseExceptionwith the line and column numbers of the previous token in the description, suitable for throwing.private voidskipWhitespace()Skip over any whitespace so that the matcher region starts at the next token.booleantryConsume(java.lang.String token)If the next token exactly matchestoken, consume it and returntrue.booleantryConsumeDouble()If the next token is a double, consume it and returntrue.booleantryConsumeFloat()If the next token is a float, consume it and returntrue.booleantryConsumeIdentifier()If the next token is an identifier, consume it and returntrue.booleantryConsumeInt64()If the next token is a 64-bit signed integer, consume it and returntrue.booleantryConsumeString()If the next token is a string, consume it and return true.booleantryConsumeUInt64()If the next token is a 64-bit unsigned integer, consume it and returntrue.TextFormat.UnknownFieldParseExceptionunknownFieldParseExceptionPreviousToken(java.lang.String unknownField, java.lang.String description)Returns aTextFormat.UnknownFieldParseExceptionwith the line and column numbers of the previous token in the description, and the unknown field name, suitable for throwing.
-
-
-
Field Detail
-
text
private final java.lang.CharSequence text
-
matcher
private final java.util.regex.Matcher matcher
-
currentToken
private java.lang.String currentToken
-
pos
private int pos
-
line
private int line
-
column
private int column
-
previousLine
private int previousLine
-
previousColumn
private int previousColumn
-
WHITESPACE
private static final java.util.regex.Pattern WHITESPACE
-
TOKEN
private static final java.util.regex.Pattern TOKEN
-
DOUBLE_INFINITY
private static final java.util.regex.Pattern DOUBLE_INFINITY
-
FLOAT_INFINITY
private static final java.util.regex.Pattern FLOAT_INFINITY
-
FLOAT_NAN
private static final java.util.regex.Pattern FLOAT_NAN
-
-
Method Detail
-
getPreviousLine
int getPreviousLine()
-
getPreviousColumn
int getPreviousColumn()
-
getLine
int getLine()
-
getColumn
int getColumn()
-
atEnd
public boolean atEnd()
Are we at the end of the input?
-
nextToken
public void nextToken()
Advance to the next token.
-
skipWhitespace
private void skipWhitespace()
Skip over any whitespace so that the matcher region starts at the next token.
-
tryConsume
public boolean tryConsume(java.lang.String token)
If the next token exactly matchestoken, consume it and returntrue. Otherwise, returnfalsewithout doing anything.
-
consume
public void consume(java.lang.String token) throws TextFormat.ParseExceptionIf the next token exactly matchestoken, consume it. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
lookingAtInteger
public boolean lookingAtInteger()
Returnstrueif the next token is an integer, but does not consume it.
-
lookingAt
public boolean lookingAt(java.lang.String text)
Returnstrueif the current token's text is equal to that specified.
-
consumeIdentifier
public java.lang.String consumeIdentifier() throws TextFormat.ParseExceptionIf the next token is an identifier, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
tryConsumeIdentifier
public boolean tryConsumeIdentifier()
If the next token is an identifier, consume it and returntrue. Otherwise, returnfalsewithout doing anything.
-
consumeInt32
public int consumeInt32() throws TextFormat.ParseExceptionIf the next token is a 32-bit signed integer, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
consumeUInt32
public int consumeUInt32() throws TextFormat.ParseExceptionIf the next token is a 32-bit unsigned integer, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
consumeInt64
public long consumeInt64() throws TextFormat.ParseExceptionIf the next token is a 64-bit signed integer, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
tryConsumeInt64
public boolean tryConsumeInt64()
If the next token is a 64-bit signed integer, consume it and returntrue. Otherwise, returnfalsewithout doing anything.
-
consumeUInt64
public long consumeUInt64() throws TextFormat.ParseExceptionIf the next token is a 64-bit unsigned integer, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
tryConsumeUInt64
public boolean tryConsumeUInt64()
If the next token is a 64-bit unsigned integer, consume it and returntrue. Otherwise, returnfalsewithout doing anything.
-
consumeDouble
public double consumeDouble() throws TextFormat.ParseExceptionIf the next token is a double, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
tryConsumeDouble
public boolean tryConsumeDouble()
If the next token is a double, consume it and returntrue. Otherwise, returnfalsewithout doing anything.
-
consumeFloat
public float consumeFloat() throws TextFormat.ParseExceptionIf the next token is a float, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
tryConsumeFloat
public boolean tryConsumeFloat()
If the next token is a float, consume it and returntrue. Otherwise, returnfalsewithout doing anything.
-
consumeBoolean
public boolean consumeBoolean() throws TextFormat.ParseExceptionIf the next token is a boolean, consume it and return its value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
consumeString
public java.lang.String consumeString() throws TextFormat.ParseExceptionIf the next token is a string, consume it and return its (unescaped) value. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
tryConsumeString
public boolean tryConsumeString()
If the next token is a string, consume it and return true. Otherwise, return false.
-
consumeByteString
public ByteString consumeByteString() throws TextFormat.ParseException
If the next token is a string, consume it, unescape it as aByteString, and return it. Otherwise, throw aTextFormat.ParseException.- Throws:
TextFormat.ParseException
-
consumeByteString
private void consumeByteString(java.util.List<ByteString> list) throws TextFormat.ParseException
LikeconsumeByteString()but adds each token of the string to the given list. String literals (whether bytes or text) may come in multiple adjacent tokens which are automatically concatenated, like in C or Python.- Throws:
TextFormat.ParseException
-
parseException
public TextFormat.ParseException parseException(java.lang.String description)
Returns aTextFormat.ParseExceptionwith the current line and column numbers in the description, suitable for throwing.
-
parseExceptionPreviousToken
public TextFormat.ParseException parseExceptionPreviousToken(java.lang.String description)
Returns aTextFormat.ParseExceptionwith the line and column numbers of the previous token in the description, suitable for throwing.
-
integerParseException
private TextFormat.ParseException integerParseException(java.lang.NumberFormatException e)
Constructs an appropriateTextFormat.ParseExceptionfor the givenNumberFormatExceptionwhen trying to parse an integer.
-
floatParseException
private TextFormat.ParseException floatParseException(java.lang.NumberFormatException e)
Constructs an appropriateTextFormat.ParseExceptionfor the givenNumberFormatExceptionwhen trying to parse a float or double.
-
unknownFieldParseExceptionPreviousToken
public TextFormat.UnknownFieldParseException unknownFieldParseExceptionPreviousToken(java.lang.String unknownField, java.lang.String description)
Returns aTextFormat.UnknownFieldParseExceptionwith the line and column numbers of the previous token in the description, and the unknown field name, suitable for throwing.
-
-