Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public static WPos getPos(Element e) {
return getParentSource(e);
}

/**
* The program spans its files, so it has no position of its own. Asking every compilation unit for
* one walked the whole program, and code the compiler generates is traced to the program.
*/
public static WPos getPos(WurstModel e) {
return standIn(e);
}

public static WPos getPos(WImports e) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Expand Down Expand Up @@ -65,7 +73,19 @@ private static WPos getParentSource(Element e) {
}
parent = parent.getParent();
}
return new WPos("<source of " + e + " not found>", new LineOffsets(), 0, -1);
return standIn(e);
}

/**
* An artificial position which names the kind of element. Not the element itself: printing it renders
* its whole subtree, and this is asked for often.
*/
private static WPos standIn(Element e) {
String kind = e.getClass().getSimpleName();
if (kind.endsWith("Impl")) {
kind = kind.substring(0, kind.length() - "Impl".length());
}
return new WPos("<source of " + kind + " not found>", new LineOffsets(), 0, -1);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.peeeq.wurstscript.jassAst.*;
import de.peeeq.wurstscript.jassIm.*;

import de.peeeq.wurstscript.parser.WPos;

import java.util.List;

import static de.peeeq.wurstscript.jassAst.JassAst.*;
Expand Down Expand Up @@ -76,8 +78,11 @@ public static void translate(ImStmts imStmts, List<JassStatement> stmts, JassFun
for (ImStmt s : imStmts) {
int first = stmts.size();
s.translate(stmts, f, translator);
for (int i = first; i < stmts.size(); i++) {
translator.recordSource(stmts.get(i), s.attrTrace().attrSource());
if (stmts.size() > first) {
WPos source = s.attrTrace().attrSource();
for (int i = first; i < stmts.size(); i++) {
translator.recordSource(stmts.get(i), source);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package tests.wurstscript.tests;

import de.peeeq.wurstscript.ast.CompilationUnit;
import de.peeeq.wurstscript.ast.FunctionCall;
import de.peeeq.wurstscript.ast.WurstModel;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.utils.Utils;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class PositionTests extends WurstScriptTest {

@Test
Expand All @@ -22,4 +26,42 @@ public void testFuncCallPos() {
WPos pos = c.attrErrorPos();

}

/**
* The program as a whole is in no one file, so its position is a stand-in. It used to be worked out
* from every compilation unit each time it was asked for, and code the compiler generates is traced
* to the program, so translating it walked the whole program once per statement.
*/
@Test
public void theProgramHasAStandInPosition() {
WurstModel model = testScript("blub", false, String.join(System.lineSeparator(),
"package Test",
"function foo() returns int",
" return 42",
""
));

WPos pos = model.attrSource();
assertTrue(pos.isArtificial(), pos.toString());
assertEquals(pos.getFile(), "<source of WurstModel not found>");
}

/** An element without a position names its kind in the stand-in; it used to print itself there. */
@Test
public void aStandInPositionNamesTheKindOfElement() {
WurstModel model = testScript("blub", false, String.join(System.lineSeparator(),
"package Test",
"function foo() returns int",
" return 42",
""
));

for (CompilationUnit cu : model) {
if (cu.getJassDecls().isEmpty()) {
assertEquals(cu.getJassDecls().attrSource().getFile(), "<source of JassToplevelDeclarations not found>");
return;
}
}
throw new AssertionError("expected a compilation unit without Jass declarations");
}
}
Loading