3import java.util.regex.*;
29 public static class Rule {
35 public Rule(
int id, Pattern pattern) {
41 public Rule(
int id, String pattern) {
42 this(id,Pattern.compile(pattern));
55 public static final Pattern
P_WHITESPACE = Pattern.compile(
"\\s+");
58 Pattern.compile(
"[_a-zA-Z]?(\\w|_)+");
61 Pattern.compile(
"[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?");
78 input_=input_.substring(m.end(),input_.length());
95 return input_==
null || input_.length()==0;
103 protected boolean match(Pattern p) {
110 Matcher m=p.matcher(input_);
115 text_=input_.substring(0,n);
116 input_=input_.substring(m.end(),input_.length());
123 protected int next(Rule[] rules) {
127 if (rules_==
null)
return id_=
NO_MATCH;
129 for (Rule rule : rules) {
130 if (
match(rule.pattern_))
143 public static void main(String[] args) {
146 new Rule(1,
"[0-9]*\\.?[0-9]+"),
151 (rules,args.length==0 ?
" 12.3a 12 bcd 34 " : args[0]);
153 System.out.println(
"input = '"+s.
remainder()+
"'");
157 System.out.println(
"syntax error near '"+s.
remainder()+
"'");
161 System.out.println(
"matched text = '"+s.
matchedText()+
"'");
162 System.out.println(
"remaining input = '"+s.
remainder()+
"'");
Base class for a simple lexical scanner.
static final int END_OF_INPUT
no more input
void eatWhiteSpace()
ignore white space (called by match
int next(Rule[] rules)
match remainder to table of rules @endiliteral
int matchedTokenId()
get result of last call to next()
void setInput(String input)
set input (resets scanner state)
boolean endOfInput()
reached end of input?
static final Pattern P_WHITESPACE
white space
String matchedText()
get text of last match or call to next
int next()
match remainder to rules provided to constructor
LexicalScanner(Rule[] rules, String input)
create new scanner processing input @endiliteral
String remainder()
get remaining text
static final int NO_MATCH
no match (usually implies a syntax error)
static final Pattern P_IDENTIFIER
identifiers
static void main(String[] args)
testing and example for usage
boolean match(Pattern p)
Match remainder against pattern p.
static final Pattern P_FLOAT
floating point number