01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.bnf;
07:
08: import java.util.HashMap;
09:
10: /**
11: * Represents a loop in a BNF object.
12: */
13: public class RuleRepeat implements Rule {
14:
15: private Rule rule;
16:
17: RuleRepeat(Rule rule) {
18: this .rule = rule;
19: }
20:
21: public String name() {
22: return rule.name();
23: }
24:
25: public Rule last() {
26: return this ;
27: }
28:
29: public void setLinks(HashMap ruleMap) {
30: // rule.setLinks(ruleMap);
31: }
32:
33: public String random(Bnf config, int level) {
34: return rule.random(config, level);
35: }
36:
37: public String matchRemove(String query, Sentence sentence) {
38: if (sentence.stop()) {
39: return null;
40: }
41: if (query.length() == 0) {
42: return null;
43: }
44: while (true) {
45: String s = rule.matchRemove(query, sentence);
46: if (s == null) {
47: return query;
48: } else if (s.length() == 0) {
49: return s;
50: }
51: query = s;
52: }
53: }
54:
55: public void addNextTokenList(String query, Sentence sentence) {
56: if (sentence.stop()) {
57: return;
58: }
59: while (true) {
60: rule.addNextTokenList(query, sentence);
61: String s = rule.matchRemove(query, sentence);
62: if (s == null || s == query) {
63: break;
64: }
65: query = s;
66: }
67: }
68:
69: }
|