Skip to content

Commit

Permalink
Removed ANTLR parser and introduced JavaCC parser
Browse files Browse the repository at this point in the history
  • Loading branch information
czengler committed May 4, 2024
1 parent a5361e4 commit 248545e
Show file tree
Hide file tree
Showing 38 changed files with 268 additions and 2,945 deletions.
31 changes: 10 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- Copyright 2023-20xx BooleWorks GmbH -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.booleworks</groupId>
<artifactId>logicng-core</artifactId>
Expand All @@ -12,7 +12,7 @@

<name>LogicNG</name>
<description>The Next Generation Logic Library</description>
<url>http://www.logicng.org</url>
<url>https://www.logicng.org</url>

<licenses>
<license>
Expand Down Expand Up @@ -48,13 +48,12 @@
<maven.compiler.target>11</maven.compiler.target>

<!-- Dependency Versions -->
<version.antlr>4.13.1</version.antlr>
<version.junit>5.10.1</version.junit>
<version.assertj>3.24.2</version.assertj>
<version.mockito>5.2.0</version.mockito>

<!-- Plugin Versions -->
<version.antlr-plugin>4.13.1</version.antlr-plugin>
<version.javacc>3.0.1</version.javacc>
<version.jacoco>0.8.11</version.jacoco>
<version.coveralls>4.3.0</version.coveralls>
<version.maven-compiler>3.13.0</version.maven-compiler>
Expand All @@ -80,19 +79,16 @@
</resources>

<plugins>
<!-- ANTLR4 (Parser Generation) -->
<!-- Parser Generation -->
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${version.antlr-plugin}</version>
<configuration>
<sourceDirectory>src/main/antlr</sourceDirectory>
<outputDirectory>target/generated-sources/antlr/com/booleworks/logicng/io/parsers</outputDirectory>
</configuration>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javacc-maven-plugin</artifactId>
<version>${version.javacc}</version>
<executions>
<execution>
<id>javacc</id>
<goals>
<goal>antlr4</goal>
<goal>javacc</goal>
</goals>
</execution>
</executions>
Expand Down Expand Up @@ -234,7 +230,7 @@
<version>${version.coveralls}</version>
<configuration>
<sourceDirectories>
<sourceDirectory>target/generated-sources/antlr</sourceDirectory>
<sourceDirectory>target/generated-sources/parser</sourceDirectory>
</sourceDirectories>
</configuration>
</plugin>
Expand All @@ -254,13 +250,6 @@
</build>

<dependencies>
<!-- Parser -->
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${version.antlr}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
95 changes: 0 additions & 95 deletions src/main/antlr/LogicNGPropositional.g4

This file was deleted.

89 changes: 8 additions & 81 deletions src/main/java/com/booleworks/logicng/io/parsers/FormulaParser.java
Original file line number Diff line number Diff line change
@@ -1,108 +1,35 @@
// SPDX-License-Identifier: Apache-2.0 and MIT
// Copyright 2015-2023 Christoph Zengler
// Copyright 2023-20xx BooleWorks GmbH

package com.booleworks.logicng.io.parsers;

import com.booleworks.logicng.formulas.Formula;
import com.booleworks.logicng.formulas.FormulaFactory;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.misc.ParseCancellationException;

import java.io.IOException;
import java.io.InputStream;

/**
* Super class for a formula parser.
* @version 1.2
* @since 1.2
* Interface for formula parsers.
* @version 3.0.0
* @since 3.0.0
*/
public abstract class FormulaParser {

private final FormulaFactory f;
private Lexer lexer;
private ParserWithFormula parser;

/**
* Constructor.
* @param f the formula factory
*/
protected FormulaParser(final FormulaFactory f) {
this.f = f;
}

/**
* Sets the internal lexer and the parser.
* @param lexer the lexer
* @param parser the parser
*/
protected void setLexerAndParser(final Lexer lexer, final ParserWithFormula parser) {
this.lexer = lexer;
this.parser = parser;
this.parser.setFormulaFactory(f);
this.lexer.removeErrorListeners();
this.parser.removeErrorListeners();
this.parser.setErrorHandler(new BailErrorStrategy());
this.parser.setBuildParseTree(false);
}

public interface FormulaParser {
/**
* Parses and returns a given input stream.
* @param inputStream an input stream
* @param inStream an input stream
* @return the {@link Formula} representation of this stream
* @throws ParserException if there was a problem with the input stream
*/
public Formula parse(final InputStream inputStream) throws ParserException {
if (inputStream == null) {
return f.verum();
}
try {
final CharStream input = CharStreams.fromStream(inputStream);
lexer.setInputStream(input);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
parser.setInputStream(tokens);
return parser.getParsedFormula();
} catch (final IOException e) {
throw new ParserException("IO exception when parsing the formula", e);
} catch (final ParseCancellationException e) {
throw new ParserException("Parse cancellation exception when parsing the formula", e);
} catch (final LexerException e) {
throw new ParserException("Lexer exception when parsing the formula.", e);
}
}
Formula parse(final InputStream inStream) throws ParserException;

/**
* Parses and returns a given string.
* @param in a string
* @return the {@link Formula} representation of this string
* @throws ParserException if the string was not a valid formula
*/
public Formula parse(final String in) throws ParserException {
if (in == null || in.isEmpty()) {
return f.verum();
}
try {
final CharStream input = CharStreams.fromString(in);
lexer.setInputStream(input);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
parser.setInputStream(tokens);
return parser.getParsedFormula();
} catch (final ParseCancellationException e) {
throw new ParserException("Parse cancellation exception when parsing the formula", e);
} catch (final LexerException e) {
throw new ParserException("Lexer exception when parsing the formula.", e);
}
}
Formula parse(final String in) throws ParserException;

/**
* Returns the factory of this parser.
* @return the factory of this parser
*/
public FormulaFactory factory() {
return f;
}
FormulaFactory factory();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class ParserException extends Exception {
* @param message the message
* @param exception the inner exception
*/
public ParserException(final String message, final Exception exception) {
public ParserException(final String message, final Throwable exception) {
super(message, exception);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 248545e

Please sign in to comment.