2525
2626import static mx .kenzie .foundation .WriteInstruction .*;
2727
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+ */
2833public interface SyntaxElement {
2934
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+ */
3049 default Pattern .Match match (String thing , Context context ) {
31- return getPattern ().match (thing , context );
50+ return this . getPattern ().match (thing , context );
3251 }
3352
53+ /**
54+ * The pattern metadata object.
55+ *
56+ * @return the pattern
57+ */
3458 Pattern getPattern ();
3559
60+ /**
61+ * The library that provides this syntax element.
62+ *
63+ * @return the provider
64+ */
3665 Library getProvider ();
3766
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+ */
3873 Method getHandler (HandlerType type );
3974
4075 void setHandler (HandlerType type , Method method );
@@ -60,14 +95,35 @@ default boolean allowAsInputFor(Type type) {
6095
6196 boolean hasHandler (HandlerType type );
6297
98+ /**
99+ * The return type of this element, used for type tracking during compilation.
100+ *
101+ * @return the return type
102+ */
63103 default Type getReturnType () {
64104 return CommonTypes .VOID ;
65105 }
66106
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+ */
67115 default void preCompile (Context context , Pattern .Match match ) throws Throwable {
68116 // Very few elements require a lookahead.
69117 }
70118
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+ */
71127 void compile (Context context , Pattern .Match match ) throws Throwable ;
72128
73129 default boolean allowedIn (State state , Context context ) {
@@ -86,6 +142,15 @@ default void addSkipInstruction(Context context, Consumer<Context> consumer) {
86142 context .addSkipInstruction (consumer );
87143 }
88144
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+ */
89154 default void writeCall (final MethodBuilder builder , final Method method , final Context context ) {
90155 final ForceInline inline = method .getAnnotation (ForceInline .class );
91156 final ForceExtract extract = method .getAnnotation (ForceExtract .class );
@@ -133,6 +198,9 @@ default void writeCall(final MethodBuilder builder, final Method method, final C
133198 }
134199 }
135200
201+ /**
202+ * A wrapped version of {@link Class#getMethod(String, Class[])} to avoid catching exceptions.
203+ */
136204 default Method findMethod (Class <?> owner , String name , Class <?>... parameters ) {
137205 try {
138206 return owner .getMethod (name , parameters );
@@ -168,7 +236,7 @@ default String name() {
168236
169237 default String description () {
170238 final Documentation documentation = this .getClass ().getAnnotation (Documentation .class );
171- if (documentation == null ) return "None ." ;
239+ if (documentation == null ) return "No description ." ;
172240 return documentation .description ();
173241 }
174242
@@ -179,9 +247,7 @@ default String[] examples() {
179247 }
180248
181249 class Handlers extends HashMap <HandlerType , Method > {
182-
183- public static final Handlers EMPTY = new Handlers ();
184-
250+
185251 }
186252
187253}
0 commit comments