01: package fri.patterns.interpreter.parsergenerator.lexer.semantics;
02:
03: import java.util.*;
04: import java.lang.reflect.Method;
05: import fri.patterns.interpreter.parsergenerator.lexer.LexerSemantic;
06: import fri.patterns.interpreter.parsergenerator.lexer.ResultTree;
07: import fri.patterns.interpreter.parsergenerator.syntax.Rule;
08:
09: /**
10: * A LexerSemantic that provides the Set of wanted String nonterminals
11: * by method reflection. Callback methods must be of the form
12: * <pre>
13: * public void EncodingDecl(ResultTree resultTree) {
14: * this.encodingDecl = resultTree.toString();
15: * }
16: * </pre>
17: *
18: * Created on 21.09.2005
19: * @author Fritz Ritzberger
20: */
21: public abstract class LexerReflectSemantic implements LexerSemantic {
22: /**
23: * As expected, this method delegates to the method that is named like the
24: * nonterminal of the passed Rule, with <i>resultTree</i> as the only argument.
25: */
26: public void ruleEvaluated(Rule rule, ResultTree resultTree) {
27: try {
28: Method m = getClass().getMethod(rule.getNonterminal(),
29: new Class[] { ResultTree.class });
30: m.setAccessible(true);
31: m.invoke(this , new Object[] { resultTree });
32: } catch (Exception e) {
33: e.printStackTrace();
34: }
35: }
36:
37: /**
38: * Provides the Set of wanted nonterminal Strings by searching for
39: * public void methods with (String, Token.Range) arguments.
40: */
41: public Set getWantedNonterminals() {
42: Method[] methods = getClass().getMethods();
43: Set set = new HashSet(methods.length);
44:
45: for (int i = 0; i < methods.length; i++) {
46: Method m = methods[i];
47: Class[] argClasses = m.getParameterTypes();
48:
49: if (m.getReturnType().equals(void.class)
50: && argClasses.length == 1
51: && argClasses[0].equals(ResultTree.class)) {
52: String name = m.getName();
53: set.add(name);
54: }
55: }
56: return set;
57: }
58:
59: /**
60: * Returns null as getWantedNonterminals() is provided.
61: */
62: public Set getIgnoredNonterminals() {
63: return null;
64: }
65:
66: }
|