Search in sources :

Example 1 with TypeDeclarationContext

use of com.linkedin.data.grammar.PdlParser.TypeDeclarationContext in project rest.li by linkedin.

the class PdlSchemaParser method parse.

/**
   * Parse list of Data objects.
   *
   * The {{DataSchema}'s parsed are in {{#topLevelDataSchemas}.
   * Parse errors are in {{#errorMessageBuilder} and indicated
   * by {{#hasError()}.
   *
   * @param document provides the source code in AST form
   */
private DataSchema parse(DocumentContext document) throws ParseException {
    PdlParser.NamespaceDeclarationContext namespaceDecl = document.namespaceDeclaration();
    if (namespaceDecl != null) {
        setCurrentNamespace(namespaceDecl.qualifiedIdentifier().value);
    } else {
        setCurrentNamespace("");
    }
    if (document.packageDeclaration() != null) {
        setCurrentPackage(document.packageDeclaration().qualifiedIdentifier().value);
    } else {
        setCurrentPackage(null);
    }
    setCurrentImports(document.importDeclarations());
    TypeDeclarationContext typeDeclaration = document.typeDeclaration();
    DataSchema schema;
    if (typeDeclaration.namedTypeDeclaration() != null) {
        NamedDataSchema namedSchema = parseNamedType(typeDeclaration.namedTypeDeclaration());
        if (!namedSchema.getNamespace().equals(getCurrentNamespace())) {
            throw new ParseException(typeDeclaration, "Top level type declaration may not be qualified with a namespace different than the file namespace: " + typeDeclaration.getText());
        }
        schema = namedSchema;
    } else if (typeDeclaration.anonymousTypeDeclaration() != null) {
        schema = parseAnonymousType(typeDeclaration.anonymousTypeDeclaration());
    } else {
        throw new ParseException(typeDeclaration, "Unrecognized type declaration: " + typeDeclaration.getText());
    }
    addTopLevelSchema(schema);
    return schema;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PdlParser(com.linkedin.data.grammar.PdlParser) TypeDeclarationContext(com.linkedin.data.grammar.PdlParser.TypeDeclarationContext) NamedTypeDeclarationContext(com.linkedin.data.grammar.PdlParser.NamedTypeDeclarationContext) AnonymousTypeDeclarationContext(com.linkedin.data.grammar.PdlParser.AnonymousTypeDeclarationContext)

Aggregations

PdlParser (com.linkedin.data.grammar.PdlParser)1 AnonymousTypeDeclarationContext (com.linkedin.data.grammar.PdlParser.AnonymousTypeDeclarationContext)1 NamedTypeDeclarationContext (com.linkedin.data.grammar.PdlParser.NamedTypeDeclarationContext)1 TypeDeclarationContext (com.linkedin.data.grammar.PdlParser.TypeDeclarationContext)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)1 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)1