22
33import org .jspecify .annotations .Nullable ;
44
5- import java .util .List ;
65import java .util .Objects ;
76import java .util .StringJoiner ;
87
98import static java .util .Objects .requireNonNull ;
10- import static java .util .stream .Collectors .joining ;
119
1210final class Ast {
1311
14- private static final char escapeCharacter = '\\' ;
15- private static final char alternationCharacter = '/' ;
16- private static final char beginParameterCharacter = '{' ;
17- private static final char endParameterCharacter = '}' ;
18- private static final char beginOptionalCharacter = '(' ;
19- private static final char endOptionalCharacter = ')' ;
20-
2112 interface Located {
2213 int start ();
2314
2415 int end ();
2516
2617 }
2718
28- static final class Node implements Located {
29-
30- private final NodeType type ;
31- private final @ Nullable List <Node > nodes ;
32- private final @ Nullable String token ;
33- private final int start ;
34- private final int end ;
35-
36- Node (NodeType type , int start , int end , String token ) {
37- this (type , start , end , null , token );
38- }
39-
40- Node (NodeType type , int start , int end , List <Node > nodes ) {
41- this (type , start , end , nodes , null );
42- }
43-
44- private Node (NodeType type , int start , int end , @ Nullable List <Node > nodes , @ Nullable String token ) {
45- this .type = requireNonNull (type );
46- this .nodes = nodes ;
47- this .token = token ;
48- this .start = start ;
49- this .end = end ;
50- }
51-
52- enum NodeType {
53- TEXT_NODE ,
54- OPTIONAL_NODE ,
55- ALTERNATION_NODE ,
56- ALTERNATIVE_NODE ,
57- PARAMETER_NODE ,
58- EXPRESSION_NODE
59- }
60-
61- @ Override
62- public int start () {
63- return start ;
64- }
65-
66- @ Override
67- public int end () {
68- return end ;
69- }
70-
71- List <Node > nodes () {
72- return requireNonNull (nodes );
73- }
74-
75- NodeType type () {
76- return type ;
77- }
78-
79- String text () {
80- if (nodes == null )
81- return requireNonNull (token );
82-
83- return nodes .stream ()
84- .map (Node ::text )
85- .collect (joining ());
86- }
87-
88- @ Override
89- public String toString () {
90- return toString (0 ).toString ();
91- }
92-
93- private StringBuilder toString (int depth ) {
94- StringBuilder sb = new StringBuilder ();
95- for (int i = 0 ; i < depth ; i ++) {
96- sb .append (" " );
97- }
98- sb .append ("{" )
99- .append ("\" type\" : \" " ).append (type )
100- .append ("\" , \" start\" : " )
101- .append (start )
102- .append (", \" end\" : " )
103- .append (end );
104-
105- if (token != null ) {
106- sb .append (", \" token\" : \" " ).append (token .replaceAll ("\\ \\ " , "\\ \\ \\ \\ " )).append ("\" " );
107- }
108-
109- if (nodes != null ) {
110- sb .append (", \" nodes\" : " );
111- if (!nodes .isEmpty ()) {
112- StringBuilder padding = new StringBuilder ();
113- for (int i = 0 ; i < depth ; i ++) {
114- padding .append (" " );
115- }
116- sb .append (nodes .stream ()
117- .map (node -> node .toString (depth + 1 ))
118- .collect (joining (",\n " , "[\n " , "\n " +padding + "]" )));
119-
120- } else {
121- sb .append ("[]" );
122- }
123- }
124- sb .append ("}" );
125- return sb ;
126- }
127-
128- @ Override
129- public boolean equals (Object o ) {
130- if (this == o )
131- return true ;
132- if (o == null || getClass () != o .getClass ())
133- return false ;
134- Node node = (Node ) o ;
135- return start == node .start &&
136- end == node .end &&
137- type == node .type &&
138- Objects .equals (nodes , node .nodes ) &&
139- Objects .equals (token , node .token );
140- }
141-
142- @ Override
143- public int hashCode () {
144- return Objects .hash (type , nodes , token , start , end );
145- }
146-
147- }
148-
14919 static final class Token implements Located {
15020
21+ private static final char escapeCharacter = '\\' ;
22+ private static final char alternationCharacter = '/' ;
23+ private static final char beginParameterCharacter = '{' ;
24+ private static final char endParameterCharacter = '}' ;
25+ private static final char beginOptionalCharacter = '(' ;
26+ private static final char endOptionalCharacter = ')' ;
27+
15128 final String text ;
152- final TokenType type ;
29+ final Type type ;
15330 final int start ;
15431 final int end ;
15532
156- Token (String text , TokenType type , int start , int end ) {
33+ Token (String text , Type type , int start , int end ) {
15734 this .text = requireNonNull (text );
15835 this .type = requireNonNull (type );
15936 this .start = start ;
@@ -165,7 +42,7 @@ static boolean canEscape(Integer token) {
16542 return true ;
16643 }
16744 return switch (token ) {
168- case (int ) escapeCharacter ,
45+ case (int ) escapeCharacter ,
16946 (int ) alternationCharacter ,
17047 (int ) beginParameterCharacter ,
17148 (int ) endParameterCharacter ,
@@ -175,17 +52,17 @@ static boolean canEscape(Integer token) {
17552 };
17653 }
17754
178- static TokenType typeOf (Integer token ) {
55+ static Type typeOf (Integer token ) {
17956 if (Character .isWhitespace (token )) {
180- return TokenType .WHITE_SPACE ;
57+ return Type .WHITE_SPACE ;
18158 }
18259 return switch (token ) {
183- case (int ) alternationCharacter -> TokenType .ALTERNATION ;
184- case (int ) beginParameterCharacter -> TokenType .BEGIN_PARAMETER ;
185- case (int ) endParameterCharacter -> TokenType .END_PARAMETER ;
186- case (int ) beginOptionalCharacter -> TokenType .BEGIN_OPTIONAL ;
187- case (int ) endOptionalCharacter -> TokenType .END_OPTIONAL ;
188- default -> TokenType .TEXT ;
60+ case (int ) alternationCharacter -> Type .ALTERNATION ;
61+ case (int ) beginParameterCharacter -> Type .BEGIN_PARAMETER ;
62+ case (int ) endParameterCharacter -> Type .END_PARAMETER ;
63+ case (int ) beginOptionalCharacter -> Type .BEGIN_OPTIONAL ;
64+ case (int ) endOptionalCharacter -> Type .END_OPTIONAL ;
65+ default -> Type .TEXT ;
18966 };
19067 }
19168
@@ -223,15 +100,15 @@ public int hashCode() {
223100
224101 @ Override
225102 public String toString () {
226- return new StringJoiner (", " , "" + " {" , "}" )
103+ return new StringJoiner (", " , "{" , "}" )
227104 .add ("\" type\" : \" " + type + "\" " )
228- .add ("\" start\" : " + start + "" )
229- .add ("\" end\" : " + end + "" )
105+ .add ("\" start\" : " + start )
106+ .add ("\" end\" : " + end )
230107 .add ("\" text\" : \" " + text + "\" " )
231108 .toString ();
232109 }
233110
234- enum TokenType {
111+ enum Type {
235112 START_OF_LINE ,
236113 END_OF_LINE ,
237114 WHITE_SPACE ,
@@ -245,11 +122,11 @@ enum TokenType {
245122 private final @ Nullable String symbol ;
246123 private final @ Nullable String purpose ;
247124
248- TokenType () {
125+ Type () {
249126 this (null , null );
250127 }
251128
252- TokenType (@ Nullable String symbol , @ Nullable String purpose ) {
129+ Type (@ Nullable String symbol , @ Nullable String purpose ) {
253130 this .symbol = symbol ;
254131 this .purpose = purpose ;
255132 }
0 commit comments