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 an optional BNF rule.
12: */
13: public class RuleOptional implements Rule {
14: private Rule rule;
15: private boolean mapSet;
16:
17: RuleOptional(Rule rule, boolean repeat) {
18: this .rule = rule;
19: }
20:
21: public String name() {
22: return null;
23: }
24:
25: public String random(Bnf config, int level) {
26: if (level > 10 ? config.getRandom().nextInt(level) == 1
27: : config.getRandom().nextInt(4) == 1) {
28: return rule.random(config, level + 1);
29: } else {
30: return "";
31: }
32: }
33:
34: public Rule last() {
35: return this ;
36: }
37:
38: public void setLinks(HashMap ruleMap) {
39: if (!mapSet) {
40: rule.setLinks(ruleMap);
41: mapSet = true;
42: }
43: }
44:
45: public String matchRemove(String query, Sentence sentence) {
46: if (sentence.stop()) {
47: return null;
48: }
49: if (query.length() == 0) {
50: return query;
51: }
52: String s = rule.matchRemove(query, sentence);
53: if (s == null) {
54: return query;
55: }
56: return s;
57: }
58:
59: public void addNextTokenList(String query, Sentence sentence) {
60: if (sentence.stop()) {
61: return;
62: }
63: rule.addNextTokenList(query, sentence);
64: }
65:
66: }
|