-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AVRO-3403: Create and use ANTLR to parse IDL files
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
Showing
97 changed files
with
5,006 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
120
lang/java/idl/src/main/java/org/apache/avro/idl/IdlFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.