org.quasiliteral.text
Class Substituter

java.lang.Object
  |
  +--org.quasiliteral.text.Substituter
All Implemented Interfaces:
MatchMaker, ValueMaker

public class Substituter
extends Object
implements ValueMaker, MatchMaker

Safe: The default -- and simplest -- of the quasi parsers

Author:
Mark S. Miller

Field Summary
private  int myMatchSize
           
private  Object[] myTemplate
          A template is "compiled" into an array of three kinds of elements:
 
Constructor Summary
(package private) Substituter(Twine template)
           
 
Method Summary
private static boolean isDigit(char c)
           
 ConstList matchBind(ConstList args, Object specimen)
          Enabled: As a quasi-pattern, "first" replace all ${i}s with args[i]s, then see if the speciman matches the pattern.
 boolean matchBind(ConstList args, Object specimen, FlexList bindings)
          Enabled:
 int numArgs()
          Enabled: The number of arguments that must be provided to substitute() or matchBind()
 int numPatterns()
          Enabled: The number of bindings that will be returned by matchBind()
private  Twine optSegments(int[] iPtr, ConstList args)
          If the iPtr[0]'th segment is a pattern, then return null and leave iPtr alone.
private  ConstList optSequence(int[] iPtr, ConstList args)
          If the iPtr[0]'th segment is a pattern, then return null and leave iPtr alone.
 Object substitute(ConstList args)
          Enabled: Evaluate as a quasi-literal by replacing ${i} with args[i], for all i
 String toString()
          Suppressed: Prints using the template string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

myTemplate

private Object[] myTemplate
A template is "compiled" into an array of three kinds of elements:

A Twine represents a literal string segment.

A positive Integer represents a position into which to substitute an arg.

A negative Integer represents a pattern position (XXX needs a real explanation).

XXX this representation is a kludge


myMatchSize

private int myMatchSize
Constructor Detail

Substituter

Substituter(Twine template)
Method Detail

isDigit

private static boolean isDigit(char c)

matchBind

public ConstList matchBind(ConstList args,
                           Object specimen)
Enabled: As a quasi-pattern, "first" replace all ${i}s with args[i]s, then see if the speciman matches the pattern. If not, return null. If so, return an array in which the i'th element is the part of the specimen that matched @{i}.

Specified by:
matchBind in interface MatchMaker

matchBind

public boolean matchBind(ConstList args,
                         Object specimen,
                         FlexList bindings)
Enabled:

Specified by:
matchBind in interface MatchMaker

optSequence

private ConstList optSequence(int[] iPtr,
                              ConstList args)
If the iPtr[0]'th segment is a pattern, then return null and leave iPtr alone. Otherwise, return the string gotten by appending the consecutive non-pattern segments starting with the iPtr[0]'th segment, and modify iPtr[0] to be the following segment number.


optSegments

private Twine optSegments(int[] iPtr,
                          ConstList args)
If the iPtr[0]'th segment is a pattern, then return null and leave iPtr alone. Otherwise, return the string gotten by appending the consecutive non-pattern segments starting with the iPtr[0]'th segment, and modify iPtr[0] to be the following segment number.


substitute

public Object substitute(ConstList args)
Enabled: Evaluate as a quasi-literal by replacing ${i} with args[i], for all i

Specified by:
substitute in interface ValueMaker

numArgs

public int numArgs()
Enabled: The number of arguments that must be provided to substitute() or matchBind()


numPatterns

public int numPatterns()
Enabled: The number of bindings that will be returned by matchBind()


toString

public String toString()
Suppressed: Prints using the template string

Overrides:
toString in class Object
Returns:
a string representation of the object.


comments?