Skip to content

Commit

Permalink
Make file parsing lazier
Browse files Browse the repository at this point in the history
  • Loading branch information
nikklassen committed Jun 15, 2016
1 parent 0bf3a7f commit cede96a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/main/antlr4/Manifold.g4
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tupleValue:
'(' ')';
functionTypeValue: tupleTypeValue '->' tupleTypeValue;
functionValue: functionTypeValue '{' (expression STATEMENT_TERMINATOR)* '}';
functionValue: functionTypeValue '{' (expression EXPRESSION_TERMINATOR)* '}';
////////////////////////////////////////////////////////
// //
Expand Down Expand Up @@ -98,7 +98,7 @@ expression:
// | declaration
;
STATEMENT_TERMINATOR: ';';
EXPRESSION_TERMINATOR: ';';
////////////////////////////////////////////////////////
Expand All @@ -107,4 +107,4 @@ STATEMENT_TERMINATOR: ';';
// //
////////////////////////////////////////////////////////
schematic: (expression STATEMENT_TERMINATOR)*;
schematic: (expression EXPRESSION_TERMINATOR)*;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

class ExpressionContextVisitor extends ManifoldBaseVisitor<ExpressionVertex> {

private ExpressionGraphParser parser;
private ExpressionGraph exprGraph;
private File inputFile;
private List<String> errors;
Expand All @@ -46,7 +45,6 @@ public ExpressionContextVisitor(File inputFile) {

public ExpressionContextVisitor(ExpressionGraph exprGraph, File inputFile) {
this.inputFile = inputFile;
this.parser = new ExpressionGraphParser();
this.exprGraph = exprGraph;
this.errors = new ArrayList<>();
}
Expand Down Expand Up @@ -366,16 +364,7 @@ public ExpressionVertex visitImportExpr(ImportExprContext context) {
return null;
}

ExpressionGraph g;
try {
g = parser.parseFile(importedFile);
} catch (Exception e) {
errors.add(e.getMessage().trim());
errors.add("Error while parsing included file " + filePath);
return null;
}

ExpressionVertex v = new ImportVertex(exprGraph, g);
ExpressionVertex v = new ImportVertex(exprGraph, importedFile);
exprGraph.addVertex(v);
return v;
}
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/org/manifold/compiler/front/ImportVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import org.manifold.compiler.Value;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ImportVertex extends ExpressionVertex {

private File importedFile;
private NamespaceIdentifier namespace;
private ExpressionGraph includedGraph;
private TypeValue type;
private Value value;

public ImportVertex(ExpressionGraph exprGraph, ExpressionGraph includedGraph) {
public ImportVertex(ExpressionGraph exprGraph, File importedFile) {
super(exprGraph);
this.includedGraph = includedGraph;
this.importedFile = importedFile;
}

@Override
Expand All @@ -37,6 +38,16 @@ public void elaborate() throws Exception {
return;
}

ExpressionGraphParser parser = new ExpressionGraphParser();
ExpressionGraph importedGraph;
try {
importedGraph = parser.parseFile(importedFile);
} catch (FrontendBuildException e) {
throw new FrontendBuildException(
"Could not parse " + importedFile.getName() + "\n" +
e.getMessage());
}

ExpressionGraph exprGraph = getExpressionGraph();
List<ExpressionEdge> targets = exprGraph.getEdgesFromSource(this);
if (targets.size() != 1) {
Expand All @@ -48,7 +59,7 @@ public void elaborate() throws Exception {
}
namespace = new NamespaceIdentifier(((VariableReferenceVertex) target).getId().getName());

exprGraph.addSubGraph(includedGraph, namespace);
exprGraph.addSubGraph(importedGraph, namespace);
this.value = new ImportValue(getExpressionGraph(), namespace);
this.type = this.value.getType();
}
Expand All @@ -75,7 +86,7 @@ public void writeToDOTFile(BufferedWriter writer) throws IOException {

@Override
public ExpressionVertex copy(ExpressionGraph g, Map<ExpressionEdge, ExpressionEdge> edgeMap) {
ImportVertex v = new ImportVertex(g, includedGraph);
ImportVertex v = new ImportVertex(g, importedFile);
v.namespace = namespace;
return v;
}
Expand Down
8 changes: 7 additions & 1 deletion tests/buildErrors/importFileWithError.manifold
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import "parseError.manifold";
error = import "parseError.manifold";

digitalIn = primitive port Bool;
outputPin = primitive node (in: digitalIn) -> (Nil);

// error isn't parsed until usage, so force the elaboration here
outputPin(error.foo);
2 changes: 1 addition & 1 deletion tests/buildErrors/importFileWithError.manifold.error
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Could not parse parseError.manifold
Error at line 2, char 0: no viable alternative at input 'compile.'
Error while parsing included file parseError.manifold

0 comments on commit cede96a

Please sign in to comment.