Vitor de Lima has uploaded a new change for review. Change subject: core: Replace ANTLRv4 with JavaCC in OsInfo validation ......................................................................
core: Replace ANTLRv4 with JavaCC in OsInfo validation A replacement for the ANTLRv4-based validator for the OsInfo properties file. It uses JavaCC instead, this has the following advantages: - Generates code that does not depend on any external dependency - The build dependency is already properly packaged in most distros On the other hand, the syntax for the grammar file is not as clean as the ANTLR version. Change-Id: Ice68f11f4616ab488d4caf7f8073b4b423ab0c46 Signed-off-by: Vitor de Lima <[email protected]> --- M backend/manager/dependencies/common/pom.xml D backend/manager/dependencies/common/src/main/modules/org/antlr/antlr4-runtime/main/module.xml M backend/manager/modules/utils/exclude-filters.xml M backend/manager/modules/utils/pom.xml D backend/manager/modules/utils/src/main/antlr4/org/ovirt/engine/core/utils/osinfo/Osinfo.g4 M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/osinfo/OsinfoPropertiesParser.java A backend/manager/modules/utils/src/main/javacc/org/ovirt/engine/core/utils/osinfo/osinfo.jj M backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml M pom.xml 9 files changed, 217 insertions(+), 283 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/82/36082/1 diff --git a/backend/manager/dependencies/common/pom.xml b/backend/manager/dependencies/common/pom.xml index 055d5f6..41beadb 100644 --- a/backend/manager/dependencies/common/pom.xml +++ b/backend/manager/dependencies/common/pom.xml @@ -347,10 +347,6 @@ <version>${jaxb-impl.version}</version> </dependency> - <dependency> - <groupId>org.antlr</groupId> - <artifactId>antlr4-runtime</artifactId> - </dependency> </dependencies> <build> @@ -680,12 +676,6 @@ <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <moduleName>com.sun.xml.bind</moduleName> - </module> - - <module> - <groupId>org.antlr</groupId> - <artifactId>antlr4-runtime</artifactId> - <moduleName>org.antlr.antlr4-runtime</moduleName> </module> </modules> diff --git a/backend/manager/dependencies/common/src/main/modules/org/antlr/antlr4-runtime/main/module.xml b/backend/manager/dependencies/common/src/main/modules/org/antlr/antlr4-runtime/main/module.xml deleted file mode 100644 index f665c8c..0000000 --- a/backend/manager/dependencies/common/src/main/modules/org/antlr/antlr4-runtime/main/module.xml +++ /dev/null @@ -1,13 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<module xmlns="urn:jboss:module:1.1" name="org.antlr.antlr4-runtime"> - - <resources> - <resource-root path="antlr4-runtime.jar"/> - </resources> - - <dependencies> - <module name="org.slf4j"/> - </dependencies> - -</module> diff --git a/backend/manager/modules/utils/exclude-filters.xml b/backend/manager/modules/utils/exclude-filters.xml index c37d9cd..770ce07 100644 --- a/backend/manager/modules/utils/exclude-filters.xml +++ b/backend/manager/modules/utils/exclude-filters.xml @@ -52,16 +52,28 @@ <!-- - ignore all warning on these classes as they are auto generated by antlr + ignore all warning on these classes as they are auto generated by javacc --> <Match> - <Class name="org.ovirt.engine.core.utils.osinfo.OsinfoParser" /> + <Class name="org.ovirt.engine.core.utils.osinfo.OsInfoParser" /> </Match> <Match> - <Class name="org.ovirt.engine.core.utils.osinfo.OsinfoParser$CommentContext" /> + <Class name="org.ovirt.engine.core.utils.osinfo.OsInfoParserConstants" /> </Match> <Match> - <Class name="org.ovirt.engine.core.utils.osinfo.OsinfoLexer" /> + <Class name="org.ovirt.engine.core.utils.osinfo.OsInfoParserTokenManager" /> + </Match> + <Match> + <Class name="org.ovirt.engine.core.utils.osinfo.ParseException" /> + </Match> + <Match> + <Class name="org.ovirt.engine.core.utils.osinfo.SimpleCharStream" /> + </Match> + <Match> + <Class name="org.ovirt.engine.core.utils.osinfo.Token" /> + </Match> + <Match> + <Class name="org.ovirt.engine.core.utils.osinfo.TokenMgrError" /> </Match> </FindBugsFilter> diff --git a/backend/manager/modules/utils/pom.xml b/backend/manager/modules/utils/pom.xml index 5f41312..ebe391e 100644 --- a/backend/manager/modules/utils/pom.xml +++ b/backend/manager/modules/utils/pom.xml @@ -146,10 +146,6 @@ <version>${openstack-client.version}</version> </dependency> - <dependency> - <groupId>org.antlr</groupId> - <artifactId>antlr4-runtime</artifactId> - </dependency> </dependencies> <build> @@ -174,14 +170,14 @@ <!-- Generate osinfo parser from grammer file--> <plugin> - <groupId>org.antlr</groupId> - <artifactId>antlr4-maven-plugin</artifactId> - <version>${antlr.version}</version> + <groupId>org.codehaus.mojo</groupId> + <artifactId>javacc-maven-plugin</artifactId> + <version>${javacc.version}</version> <executions> <execution> - <id>antlr</id> + <id>javacc</id> <goals> - <goal>antlr4</goal> + <goal>javacc</goal> </goals> </execution> </executions> diff --git a/backend/manager/modules/utils/src/main/antlr4/org/ovirt/engine/core/utils/osinfo/Osinfo.g4 b/backend/manager/modules/utils/src/main/antlr4/org/ovirt/engine/core/utils/osinfo/Osinfo.g4 deleted file mode 100644 index 2091b23..0000000 --- a/backend/manager/modules/utils/src/main/antlr4/org/ovirt/engine/core/utils/osinfo/Osinfo.g4 +++ /dev/null @@ -1,208 +0,0 @@ -grammar Osinfo; - - -parse - : - ((osRecord | compatibilityRecord | comment) (EOL|EOF))* - ; - - -osRecord - : - 'os' DOT OS_UNIQUE_NAME '.' attribute - ; - -compatibilityRecord - : - 'backwardCompatibility' '.' OS_UNIQUE_NAME WS* EQUALS WS* INT - ; - -comment - : - LineComment - ; - -attribute - : - ID intValue - | NAME stringValue - | DESCRIPTION stringValue - | 'derivedFrom' stringValue - | 'family' stringValue - | 'cpuArchitecture' archValue - | 'cpu.unsupported' stringValue - | 'bus' busValue - | 'sysprepPath' stringValue - | 'sysprepFileName' stringValue - | 'productKey' stringValue - | 'isTimezoneTypeInteger' booleanValue - | resources - | devices - ; - -resources - : - 'resources' - ( DOT 'minimum' DOT ('ram'|'disksize'|'numberOfCpus') intValue - | DOT 'maximum' DOT ('ram'|'disksize'|'numberOfCpus') intValue - )+ - ; - -devices - : - 'devices' - ( DOT DISPLAY_PROTOCOLS displayValue - | DOT 'watchdog.models' watchdogValue - | DOT 'network' networkValue - | DOT 'network.hotplugSupport' booleanValue - | DOT 'disk.hotpluggableInterfaces' hardwareInterfacesValue - | DOT 'balloon.enabled' booleanValue - | DOT 'audio' audioValue - | DOT 'cdInterface' cdInterfaceValue - | DOT 'diskInterfaces' hardwareInterfacesValue - | DOT 'maxPciDevices' intValue - | DOT 'hyperv.enabled' booleanValue - ) - ; - -intValue - : - valueSpecifier (INT | bus_width ) - ; - -stringValue - : - valueSpecifier rest_of_line - ; - -rest_of_line - : - ~EOL* - ; - -booleanValue - : - valueSpecifier ('true' | 'false') - ; - -archValue - : - valueSpecifier ('x86_64' | 'ppc64') - ; - -busValue - : - valueSpecifier bus_width - ; - -displayValue - : - valueSpecifier DISPLAY_PROTOCOLS_TYPE(',' WS* DISPLAY_PROTOCOLS_TYPE )* - ; - - -watchdogValue - : - valueSpecifier ('i6300esb') - ; - -networkValue - : - valueSpecifier NETWORK_DEVICE_TYPE (',' WS* NETWORK_DEVICE_TYPE)* - ; - - -audioValue - : - valueSpecifier ('ich6' | 'ac97') - ; - -cdInterfaceValue - : - valueSpecifier ('ide' | 'scsi') - ; - - -hardwareInterfacesValue - : - valueSpecifier HARDWARE_INTERFACE_TYPE* (',' WS* HARDWARE_INTERFACE_TYPE)* - ; - -valueSpecifier - : - VALUE (DOT VERSION)* WS* EQUALS WS* - ; - - -//keywords -OS : 'os' ; -BACKWARDCOMPATIBILITY : 'backwardCompatibility' ; -ID : 'id' ; -NAME : 'name' ; -DESCRIPTION : 'description' ; -DERIVEDFROM : 'derivedFrom' ; -FAMILY : 'family' ; -CPUARCHITECTURE : 'cpuArchitecture' ; -BUS : 'bus' ; -RESOURCES : 'resources' ; -MINIMUM : 'minimum' ; -RAM : 'ram' ; -DISKSIZE : 'disksize' ; -NUMBEROFCPUS : 'numberOfCpus' ; -MAXIMUM : 'maximum' ; -DEVICES : 'devices' ; -DISPLAY_PROTOCOLS : 'display.protocols' ; -WATCHDOG_MODELS : 'watchdog.models' ; -NETWORK : 'network' ; -NETWORK_HOTPLUGSUPPORT : 'network.hotplugSupport' ; -DISK_HOTPLUGGABLEINTERFACES : 'disk.hotpluggableInterfaces' ; -BALLOON_ENABLED : 'balloon.enabled' ; -AUDIO : 'audio' ; -CDINTERFACE : 'cdInterface' ; -DISKINTERFACES : 'diskInterfaces' ; -MAXPCIDEVICES : 'maxPciDevices' ; -TRUE : 'true' ; -FALSE : 'false' ; -X86_64 : 'x86_64' ; -PPC64 : 'ppc64' ; -COMMA : ',' ; -I6300ESB : 'i6300esb' ; -ICH6 : 'ich6' ; -AC97 : 'ac97' ; -IDE : 'ide' ; -SCSI : 'scsi' ; - -DISPLAY_PROTOCOLS_TYPE - : - 'vnc/cirrus' | 'qxl/qxl' | 'vnc/vga' - ; - -NETWORK_DEVICE_TYPE - : - 'rtl8139_pv' | 'rtl8139' | 'e1000' | 'pv' | 'spaprVlan' - ; - -HARDWARE_INTERFACE_TYPE - : - 'IDE' | 'VirtIO' | 'VirtIO_SCSI' | 'SPAPR_VSCSI' - ; - - -INT : DIGIT+ ; -bus_width: '32' | '64' ; - -fragment DIGIT : [0-9] ; -fragment CHAR : [a-zA-Z0-9_] ; -DOT: '.' ; -VALUE: '.value' ; -VERSION: '3' '.' '0'..'5' ; -OS_UNIQUE_NAME : CHAR+ ; -EQUALS : '=' ; -WS : [ \t] ; -TEXT: [a-zA-Z0-9\\\/${}]+ ; -LineComment - : - '#' ~[\r\n]* - ; - -EOL : [\r\n]+ ; diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/osinfo/OsinfoPropertiesParser.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/osinfo/OsinfoPropertiesParser.java index 31f024d..05d6c66 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/osinfo/OsinfoPropertiesParser.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/osinfo/OsinfoPropertiesParser.java @@ -1,48 +1,32 @@ package org.ovirt.engine.core.utils.osinfo; -import java.io.IOException; - -import org.antlr.v4.runtime.ANTLRFileStream; -import org.antlr.v4.runtime.BaseErrorListener; -import org.antlr.v4.runtime.BufferedTokenStream; -import org.antlr.v4.runtime.RecognitionException; -import org.antlr.v4.runtime.Recognizer; -import org.antlr.v4.runtime.misc.NotNull; -import org.antlr.v4.runtime.misc.Nullable; -import org.antlr.v4.runtime.tree.ParseTreeWalker; +import java.io.FileInputStream; +import java.io.FileNotFoundException; public class OsinfoPropertiesParser { /** - * walk the parse tree which is auto-generated by antlr from the Osinfo.g4 grammar file. walking the parse + * walk the parse tree which is auto-generated by javacc from the osinfo.jj file. walking the parse * tree will throw an exception in case the file structure doesn't follow the grammar. * * @param path */ public static void parse(String path) { - ParseTreeWalker walker = new ParseTreeWalker(); - OsinfoLexer osinfoLexer; - try { - osinfoLexer = new OsinfoLexer(new ANTLRFileStream(path)); - } catch (IOException e) { - throw new RuntimeException(e); - } - OsinfoParser osinfoParser = new OsinfoParser(new BufferedTokenStream(osinfoLexer)); - osinfoParser.addErrorListener(getListener()); - walker.walk(new OsinfoBaseListener(), osinfoParser.parse()); - } - private static BaseErrorListener getListener() { - return new BaseErrorListener() { - @Override public void syntaxError(@NotNull Recognizer<?, ?> recognizer, - @Nullable Object offendingSymbol, - int line, - int charPositionInLine, - @NotNull String msg, - @Nullable RecognitionException e) { - throw new RuntimeException("osinfo properties syntax error: " + - ("line " + line + ":" + charPositionInLine + " " + msg)); - } - }; + try { + FileInputStream inputStream = new FileInputStream(path); + + OsInfoParser parser = new OsInfoParser(inputStream); + parser.parse(); + } catch (FileNotFoundException e) { + throw new RuntimeException("osinfo properties file cannot be found"); + } catch (ParseException e) { + int line = e.currentToken.beginLine; + int charPositionInLine = e.currentToken.beginColumn; + String msg = e.getMessage(); + + throw new RuntimeException("osinfo properties syntax error: " + + ("line " + line + ":" + charPositionInLine + " " + msg)); + } } } diff --git a/backend/manager/modules/utils/src/main/javacc/org/ovirt/engine/core/utils/osinfo/osinfo.jj b/backend/manager/modules/utils/src/main/javacc/org/ovirt/engine/core/utils/osinfo/osinfo.jj new file mode 100644 index 0000000..b2fe9f1 --- /dev/null +++ b/backend/manager/modules/utils/src/main/javacc/org/ovirt/engine/core/utils/osinfo/osinfo.jj @@ -0,0 +1,174 @@ +options { + STATIC = false; +} + +PARSER_BEGIN( OsInfoParser ) + +package org.ovirt.engine.core.utils.osinfo; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.io.StringReader; + +public class OsInfoParser { + +} +PARSER_END( OsInfoParser ) + +void parse() : {} { ((osRecord() | compatibilityRecord() | comment()) (<EOL>|<EOF>))* } + +void osRecord() : {} { <OS> <DOT> <OS_UNIQUE_NAME> <DOT> attribute() } + +void compatibilityRecord() : {} { <BACKWARDCOMPATIBILITY> <DOT> <OS_UNIQUE_NAME> (<WS>)* <EQUALS> (<WS>)* <INT> } + +void comment() : {} { <LINECOMMENT> } + +void attribute() : {} { + ( (<ID> intValue()) + | (<NAME> stringValue()) + | (<DESCRIPTION> stringValue()) + | (<DERIVED_FROM> stringValue()) + | (<FAMILY> stringValue()) + | ("cpuArchitecture" archValue()) + | (<CPU_UNSUPPORTED> stringValue()) + | ("bus" busValue()) + | (<SYSPREP_PATH> stringValue()) + | (<SYSPREP_FILENAME> stringValue()) + | (<PRODUCT_KEY> stringValue()) + | ("isTimezoneTypeInteger" booleanValue()) + | resources() + | devices() + ) +} + +void resources() : {} { + "resources" <DOT> + ( + ("minimum" <DOT> ("ram"|"disksize"|"numberOfCpus") intValue()) + | ("maximum" <DOT> ("ram"|"disksize"|"numberOfCpus") intValue()) + )+ +} + +void devices() : {} { + "devices" <DOT> + ( + ("display.protocols" displayValue()) + | ("watchdog.models" watchdogValue()) + | ("network" networkValue()) + | ("network.hotplugSupport" booleanValue()) + | ("disk.hotpluggableInterfaces" hardwareInterfacesValue()) + | ("balloon.enabled" booleanValue()) + | ("audio" audioValue()) + | ("cdInterface" cdInterfaceValue()) + | ("diskInterfaces" hardwareInterfacesValue()) + | ("maxPciDevices" intValue()) + | ("hyperv.enabled" booleanValue()) + ) +} + +void intValue() : {} { + valueSpecifier() (<INT> | <BUS_WIDTH>) +} + +void stringValue() : {} { + <VALUE> (<DOT> <VERSION>)* (<WS>)* <EQUALS_STR> +} + +void booleanValue() : {} { + valueSpecifier() ("true" | "false") +} + +void archValue() : {} { + valueSpecifier() ("x86_64" | "ppc64") +} + +void busValue() : {} { + valueSpecifier() <BUS_WIDTH> +} + +void displayValue() : {} { + valueSpecifier() <DISPLAY_PROTOCOLS_TYPE> (<COMMA> (<WS>)* <DISPLAY_PROTOCOLS_TYPE> )* +} + +void watchdogValue() : {} { + valueSpecifier() ("i6300esb") +} + +void networkValue() : {} { + valueSpecifier() <NETWORK_DEVICE_TYPE> (<COMMA> (<WS>)* <NETWORK_DEVICE_TYPE>)* +} + +void audioValue() : {} { + valueSpecifier() ("ich6" | "ac97") +} + +void cdInterfaceValue() : {} { + valueSpecifier() ("ide" | "scsi") +} + +void hardwareInterfacesValue() : {} { + valueSpecifier() (<HARDWARE_INTERFACE_TYPE>)* (<COMMA> (<WS>)* <HARDWARE_INTERFACE_TYPE>)* +} + +void valueSpecifier() : {} { + <VALUE> (<DOT> <VERSION>)* (<WS>)* <EQUALS> (<WS>)* +} + +<STRING_VALUE, DEFAULT> TOKEN: +{ + < VALUE : ".value" > +| < DOT : "." > +| < VERSION : "3" "." ["0"-"5"] > +| < WS : [" ", "\t"] > +} + +<STRING_VALUE> TOKEN: +{ + < EQUALS_STR: "=" > : STRING_LITERAL +} + +<STRING_LITERAL> SKIP: +{ + < ~["\r","\n"] > +} + +<STRING_LITERAL, DEFAULT> TOKEN: +{ + < EOL: (["\r","\n"])+ > : DEFAULT +} + +<DEFAULT> TOKEN: +{ + < EQUALS : "=" > +| < COMMA : "," > +| < DISPLAY_PROTOCOLS_TYPE + : + ( "vnc/cirrus" | "qxl/qxl" | "vnc/vga" ) + > +| < NETWORK_DEVICE_TYPE + : + ("rtl8139_pv" | "rtl8139" | "e1000" | "pv" | "spaprVlan") + > +| < HARDWARE_INTERFACE_TYPE + : + ("IDE" | "VirtIO" | "VirtIO_SCSI" | "SPAPR_VSCSI") + > +| < BUS_WIDTH : ("32" | "64") > +| < INT : (<DIGIT>)+ > +| < DIGIT : ["0"-"9"] > +| < OS : "os" > +| < BACKWARDCOMPATIBILITY : "backwardCompatibility" > +| < ID : "id" > +| < NAME : "name" > : STRING_VALUE +| < DESCRIPTION : "description" > : STRING_VALUE +| < DERIVED_FROM : "derivedFrom" > : STRING_VALUE +| < FAMILY : "family" > : STRING_VALUE +| < CPU_UNSUPPORTED : "cpu.unsupported" > : STRING_VALUE +| < SYSPREP_PATH : "sysprepPath" > : STRING_VALUE +| < SYSPREP_FILENAME : "sysprepFileName" > : STRING_VALUE +| < PRODUCT_KEY : "productKey" > : STRING_VALUE +| < CHAR : ["a"-"z","A"-"Z","0"-"9","_"] > +| < OS_UNIQUE_NAME : (<CHAR>)+ > +| < LINECOMMENT : "#" (~["\r","\n"])* > +} diff --git a/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml b/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml index 84c71d4..0a580fe 100644 --- a/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml +++ b/backend/manager/modules/utils/src/main/modules/org/ovirt/engine/core/utils/main/module.xml @@ -29,7 +29,6 @@ <module name="org.postgresql"/> <module name="org.springframework"/> <module name="org.slf4j"/> - <module name="org.antlr.antlr4-runtime"/> </dependencies> </module> diff --git a/pom.xml b/pom.xml index ef57986..f3add8e 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,7 @@ <!-- https://jira.codehaus.org/browse/MGWT-350 --> <gwt-maven-plugin.workingRefresh.version>2.4.0</gwt-maven-plugin.workingRefresh.version> <cdi.api.version>1.0-SP4</cdi.api.version> - <antlr.version>4.2.2</antlr.version> + <javacc.version>2.6</javacc.version> </properties> <dependencyManagement> <dependencies> @@ -321,9 +321,9 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.antlr</groupId> - <artifactId>antlr4-runtime</artifactId> - <version>${antlr.version}</version> + <groupId>org.codehaus.mojo</groupId> + <artifactId>javacc-maven-plugin</artifactId> + <version>${javacc.version}</version> </dependency> </dependencies> </dependencyManagement> -- To view, visit http://gerrit.ovirt.org/36082 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ice68f11f4616ab488d4caf7f8073b4b423ab0c46 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Vitor de Lima <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
