Skip to content

Commit

Permalink
Make parsers fail on error in lexer (#603)
Browse files Browse the repository at this point in the history
Co-authored-by: Natalia Gavrilenko <[email protected]>
  • Loading branch information
natgavrilenko and Natalia Gavrilenko authored Jan 17, 2024
1 parent 4d76582 commit 83b3969
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;

public class ParserErrorListener extends ConsoleErrorListener {
public class AbortErrorListener extends ConsoleErrorListener {

@Override
public void syntaxError(
Recognizer<?, ?> recognizer,
Object offendingSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import com.dat3m.dartagnan.parsers.CatLexer;
import com.dat3m.dartagnan.parsers.CatParser;
import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.wmm.Wmm;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -27,10 +24,13 @@ public Wmm parse(String raw) {

private Wmm parse(CharStream charStream){
CatLexer lexer = new CatLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

CatParser parser = new CatParser(tokenStream);
parser.addErrorListener(new ParserErrorListener());
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
ParserRuleContext parserEntryPoint = parser.mcm();
return (Wmm) parserEntryPoint.accept(new VisitorBase());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusAArch64Lexer;
import com.dat3m.dartagnan.parsers.LitmusAArch64Parser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusAArch64;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusAArch64 implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusAArch64Lexer lexer = new LitmusAArch64Lexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusAArch64Parser parser = new LitmusAArch64Parser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusAArch64 visitor = new VisitorLitmusAArch64();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusCLexer;
import com.dat3m.dartagnan.parsers.LitmusCParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusC;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusC implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusCLexer lexer = new LitmusCLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusCParser parser = new LitmusCParser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
parser.addErrorListener(new AbortErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusC visitor = new VisitorLitmusC();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusLISALexer;
import com.dat3m.dartagnan.parsers.LitmusLISAParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusLISA;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusLISA implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusLISALexer lexer = new LitmusLISALexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusLISAParser parser = new LitmusLISAParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusLISA visitor = new VisitorLitmusLISA();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusPPCLexer;
import com.dat3m.dartagnan.parsers.LitmusPPCParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusPPC;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusPPC implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusPPCLexer lexer = new LitmusPPCLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusPPCParser parser = new LitmusPPCParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusPPC visitor = new VisitorLitmusPPC();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusPTXLexer;
import com.dat3m.dartagnan.parsers.LitmusPTXParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusPTX;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusPTX implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusPTXLexer lexer = new LitmusPTXLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusPTXParser parser = new LitmusPTXParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusPTX visitor = new VisitorLitmusPTX();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusRISCVLexer;
import com.dat3m.dartagnan.parsers.LitmusRISCVParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusRISCV;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusRISCV implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusRISCVLexer lexer = new LitmusRISCVLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusRISCVParser parser = new LitmusRISCVParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusRISCV visitor = new VisitorLitmusRISCV();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusVulkanLexer;
import com.dat3m.dartagnan.parsers.LitmusVulkanParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusVulkan;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusVulkan implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusVulkanLexer lexer = new LitmusVulkanLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusVulkanParser parser = new LitmusVulkanParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusVulkan visitor = new VisitorLitmusVulkan();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LitmusX86Lexer;
import com.dat3m.dartagnan.parsers.LitmusX86Parser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLitmusX86;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLitmusX86 implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LitmusX86Lexer lexer = new LitmusX86Lexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LitmusX86Parser parser = new LitmusX86Parser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.main();
VisitorLitmusX86 visitor = new VisitorLitmusX86();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.LLVMIRLexer;
import com.dat3m.dartagnan.parsers.LLVMIRParser;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorLlvm;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

class ParserLlvm implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
LLVMIRLexer lexer = new LLVMIRLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

LLVMIRParser parser = new LLVMIRParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
ParserRuleContext parserEntryPoint = parser.compilationUnit();
VisitorLlvm visitor = new VisitorLlvm();

Expand All @@ -25,4 +27,3 @@ public Program parse(CharStream charStream) {
return visitor.buildProgram();
}
}

Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.dat3m.dartagnan.parsers.program;

import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.*;
import com.dat3m.dartagnan.parsers.program.visitors.VisitorSpirv;
import com.dat3m.dartagnan.program.Program;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;

public class ParserSpirv implements ParserInterface {

@Override
public Program parse(CharStream charStream) {
SpirvLexer lexer = new SpirvLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

SpirvParser parser = new SpirvParser(tokenStream);
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
parser.addErrorListener(new ParserErrorListener());
ParserRuleContext parserEntryPoint = parser.spv();
VisitorSpirv visitor = new VisitorSpirv();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import com.dat3m.dartagnan.parsers.XMLLexer;
import com.dat3m.dartagnan.parsers.XMLParser;
import com.dat3m.dartagnan.exception.ParserErrorListener;
import com.dat3m.dartagnan.exception.AbortErrorListener;
import com.dat3m.dartagnan.parsers.witness.visitors.VisitorXML;
import com.dat3m.dartagnan.witness.WitnessGraph;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -22,10 +19,13 @@ public class ParserWitness {

public WitnessGraph parse(CharStream charStream) {
XMLLexer lexer = new XMLLexer(charStream);
lexer.addErrorListener(new AbortErrorListener());
lexer.addErrorListener(new DiagnosticErrorListener(true));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);

XMLParser parser = new XMLParser(tokenStream);
parser.addErrorListener(new ParserErrorListener());
parser.addErrorListener(new AbortErrorListener());
parser.addErrorListener(new DiagnosticErrorListener(true));
ParserRuleContext parserEntryPoint = parser.document();
VisitorXML visitor = new VisitorXML();

Expand Down

0 comments on commit 83b3969

Please sign in to comment.