1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Stack;
|
---|
5 |
|
---|
6 | import org.eclipse.emf.ecore.EObject;
|
---|
7 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
8 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
9 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
10 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
11 |
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.Atom;
|
---|
13 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
14 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
15 | import de.ugoe.cs.swe.bnftools.ebnf.DeltaEntry;
|
---|
16 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
17 | import de.ugoe.cs.swe.bnftools.ebnf.ExtRule;
|
---|
18 | import de.ugoe.cs.swe.bnftools.ebnf.GlobalCombinator;
|
---|
19 | import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence;
|
---|
20 | import de.ugoe.cs.swe.bnftools.ebnf.HookCombinator;
|
---|
21 | import de.ugoe.cs.swe.bnftools.ebnf.Import;
|
---|
22 | import de.ugoe.cs.swe.bnftools.ebnf.ImportSection;
|
---|
23 | import de.ugoe.cs.swe.bnftools.ebnf.MergeEntry;
|
---|
24 | import de.ugoe.cs.swe.bnftools.ebnf.MergeRule;
|
---|
25 | import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence;
|
---|
26 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence;
|
---|
27 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
28 | import de.ugoe.cs.swe.bnftools.ebnf.RuleCombinator;
|
---|
29 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
30 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
31 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition;
|
---|
32 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
|
---|
33 | import de.ugoe.cs.swe.bnftools.ebnf.Term;
|
---|
34 | import de.ugoe.cs.swe.bnftools.visitor.EbnfVisitor;
|
---|
35 |
|
---|
36 | public class EbnfFormatterVisitor extends EbnfVisitor {
|
---|
37 | private StringBuffer buf;
|
---|
38 | private FormatterConfig config;
|
---|
39 | private int bufferPositionFormattedTextNoWhitespaces = 0;
|
---|
40 | private int bufferPositionOriginalText = 0;
|
---|
41 | private int allCommentsPosition = 0;
|
---|
42 | private ArrayList<LeafNode> allComments = new ArrayList<LeafNode>();
|
---|
43 |
|
---|
44 | private boolean lastWasSectionHeading=false;
|
---|
45 | private CompositeNode parserEtsiBnfNode;
|
---|
46 | private String originalText;
|
---|
47 | private int bufferPositionFormattedText;
|
---|
48 | private String formattedText;
|
---|
49 | private String formattedTextNoWhitespaces;
|
---|
50 | private String originalTextNoWhitespaces;
|
---|
51 | private int newLineOffsetCounter = 0;
|
---|
52 | private int rightHandSideRuleOffset = 0;
|
---|
53 | private Stack<Integer> ruleSpacingStack = new Stack<Integer>();
|
---|
54 |
|
---|
55 | public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
56 | super(rootNode);
|
---|
57 | this.config = config;
|
---|
58 | buf = new StringBuffer();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public EbnfFormatterVisitor(FormatterConfig config) {
|
---|
62 | this.config = config;
|
---|
63 | buf = new StringBuffer();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public StringBuffer getBuf() {
|
---|
67 | return buf;
|
---|
68 | }
|
---|
69 |
|
---|
70 | private boolean isCommentNode(LeafNode node) {
|
---|
71 | if ((node.getText().trim().startsWith("//") || node.getText().trim().startsWith("/*")) && (node.isHidden()))
|
---|
72 | return true;
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void collectAllComments(CompositeNode node) {
|
---|
77 | for (int i=0; i < node.getChildren().size(); i++) {
|
---|
78 | AbstractNode currentNode = node.getChildren().get(i);
|
---|
79 | if (currentNode instanceof LeafNode) {
|
---|
80 | LeafNode leafNode = (LeafNode) currentNode;
|
---|
81 | if (isCommentNode(leafNode)) {
|
---|
82 | allComments.add(leafNode);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (currentNode instanceof CompositeNode) {
|
---|
87 | collectAllComments((CompositeNode) currentNode);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private boolean isSingleLineComment(String str) {
|
---|
93 | if (str.startsWith("//"))
|
---|
94 | return true;
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private boolean isMultiLineComment(String str) {
|
---|
99 | if (str.startsWith("/*"))
|
---|
100 | return true;
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private boolean isWhitespace(char ch) {
|
---|
105 | if ((ch==' ') || (ch == '\t') || (ch == '\n') || (ch == '\r'))
|
---|
106 | return true;
|
---|
107 | return false;
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void skipWhitespacesOriginalText() {
|
---|
111 | while (bufferPositionOriginalText < originalText.length() && isWhitespace(originalText.charAt(bufferPositionOriginalText))) {
|
---|
112 | bufferPositionOriginalText++;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void skipWhitespacesFormattedText(StringBuffer result) {
|
---|
117 | while (bufferPositionFormattedText < formattedText.length() && isWhitespace(formattedText.charAt(bufferPositionFormattedText))) {
|
---|
118 | result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1));
|
---|
119 | bufferPositionFormattedText++;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private boolean isSingleLineCommentNext(String str, int position) {
|
---|
124 | if ((str.charAt(position) == '/') && (str.charAt(position) == '/'))
|
---|
125 | return true;
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private boolean isMultiLineCommentNext(String str, int position) {
|
---|
130 | if ((str.charAt(position) == '/') && (str.charAt(position) == '*'))
|
---|
131 | return true;
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private boolean isCommentNext(String str, int position) {
|
---|
136 | if (isSingleLineCommentNext(str, position) || isMultiLineCommentNext(str, position))
|
---|
137 | return true;
|
---|
138 | else
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | private String scanBackWhitespaces(String str, int position) {
|
---|
143 | StringBuffer whiteSpaces = new StringBuffer();
|
---|
144 | int currentPosition = position;
|
---|
145 | while (isWhitespace(str.charAt(currentPosition))) {
|
---|
146 | whiteSpaces.append(str.charAt(currentPosition));
|
---|
147 | currentPosition--;
|
---|
148 | }
|
---|
149 | return whiteSpaces.toString();
|
---|
150 | }
|
---|
151 |
|
---|
152 | private String stripEndingNewline(String str) {
|
---|
153 | int position = str.length() - 1;
|
---|
154 | while ((str.charAt(position) == '\n') || (str.charAt(position) == '\r')) {
|
---|
155 | position--;
|
---|
156 | }
|
---|
157 | return str.substring(0, position + 1);
|
---|
158 | }
|
---|
159 |
|
---|
160 | private int scanBackNewlinesCount(String str, int position) {
|
---|
161 | int newLinesCount = 0;
|
---|
162 | int currentPosition = position;
|
---|
163 | while ((str.charAt(currentPosition) == '\n') || (str.charAt(currentPosition) == '\r')) {
|
---|
164 | if (str.charAt(currentPosition) == '\n') {
|
---|
165 | if (str.charAt(currentPosition - 1) == '\r') {
|
---|
166 | currentPosition -= 2;
|
---|
167 | } else {
|
---|
168 | currentPosition -= 1;
|
---|
169 | }
|
---|
170 | newLinesCount++;
|
---|
171 | } else if (str.charAt(currentPosition) == '\r') {
|
---|
172 | currentPosition -= 1;
|
---|
173 | newLinesCount++;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | return newLinesCount;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void weaveComments() {
|
---|
181 | // if (true) {
|
---|
182 | // StringBuffer result = new StringBuffer();
|
---|
183 | // result.append(buf.toString());
|
---|
184 | // buf = result;
|
---|
185 | // return;
|
---|
186 | // }
|
---|
187 |
|
---|
188 | bufferPositionOriginalText = 0;
|
---|
189 | bufferPositionFormattedTextNoWhitespaces = 0;
|
---|
190 | bufferPositionFormattedText = 0;
|
---|
191 |
|
---|
192 | StringBuffer result = new StringBuffer();
|
---|
193 | formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
|
---|
194 | formattedText = buf.toString();
|
---|
195 |
|
---|
196 | while (bufferPositionFormattedTextNoWhitespaces <= formattedTextNoWhitespaces.length()) {
|
---|
197 | skipWhitespacesOriginalText();
|
---|
198 | skipWhitespacesFormattedText(result);
|
---|
199 |
|
---|
200 | if (!(bufferPositionOriginalText < originalText.length()))
|
---|
201 | break;
|
---|
202 |
|
---|
203 | char formattedPositionNoWhitespaces;
|
---|
204 | if (bufferPositionFormattedTextNoWhitespaces == formattedTextNoWhitespaces.length()) {
|
---|
205 | formattedPositionNoWhitespaces = ' ';
|
---|
206 | } else {
|
---|
207 | formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces);
|
---|
208 | }
|
---|
209 | char originalPosition = originalText.charAt(bufferPositionOriginalText);
|
---|
210 |
|
---|
211 | if (formattedPositionNoWhitespaces != originalPosition) {
|
---|
212 | if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary
|
---|
213 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
214 | bufferPositionFormattedText++;
|
---|
215 | } else if (isCommentNext(originalText, bufferPositionOriginalText)) {
|
---|
216 | LeafNode currentComment = allComments.get(allCommentsPosition);
|
---|
217 | if (currentComment.getTotalOffset() == bufferPositionOriginalText) {
|
---|
218 | if (isMultiLineComment(currentComment.getText())) {
|
---|
219 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
220 | if (newLinesCount > 0) {
|
---|
221 | if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) {
|
---|
222 | result.append("\n\n");
|
---|
223 | }
|
---|
224 |
|
---|
225 | result.append(currentComment.getText());
|
---|
226 | result.append("\n");
|
---|
227 | } else {
|
---|
228 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
229 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
230 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
231 | result.append(lastWhiteSpaces);
|
---|
232 | }
|
---|
233 | } else if (isSingleLineComment(currentComment.getText())) {
|
---|
234 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
235 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
236 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
237 | if (newLinesCount > 0) {
|
---|
238 | result.append("\n\n" + stripEndingNewline(currentComment.getText()));
|
---|
239 | } else {
|
---|
240 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
241 | }
|
---|
242 | result.append(lastWhiteSpaces);
|
---|
243 | }
|
---|
244 | bufferPositionOriginalText+=currentComment.getLength();
|
---|
245 | allCommentsPosition++;
|
---|
246 | }
|
---|
247 | } else { // disaster handling: return original unformatted text!
|
---|
248 | buf = new StringBuffer();
|
---|
249 | buf.append(originalText);
|
---|
250 | return;
|
---|
251 | }
|
---|
252 | } else {
|
---|
253 | result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1));
|
---|
254 | bufferPositionOriginalText++;
|
---|
255 | bufferPositionFormattedText++;
|
---|
256 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | buf = result;
|
---|
260 |
|
---|
261 | }
|
---|
262 |
|
---|
263 | private void newLine() {
|
---|
264 | buf.append("\n");
|
---|
265 | }
|
---|
266 |
|
---|
267 | private void text(String str) {
|
---|
268 | buf.append(str);
|
---|
269 | newLineOffsetCounter += str.length();
|
---|
270 | }
|
---|
271 |
|
---|
272 | private void space() {
|
---|
273 | buf.append(" ");
|
---|
274 | newLineOffsetCounter++;
|
---|
275 | }
|
---|
276 |
|
---|
277 | private void spaces(int count) {
|
---|
278 | for (int i=0; i < count; i++) {
|
---|
279 | buf.append(" ");
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | // -----------------------------------------------------------------------------
|
---|
284 |
|
---|
285 | protected void visitBefore(EtsiBnf node) {
|
---|
286 | parserEtsiBnfNode = NodeUtil.getNodeAdapter(node).getParserNode();
|
---|
287 | collectAllComments(parserEtsiBnfNode);
|
---|
288 | originalText = NodeUtil.getNodeAdapter(node).getParserNode().serialize();
|
---|
289 | originalTextNoWhitespaces = originalText.replaceAll("[ \t\n\r]", "");
|
---|
290 |
|
---|
291 | //System.out.println(allComments.toString());
|
---|
292 | text("grammar " + node.getName());
|
---|
293 | if (node.getType() != null)
|
---|
294 | text(node.getType());
|
---|
295 | text(";");
|
---|
296 |
|
---|
297 | newLine();
|
---|
298 | newLine();
|
---|
299 | }
|
---|
300 |
|
---|
301 | protected void visitAfter(EtsiBnf node) {
|
---|
302 | weaveComments();
|
---|
303 | }
|
---|
304 |
|
---|
305 | protected void visitBefore(ImportSection node) {
|
---|
306 | }
|
---|
307 |
|
---|
308 | protected void visitAfter(ImportSection node) {
|
---|
309 | newLine();
|
---|
310 | }
|
---|
311 |
|
---|
312 | protected void visitBefore(BnfEntry node) {
|
---|
313 | }
|
---|
314 |
|
---|
315 | protected void visitAfter(BnfEntry node) {
|
---|
316 | }
|
---|
317 |
|
---|
318 | protected void visitBefore(DeltaEntry node) {
|
---|
319 | }
|
---|
320 |
|
---|
321 | protected void visitAfter(DeltaEntry node) {
|
---|
322 | }
|
---|
323 |
|
---|
324 | protected void visitBefore(MergeEntry node) {
|
---|
325 | }
|
---|
326 |
|
---|
327 | protected void visitAfter(MergeEntry node) {
|
---|
328 | }
|
---|
329 |
|
---|
330 | protected void visitBefore(Atom node) {
|
---|
331 | }
|
---|
332 |
|
---|
333 | protected void visitAfter(Atom node) {
|
---|
334 | }
|
---|
335 |
|
---|
336 | protected void visitBefore(Term node) {
|
---|
337 | }
|
---|
338 |
|
---|
339 | protected void visitAfter(Term node) {
|
---|
340 | if (!isLastElement())
|
---|
341 | space();
|
---|
342 | }
|
---|
343 |
|
---|
344 | protected void visitBefore(DefinitionList node) {
|
---|
345 | }
|
---|
346 |
|
---|
347 | protected void visitAfter(DefinitionList node) {
|
---|
348 | }
|
---|
349 |
|
---|
350 | protected void visitBefore(ExtRule node) {
|
---|
351 | }
|
---|
352 |
|
---|
353 | protected void visitAfter(ExtRule node) {
|
---|
354 | }
|
---|
355 |
|
---|
356 | protected void visitBefore(GlobalCombinator node) {
|
---|
357 | }
|
---|
358 |
|
---|
359 | protected void visitAfter(GlobalCombinator node) {
|
---|
360 | }
|
---|
361 |
|
---|
362 | protected void visitBefore(GroupedSequence node) {
|
---|
363 | text("(");
|
---|
364 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
365 | }
|
---|
366 |
|
---|
367 | protected void visitAfter(GroupedSequence node) {
|
---|
368 | text(")");
|
---|
369 | ruleSpacingStack.pop();
|
---|
370 | }
|
---|
371 |
|
---|
372 | protected void visitBefore(HookCombinator node) {
|
---|
373 | }
|
---|
374 |
|
---|
375 | protected void visitAfter(HookCombinator node) {
|
---|
376 | }
|
---|
377 |
|
---|
378 | protected void visitBefore(Import node) {
|
---|
379 | text("import \"" + node.getImportURI() + "\";");
|
---|
380 | newLine();
|
---|
381 | }
|
---|
382 |
|
---|
383 | protected void visitAfter(Import node) {
|
---|
384 | }
|
---|
385 |
|
---|
386 | protected void visitBefore(MergeRule node) {
|
---|
387 | }
|
---|
388 |
|
---|
389 | protected void visitAfter(MergeRule node) {
|
---|
390 | }
|
---|
391 |
|
---|
392 | protected void visitBefore(OptionalSequence node) {
|
---|
393 | text("[");
|
---|
394 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
395 | }
|
---|
396 |
|
---|
397 | protected void visitAfter(OptionalSequence node) {
|
---|
398 | text("]");
|
---|
399 | ruleSpacingStack.pop();
|
---|
400 | }
|
---|
401 |
|
---|
402 | protected void visitBefore(RepeatedSequence node) {
|
---|
403 | text("{");
|
---|
404 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
405 | }
|
---|
406 |
|
---|
407 | protected void visitAfter(RepeatedSequence node) {
|
---|
408 | text("}");
|
---|
409 | if (node.isMorethanonce())
|
---|
410 | text("+");
|
---|
411 | ruleSpacingStack.pop();
|
---|
412 | }
|
---|
413 |
|
---|
414 | protected void visitBefore(Rule node) {
|
---|
415 | if (lastWasSectionHeading)
|
---|
416 | newLine();
|
---|
417 |
|
---|
418 | lastWasSectionHeading=false;
|
---|
419 |
|
---|
420 | newLineOffsetCounter = 0;
|
---|
421 |
|
---|
422 | if (node.getRulenumber() > 0)
|
---|
423 | text(node.getRulenumber() + ". ");
|
---|
424 |
|
---|
425 | text(node.getName() + " ::= ");
|
---|
426 |
|
---|
427 | rightHandSideRuleOffset = newLineOffsetCounter;
|
---|
428 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
429 | }
|
---|
430 |
|
---|
431 | protected void visitAfter(Rule node) {
|
---|
432 | text(";");
|
---|
433 | newLine();
|
---|
434 | ruleSpacingStack.pop();
|
---|
435 | }
|
---|
436 |
|
---|
437 | protected void visitBefore(RuleCombinator node) {
|
---|
438 | }
|
---|
439 |
|
---|
440 | protected void visitAfter(RuleCombinator node) {
|
---|
441 | }
|
---|
442 |
|
---|
443 | protected void visitBefore(RuleReference node) {
|
---|
444 | text(node.getRuleref().getName());
|
---|
445 | }
|
---|
446 |
|
---|
447 | protected void visitAfter(RuleReference node) {
|
---|
448 | }
|
---|
449 |
|
---|
450 | protected void visitBefore(SectionHeading node) {
|
---|
451 | if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
452 | newLine();
|
---|
453 |
|
---|
454 | lastWasSectionHeading=true;
|
---|
455 | // if (!lastWasSectionHeading || !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
456 | // if (!buf.substring(buf.length()-2).equals("\n\n"))
|
---|
457 | // buf.append("\n");
|
---|
458 |
|
---|
459 | text(node.getSectionHeader());
|
---|
460 | }
|
---|
461 |
|
---|
462 | protected void visitAfter(SectionHeading node) {
|
---|
463 | }
|
---|
464 |
|
---|
465 | protected void visitBefore(SingleDefinition node) {
|
---|
466 | }
|
---|
467 |
|
---|
468 | protected void visitAfter(SingleDefinition node) {
|
---|
469 | if (!isLastElement()) {
|
---|
470 | text(" | ");
|
---|
471 | if (config.isNewLineAfterAlternative()) {
|
---|
472 | if (config.isPreventNewLineAfterAlternativeOnLessThanThreeElements()) {
|
---|
473 | DefinitionList definitionList = (DefinitionList) node.eContainer();
|
---|
474 | if (definitionList.eContents().size() > 2) {
|
---|
475 | newLine();
|
---|
476 | spaces(ruleSpacingStack.peek());
|
---|
477 | }
|
---|
478 | } else {
|
---|
479 | newLine();
|
---|
480 | spaces(ruleSpacingStack.peek());
|
---|
481 | }
|
---|
482 | }
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | protected void visitBefore(StringRule node) {
|
---|
487 | if (node.getLiteral() != null)
|
---|
488 | text("\"" + node.getLiteral() + "\"");
|
---|
489 | else if (node.getColon() != null)
|
---|
490 | text("\"\"\"");
|
---|
491 | }
|
---|
492 |
|
---|
493 | protected void visitAfter(StringRule node) {
|
---|
494 | }
|
---|
495 |
|
---|
496 | }
|
---|