Skip to content

Commit 261ff50

Browse files
committed
Treat boolean literals as booleans
Previously the text probe parser would treat boolean literals as strings. This doesn't matter for assertions, as all int/string/bools are compared as strings. However, it does matter for arguments to methods. Changed it so that booleans are treated as booleans.
1 parent 1ae1392 commit 261ff50

11 files changed

Lines changed: 47 additions & 6 deletions

File tree

addnum/AddNum.jar

101 Bytes
Binary file not shown.

addnum/src/addnum/ast/Node.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public String toString() {
8787
return getClass().getSimpleName();
8888
}
8989

90+
public String valueToString(boolean hex) {
91+
return hex //
92+
? Integer.toHexString(value()) //
93+
: Integer.toString(value());
94+
}
95+
9096
@Override
9197
public Node clone() throws CloneNotSupportedException {
9298
return (Node) super.clone();

addnum/workspace/expected_test_output.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
[10:6->10:9] Expected parameter types: [java.lang.String, java.lang.Object...]
6868
[11:6->11:9] Expected parameter types: [java.lang.String, java.lang.Object...]
6969
✅ ts/ok_add.addn
70+
✅ ts/ok_boolarg.addn
7071
✅ ts/ok_boolstrcomparison.addn
7172
✅ ts/ok_bumps.addn
7273
✅ ts/ok_clone.addn
@@ -84,4 +85,4 @@
8485
✅ ts/ok_vars.addn
8586
✅ ts/ok_varspaces.addn
8687
✅ ts/ok_varvar.addn
87-
Done: 99 pass, 46 fail
88+
Done: 101 pass, 46 fail
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123 // [[$n:=Num]]
2+
// [[$n.valueToString(true)=7b]]
3+
// [[$n.valueToString(false)=123]]

server/src-test/codeprober/ExistingTextProbeTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ private void checkQueryCompletion(Query q) {
216216
case STRING:
217217
cval = DecorationsHandler.wrapLiteralValueInQuotesIfNecessary(String.valueOf(cv.asString()));
218218
break;
219+
case BOOLEAN:
220+
cval = String.valueOf(cv.asBoolean());
221+
break;
219222
default:
220223
return TraversalResult.CONTINUE;
221224
}

server/src/codeprober/requesthandler/CompleteHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ private static CompleteRes completePropAccess(TextProbeEnvironment env, Query qu
329329
case any: // a.k.a "Object"
330330
case integer:
331331
case string:
332+
case bool:
332333
return true;
333334
default:
334335
return false;

server/src/codeprober/textprobe/TextProbeEnvironment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ public QueryResult evaluateQuery(Query query, boolean automaticallyExtractErrors
244244
argValues.add(arg.asString());
245245
break;
246246

247+
case BOOLEAN:
248+
argTypes.add(Boolean.TYPE);
249+
argValues.add(arg.asBoolean());
250+
break;
251+
247252
case QUERY:
248253
final QueryResult qval = evaluateQuery(arg.asQuery());
249254
if (qval == null) {
@@ -359,6 +364,9 @@ public boolean evaluateComparison(Query tam, QueryResult lhsBody) {
359364
case STRING:
360365
rhsBody = new QueryResult(qassert.expectedValue.asString(), String.class);
361366
break;
367+
case BOOLEAN:
368+
rhsBody = new QueryResult(qassert.expectedValue.asBoolean(), Boolean.TYPE);
369+
break;
362370
default:
363371
throw new IllegalArgumentException("Unknown ExpectedValue " + qassert.expectedValue.type);
364372
}

textprobe/src/codeprober/textprobe/Parser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,16 @@ private static Expr parseExpr(Position start, ParserSource src, boolean allowLit
316316
Position numEnd = new Position(start.line, start.column + src.getOffset() - 1);
317317
return Expr.fromInt(numStart, numEnd, litNum);
318318
}
319-
for (String kw : new String[] { "true", "false", "null" }) {
319+
for (String boolLit : new String[] { "true", "false" }) {
320+
if (src.accept(boolLit)) {
321+
Position strStart = new Position(start.line, start.column + argOffset);
322+
Position strEnd = new Position(start.line, start.column + src.getOffset() - 1);
323+
return Expr.fromBoolean(strStart, strEnd, boolLit.equals("true"));
324+
}
325+
}
326+
for (String kw : new String[] { "null" }) {
320327
if (src.accept(kw)) {
321-
Position strStart = new Position(start.line, start.column + argOffset + 1);
328+
Position strStart = new Position(start.line, start.column + argOffset);
322329
Position strEnd = new Position(start.line, start.column + src.getOffset() - 1);
323330
return Expr.fromString(strStart, strEnd, kw);
324331
}

textprobe/src/codeprober/textprobe/ast/Expr.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class Expr extends AbstractASTNode {
66

77
public static enum Type {
8-
INT, STRING, QUERY,
8+
INT, STRING, BOOLEAN, QUERY,
99
}
1010

1111
public final Type type;
@@ -31,6 +31,10 @@ public static Expr fromString(Position start, Position end, String value) {
3131
return new Expr(Type.STRING, start, end, value);
3232
}
3333

34+
public static Expr fromBoolean(Position start, Position end, boolean value) {
35+
return new Expr(Type.BOOLEAN, start, end, value);
36+
}
37+
3438
public static Expr fromQuery(Query value) {
3539
return new Expr(Type.QUERY, value);
3640
}
@@ -48,6 +52,10 @@ public String asString() {
4852
return (String) value;
4953
}
5054

55+
public boolean asBoolean() {
56+
return (boolean) value;
57+
}
58+
5159
public Query asQuery() {
5260
return (Query) value;
5361
}
@@ -59,6 +67,8 @@ public String pp() {
5967
return String.valueOf(asInt());
6068
case STRING:
6169
return "\"" + asString() + "\"";
70+
case BOOLEAN:
71+
return String.valueOf(asBoolean());
6272
case QUERY:
6373
return asQuery().pp();
6474
default:

textprobe/workspace/future_syntax/magic_literals.tp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{{A.b=true}} // [[Expr.type="STRING"]] [[Expr.asString="true"]]
2-
{{A.b=false}} // [[Expr.type="STRING"]] [[Expr.asString="false"]]
1+
{{A.b=true}} // [[Expr.type="BOOLEAN"]] [[Expr.asBoolean="true"]] [[Expr.asBoolean=true]]
2+
{{A.b=false}} // [[Expr.type="BOOLEAN"]] [[Expr.asBoolean="false"]] [[Expr.asBoolean=false]]
33
{{A.b=null}} // [[Expr.type="STRING"]] [[Expr.asString="null"]]
44
{{A.b=123}} // [[Expr.type="INT"]]
55
{{A.b=foo}}

0 commit comments

Comments
 (0)