fri.patterns.interpreter.parsergenerator.semantics
Class ReflectSemantic

java.lang.Object
  |
  +--fri.patterns.interpreter.parsergenerator.semantics.ReflectSemantic
All Implemented Interfaces:
Semantic
Direct Known Subclasses:
Calculator

public abstract class ReflectSemantic
extends java.lang.Object
implements Semantic

Semantic base class that tries to call methods with the same name as nonterminal on left side of rule. The number of parameters of the method must be the same as the number of symbols on the right side of the rule, all parameters must be of type Object, and the return type must be Object. The method might process the parse results and return an arbitrary Object to be processed by another callback method. The ranges of the currently dispatched rule is available by protected getRanges() method.

Example: Both rules

                expression ::= expression "+" term ;
                expression ::= expression "-" term ;
        
would be dispatched by
                public Object expression(Object expression, Object operator, Object term)	{
                        int e = ((Integer) expression).intValue();
                        int t = ((Integer) term).intValue();
                        if (operator.equals("+"))
                                return new Integer(e + t);
                        else
                        if (operator.equals("-"))
                                return new Integer(e - t);
                        else
                                throw new IllegalArgumentException("Unknown operator: "+operator);
                }
        

Author:
(c) 2000, Fritz Ritzberger
See Also:
Calculator

Constructor Summary
ReflectSemantic()
           
 
Method Summary
 java.lang.Object doSemantic(Rule rule, java.util.List inputTokens, java.util.List ranges)
          Tries to find a method with same name as left side of rule and number of arguments as much as elements in inputTokens list.
protected  java.lang.Object fallback(Rule rule, java.util.List inputTokens, java.util.List ranges)
          Fallback semantic handler that is called when no method was found by reflection.
protected  java.util.List getRanges()
          Returns the current Token.Range list of all input tokens.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectSemantic

public ReflectSemantic()
Method Detail

doSemantic

public java.lang.Object doSemantic(Rule rule,
                                   java.util.List inputTokens,
                                   java.util.List ranges)
Tries to find a method with same name as left side of rule and number of arguments as much as elements in inputTokens list. All argument types are Object.

Specified by:
doSemantic in interface Semantic
Parameters:
rule - the rule that was recognized by the parser.
inputTokens - all semantic call returns from underlying rules, collected according to current rule.
ranges - all line ranges for parseResults elements. Cast elements to Token.Range to get the start and end position of every Object in parseResult List.
Returns:
object to push on parser value-stack.

fallback

protected java.lang.Object fallback(Rule rule,
                                    java.util.List inputTokens,
                                    java.util.List ranges)
Fallback semantic handler that is called when no method was found by reflection. This method provides a default List aggregation for left recursive rules.

Returns:
first list element if input list has only one element, a List if the passed rule is left recursive, else the unmodified inputTokens list.

getRanges

protected java.util.List getRanges()
Returns the current Token.Range list of all input tokens.