Package com.squareup.protoparser
Class ProtoSchemaParser
- java.lang.Object
-
- com.squareup.protoparser.ProtoSchemaParser
-
public final class ProtoSchemaParser extends java.lang.ObjectBasic parser for.protoschema declarations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classProtoSchemaParser.Context
-
Field Summary
Fields Modifier and Type Field Description private char[]dataThe entire document.private java.util.List<java.lang.String>dependenciesImported files.private java.util.List<ExtendDeclaration>extendDeclarationsDeclared 'extend's.private java.lang.StringfileNameThe path to the.protofile.private intlineThe number of newline characters encountered thus far.private intlineStartThe index of the most recent newline character.private java.util.List<Option>optionsGlobal options.private java.lang.StringpackageNameOutput package name, or null if none yet encountered.private intposOur cursor within the document.private java.lang.StringprefixThe current package name + nested type names, separated by dots.private java.util.List<java.lang.String>publicDependenciesPublic imported files.private java.util.List<Service>servicesDeclared services.private java.util.List<Type>typesDeclared message types and enum types.
-
Constructor Summary
Constructors Constructor Description ProtoSchemaParser(java.lang.String fileName, char[] data)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddToList(java.util.List<java.lang.Object> list, java.lang.Object value)Adds an object or objects to a List.private intcolumn()private static char[]fileToCharArray(java.io.File file)private inthexDigit(char c)private intline()private voidnewline()Call this every time a '\n' is encountered.static ProtoFileparse(java.io.File file)Parse a.protodefinition file.static ProtoFileparse(java.lang.String name, java.io.Reader reader)Parse a named.protoschema.static ProtoFileparse(java.lang.String name, java.lang.String data)Parse a named.protoschema.static ProtoFileparseUtf8(java.lang.String name, java.io.InputStream is)Parse a named.protoschema.private charpeekChar()Peeks a non-whitespace character and returns it.private charreadChar()Reads a non-whitespace character and returns it.private java.lang.StringreadComment()Reads a comment and returns its body.private java.lang.ObjectreadDeclaration(java.lang.String documentation, ProtoSchemaParser.Context context)private java.lang.StringreadDocumentation()LikeskipWhitespace(boolean), but this returns a string containing all comment text.private EnumTypereadEnumType(java.lang.String documentation)Reads an enumerated type declaration and returns it.private static char[]readerToCharArray(java.io.Reader reader)private ExtendDeclarationreadExtend(java.lang.String documentation)Reads an extend declaration.private ExtensionsreadExtensions(java.lang.String documentation)Reads extensions like "extensions 101;" or "extensions 101 to max;".private MessageType.FieldreadField(java.lang.String documentation, java.lang.String label)Reads an field declaration and returns it.private intreadInt()Reads an integer and returns it.private java.util.List<java.lang.Object>readList()Returns a list of values.private java.util.Map<java.lang.String,java.lang.Object>readMap(char openBrace, char closeBrace, char keyValueSeparator)Returns a map of string keys and values.private MessageTypereadMessage(java.lang.String documentation)Reads a message declaration.private java.lang.StringreadName()Reads a (paren-wrapped), [square-wrapped] or naked symbol name.private charreadNumericEscape(int radix, int len)private OptionreadOption(char keyValueSeparator)Reads a option containing a name, an '=' or ':', and a value.(package private) ProtoFilereadProtoFile()private java.lang.StringreadQuotedString()private Service.MethodreadRpc(java.lang.String documentation)Reads an rpc method and returns it.private ServicereadService(java.lang.String documentation)Reads a service declaration and returns it.private java.lang.StringreadString()Reads a quoted or unquoted string and returns it.private java.lang.ObjectreadValue()Reads a value that can be a map, list, string, number, boolean or enum.private java.lang.StringreadWord()Reads a non-empty word and returns it.private voidskipWhitespace(boolean skipComments)Skips whitespace characters and optionally comments.private static char[]streamToCharArray(java.io.InputStream is)private java.lang.RuntimeExceptionunexpected(java.lang.String message)
-
-
-
Field Detail
-
fileName
private final java.lang.String fileName
The path to the.protofile.
-
data
private final char[] data
The entire document.
-
pos
private int pos
Our cursor within the document.data[pos]is the next character to be read.
-
line
private int line
The number of newline characters encountered thus far.
-
lineStart
private int lineStart
The index of the most recent newline character.
-
packageName
private java.lang.String packageName
Output package name, or null if none yet encountered.
-
prefix
private java.lang.String prefix
The current package name + nested type names, separated by dots.
-
dependencies
private final java.util.List<java.lang.String> dependencies
Imported files.
-
publicDependencies
private final java.util.List<java.lang.String> publicDependencies
Public imported files.
-
types
private final java.util.List<Type> types
Declared message types and enum types.
-
services
private final java.util.List<Service> services
Declared services.
-
extendDeclarations
private final java.util.List<ExtendDeclaration> extendDeclarations
Declared 'extend's.
-
options
private final java.util.List<Option> options
Global options.
-
-
Method Detail
-
parse
public static ProtoFile parse(java.io.File file) throws java.io.IOException
Parse a.protodefinition file.- Throws:
java.io.IOException
-
parseUtf8
public static ProtoFile parseUtf8(java.lang.String name, java.io.InputStream is) throws java.io.IOException
Parse a named.protoschema. TheInputStreamis not closed.- Throws:
java.io.IOException
-
parse
public static ProtoFile parse(java.lang.String name, java.io.Reader reader) throws java.io.IOException
Parse a named.protoschema. TheReaderis not closed.- Throws:
java.io.IOException
-
parse
public static ProtoFile parse(java.lang.String name, java.lang.String data)
Parse a named.protoschema.
-
fileToCharArray
private static char[] fileToCharArray(java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
streamToCharArray
private static char[] streamToCharArray(java.io.InputStream is) throws java.io.IOException- Throws:
java.io.IOException
-
readerToCharArray
private static char[] readerToCharArray(java.io.Reader reader) throws java.io.IOException- Throws:
java.io.IOException
-
readProtoFile
ProtoFile readProtoFile()
-
readDeclaration
private java.lang.Object readDeclaration(java.lang.String documentation, ProtoSchemaParser.Context context)
-
readMessage
private MessageType readMessage(java.lang.String documentation)
Reads a message declaration.
-
readExtend
private ExtendDeclaration readExtend(java.lang.String documentation)
Reads an extend declaration.
-
readService
private Service readService(java.lang.String documentation)
Reads a service declaration and returns it.
-
readEnumType
private EnumType readEnumType(java.lang.String documentation)
Reads an enumerated type declaration and returns it.
-
readField
private MessageType.Field readField(java.lang.String documentation, java.lang.String label)
Reads an field declaration and returns it.
-
readExtensions
private Extensions readExtensions(java.lang.String documentation)
Reads extensions like "extensions 101;" or "extensions 101 to max;".
-
readOption
private Option readOption(char keyValueSeparator)
Reads a option containing a name, an '=' or ':', and a value.
-
readValue
private java.lang.Object readValue()
Reads a value that can be a map, list, string, number, boolean or enum.
-
readMap
private java.util.Map<java.lang.String,java.lang.Object> readMap(char openBrace, char closeBrace, char keyValueSeparator)Returns a map of string keys and values. This is similar to a JSON object, with '{' and '}' surrounding the map, ':' separating keys from values, and ',' separating entries.
-
addToList
private void addToList(java.util.List<java.lang.Object> list, java.lang.Object value)Adds an object or objects to a List.
-
readList
private java.util.List<java.lang.Object> readList()
Returns a list of values. This is similar to JSON with '[' and ']' surrounding the list and ',' separating values.
-
readRpc
private Service.Method readRpc(java.lang.String documentation)
Reads an rpc method and returns it.
-
readChar
private char readChar()
Reads a non-whitespace character and returns it.
-
peekChar
private char peekChar()
Peeks a non-whitespace character and returns it. The only difference between this andreadCharis that this doesn't consume the char.
-
readString
private java.lang.String readString()
Reads a quoted or unquoted string and returns it.
-
readQuotedString
private java.lang.String readQuotedString()
-
readNumericEscape
private char readNumericEscape(int radix, int len)
-
hexDigit
private int hexDigit(char c)
-
readName
private java.lang.String readName()
Reads a (paren-wrapped), [square-wrapped] or naked symbol name.
-
readWord
private java.lang.String readWord()
Reads a non-empty word and returns it.
-
readInt
private int readInt()
Reads an integer and returns it.
-
readDocumentation
private java.lang.String readDocumentation()
LikeskipWhitespace(boolean), but this returns a string containing all comment text. By convention, comments before a declaration document that declaration.
-
readComment
private java.lang.String readComment()
Reads a comment and returns its body.
-
skipWhitespace
private void skipWhitespace(boolean skipComments)
Skips whitespace characters and optionally comments. When this returns, eitherpos == data.lengthor a non-whitespace character.
-
newline
private void newline()
Call this every time a '\n' is encountered.
-
column
private int column()
-
line
private int line()
-
unexpected
private java.lang.RuntimeException unexpected(java.lang.String message)
-
-