|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.caja.lexer.PositionInferer
public abstract class PositionInferer
Does some simple constraint solving to assign reasonable position values to generated parse tree nodes.
PositionInferer
, and implement the abstract methods.
Pass to the constructor a file position from the same
source
as nodes with known file positions.
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.
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.
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 |
---|
private final java.util.List<PositionInferer.Boundary> boundaries
private final java.util.List<PositionInferer.Relation> relations
private final java.util.Map<java.lang.Object,PositionInferer.Region> boundsByNode
private final SourceBreaks breaks
private static final int UNSPECIFIED_MAX
private static final int UNSPECIFIED_MIN
Constructor Detail |
---|
public PositionInferer(FilePosition spanningPos)
Method Detail |
---|
public void contains(java.lang.Object container, java.lang.Object contained)
container
- a valid node descriptor.contained
- a valid node descriptor.public void precedes(java.lang.Object before, java.lang.Object after)
before
- a valid node descriptor.after
- a valid node descriptor.public void adjacent(java.lang.Object before, java.lang.Object after)
precedes(java.lang.Object, java.lang.Object)
.
before
- a valid node descriptor.after
- a valid node descriptor.public void solve()
setPosForNode(java.lang.Object, com.google.caja.lexer.FilePosition)
for each node descriptor for which it could conclude
a position.
protected abstract FilePosition getPosForNode(java.lang.Object o)
o
- a node descriptor.
source
is
unknown
if the node does not have accurate
position info.protected abstract void setPosForNode(java.lang.Object o, FilePosition pos)
source
different than
that of the position passed to the
constructor
.
o
- a node descriptor.pos
- non null.private PositionInferer.Region boundsForNode(java.lang.Object o)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |