Skip to content

Commit d5f45da

Browse files
committed
Java: Clean up public API
1 parent 4fa0c90 commit d5f45da

10 files changed

Lines changed: 123 additions & 78 deletions

File tree

docs/docs/bindings/java/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class Example {
6161

6262
ParseResult result = Herb.parse(source);
6363

64-
if (result.getValue() != null) {
65-
System.out.println(result.getValue().treeInspect());
64+
if (result.value != null) {
65+
System.out.println(result.value.inspect());
6666
}
6767
}
6868
}
@@ -85,7 +85,7 @@ public class LexExample {
8585

8686
LexResult result = Herb.lex(source);
8787

88-
for (Token token : result.getTokens()) {
88+
for (Token token : result.tokens) {
8989
System.out.println(token.inspect());
9090
}
9191
}

docs/docs/bindings/java/reference.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ The `LexResult` class provides access to the lexed tokens:
5151

5252
```java
5353
public class LexResult {
54-
public List<Token> getTokens();
55-
public String getSource();
54+
public List<Token> tokens;
55+
public String source;
5656
public int getTokenCount();
5757
public boolean isEmpty();
5858
}
@@ -73,8 +73,8 @@ String source = "<p>Hello <%= user.name %></p>";
7373

7474
ParseResult result = Herb.parse(source);
7575

76-
if (result.getValue() != null) {
77-
System.out.println(result.getValue().treeInspect());
76+
if (result.value != null) {
77+
System.out.println(result.value.inspect();
7878
}
7979
// Output:
8080
// @ DocumentNode (location: (1:0)-(1:29))
@@ -133,12 +133,12 @@ The `ParseResult` class provides access to the parsed AST and any errors:
133133

134134
```java
135135
public class ParseResult {
136-
public Node getValue();
137-
public List<Node> getErrors();
138-
public String getSource();
136+
public Node value;
137+
public List<Node> errors;
138+
public String source;
139139
public boolean hasErrors();
140140
public int getErrorCount();
141-
public boolean isSuccess();
141+
public boolean isSuccessful();
142142
}
143143
```
144144

@@ -278,7 +278,7 @@ public interface Node {
278278
String getNodeType();
279279
Location getLocation();
280280
List<Node> getErrors();
281-
String treeInspect();
281+
String inspect();
282282
<T> T accept(Visitor<T> visitor);
283283
}
284284
```
@@ -291,8 +291,8 @@ Parse errors are accessible through the `ParseResult`:
291291
ParseResult result = Herb.parse(source);
292292

293293
if (result.hasErrors()) {
294-
for (Node error : result.getErrors()) {
295-
System.out.println(error.treeInspect());
294+
for (Node error : result.recursiveErrors()) {
295+
System.out.println(error.inspect());
296296
}
297297
}
298298
```

java/org/herb/CLI.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,12 @@ public static void main(String[] args) {
4141

4242
case "lex":
4343
LexResult lexResult = Herb.lex(source);
44-
45-
if (lexResult.getTokens() != null) {
46-
for (Token token : lexResult.getTokens()) {
47-
System.out.println(token.inspect());
48-
}
49-
}
44+
System.out.print(lexResult.inspect());
5045
break;
5146

5247
case "parse":
5348
ParseResult parseResult = Herb.parse(source);
54-
55-
if (parseResult.getValue() != null) {
56-
System.out.print(parseResult.getValue().treeInspect());
57-
}
58-
59-
if (parseResult.getErrors() != null && !parseResult.getErrors().isEmpty()) {
60-
System.out.println("Errors:");
61-
62-
for (Object error : parseResult.getErrors()) {
63-
if (error instanceof org.herb.ast.Node) {
64-
System.out.println(((org.herb.ast.Node) error).treeInspect());
65-
} else {
66-
System.out.println(" " + error);
67-
}
68-
}
69-
}
70-
49+
System.out.print(parseResult.inspect());
7150
break;
7251

7352
case "ruby":

java/org/herb/LexResult.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@
44
import java.util.List;
55

66
public class LexResult {
7-
private final List<Token> tokens;
8-
private final String source;
7+
public final List<Token> tokens;
8+
public final String source;
99

1010
public LexResult(List<Token> tokens, String source) {
1111
this.tokens = Collections.unmodifiableList(tokens);
1212
this.source = source;
1313
}
1414

15-
public List<Token> getTokens() {
16-
return tokens;
17-
}
18-
19-
public String getSource() {
20-
return source;
21-
}
22-
23-
public int getTokenCount() {
24-
return tokens.size();
25-
}
26-
2715
public boolean isEmpty() {
2816
return tokens.isEmpty();
2917
}
@@ -32,4 +20,14 @@ public boolean isEmpty() {
3220
public String toString() {
3321
return String.format("LexResult{tokens=%d, source=%d chars}", tokens.size(), source.length());
3422
}
23+
24+
public String inspect() {
25+
StringBuilder builder = new StringBuilder();
26+
27+
for (Token token : tokens) {
28+
builder.append(token.inspect()).append("\n");
29+
}
30+
31+
return builder.toString();
32+
}
3533
}

java/org/herb/ParseResult.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
11
package org.herb;
22

33
import org.herb.ast.Node;
4+
5+
import java.util.ArrayList;
46
import java.util.Collections;
57
import java.util.List;
68

79
public class ParseResult {
8-
private final Node value;
10+
public final Node value;
911
private final List<Node> errors;
10-
private final String source;
12+
public final String source;
1113

1214
public ParseResult(Node value, List<Node> errors, String source) {
1315
this.value = value;
1416
this.errors = Collections.unmodifiableList(errors);
1517
this.source = source;
1618
}
1719

18-
public Node getValue() {
19-
return value;
20-
}
20+
public List<Node> recursiveErrors() {
21+
List<Node> result = new ArrayList<>();
2122

22-
public List<Node> getErrors() {
23-
return errors;
24-
}
23+
result.addAll(errors);
2524

26-
public String getSource() {
27-
return source;
25+
if (value != null) {
26+
result.addAll(value.recursiveErrors());
27+
}
28+
29+
return result;
2830
}
2931

3032
public boolean hasErrors() {
31-
return !errors.isEmpty();
33+
return !recursiveErrors().isEmpty();
3234
}
3335

3436
public int getErrorCount() {
35-
return errors.size();
37+
return recursiveErrors().size();
3638
}
3739

38-
public boolean isSuccess() {
40+
public boolean isSuccessful() {
3941
return errors.isEmpty();
4042
}
4143

4244
@Override
4345
public String toString() {
4446
return String.format("ParseResult{errors=%d, source=%d chars}", errors.size(), source.length());
4547
}
48+
49+
public String inspect() {
50+
StringBuilder builder = new StringBuilder();
51+
52+
if (value != null) {
53+
builder.append(value.inspect());
54+
}
55+
56+
if (hasErrors()) {
57+
builder.append("\n\nErrors:\n");
58+
59+
for (Node error : recursiveErrors()) {
60+
builder.append(error.inspect()).append("\n");
61+
}
62+
}
63+
64+
return builder.toString();
65+
}
4666
}

java/org/herb/ast/BaseNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected String inspectErrors(String prefix) {
5959
String nextPrefix = isLast ? " " : "│ ";
6060

6161
if (error != null) {
62-
String tree = error.treeInspect();
62+
String tree = error.inspect();
6363
if (tree.endsWith("\n")) {
6464
tree = tree.substring(0, tree.length() - 1);
6565
}
@@ -92,7 +92,7 @@ protected String inspectArray(java.util.List<Node> array, String prefix) {
9292
String nextPrefix = isLast ? " " : "│ ";
9393

9494
if (item != null) {
95-
String tree = item.treeInspect();
95+
String tree = item.inspect();
9696

9797
if (tree.endsWith("\n")) {
9898
tree = tree.substring(0, tree.length() - 1);
@@ -120,7 +120,7 @@ protected String inspectArray(java.util.List<Node> array, String prefix) {
120120
*/
121121
protected String inspectNode(Node node, String prefix) {
122122
if (node == null) return "∅\n";
123-
String tree = node.treeInspect();
123+
String tree = node.inspect();
124124

125125
if (tree.endsWith("\n")) {
126126
tree = tree.substring(0, tree.length() - 1);

java/org/herb/ast/ErrorNode.java

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

33
import org.herb.Location;
44

5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
59
/**
610
* Simple wrapper for parser errors returned in ParseResult.
711
* This is a lightweight representation that can be cast to specific error types if needed.
812
*/
913
public class ErrorNode extends BaseNode {
10-
private final String errorMessage;
14+
public final String errorMessage;
1115

1216
public ErrorNode(String type, Location location, String errorMessage) {
1317
super(type, location, null);
1418
this.errorMessage = errorMessage;
1519
}
1620

17-
public String getErrorMessage() {
18-
return errorMessage;
19-
}
20-
2121
@Override
22-
public String treeInspect() {
22+
public String inspect() {
2323
StringBuilder output = new StringBuilder();
2424

2525
output.append("@ ").append(type).append(" ");
@@ -30,6 +30,15 @@ public String treeInspect() {
3030
return output.toString();
3131
}
3232

33+
@Override
34+
public List<Node> recursiveErrors() {
35+
if (errors != null && !errors.isEmpty()) {
36+
return new ArrayList<>(errors);
37+
}
38+
39+
return Collections.emptyList();
40+
}
41+
3342
@Override
3443
public String toString() {
3544
return String.format("ErrorNode{type='%s', message='%s', location=%s}", type, errorMessage, location);

java/org/herb/ast/Node.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.herb.ast;
22

33
import org.herb.Location;
4+
import java.util.List;
45

56
/**
67
* Base interface for all AST nodes.
@@ -29,5 +30,10 @@ public interface Node {
2930
/**
3031
* Return a tree-like string representation of this node with all its fields.
3132
*/
32-
String treeInspect();
33+
String inspect();
34+
35+
/**
36+
* Get all errors from this node and recursively from all child nodes.
37+
*/
38+
List<Node> recursiveErrors();
3339
}

templates/java/org/herb/ast/Errors.java.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class HerbError {
3434
return String.format("HerbError{type='%s', message='%s', location=%s}", type, message, location);
3535
}
3636

37-
public abstract String treeInspect();
37+
public abstract String inspect();
3838

3939
protected String indent(String str, int level) {
4040
if (level == 0) return str;
@@ -90,7 +90,7 @@ class <%= error.name %> extends HerbError {
9090
<%- end -%>
9191
<%- end -%>
9292
@Override
93-
public String treeInspect() {
93+
public String inspect() {
9494
StringBuilder output = new StringBuilder();
9595

9696
output.append("@ <%= error.name %> ");
@@ -109,7 +109,7 @@ class <%= error.name %> extends HerbError {
109109
output.append("\n");
110110
<%- when Herb::Template::TokenField -%>
111111
output.append("<%= symbol %> <%= field.name %>: ");
112-
output.append(<%= field.name %> != null ? <%= field.name %>.treeInspect() : "∅");
112+
output.append(<%= field.name %> != null ? <%= field.name %>.inspect() : "∅");
113113
output.append("\n");
114114
<%- end -%>
115115
<%- end -%>

0 commit comments

Comments
 (0)