/* LLString.java */
import java.util.ArrayList;
/**
* The rule for variable length strings where the length of a
* string is declared, in ASCII, in the first two characters.
*/
final public class LLString extends Rule
{
/**
* The parser of length prefixed strings.
*/
public static LLString parse(ParserContext context)
{
/*
* Let aparse know which rule has been called.
*/
context.push("LLString");
LLString rule = null;
try
{
/*
* Get the length of the string.
*/
int length =
Integer.parseInt(
context.text.substring(
context.index,
context.index + 2));
/*
* Create a new rule for the string.
*/
rule =
new LLString(
context.text.substring(
context.index + 2,
context.index + length + 2),
null);
/*
* Let aparse know how many characters of
* the input have been consumed.
*/
context.index += (2 + length);
}
catch (StringIndexOutOfBoundsException e) {}
/*
* Let aparse know whether or not the parse
* was successful.
*/
context.pop("LLString", rule != null);
/*
* Return an instance of this rule, or null
* if the parse failed, to the caller.
*/
return rule;
}
/**
* A private constructor that is used by the parse method.
*/
private LLString(String spelling, ArrayList<Rule> rules)
{
super(spelling, rules);
}
/**
* The visitor accept method that passes this length prefixed
* string to the specified visitor.
*/
public Object accept(Visitor visitor)
{
return visitor.visit(this);
}
}
/* eof */