Skip to content
Snippets Groups Projects
Unverified Commit a2626259 authored by Luc Everse's avatar Luc Everse :passport_control:
Browse files

Add ANTLR errors as a metric and fix flonum being greedy

parent c7a17df4
No related branches found
No related tags found
2 merge requests!148Release 2.3.0,!146Better assembly analysis redux
Pipeline #372415 canceled
......@@ -398,7 +398,12 @@ public enum MetricName {
/**
* Call graph formatted as an SVG.
*/
SVG_FLOW_GRAPH(StringMetric.class);
SVG_FLOW_GRAPH(StringMetric.class),
/**
* Any errors from the parser.
*/
PARSER_ERRORS(StringListMetric.class);
/**
* The type of the metric produced by instances with this name.
......
......
......@@ -61,7 +61,7 @@ absolute
;
constant
: CharConstant|StringConstant|BinConstant|OctConstant|DecConstant|HexConstant|Flonum|('.'? Symbol)
: CharConstant|StringConstant|BinConstant|OctConstant|DecConstant|HexConstant|Flonum|(Symbol '.' Symbol)|('.' Symbol)|Symbol
;
fragment
......@@ -82,7 +82,7 @@ Digit
fragment
FlonumTag
: '0'? [acdefghijklmnopqrstuvwyzACDEFGHIJKLMNOPQRSTUVWYZ]
: '0' [acdefghijklmnopqrstuvwyzACDEFGHIJKLMNOPQRSTUVWYZ]
;
fragment
......
......
......@@ -3,7 +3,9 @@ package nl.tudelft.ewi.auta.checker.asm;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
......@@ -17,14 +19,21 @@ import nl.tudelft.ewi.auta.checker.EntityAnalyzer;
import nl.tudelft.ewi.auta.common.model.entity.FileEntity;
import nl.tudelft.ewi.auta.common.model.metric.IntegerMetric;
import nl.tudelft.ewi.auta.common.model.metric.MetricName;
import nl.tudelft.ewi.auta.common.model.metric.StringListMetric;
import nl.tudelft.ewi.auta.common.model.metric.StringMetric;
import nl.tudelft.ewi.auta.common.model.metric.StringSetMetric;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import nl.tudelft.ewi.auta.checker.grammar.AttAsmLexer;
import nl.tudelft.ewi.auta.checker.grammar.AttAsmParser;
import nl.tudelft.ewi.auta.common.model.metric.MetricCriteriaScript;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import javax.annotation.Nullable;
......@@ -61,9 +70,12 @@ public class AttAsmAnalyzer extends EntityAnalyzer {
final var tokens = new CommonTokenStream(lexer);
final var parser = new AttAsmParser(tokens);
final var errorListener = new AttAsmErrorListener();
parser.addErrorListener(errorListener);
final var info = this.asmReader.analyze(tokens, parser);
victim.addMetric(new StringListMetric(errorListener.getErrors(), MetricName.PARSER_ERRORS));
victim.addMetric(new IntegerMetric(info.getCommentCount(),
MetricName.COMMENTED_LINE_COUNT));
victim.addMetric(new IntegerMetric(info.getInstructionCount(),
......@@ -162,7 +174,8 @@ public class AttAsmAnalyzer extends EntityAnalyzer {
MetricName.COMMENTED_LINE_COUNT,
MetricName.ASSEMBLY_INSTRUCTION_COUNT,
MetricName.ASSEMBLY_RECURSION_TARGET,
MetricName.SVG_FLOW_GRAPH
MetricName.SVG_FLOW_GRAPH,
MetricName.PARSER_ERRORS
);
}
......@@ -249,6 +262,23 @@ public class AttAsmAnalyzer extends EntityAnalyzer {
)
));
map.put(MetricName.PARSER_ERRORS, Arrays.asList(
new MetricCriteriaScript("Fail on errors",
"(() => errors => {\n"
+ " for (const error of errors) {\n"
+ " fail(error);\n"
+ " }\n"
+ "})();\n"
),
new MetricCriteriaScript("Warn on errors",
"(() => errors => {\n"
+ " for (const error of errors) {\n"
+ " warn(error);\n"
+ " }\n"
+ "})();\n"
)
));
return map;
}
}
package nl.tudelft.ewi.auta.checker.asm;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
/**
* A listener for ANTLR errors.
*/
public class AttAsmErrorListener implements ANTLRErrorListener {
/**
* The list of errors.
*/
private final ArrayList<String> errors = new ArrayList<>();
@Override
public void syntaxError(
final Recognizer<?, ?> recognizer,
final Object offendingSymbol,
final int line,
final int charPositionInLine,
final String msg,
final RecognitionException e
) {
this.errors.add("Syntax error starting at line " + line + ", column " + charPositionInLine + ": " + msg);
}
@Override
public void reportAmbiguity(
final Parser recognizer,
final DFA dfa,
final int startIndex,
final int stopIndex,
final boolean exact,
final BitSet ambigAlts,
final ATNConfigSet configs
) {
/* void */
}
@Override
public void reportAttemptingFullContext(
final Parser recognizer,
final DFA dfa,
final int startIndex,
final int stopIndex,
final BitSet conflictingAlts,
final ATNConfigSet configs
) {
/* void */
}
@Override
public void reportContextSensitivity(
final Parser recognizer,
final DFA dfa,
final int startIndex,
final int stopIndex,
final int prediction,
final ATNConfigSet configs
) {
/* void */
}
/**
* Returns the errors collected during parsing.
*
* @return the errors
*/
public List<String> getErrors() {
return this.errors;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment