Skip to content

Commit

Permalink
AVRO-3403: Create and use ANTLR to parse IDL files
Browse files Browse the repository at this point in the history
Added a shared ANTLR grammar, without actions or predicates. This
guarantees ANTLR can create usable parsers in any language it supports.
The Java code creating a parse tree and transforming it into an IdlFile
is the code to be ported to add IDL support to other languages.

The IDL reader using the ANTLR generated parser replaces the JavaCC
parser in the maven plugin and in the tools. The JavaCC parser has been
deprecated, but not removed.
  • Loading branch information
opwvhk committed Sep 8, 2022
1 parent 7c17fd5 commit 630e815
Show file tree
Hide file tree
Showing 97 changed files with 5,006 additions and 98 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ indent_style = space
indent_size = 2
trim_trailing_whitespace=true

ij_continuation_indent_size = 4
ij_java_wrap_comments = true
ij_any_indent_case_from_switch = false

[*.{avsc,avpr,avdl}]
indent_style = space
indent_size = 2
trim_trailing_whitespace=true

ij_continuation_indent_size = 4
ij_json_space_after_colon = true
ij_json_space_before_colon = true
ij_json_spaces_within_brackets = true
ij_any_array_initializer_wrap = off

[*.{ps1}]
indent_style = space
indent_size = 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<name>Simple Avro Ordering Service</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<avro.version>${project.version}</avro.version>
<jackson-bom.version>${jackson-bom.version}</jackson-bom.version>
<junit.version>${junit.version}</junit.version>
Expand Down
2 changes: 1 addition & 1 deletion lang/java/avro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<artifactId>avro-parent</artifactId>
<groupId>org.apache.avro</groupId>
<version>1.12.0-SNAPSHOT</version>
<relativePath>../</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>avro</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,14 @@ public SpecificCompiler(Protocol protocol) {
}

public SpecificCompiler(Schema schema) {
this(Collections.singleton(schema));
}

public SpecificCompiler(Collection<Schema> schemas) {
this();
enqueue(schema);
for (Schema schema : schemas) {
enqueue(schema);
}
this.protocol = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ import org.apache.commons.text.StringEscapeUtils;
*
* Note: each instance is not thread-safe, but multiple separate
* instances are safely independent.
*
* @deprecated Use org.apache.avro.idl.IdlReader from avro-idl instead.
*/
@Deprecated
public class Idl implements Closeable {
static JsonNodeFactory FACTORY = JsonNodeFactory.instance;
private static final String OPTIONAL_NULLABLE_TYPE_PROPERTY = "org.apache.avro.compiler.idl.Idl.NullableType.optional";
Expand Down
2 changes: 1 addition & 1 deletion lang/java/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<groupId>org.apache.avro</groupId>
<artifactId>avro-parent</artifactId>
<version>1.12.0-SNAPSHOT</version>
<relativePath>../</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>avro-grpc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion lang/java/grpc/src/test/avro/TestService.avdl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand Down
162 changes: 162 additions & 0 deletions lang/java/idl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>avro-parent</artifactId>
<groupId>org.apache.avro</groupId>
<version>1.12.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>avro-idl</artifactId>

<name>Apache Avro IDL</name>
<packaging>bundle</packaging>
<url>https://avro.apache.org</url>
<description>Compilers for Avro IDL and Avro Specific Java API</description>

<properties>
<main.basedir>${project.parent.parent.basedir}</main.basedir>
<osgi.import>
!org.apache.avro.idl*,
org.apache.avro*;version="${project.version}",
org.apache.commons.text*,
*
</osgi.import>
<osgi.export>org.apache.avro.idl*;version="${project.version}"</osgi.export>
<antlr.version>4.9.3</antlr.version>
</properties>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/idl</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>org.apache.avro.idl</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>prepare-test-jar</id>
<phase>generate-test-resources</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<classifier>test-resource</classifier>
<testClassesDirectory>src/test/idl/putOnClassPath</testClassesDirectory>
<finalName>putOnClassPath</finalName>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>${project.basedir}/../../../share/idl_grammar</sourceDirectory>
<libDirectory>${project.basedir}/../../../share/idl_grammar/imports</libDirectory>
<listener>true</listener>
<visitor>false</visitor>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<versionRange>[1.0,)</versionRange>
<goals>
<goal>exec</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>


<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>avro</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
120 changes: 120 additions & 0 deletions lang/java/idl/src/main/java/org/apache/avro/idl/IdlFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avro.idl;

import org.apache.avro.Protocol;
import org.apache.avro.Schema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A parsed IdlFile. Provides access to the named schemas in the IDL file and
* the protocol containing the schemas.
*/
public class IdlFile {
private final Protocol protocol;
private final String namespace;
private final Map<String, Schema> namedSchemas;
private final List<String> warnings;

IdlFile(Protocol protocol, List<String> warnings) {
this(protocol.getNamespace(), protocol.getTypes(), protocol, warnings);
}

private IdlFile(String namespace, Iterable<Schema> schemas, Protocol protocol, List<String> warnings) {
this.namespace = namespace;
this.namedSchemas = new LinkedHashMap<>();
for (Schema namedSchema : schemas) {
this.namedSchemas.put(namedSchema.getFullName(), namedSchema);
}
this.protocol = protocol;
this.warnings = Collections.unmodifiableList(new ArrayList<>(warnings));
}

/**
* The protocol defined by the IDL file.
*/
public Protocol getProtocol() {
return protocol;
}

public List<String> getWarnings() {
return warnings;
}

public List<String> getWarnings(String importFile) {
return warnings.stream()
.map(warning -> importFile + " " + Character.toLowerCase(warning.charAt(0)) + warning.substring(1))
.collect(Collectors.toList());
}

/**
* The default namespace to resolve schema names against.
*/
public String getNamespace() {
return namespace;
}

/**
* The named schemas defined by the IDL file, mapped by their full name.
*/
public Map<String, Schema> getNamedSchemas() {
return Collections.unmodifiableMap(namedSchemas);
}

/**
* Get a named schema defined by the IDL file, by name. The name can be a simple
* name in the default namespace of the IDL file (e.g., the namespace of the
* protocol), or a full name.
*
* @param name the full name of the schema, or a simple name
* @return the schema, or {@code null} if it does not exist
*/
public Schema getNamedSchema(String name) {
Schema result = namedSchemas.get(name);
if (result != null) {
return result;
}
if (namespace != null && !name.contains(".")) {
result = namedSchemas.get(namespace + "." + name);
}
return result;
}

// Visible for testing
String outputString() {
if (protocol != null) {
return protocol.toString();
}
if (namedSchemas.isEmpty()) {
return "[]";
} else {
StringBuilder buffer = new StringBuilder();
for (Schema schema : namedSchemas.values()) {
buffer.append(",").append(schema);
}
buffer.append("]").setCharAt(0, '[');
return buffer.toString();
}
}
}
Loading

0 comments on commit 630e815

Please sign in to comment.