11package io .cucumber .cucumberexpressions ;
22
3- import org .apiguardian .api .API ;
4-
5- import java .util .List ;
63import java .util .Objects ;
74import java .util .StringJoiner ;
85
96import static java .util .Objects .requireNonNull ;
10- import static java .util .stream .Collectors .joining ;
11- import static org .apiguardian .api .API .Status .EXPERIMENTAL ;
12-
13- @ API (since = "18.1" , status = EXPERIMENTAL )
14- public final class Ast {
157
16- public static final char escapeCharacter = '\\' ;
17- public static final char alternationCharacter = '/' ;
18- public static final char beginParameterCharacter = '{' ;
19- public static final char endParameterCharacter = '}' ;
20- public static final char beginOptionalCharacter = '(' ;
21- public static final char endOptionalCharacter = ')' ;
8+ final class Ast {
229
2310 interface Located {
2411 int start ();
@@ -27,143 +14,21 @@ interface Located {
2714
2815 }
2916
30- public static final class Node implements Located {
31-
32- private final NodeType type ;
33- private final List <Node > nodes ;
34- private final String token ;
35- private final int start ;
36- private final int end ;
37-
38- Node (NodeType type , int start , int end , String token ) {
39- this (type , start , end , null , requireNonNull (token ));
40- }
41-
42- Node (NodeType type , int start , int end , List <Node > nodes ) {
43- this (type , start , end , requireNonNull (nodes ), null );
44- }
45-
46- private Node (NodeType type , int start , int end , List <Node > nodes , String token ) {
47- this .type = requireNonNull (type );
48- this .nodes = nodes ;
49- this .token = token ;
50- this .start = start ;
51- this .end = end ;
52- }
53-
54- public enum NodeType {
55- TEXT_NODE ,
56- OPTIONAL_NODE ,
57- ALTERNATION_NODE ,
58- ALTERNATIVE_NODE ,
59- PARAMETER_NODE ,
60- EXPRESSION_NODE
61- }
62-
63- public int start () {
64- return start ;
65- }
66-
67- public int end () {
68- return end ;
69- }
70-
71- /**
72- * @return child nodes, {@code null} if a leaf-node
73- */
74- public List <Node > nodes () {
75- return nodes ;
76- }
77-
78- public NodeType type () {
79- return type ;
80- }
81-
82- /**
83- * @return the text contained with in this node, {@code null} if not a leaf-node
84- */
85- public String token () {
86- return token ;
87- }
88-
89- String text () {
90- if (nodes == null )
91- return token ;
92-
93- return nodes ().stream ()
94- .map (Node ::text )
95- .collect (joining ());
96- }
97-
98- @ Override
99- public String toString () {
100- return toString (0 ).toString ();
101- }
102-
103- private StringBuilder toString (int depth ) {
104- StringBuilder sb = new StringBuilder ();
105- for (int i = 0 ; i < depth ; i ++) {
106- sb .append (" " );
107- }
108- sb .append ("{" )
109- .append ("\" type\" : \" " ).append (type )
110- .append ("\" , \" start\" : " )
111- .append (start )
112- .append (", \" end\" : " )
113- .append (end );
114-
115- if (token != null ) {
116- sb .append (", \" token\" : \" " ).append (token .replaceAll ("\\ \\ " , "\\ \\ \\ \\ " )).append ("\" " );
117- }
118-
119- if (nodes != null ) {
120- sb .append (", \" nodes\" : " );
121- if (!nodes .isEmpty ()) {
122- StringBuilder padding = new StringBuilder ();
123- for (int i = 0 ; i < depth ; i ++) {
124- padding .append (" " );
125- }
126- sb .append (nodes .stream ()
127- .map (node -> node .toString (depth + 1 ))
128- .collect (joining (",\n " , "[\n " , "\n " + padding + "]" )));
129-
130- } else {
131- sb .append ("[]" );
132- }
133- }
134- sb .append ("}" );
135- return sb ;
136- }
137-
138- @ Override
139- public boolean equals (Object o ) {
140- if (this == o )
141- return true ;
142- if (o == null || getClass () != o .getClass ())
143- return false ;
144- Node node = (Node ) o ;
145- return start == node .start &&
146- end == node .end &&
147- type == node .type &&
148- Objects .equals (nodes , node .nodes ) &&
149- Objects .equals (token , node .token );
150- }
151-
152- @ Override
153- public int hashCode () {
154- return Objects .hash (type , nodes , token , start , end );
155- }
156-
157- }
158-
15917 static final class Token implements Located {
16018
19+ private static final char escapeCharacter = '\\' ;
20+ private static final char alternationCharacter = '/' ;
21+ private static final char beginParameterCharacter = '{' ;
22+ private static final char endParameterCharacter = '}' ;
23+ private static final char beginOptionalCharacter = '(' ;
24+ private static final char endOptionalCharacter = ')' ;
25+
16126 final String text ;
162- final TokenType type ;
27+ final Type type ;
16328 final int start ;
16429 final int end ;
16530
166- Token (String text , TokenType type , int start , int end ) {
31+ Token (String text , Type type , int start , int end ) {
16732 this .text = requireNonNull (text );
16833 this .type = requireNonNull (type );
16934 this .start = start ;
@@ -186,23 +51,23 @@ static boolean canEscape(Integer token) {
18651 return false ;
18752 }
18853
189- static TokenType typeOf (Integer token ) {
54+ static Type typeOf (Integer token ) {
19055 if (Character .isWhitespace (token )) {
191- return TokenType .WHITE_SPACE ;
56+ return Type .WHITE_SPACE ;
19257 }
19358 switch (token ) {
19459 case (int ) alternationCharacter :
195- return TokenType .ALTERNATION ;
60+ return Type .ALTERNATION ;
19661 case (int ) beginParameterCharacter :
197- return TokenType .BEGIN_PARAMETER ;
62+ return Type .BEGIN_PARAMETER ;
19863 case (int ) endParameterCharacter :
199- return TokenType .END_PARAMETER ;
64+ return Type .END_PARAMETER ;
20065 case (int ) beginOptionalCharacter :
201- return TokenType .BEGIN_OPTIONAL ;
66+ return Type .BEGIN_OPTIONAL ;
20267 case (int ) endOptionalCharacter :
203- return TokenType .END_OPTIONAL ;
68+ return Type .END_OPTIONAL ;
20469 }
205- return TokenType .TEXT ;
70+ return Type .TEXT ;
20671 }
20772
20873 static boolean isEscapeCharacter (int token ) {
@@ -245,7 +110,7 @@ public String toString() {
245110 .toString ();
246111 }
247112
248- enum TokenType {
113+ enum Type {
249114 START_OF_LINE ,
250115 END_OF_LINE ,
251116 WHITE_SPACE ,
@@ -259,11 +124,11 @@ enum TokenType {
259124 private final String symbol ;
260125 private final String purpose ;
261126
262- TokenType () {
127+ Type () {
263128 this (null , null );
264129 }
265130
266- TokenType (String symbol , String purpose ) {
131+ Type (String symbol , String purpose ) {
267132 this .symbol = symbol ;
268133 this .purpose = purpose ;
269134 }
0 commit comments