Skip to content

Commit

Permalink
Retired EpsilonParseProblemManager singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
kolovos committed Dec 9, 2024
1 parent 13a36db commit 6f0ac0e
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.epsilon.common.parse.problem.ParseProblem;

@Deprecated
public class EpsilonParseProblemManager {

public static EpsilonParseProblemManager INSTANCE = new EpsilonParseProblemManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
package org.eclipse.epsilon.common.parse;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.antlr.runtime.Parser;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.RecognizerSharedState;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.TreeAdaptor;
import org.eclipse.epsilon.common.parse.problem.ParseProblem;
import org.eclipse.epsilon.common.util.ReflectionUtil;

public abstract class EpsilonParser extends Parser {

private boolean printErrors = false;
protected List<ParseProblem> parseProblems = new ArrayList<>();

public EpsilonParser(TokenStream tokenstream) {
super(tokenstream);
Expand Down Expand Up @@ -71,12 +75,22 @@ public void prepareForGUnit() {

@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException re) {
EpsilonParseProblemManager.INSTANCE.reportException(
re.line, re.charPositionInLine, getErrorMessage(re, getTokenNames())
);
reportException(re.line, re.charPositionInLine, getErrorMessage(re, getTokenNames()));

if (printErrors) {
super.displayRecognitionError(tokenNames, re);
}
}

public void reportException(int line, int column, String reason) {
ParseProblem problem = new ParseProblem();
problem.setLine(line);
problem.setColumn(column);
problem.setReason(reason);
parseProblems.add(problem);
}

public List<ParseProblem> getParseProblems() {
return parseProblems;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package org.eclipse.epsilon.common.parse;

import java.util.ArrayList;
import java.util.List;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.RecognizerSharedState;
import org.eclipse.epsilon.common.parse.problem.ParseProblem;

// Custom superclass for Epsilon's lexers
// See https://www.antlr3.org/pipermail/antlr-interest/2007-September/023602.html
public abstract class Lexer extends org.antlr.runtime.Lexer {


List<ParseProblem> parseProblems = new ArrayList<>();

public Lexer() {
super();
}
Expand All @@ -21,7 +27,18 @@ public Lexer(CharStream input) {
}

public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
EpsilonParseProblemManager.INSTANCE.reportException(e.line, e.charPositionInLine, getErrorMessage(e, getTokenNames()));
reportException(e.line, e.charPositionInLine, getErrorMessage(e, getTokenNames()));
}

public void reportException(int line, int column, String reason) {
ParseProblem problem = new ParseProblem();
problem.setLine(line);
problem.setColumn(column);
problem.setReason(reason);
parseProblems.add(problem);
}

public List<ParseProblem> getParseProblems() {
return parseProblems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public Program<R> parse(String code) {
EpsilonTreeAdaptor adaptor = new EpsilonTreeAdaptor((File) null, this.module);
EpsilonParser parser = createParser(stream);
parser.setDeepTreeAdaptor(adaptor);
parser.getParseProblems().addAll(((org.eclipse.epsilon.common.parse.Lexer) lexer).getParseProblems());

return invokeMainParserRule(parser, comments);
}

Expand All @@ -117,7 +119,7 @@ public Program<R> parse(String code) {
private final String mainParserRule;

private Program<R> invokeMainParserRule(EpsilonParser parser, List<CommonToken> comments) {
EpsilonParseProblemManager.INSTANCE.reset();

AST cst = null;
try {
cst = (AST) ((ParserRuleReturnScope) ReflectionUtil
Expand All @@ -128,8 +130,7 @@ private Program<R> invokeMainParserRule(EpsilonParser parser, List<CommonToken>
} catch (Throwable ex) {
return this.error(ex, parser.input.LT(1));
}
ArrayList<ParseProblem> parseProblems = new ArrayList<>(EpsilonParseProblemManager.INSTANCE.getParseProblems());
EpsilonParseProblemManager.INSTANCE.reset();
ArrayList<ParseProblem> parseProblems = new ArrayList<>(parser.getParseProblems());
assignAnnotations(cst);
assignComments(cst, comments);
createAst(cst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.antlr.runtime.*;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.ParserRuleReturnScope;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.eclipse.epsilon.common.module.AbstractModuleElement;
import org.eclipse.epsilon.common.module.IModule;
import org.eclipse.epsilon.common.module.ModuleElement;
import org.eclipse.epsilon.common.parse.*;
import org.eclipse.epsilon.common.parse.AST;
import org.eclipse.epsilon.common.parse.EpsilonParser;
import org.eclipse.epsilon.common.parse.EpsilonTreeAdaptor;
import org.eclipse.epsilon.common.parse.Position;
import org.eclipse.epsilon.common.parse.problem.ParseProblem;
import org.eclipse.epsilon.common.util.AstUtil;
import org.eclipse.epsilon.eol.parse.EolLexer;
Expand Down Expand Up @@ -103,8 +113,7 @@ protected boolean invokeMainRule(List<CommonToken> comments) throws Exception {
getParseProblems().add(problem);
}

parseProblems.addAll(EpsilonParseProblemManager.INSTANCE.getParseProblems());
EpsilonParseProblemManager.INSTANCE.reset();
parseProblems.addAll(parser.getParseProblems());

assignAnnotations(cst);
assignComments(cst, comments);
Expand Down Expand Up @@ -163,15 +172,15 @@ private boolean parse(URI uri, final InputStream iStream) throws Exception {
String contents = s.hasNext() ? s.next() : "";
ByteArrayInputStream noTabsStream = new ByteArrayInputStream(contents.replaceAll("\t", " ").getBytes());

EpsilonParseProblemManager.INSTANCE.reset();
final Lexer lexer = createLexer(new ANTLRInputStream(noTabsStream));
final CommonTokenStream stream = new CommonTokenStream(lexer);
List<CommonToken> comments = extractComments(stream);
final EpsilonTreeAdaptor adaptor = new EpsilonTreeAdaptor(uri, this);

parser = createParser(stream);
parser.setDeepTreeAdaptor(adaptor);

parser.getParseProblems().addAll(((org.eclipse.epsilon.common.parse.Lexer) lexer).getParseProblems());

return invokeMainRule(comments);
}
catch (Exception ex) {
Expand Down
Loading

0 comments on commit 6f0ac0e

Please sign in to comment.