25
25
26
26
import static mx .kenzie .foundation .WriteInstruction .*;
27
27
28
+ /**
29
+ * A syntax element.
30
+ * This instance controls the matching and compiling for a specific element.
31
+ * The library it comes from controls where it can be matched.
32
+ */
28
33
public interface SyntaxElement {
29
34
35
+ /**
36
+ * Attempts to match this element to a source string.
37
+ * The context can be accessed, but should not be modified here.
38
+ * If the source does not match this element, return `null`.
39
+ * If the source does match this element, return a match object with the match data in.
40
+ * <p>
41
+ * Complex patterns are advised to override this and attempt faster
42
+ * checks with {@link String#contains(CharSequence)} before running the super matcher.
43
+ * This will circumvent a complex regex having to be checked every time.
44
+ *
45
+ * @param thing the source
46
+ * @param context the context of this source
47
+ * @return the match result
48
+ */
30
49
default Pattern .Match match (String thing , Context context ) {
31
- return getPattern ().match (thing , context );
50
+ return this . getPattern ().match (thing , context );
32
51
}
33
52
53
+ /**
54
+ * The pattern metadata object.
55
+ *
56
+ * @return the pattern
57
+ */
34
58
Pattern getPattern ();
35
59
60
+ /**
61
+ * The library that provides this syntax element.
62
+ *
63
+ * @return the provider
64
+ */
36
65
Library getProvider ();
37
66
67
+ /**
68
+ * For basic syntax, this can return a method that will be used as a handle.
69
+ *
70
+ * @param type the handler mode
71
+ * @return the method handle
72
+ */
38
73
Method getHandler (HandlerType type );
39
74
40
75
void setHandler (HandlerType type , Method method );
@@ -60,14 +95,35 @@ default boolean allowAsInputFor(Type type) {
60
95
61
96
boolean hasHandler (HandlerType type );
62
97
98
+ /**
99
+ * The return type of this element, used for type tracking during compilation.
100
+ *
101
+ * @return the return type
102
+ */
63
103
default Type getReturnType () {
64
104
return CommonTypes .VOID ;
65
105
}
66
106
107
+ /**
108
+ * This is called during the first compiler pass, Out->In, L->R.
109
+ * Simple elements will not need this, but lookahead or re-ordering operations can be run here.
110
+ *
111
+ * @param context the compiler context
112
+ * @param match the match used
113
+ * @throws Throwable to avoid unnecessary try/catch blocks
114
+ */
67
115
default void preCompile (Context context , Pattern .Match match ) throws Throwable {
68
116
// Very few elements require a lookahead.
69
117
}
70
118
119
+ /**
120
+ * This is called during the second compiler pass, In->Out, L->R.
121
+ * Non-trivial syntax may need to override this to provide special behaviour.
122
+ *
123
+ * @param context the compiler context
124
+ * @param match the match used
125
+ * @throws Throwable to avoid unnecessary try/catch blocks
126
+ */
71
127
void compile (Context context , Pattern .Match match ) throws Throwable ;
72
128
73
129
default boolean allowedIn (State state , Context context ) {
@@ -86,6 +142,15 @@ default void addSkipInstruction(Context context, Consumer<Context> consumer) {
86
142
context .addSkipInstruction (consumer );
87
143
}
88
144
145
+ /**
146
+ * This writes a smart method call for the syntax, obeying rewrite rules.
147
+ * This can perform inlining, extraction and basic rewrites.
148
+ * This also has access to the bridge compiler.
149
+ *
150
+ * @param builder the method builder to use
151
+ * @param method the target method
152
+ * @param context the compiler context
153
+ */
89
154
default void writeCall (final MethodBuilder builder , final Method method , final Context context ) {
90
155
final ForceInline inline = method .getAnnotation (ForceInline .class );
91
156
final ForceExtract extract = method .getAnnotation (ForceExtract .class );
@@ -133,6 +198,9 @@ default void writeCall(final MethodBuilder builder, final Method method, final C
133
198
}
134
199
}
135
200
201
+ /**
202
+ * A wrapped version of {@link Class#getMethod(String, Class[])} to avoid catching exceptions.
203
+ */
136
204
default Method findMethod (Class <?> owner , String name , Class <?>... parameters ) {
137
205
try {
138
206
return owner .getMethod (name , parameters );
@@ -168,7 +236,7 @@ default String name() {
168
236
169
237
default String description () {
170
238
final Documentation documentation = this .getClass ().getAnnotation (Documentation .class );
171
- if (documentation == null ) return "None ." ;
239
+ if (documentation == null ) return "No description ." ;
172
240
return documentation .description ();
173
241
}
174
242
@@ -179,9 +247,7 @@ default String[] examples() {
179
247
}
180
248
181
249
class Handlers extends HashMap <HandlerType , Method > {
182
-
183
- public static final Handlers EMPTY = new Handlers ();
184
-
250
+
185
251
}
186
252
187
253
}
0 commit comments