Debian Codegraph @

src/tools/org/h2/build/doc/BnfSyntax.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.build.doc;
7
8 import java.util.ArrayDeque;
9 import java.util.ArrayList;
10 import java.util.StringTokenizer;
11 import org.h2.bnf.Bnf;
12 import org.h2.bnf.BnfVisitor;
13 import org.h2.bnf.Rule;
14 import org.h2.bnf.RuleFixed;
15 import org.h2.bnf.RuleHead;
16 import org.h2.util.StringUtils;
17
18 /**
19 * A BNF visitor that generates BNF in HTML form.
20 */
21 public class BnfSyntax implements BnfVisitor {
22
23 private String html;
24
25 /**
26 * Get the HTML syntax for the given syntax.
27 *
28 * @param bnf the BNF
29 * @param syntaxLines the syntax
30 * @return the HTML
31 */
32 public String getHtml(Bnf bnf, String syntaxLines) {
33 syntaxLines = StringUtils.replaceAll(syntaxLines, "\n ", "\n");
34 StringTokenizer tokenizer = Bnf.getTokenizer(syntaxLines);
35 StringBuilder buff = new StringBuilder();
36 ArrayDeque<Character> deque = new ArrayDeque<>();
37 boolean extension = false;
38 while (tokenizer.hasMoreTokens()) {
39 String s = tokenizer.nextToken();
40 if (s.equals("@c@")) {
41 if (!extension) {
42 extension = true;
43 buff.append("<span class=\"ruleCompat\">");
44 }
45 s = skipAfterExtensionStart(tokenizer);
46 } else if (s.equals("@h2@")) {
47 if (!extension) {
48 extension = true;
49 buff.append("<span class=\"ruleH2\">");
50 }
51 s = skipAfterExtensionStart(tokenizer);
52 }
53 if (extension) {
54 if (s.length() == 1) {
55 char c = s.charAt(0);
56 switch (c) {
57 case '[':
58 deque.addLast(']');
59 break;
60 case '{':
61 deque.addLast('}');
62 break;
63 case ']':
64 case '}':
65 char c2 = deque.removeLast();
66 if (c != c2) {
67 throw new AssertionError("Expected " + c2 + " got " + c);
68 }
69 break;
70 default:
71 if (deque.isEmpty()) {
72 deque.add('*');
73 }
74 }
75 } else if (deque.isEmpty()) {
76 deque.add('*');
77 }
78 }
79 if (s.length() == 1 || StringUtils.toUpperEnglish(s).equals(s)) {
80 buff.append(StringUtils.xmlText(s));
81 if (extension && deque.isEmpty()) {
82 extension = false;
83 buff.append("</span>");
84 }
85 continue;
86 }
87 buff.append(getLink(bnf, s));
88 }
89 if (extension) {
90 if (deque.size() != 1 || deque.getLast() != '*') {
91 throw new AssertionError("Expected " + deque.getLast() + " got end of data");
92 }
93 buff.append("</span>");
94 }
95 String s = buff.toString();
96 // ensure it works within XHTML comments
97 s = StringUtils.replaceAll(s, "--", "&#45;-");
98 return s;
99 }
100
101 private static String skipAfterExtensionStart(StringTokenizer tokenizer) {
102 String s;
103 do {
104 s = tokenizer.nextToken();
105 } while (s.equals(" "));
106 return s;
107 }
108
109 /**
110 * Get the HTML link to the given token.
111 *
112 * @param bnf the BNF
113 * @param token the token
114 * @return the HTML link
115 */
116 String getLink(Bnf bnf, String token) {
117 RuleHead found = null;
118 String key = Bnf.getRuleMapKey(token);
119 for (int i = 0; i < token.length(); i++) {
120 String test = StringUtils.toLowerEnglish(key.substring(i));
121 RuleHead r = bnf.getRuleHead(test);
122 if (r != null) {
123 found = r;
124 break;
125 }
126 }
127 if (found == null) {
128 return token;
129 }
130 String page = "grammar.html";
131 String section = found.getSection();
132 if (section.startsWith("Commands")) {
133 page = "commands.html";
134 } if (section.startsWith("Data Types") || section.startsWith("Interval Data Types")) {
135 page = "datatypes.html";
136 } else if (section.startsWith("Functions")) {
137 page = "functions.html";
138 } else if (token.equals("@func@")) {
139 return "<a href=\"functions.html\">Function</a>";
140 } else if (found.getRule() instanceof RuleFixed) {
141 found.getRule().accept(this);
142 return html;
143 }
144 String link = found.getTopic().toLowerCase().replace(' ', '_');
145 link = page + "#" + StringUtils.urlEncode(link);
146 return "<a href=\"" + link + "\">" + token + "</a>";
147 }
148
149 @Override
150 public void visitRuleElement(boolean keyword, String name, Rule link) {
151 // not used
152 }
153
154 @Override
155 public void visitRuleFixed(int type) {
156 html = BnfRailroad.getHtmlText(type);
157 }
158
159 @Override
160 public void visitRuleList(boolean or, ArrayList<Rule> list) {
161 // not used
162 }
163
164 @Override
165 public void visitRuleOptional(Rule rule) {
166 // not used
167 }
168
169 @Override
170 public void visitRuleOptional(ArrayList<Rule> list) {
171 // not used
172 }
173
174 @Override
175 public void visitRuleRepeat(boolean comma, Rule rule) {
176 // not used
177 }
178
179 @Override
180 public void visitRuleExtension(Rule rule, boolean compatibility) {
181 // not used
182 }
183
184 }