All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squareup.protoparser.ProtoSchemaParser Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
// Copyright 2013 Square, Inc.
package com.squareup.protoparser;

import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Basic parser for {@code .proto} schema declarations.
 *
 * 

This parser throws away data that it doesn't care about. In particular, * unrecognized options, and extensions are discarded. It doesn't retain nesting * within types. */ public final class ProtoSchemaParser { /** Parse a {@code .proto} definition file. */ public static ProtoFile parse(File file) throws IOException { return new ProtoSchemaParser(file.getName(), fileToCharArray(file)).readProtoFile(); } /** Parse a named {@code .proto} schema. The {@code InputStream} is not closed. */ public static ProtoFile parseUtf8(String name, InputStream is) throws IOException { return new ProtoSchemaParser(name, streamToCharArray(is)).readProtoFile(); } /** Parse a named {@code .proto} schema. The {@code Reader} is not closed. */ public static ProtoFile parse(String name, Reader reader) throws IOException { return new ProtoSchemaParser(name, readerToCharArray(reader)).readProtoFile(); } /** Parse a named {@code .proto} schema. */ public static ProtoFile parse(String name, String data) { return new ProtoSchemaParser(name, data.toCharArray()).readProtoFile(); } /** The path to the {@code .proto} file. */ private final String fileName; /** The entire document. */ private final char[] data; /** Our cursor within the document. {@code data[pos]} is the next character to be read. */ private int pos; /** The number of newline characters encountered thus far. */ private int line; /** The index of the most recent newline character. */ private int lineStart; /** Output package name, or null if none yet encountered. */ private String packageName; /** The current package name + nested type names, separated by dots. */ private String prefix = ""; /** Imported files. */ private final List dependencies = new ArrayList(); /** Public imported files. */ private final List publicDependencies = new ArrayList(); /** Declared message types and enum types. */ private final List types = new ArrayList(); /** Declared services. */ private final List services = new ArrayList(); /** Declared 'extend's. */ private final List extendDeclarations = new ArrayList(); /** Global options. */ private final List