Class ProtoSchemaParser


  • public final class ProtoSchemaParser
    extends java.lang.Object
    Basic parser for .proto schema declarations.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  ProtoSchemaParser.Context  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private char[] data
      The entire document.
      private java.util.List<java.lang.String> dependencies
      Imported files.
      private java.util.List<ExtendDeclaration> extendDeclarations
      Declared 'extend's.
      private java.lang.String fileName
      The path to the .proto file.
      private int line
      The number of newline characters encountered thus far.
      private int lineStart
      The index of the most recent newline character.
      private java.util.List<Option> options
      Global options.
      private java.lang.String packageName
      Output package name, or null if none yet encountered.
      private int pos
      Our cursor within the document.
      private java.lang.String prefix
      The current package name + nested type names, separated by dots.
      private java.util.List<java.lang.String> publicDependencies
      Public imported files.
      private java.util.List<Service> services
      Declared services.
      private java.util.List<Type> types
      Declared 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 void addToList​(java.util.List<java.lang.Object> list, java.lang.Object value)
      Adds an object or objects to a List.
      private int column()  
      private static char[] fileToCharArray​(java.io.File file)  
      private int hexDigit​(char c)  
      private int line()  
      private void newline()
      Call this every time a '\n' is encountered.
      static ProtoFile parse​(java.io.File file)
      Parse a .proto definition file.
      static ProtoFile parse​(java.lang.String name, java.io.Reader reader)
      Parse a named .proto schema.
      static ProtoFile parse​(java.lang.String name, java.lang.String data)
      Parse a named .proto schema.
      static ProtoFile parseUtf8​(java.lang.String name, java.io.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 java.lang.String readComment()
      Reads a comment and returns its body.
      private java.lang.Object readDeclaration​(java.lang.String documentation, ProtoSchemaParser.Context context)  
      private java.lang.String readDocumentation()
      Like skipWhitespace(boolean), but this returns a string containing all comment text.
      private EnumType readEnumType​(java.lang.String documentation)
      Reads an enumerated type declaration and returns it.
      private static char[] readerToCharArray​(java.io.Reader reader)  
      private ExtendDeclaration readExtend​(java.lang.String documentation)
      Reads an extend declaration.
      private Extensions readExtensions​(java.lang.String documentation)
      Reads extensions like "extensions 101;" or "extensions 101 to max;".
      private MessageType.Field readField​(java.lang.String documentation, java.lang.String label)
      Reads an field declaration and returns it.
      private int readInt()
      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 MessageType readMessage​(java.lang.String documentation)
      Reads a message declaration.
      private java.lang.String readName()
      Reads a (paren-wrapped), [square-wrapped] or naked symbol name.
      private char readNumericEscape​(int radix, int len)  
      private Option readOption​(char keyValueSeparator)
      Reads a option containing a name, an '=' or ':', and a value.
      (package private) ProtoFile readProtoFile()  
      private java.lang.String readQuotedString()  
      private Service.Method readRpc​(java.lang.String documentation)
      Reads an rpc method and returns it.
      private Service readService​(java.lang.String documentation)
      Reads a service declaration and returns it.
      private java.lang.String readString()
      Reads a quoted or unquoted string and returns it.
      private java.lang.Object readValue()
      Reads a value that can be a map, list, string, number, boolean or enum.
      private java.lang.String readWord()
      Reads a non-empty word and returns it.
      private void skipWhitespace​(boolean skipComments)
      Skips whitespace characters and optionally comments.
      private static char[] streamToCharArray​(java.io.InputStream is)  
      private java.lang.RuntimeException unexpected​(java.lang.String message)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • fileName

        private final java.lang.String fileName
        The path to the .proto file.
      • 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.
    • Constructor Detail

      • ProtoSchemaParser

        ProtoSchemaParser​(java.lang.String fileName,
                          char[] data)
    • Method Detail

      • parse

        public static ProtoFile parse​(java.io.File file)
                               throws java.io.IOException
        Parse a .proto definition 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 .proto schema. The InputStream is 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 .proto schema. The Reader is not closed.
        Throws:
        java.io.IOException
      • parse

        public static ProtoFile parse​(java.lang.String name,
                                      java.lang.String data)
        Parse a named .proto schema.
      • 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
      • 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 and readChar is 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()
        Like skipWhitespace(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, either pos == data.length or 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)