Top Description Inners Fields Constructors Methods
com.squareup.protoparser

public final Class ProtoParser

extends Object
Class Inheritance
Imports
com.google.auto.value.AutoValue, com.squareup.protoparser.DataType.MapType, .DataType.NamedType, .DataType.ScalarType, java.io.CharArrayWriter, .File, .FileInputStream, .IOException, .InputStream, .InputStreamReader, .Reader, java.nio.file.Files, .Path, java.util.ArrayList, .LinkedHashMap, .List, .Locale, .Map

Basic parser for .proto schema declarations.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
pack-priv abstract static class

Field Summary

Modifier and TypeField and Description
private final char[]
private final ProtoFile.Builder
private final String
private int
line

The number of newline characters encountered thus far.

private int
lineStart

The index of the most recent newline character.

private String
packageName

Output package name, or null if none yet encountered.

private int
pos

Our cursor within the document.

private String
prefix

The current package name + nested type names, separated by dots.

Constructor Summary

AccessConstructor and Description
pack-priv
ProtoParser(String filePath, char[] data)

Method Summary

Modifier and TypeMethod and Description
private void
addToList(List<Object> list, Object value)

Adds an object or objects to a List.

private int
private int
hexDigit(char c)

private int
line()

private void
newline()

Call this every time a '\n' is encountered.

public static ProtoFile
parse(String name, Reader reader)

Parse a named .proto schema.

public static ProtoFile
parse(String name, String data)

Parse a named .proto schema.

public static ProtoFile
parseUtf8(File file)

Parse a .proto definition file.

public static ProtoFile
parseUtf8(Path path)

Parse a .proto definition file.

public static ProtoFile
parseUtf8(String name, InputStream is)

Parse a named .proto schema.

private char
peekChar()

Peeks a non-whitespace character and returns it.

private char
readChar()

Reads a non-whitespace character and returns it.

private String
readComment()

Reads a comment and returns its body.

private DataType
readDataType()

Reads a scalar, map, or type name.

private Object
readDeclaration(String documentation, ProtoParser.Context context)

private String
readDocumentation()

Like skipWhitespace, but this returns a string containing all comment text.

private EnumElement
readEnumElement(String documentation)

Reads an enumerated type declaration and returns it.

private ExtendElement
readExtend(String documentation)

Reads an extend declaration.

private ExtensionsElement
readExtensions(String documentation)

Reads extensions like "extensions 101;" or "extensions 101 to max;".

private FieldElement
readField(String documentation, FieldElement.Label label)

Reads an field declaration and returns it.

private int
readInt()

Reads an integer and returns it.

private ProtoParser.OptionKindAndValue
readKindAndValue()

Reads a value that can be a map, list, string, number, boolean or enum.

private List<Object>
readList()

Returns a list of values.

private Map<String, Object>
readMap(char openBrace, char closeBrace, char keyValueSeparator)

Returns a map of string keys and values.

private MessageElement
readMessage(String documentation)

Reads a message declaration.

private String
readName()

Reads a (paren-wrapped), [square-wrapped] or naked symbol name.

private char
readNumericEscape(int radix, int len)

private OneOfElement
readOneOf(String documentation)

private OptionElement
readOption(char keyValueSeparator)

Reads a option containing a name, an '=' or ':', and a value.

pack-priv ProtoFile
private String
private RpcElement
readRpc(String documentation)

Reads an rpc and returns it.

private ServiceElement
readService(String documentation)

Reads a service declaration and returns it.

private String
readString()

Reads a quoted or unquoted string and returns it.

private String
readWord()

Reads a non-empty word and returns it.

private void
skipWhitespace(boolean skipComments)

Skips whitespace characters and optionally comments.

private String
private RuntimeException
unexpected(String message)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databack to summary
private final char[] data
fileBuilderback to summary
private final ProtoFile.Builder fileBuilder
filePathback to summary
private final String filePath
lineback to summary
private int line

The number of newline characters encountered thus far.

lineStartback to summary
private int lineStart

The index of the most recent newline character.

packageNameback to summary
private String packageName

Output package name, or null if none yet encountered.

posback to summary
private int pos

Our cursor within the document. data[pos] is the next character to be read.

prefixback to summary
private String prefix

The current package name + nested type names, separated by dots.

Constructor Detail

ProtoParserback to summary
pack-priv ProtoParser(String filePath, char[] data)

Method Detail

addToListback to summary
private void addToList(List<Object> list, Object value)

Adds an object or objects to a List.

columnback to summary
private int column()
hexDigitback to summary
private int hexDigit(char c)
lineback to summary
private int line()
newlineback to summary
private void newline()

Call this every time a '\n' is encountered.

parseback to summary
public static ProtoFile parse(String name, Reader reader) throws IOException

Parse a named .proto schema. The Reader is not closed.

parseback to summary
public static ProtoFile parse(String name, String data)

Parse a named .proto schema.

parseUtf8back to summary
public static ProtoFile parseUtf8(File file) throws IOException

Parse a .proto definition file.

parseUtf8back to summary
public static ProtoFile parseUtf8(Path path) throws IOException

Parse a .proto definition file.

