Skip to content

Commit 1fb9c8b

Browse files
committed
Show function call position in call stack
1 parent 39b94a0 commit 1fb9c8b

File tree

14 files changed

+65
-16
lines changed

14 files changed

+65
-16
lines changed

ownlang-core/src/main/java/com/annimon/ownlang/lib/CallStack.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ public static synchronized void clear() {
1313
calls.clear();
1414
}
1515

16-
public static synchronized void enter(String name, Function function) {
17-
calls.push(new CallInfo(name, function.toString()));
16+
public static synchronized void enter(String name, Function function, String position) {
17+
String func = function.toString();
18+
if (func.contains("com.annimon.ownlang.modules")) {
19+
func = func.replaceAll(
20+
"com.annimon.ownlang.modules.(\\w+)\\.?\\1?", "module $1");
21+
}
22+
if (func.contains("\n")) {
23+
func = func.substring(0, func.indexOf("\n")).trim();
24+
}
25+
calls.push(new CallInfo(name, func, position));
1826
}
1927

2028
public static synchronized void exit() {
@@ -25,10 +33,14 @@ public static synchronized Deque<CallInfo> getCalls() {
2533
return calls;
2634
}
2735

28-
public record CallInfo(String name, String function) {
36+
public record CallInfo(String name, String function, String position) {
2937
@Override
3038
public String toString() {
31-
return String.format("%s: %s", name, function);
39+
if (position == null) {
40+
return String.format("%s: %s", name, function);
41+
} else {
42+
return String.format("%s: %s %s", name, function, position);
43+
}
3244
}
3345
}
3446
}

ownlang-parser/src/main/java/com/annimon/ownlang/parser/Pos.java renamed to ownlang-core/src/main/java/com/annimon/ownlang/util/Pos.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.annimon.ownlang.parser;
1+
package com.annimon.ownlang.util;
22

33
public record Pos(int row, int col) {
44
public static final Pos UNKNOWN = new Pos(-1, -1);

ownlang-parser/src/main/java/com/annimon/ownlang/parser/Range.java renamed to ownlang-core/src/main/java/com/annimon/ownlang/util/Range.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.annimon.ownlang.parser;
1+
package com.annimon.ownlang.util;
22

33
import java.util.Objects;
44

@@ -18,8 +18,10 @@ public boolean isOnSameLine() {
1818
}
1919

2020
public String format() {
21-
if (isOnSameLine()) {
21+
if (isEqualPosition())
2222
return start.format();
23+
else if (isOnSameLine()) {
24+
return "[%d:%d~%d]".formatted(start.row(), start.col(), end.col());
2325
} else {
2426
return start.format() + "..." + end.format();
2527
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.annimon.ownlang.util;
2+
3+
public interface SourceLocation {
4+
5+
default Range getRange() {
6+
return null;
7+
}
8+
9+
default String formatRange() {
10+
final var range = getRange();
11+
return range == null ? "" : range.format();
12+
}
13+
}

ownlang-parser/src/main/java/com/annimon/ownlang/exceptions/BaseParserException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.annimon.ownlang.exceptions;
22

3-
import com.annimon.ownlang.parser.Range;
3+
import com.annimon.ownlang.util.Range;
44

55
/**
66
* Base type for all lexer and parser exceptions

ownlang-parser/src/main/java/com/annimon/ownlang/exceptions/ParseException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.annimon.ownlang.exceptions;
22

3-
import com.annimon.ownlang.parser.Range;
3+
import com.annimon.ownlang.util.Range;
44

55
/**
66
*

ownlang-parser/src/main/java/com/annimon/ownlang/lib/UserDefinedFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public String getArgsName(int index) {
3131
}
3232

3333
@Override
34-
public Value execute(Value... values) {
34+
public Value execute(Value[] values) {
3535
final int size = values.length;
3636
final int requiredArgsCount = arguments.getRequiredArgumentsCount();
3737
if (size < requiredArgsCount) {

ownlang-parser/src/main/java/com/annimon/ownlang/parser/Lexer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.annimon.ownlang.exceptions.OwnLangParserException;
44
import com.annimon.ownlang.parser.error.ParseError;
5+
import com.annimon.ownlang.util.Pos;
6+
import com.annimon.ownlang.util.Range;
57
import java.util.*;
68

79
/**

ownlang-parser/src/main/java/com/annimon/ownlang/parser/Parser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.annimon.ownlang.parser.ast.*;
99
import com.annimon.ownlang.parser.error.ParseError;
1010
import com.annimon.ownlang.parser.error.ParseErrors;
11+
import com.annimon.ownlang.util.Pos;
12+
import com.annimon.ownlang.util.Range;
1113
import java.util.*;
1214
import java.util.function.Function;
1315
import java.util.stream.Collectors;
@@ -351,12 +353,14 @@ private Expression functionChain(Expression qualifiedNameExpr) {
351353

352354
private FunctionalExpression function(Expression qualifiedNameExpr) {
353355
// function(arg1, arg2, ...)
356+
final var startTokenIndex = index - 1;
354357
consume(TokenType.LPAREN);
355358
final FunctionalExpression function = new FunctionalExpression(qualifiedNameExpr);
356359
while (!match(TokenType.RPAREN)) {
357360
function.addArgument(expression());
358361
match(TokenType.COMMA);
359362
}
363+
function.setRange(getRange(startTokenIndex, index - 1));
360364
return function;
361365
}
362366

ownlang-parser/src/main/java/com/annimon/ownlang/parser/Token.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.annimon.ownlang.parser;
22

3+
import com.annimon.ownlang.util.Pos;
4+
35
/**
46
* @author aNNiMON
57
*/

0 commit comments

Comments
 (0)