Debian Codegraph @

src/main/org/h2/bnf/RuleRepeat.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.bnf;
7
8 import java.util.HashMap;
9
10 /**
11 * Represents a loop in a BNF object.
12 */
13 public class RuleRepeat implements Rule {
14
15 private final Rule rule;
16 private final boolean comma;
17
18 public RuleRepeat(Rule rule, boolean comma) {
19 this.rule = rule;
20 this.comma = comma;
21 }
22
23 @Override
24 public void accept(BnfVisitor visitor) {
25 visitor.visitRuleRepeat(comma, rule);
26 }
27
28 @Override
29 public void setLinks(HashMap<String, RuleHead> ruleMap) {
30 // not required, because it's already linked
31 }
32
33 @Override
34 public boolean autoComplete(Sentence sentence) {
35 sentence.stopIfRequired();
36 while (rule.autoComplete(sentence)) {
37 // nothing to do
38 }
39 String s = sentence.getQuery();
40 while (Bnf.startWithSpace(s)) {
41 s = s.substring(1);
42 }
43 sentence.setQuery(s);
44 return true;
45 }
46
47 @Override
48 public String toString() {
49 return comma ? ", ..." : " ...";
50 }
51
52 }