parseUtf8back to summary
public static ProtoFile parseUtf8(String name, InputStream is) throws IOException

Parse a named .proto schema. The InputStream is not closed.

peekCharback to summary
private char peekChar()

Peeks a non-whitespace character and returns it. The only difference between this and readChar is that this doesn't consume the char.

readCharback to summary
private char readChar()

Reads a non-whitespace character and returns it.

readCommentback to summary
private String readComment()

Reads a comment and returns its body.

readDataTypeback to summary
private DataType readDataType()

Reads a scalar, map, or type name.

readDeclarationback to summary
private Object readDeclaration(String documentation, ProtoParser.Context context)
readDocumentationback to summary
private String readDocumentation()

Like skipWhitespace, but this returns a string containing all comment text. By convention, comments before a declaration document that declaration.

readEnumElementback to summary
private EnumElement readEnumElement(String documentation)

Reads an enumerated type declaration and returns it.

readExtendback to summary
private ExtendElement readExtend(String documentation)

Reads an extend declaration.

readExtensionsback to summary
private ExtensionsElement readExtensions(String documentation)

Reads extensions like "extensions 101;" or "extensions 101 to max;".

readFieldback to summary
private FieldElement readField(String documentation, FieldElement.Label label)

Reads an field declaration and returns it.

readIntback to summary
private int readInt()

Reads an integer and returns it.

readKindAndValueback to summary
private ProtoParser.OptionKindAndValue readKindAndValue()

Reads a value that can be a map, list, string, number, boolean or enum.

readListback to summary
private List<Object> readList()

Returns a list of values. This is similar to JSON with '[' and ']' surrounding the list and ',' separating values.

readMapback to summary
private Map<String, 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.

Annotations
@SuppressWarnings:unchecked
readMessageback to summary
private MessageElement readMessage(String documentation)

Reads a message declaration.

readNameback to summary
private String readName()

Reads a (paren-wrapped), [square-wrapped] or naked symbol name.

readNumericEscapeback to summary
private char readNumericEscape(int radix, int len)
readOneOfback to summary
private OneOfElement readOneOf(String documentation)
readOptionback to summary
private OptionElement readOption(char keyValueSeparator)

Reads a option containing a name, an '=' or ':', and a value.

readProtoFileback to summary
pack-priv ProtoFile readProtoFile()
readQuotedStringback to summary
private String readQuotedString()
readRpcback to summary
private RpcElement readRpc(String documentation)

Reads an rpc and returns it.

readServiceback to summary
private ServiceElement readService(String documentation)

Reads a service declaration and returns it.

readStringback to summary
private String readString()

Reads a quoted or unquoted string and returns it.

readWordback to summary
private String readWord()

Reads a non-empty word and returns it.

skipWhitespaceback to summary
private void skipWhitespace(boolean skipComments)

Skips whitespace characters and optionally comments. When this returns, either pos == data.length or a non-whitespace character.

tryAppendTrailingDocumentationback to summary
private String tryAppendTrailingDocumentation(String documentation)
unexpectedback to summary
private RuntimeException unexpected(String message)
com.squareup.protoparser back to summary

pack-priv final Enum ProtoParser.Context

extends Enum<ProtoParser.Context>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ProtoParser.Context
public static final ProtoParser.Context
public static final ProtoParser.Context
public static final ProtoParser.Context
public static final ProtoParser.Context
public static final ProtoParser.Context

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
public boolean
public boolean
public boolean
public boolean
public boolean
public boolean
public static ProtoParser.Context
public static ProtoParser.Context[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ENUMback to summary
public static final ProtoParser.Context ENUM
EXTENDback to summary
public static final ProtoParser.Context EXTEND
FILEback to summary
public static final ProtoParser.Context FILE
MESSAGEback to summary
public static final ProtoParser.Context MESSAGE
RPCback to summary
public static final ProtoParser.Context RPC
SERVICEback to summary
public static final ProtoParser.Context SERVICE

Constructor Detail

Contextback to summary
private Context()

Method Detail

permitsExtensionsback to summary
public boolean permitsExtensions()
permitsFieldback to summary
public boolean permitsField()
permitsImportback to summary
public boolean permitsImport()
permitsOneOfback to summary
public boolean permitsOneOf()
permitsPackageback to summary
public boolean permitsPackage()
permitsRpcback to summary
public boolean permitsRpc()
permitsSyntaxback to summary
public boolean permitsSyntax()
valueOfback to summary
public static ProtoParser.Context valueOf(String name)
valuesback to summary
public static ProtoParser.Context[] values()
com.squareup.protoparser back to summary

pack-priv abstract Class ProtoParser.OptionKindAndValue

extends Object
Class Inheritance
Annotations
@AutoValue

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract OptionElement.Kind
kind()

pack-priv static ProtoParser.OptionKindAndValue
of(OptionElement.Kind kind, Object value)

pack-priv abstract Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

OptionKindAndValueback to summary
pack-priv OptionKindAndValue()

Method Detail

kindback to summary
pack-priv abstract OptionElement.Kind kind()
ofback to summary
pack-priv static ProtoParser.OptionKindAndValue of(OptionElement.Kind kind, Object value)
valueback to summary
pack-priv abstract Object value()