天天看點

caclite parser代碼生成詳解

caclite parser代碼生成詳解

本文代碼均已上傳到gitee https://gitee.com/shoothzj/calcite-examples caclite的parser代碼生成分為如下兩個步驟

caclite parser代碼生成詳解

生成Parse.jj

檔案目錄如下

├── pom.xml
└── src
    ├── main
    │   ├── codegen
    │   │   ├── config.fmpp
    │   │   ├── includes
    │   │   │   ├── compoundIdentifier.ftl
    │   │   │   └── parserImpls.ftl
    │   │   └── templates
    │   │       └── Parser.jj           

添加calcite dependency

<dependency>
            <groupId>org.apache.calcite</groupId>
            <artifactId>calcite-core</artifactId>
        </dependency>           

配置drill-fmpp-maven-plugin插件如下

<plugin>
                <groupId>org.apache.drill.tools</groupId>
                <artifactId>drill-fmpp-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <config>src/main/codegen/config.fmpp</config>
                            <output>${project.build.directory}/generated-sources/fmpp</output>
                            <templates>src/main/codegen/templates</templates>
                        </configuration>
                        <id>generate-fmpp-sources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>           

codegen 子產品的檔案都拷貝自對應版本的calclite core/src/main/codegen路徑 https://github.com/apache/calcite/tree/main/core/src/main/codegen

然後把https://github.com/apache/calcite/blob/main/core/src/main/codegen/default_config.fmpp 中的parser屬性與config.fmpp中的parser屬性合并。就可以通過mvn package指令生成Parser.jj了。當然,如果有定制化修改的需求,也可以在這個階段修改config.fmpp

caclite parser代碼生成詳解

Parser.jj生成java代碼

檔案目錄如下

├── pom.xml
├── src
│   ├── main
│   │   ├── codegen
│   │   │   └── Parser.jj           

Parser.jj就是我們上一步生成的Parser.jj,如果有什麼想要的定制化修改,也可以在這個步驟改入到Parser.jj中。

添加calcite dependency

<dependency>
            <groupId>org.apache.calcite</groupId>
            <artifactId>calcite-core</artifactId>
        </dependency>           

配置javacc-maven-plugin如下

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>javacc-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>javacc</id>
                        <goals>
                            <goal>javacc</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.basedir}/src/main/codegen</sourceDirectory>
                            <includes>
                                <include>**/Parser.jj</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>           

生成代碼

caclite parser代碼生成詳解

無Parser.jj定制化修改,一步生成

如果不需要對Parser.jj進行定制化修改,那麼可以通過連續運作兩個插件來生成代碼,這裡給出pom檔案樣例,不再贅述

<plugin>
                <groupId>org.apache.drill.tools</groupId>
                <artifactId>drill-fmpp-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <config>src/main/codegen/config.fmpp</config>
                            <output>${project.build.directory}/generated-sources/fmpp</output>
                            <templates>src/main/codegen/templates</templates>
                        </configuration>
                        <id>generate-fmpp-sources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>javacc-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>javacc</id>
                        <goals>
                            <goal>javacc</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.build.directory}/generated-sources/fmpp</sourceDirectory>
                            <includes>
                                <include>**/Parser.jj</include>
                            </includes>
                            <lookAhead>2</lookAhead>
                            <isStatic>false</isStatic>
                        </configuration>
                    </execution>
                    <execution>
                        <id>javacc-test</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>javacc</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.build.directory}/generated-test-sources/fmpp</sourceDirectory>
                            <outputDirectory>${project.build.directory}/generated-test-sources/javacc</outputDirectory>
                            <includes>
                                <include>**/Parser.jj</include>
                            </includes>
                            <isStatic>false</isStatic>
                            <ignoreCase>true</ignoreCase>
                            <unicodeInput>true</unicodeInput>
                        </configuration>
                    </execution>
                </executions>
            </plugin>           

繼續閱讀