com.google.caja.lexer
Class PositionInferer

java.lang.Object
  extended by com.google.caja.lexer.PositionInferer

public abstract class PositionInferer
extends java.lang.Object

Does some simple constraint solving to assign reasonable position values to generated parse tree nodes.

Usage

  1. Create a PositionInferer, and implement the abstract methods. Pass to the constructor a file position from the same source as nodes with known file positions.
  2. Add constraints by calling contains(java.lang.Object, java.lang.Object), precedes(java.lang.Object, java.lang.Object) and friends. This class is meant to be agnostic to the classes used to implement the parse tree, but a node descriptor should typically be a ParseTreeNode or org.w3c.dom.Node. Object descriptors are compared by reference identity, not Object.equals(java.lang.Object). Any node descriptor not mentioned in a constraint will not have a position inferred. If there are contradictory constraints, then solve will behave as if some non-contradictory subset of constraints were added, though this subset is unpredictable.
  3. Call solve() to solve constraints. This will cause a flurry of calls to setPosForNode(java.lang.Object, com.google.caja.lexer.FilePosition). Consider the file positions as advisory and ignore as you like. Ignoring a set will not affect the quality of later inferences.

Author:
mikesamuel@gmail.com

Nested Class Summary
private static class PositionInferer.Boundary
          An edge of a node descriptor's position.
private static class PositionInferer.EqualRelation
          A relation between two boundaries that should be at the same actual position.
private static class PositionInferer.LessThanRelation
          A relation between a boundary that appears at or before another boundary.
private static class PositionInferer.Region
          A start boundary and an end boundary.
private static class PositionInferer.Relation
          A relationship between two boundaries that constrains the positions of the set of boundaries.
 
Field Summary
private  java.util.List<PositionInferer.Boundary> boundaries
           
private  java.util.Map<java.lang.Object,PositionInferer.Region> boundsByNode
           
private  SourceBreaks breaks
          Used to construct inferred file positions.
private  java.util.List<PositionInferer.Relation> relations
           
private static int UNSPECIFIED_MAX
           
private static int UNSPECIFIED_MIN
           
 
Constructor Summary
PositionInferer(FilePosition spanningPos)
           
 
Method Summary
 void adjacent(java.lang.Object before, java.lang.Object after)
          Adds a constraint that requires that the end of before is the same as the start of after.
private  PositionInferer.Region boundsForNode(java.lang.Object o)
           
 void contains(java.lang.Object container, java.lang.Object contained)
          Adds a constraint that requires that contained's start and end falls (inclusively) between container's start and end.
protected abstract  FilePosition getPosForNode(java.lang.Object o)
          Returns the file position for the given node descriptor or an unknown position if the position needs to be inferred.
 void precedes(java.lang.Object before, java.lang.Object after)
          Adds a constraint that requires that the end of before is at or before the start of after.
protected abstract  void setPosForNode(java.lang.Object o, FilePosition pos)
          Informs the client that it has inferred a position for the given node.
 void solve()
          Attempts to satisfy all constraints added thus far, and then invokes setPosForNode(java.lang.Object, com.google.caja.lexer.FilePosition) for each node descriptor for which it could conclude a position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

boundaries

private final java.util.List<PositionInferer.Boundary> boundaries

relations

private final java.util.List<PositionInferer.Relation> relations

boundsByNode

private final java.util.Map<java.lang.Object,PositionInferer.Region> boundsByNode

breaks

private final SourceBreaks breaks
Used to construct inferred file positions.


UNSPECIFIED_MAX

private static final int UNSPECIFIED_MAX
See Also:
Constant Field Values

UNSPECIFIED_MIN

private static final int UNSPECIFIED_MIN
Constructor Detail

PositionInferer

public PositionInferer(FilePosition spanningPos)
Method Detail

contains

public void contains(java.lang.Object container,
                     java.lang.Object contained)
Adds a constraint that requires that contained's start and end falls (inclusively) between container's start and end.

Parameters:
container - a valid node descriptor.
contained - a valid node descriptor.

precedes

public void precedes(java.lang.Object before,
                     java.lang.Object after)
Adds a constraint that requires that the end of before is at or before the start of after.

Parameters:
before - a valid node descriptor.
after - a valid node descriptor.

adjacent

public void adjacent(java.lang.Object before,
                     java.lang.Object after)
Adds a constraint that requires that the end of before is the same as the start of after. Similar to, but more constraining than precedes(java.lang.Object, java.lang.Object).

Parameters:
before - a valid node descriptor.
after - a valid node descriptor.

solve

public void solve()
Attempts to satisfy all constraints added thus far, and then invokes setPosForNode(java.lang.Object, com.google.caja.lexer.FilePosition) for each node descriptor for which it could conclude a position.


getPosForNode

protected abstract FilePosition getPosForNode(java.lang.Object o)
Returns the file position for the given node descriptor or an unknown position if the position needs to be inferred.

Parameters:
o - a node descriptor.
Returns:
non null. Should return a position whose source is unknown if the node does not have accurate position info.

setPosForNode

protected abstract void setPosForNode(java.lang.Object o,
                                      FilePosition pos)
Informs the client that it has inferred a position for the given node. The client may ignore this, and probably should if the given node already has accurate position data from a source different than that of the position passed to the constructor.

Parameters:
o - a node descriptor.
pos - non null.

boundsForNode

private PositionInferer.Region boundsForNode(java.lang.Object o)


Copyright (C) 2008 Google Inc.
Licensed under the Apache License, Version 2.0