-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed ANTLR parser and introduced JavaCC parser
- Loading branch information
Showing
38 changed files
with
268 additions
and
2,945 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 was deleted.
Oops, something went wrong.
89 changes: 8 additions & 81 deletions
89
src/main/java/com/booleworks/logicng/io/parsers/FormulaParser.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 |
---|---|---|
@@ -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(); | ||
} |
21 changes: 0 additions & 21 deletions
21
src/main/java/com/booleworks/logicng/io/parsers/LexerException.java
This file was deleted.
Oops, something went wrong.
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
43 changes: 0 additions & 43 deletions
43
src/main/java/com/booleworks/logicng/io/parsers/ParserWithFormula.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
src/main/java/com/booleworks/logicng/io/parsers/PropositionalLexer.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.