xtc.parser
Class VariantSorter

java.lang.Object
  extended by xtc.tree.Visitor
      extended by xtc.parser.VariantSorter

public class VariantSorter
extends Visitor

Visitor to infer a grammar's variants.

This visitor assumes that the entire grammar is contained in a single module. It also assumes that the grammar's real root has been annotated and that directly left-recursive productions have not been transformed into equivalent right-iterations.

Version:
$Revision: 1.34 $
Author:
Robert Grimm

Nested Class Summary
 class VariantSorter.Registrar
          Visitor to register all generic node names with the type operations class.
 class VariantSorter.Typer
          Visitor to determine a generic production's variant type.
 
Field Summary
protected  Analyzer analyzer
          The analyzer.
protected  AST ast
          The type operations.
protected  java.util.List<Element> elements
          The list of elements representing the current alternative.
protected  VariantSorter.Typer gtyper
          The generic production typer.
protected  boolean hasChanged
          The flag for whether productions have changed in push mode.
protected  boolean isGeneric
          The flag for whether the current production is generic.
protected  boolean isPushMode
          The flag for whether the current mode is push or pull.
protected  java.util.Map<Node,Node> malformed
          The set of AST nodes resulting in an error.
protected  FullProduction production
          The current production.
protected  java.util.List<Production> productions
          The productions to be processed in push mode.
protected  Runtime runtime
          The runtime.
protected  java.util.List<Type> types
          The current types.
 
Constructor Summary
VariantSorter(Runtime runtime, Analyzer analyzer, AST ast)
          Create a new variant sorter.
 
Method Summary
protected  Type merge(VariantT v1, VariantT v2, Production p)
          Merge the two variants.
protected  void pushPull(Module m)
          Push and pull any variant types.
protected  void recurse(Element e)
          Recurse on the specified element.
protected  void setType(Production p, Type t)
          Set the specified production's type to the specified type.
 void visit(FullProduction p)
          Visit the specified production.
 void visit(Module m)
          Visit the specified grammar.
 void visit(OrderedChoice c)
          Visit the specified choice.
 void visit(Sequence s)
          Visit the specified sequence.
 
Methods inherited from class xtc.tree.Visitor
dispatch, equals, hashCode, iterate, map, mapInPlace, unableToVisit, visit
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

runtime

protected final Runtime runtime
The runtime.


analyzer

protected final Analyzer analyzer
The analyzer.


ast

protected final AST ast
The type operations.


gtyper

protected final VariantSorter.Typer gtyper
The generic production typer.


malformed

protected java.util.Map<Node,Node> malformed
The set of AST nodes resulting in an error.


isPushMode

protected boolean isPushMode
The flag for whether the current mode is push or pull.


productions

protected java.util.List<Production> productions
The productions to be processed in push mode.


hasChanged

protected boolean hasChanged
The flag for whether productions have changed in push mode.


types

protected java.util.List<Type> types
The current types. In push mode, the list contains exactly one variant type, which is being pushed through productions. In pull mode, the list contains the types of all alternatives, which may be variant types, type parameters, and error types and which are unified in visit(FullProduction).


production

protected FullProduction production
The current production.


isGeneric

protected boolean isGeneric
The flag for whether the current production is generic. We need to track this information in an explicit flag, because this visitor processes choices and sequences that will be lifted into their own productions.


elements

protected java.util.List<Element> elements
The list of elements representing the current alternative.

Constructor Detail

VariantSorter

public VariantSorter(Runtime runtime,
                     Analyzer analyzer,
                     AST ast)
Create a new variant sorter.

Parameters:
runtime - The runtime.
analyzer - The analyzer utility.
ast - The type operations.
Method Detail

merge

protected Type merge(VariantT v1,
                     VariantT v2,
                     Production p)
Merge the two variants. This method merges the second variant into the first variant, which must be polymorphic. If the second variant also is polymorphic, it simply adds the second variant's tuples to the first variant. Otherwise, it tries to create a new tuple with the second variant's original name and then adds that tuple to the first variant.

Parameters:
v1 - The first variant.
v2 - The second variant.
p - The production for error reporting.
Returns:
The first variant or the error type in case of errors.

setType

protected void setType(Production p,
                       Type t)
Set the specified production's type to the specified type. This method preserves the original type's generic attribute, unless the specified type is the error type.

Parameters:
p - The production.
t - The type.

pushPull

protected void pushPull(Module m)
Push and pull any variant types.

Parameters:
m - The grammar.

visit

public void visit(Module m)
Visit the specified grammar.


visit

public void visit(FullProduction p)
Visit the specified production.


visit

public void visit(OrderedChoice c)
Visit the specified choice.


visit

public void visit(Sequence s)
Visit the specified sequence.


recurse

protected void recurse(Element e)
Recurse on the specified element. If the element is an optionally bound nonterminal, this method processes the corresponding production. If the element is an optionally bound sequence or choice, this method processes the sequence or choice. Otherwise, it reports an error condition and returns.

Parameters:
e - The element.


Copyright © 2012. All Rights Reserved.