1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Iterator;
|
---|
5 |
|
---|
6 | import org.eclipse.emf.ecore.EObject;
|
---|
7 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
10 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
11 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
13 |
|
---|
14 | public class EbnfHtmlFormatterVisitor extends EbnfFormatterVisitor {
|
---|
15 |
|
---|
16 | private ArrayList<MetaTextEntry> metaTexts = new ArrayList<MetaTextEntry>();
|
---|
17 |
|
---|
18 | public EbnfHtmlFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
19 | super(rootNode, config);
|
---|
20 | this.config = config;
|
---|
21 | buf = new StringBuffer();
|
---|
22 | }
|
---|
23 |
|
---|
24 | protected void weaveComments() {
|
---|
25 | bufferPositionOriginalText = 0;
|
---|
26 | bufferPositionFormattedTextNoWhitespaces = 0;
|
---|
27 | bufferPositionFormattedText = 0;
|
---|
28 |
|
---|
29 | StringBuffer result = new StringBuffer();
|
---|
30 | formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
|
---|
31 | formattedText = buf.toString();
|
---|
32 |
|
---|
33 | MetaTextEntry currentMetaText = null;
|
---|
34 | if (metaTexts.iterator().hasNext())
|
---|
35 | currentMetaText = metaTexts.iterator().next();
|
---|
36 |
|
---|
37 | while (bufferPositionFormattedTextNoWhitespaces <= formattedTextNoWhitespaces.length()) {
|
---|
38 | skipWhitespacesOriginalText();
|
---|
39 | skipWhitespacesFormattedText(result);
|
---|
40 |
|
---|
41 | if (!(bufferPositionOriginalText < originalText.length()))
|
---|
42 | break;
|
---|
43 |
|
---|
44 | char formattedPositionNoWhitespaces;
|
---|
45 | if (bufferPositionFormattedTextNoWhitespaces == formattedTextNoWhitespaces.length()) {
|
---|
46 | formattedPositionNoWhitespaces = ' ';
|
---|
47 | } else {
|
---|
48 | formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces);
|
---|
49 | }
|
---|
50 | char originalPosition = originalText.charAt(bufferPositionOriginalText);
|
---|
51 |
|
---|
52 | if (formattedPositionNoWhitespaces != originalPosition) {
|
---|
53 | if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary
|
---|
54 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
55 | bufferPositionFormattedText++;
|
---|
56 | } else if (isCommentNext(originalText, bufferPositionOriginalText)) {
|
---|
57 | LeafNode currentComment = allComments.get(allCommentsPosition);
|
---|
58 | if (currentComment.getTotalOffset() == bufferPositionOriginalText) {
|
---|
59 | if (isMultiLineComment(currentComment.getText())) {
|
---|
60 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
61 | if (newLinesCount > 0) {
|
---|
62 | if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) {
|
---|
63 | result.append("\n\n");
|
---|
64 | }
|
---|
65 |
|
---|
66 | result.append(currentComment.getText());
|
---|
67 | result.append("\n");
|
---|
68 | } else {
|
---|
69 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
70 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
71 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
72 | result.append(lastWhiteSpaces);
|
---|
73 | }
|
---|
74 | } else if (isSingleLineComment(currentComment.getText())) {
|
---|
75 | int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
|
---|
76 | String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1);
|
---|
77 | result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length());
|
---|
78 | if (newLinesCount > 0) {
|
---|
79 | result.append("\n\n" + stripEndingNewline(currentComment.getText()));
|
---|
80 | } else {
|
---|
81 | result.append(" " + stripEndingNewline(currentComment.getText()));
|
---|
82 | }
|
---|
83 | result.append(lastWhiteSpaces);
|
---|
84 | }
|
---|
85 | bufferPositionOriginalText+=currentComment.getLength();
|
---|
86 | allCommentsPosition++;
|
---|
87 | }
|
---|
88 | } else { // disaster handling: return original unformatted text!
|
---|
89 | System.err.println("Disaster Recovery: returning original text!!");
|
---|
90 | buf = new StringBuffer();
|
---|
91 | buf.append(originalText);
|
---|
92 | return;
|
---|
93 | }
|
---|
94 | } else {
|
---|
95 | if ((currentMetaText != null) && (bufferPositionFormattedText >= currentMetaText.getOffset())) {
|
---|
96 | result.append(currentMetaText.getText());
|
---|
97 | metaTexts.remove(currentMetaText);
|
---|
98 | if (metaTexts.iterator().hasNext())
|
---|
99 | currentMetaText = metaTexts.iterator().next();
|
---|
100 | else
|
---|
101 | currentMetaText = null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1));
|
---|
105 | bufferPositionOriginalText++;
|
---|
106 | bufferPositionFormattedText++;
|
---|
107 | bufferPositionFormattedTextNoWhitespaces++;
|
---|
108 |
|
---|
109 | if ((currentMetaText != null) && (bufferPositionFormattedText >= currentMetaText.getOffset())) {
|
---|
110 | result.append(currentMetaText.getText());
|
---|
111 | metaTexts.remove(currentMetaText);
|
---|
112 | if (metaTexts.iterator().hasNext())
|
---|
113 | currentMetaText = metaTexts.iterator().next();
|
---|
114 | else
|
---|
115 | currentMetaText = null;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | buf = result;
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected void metaText(String str) {
|
---|
123 | MetaTextEntry metaTextEntry = new MetaTextEntry(str, buf.length());
|
---|
124 | metaTexts.add(metaTextEntry);
|
---|
125 | }
|
---|
126 |
|
---|
127 | // -----------------------------------------------------------------------------
|
---|
128 |
|
---|
129 | protected void visitAfter(EtsiBnf node) {
|
---|
130 | weaveComments();
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected void visitBefore(SectionHeading node) {
|
---|
134 | if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
135 | newLine();
|
---|
136 |
|
---|
137 | lastWasSectionHeading=true;
|
---|
138 |
|
---|
139 | metaText("<h1>");
|
---|
140 | text(node.getSectionHeader().replaceAll("[\n\r]", ""));
|
---|
141 | metaText("</h1>");
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected void visitAfter(SectionHeading node) {
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void visitBefore(RuleReference node) {
|
---|
148 | wrap();
|
---|
149 |
|
---|
150 | metaText("<a href=\"#" + node.getRuleref().getName() + "\">");
|
---|
151 | text(node.getRuleref().getName());
|
---|
152 | metaText("</a>");
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected void visitAfter(RuleReference node) {
|
---|
156 | }
|
---|
157 |
|
---|
158 | protected void visitBefore(Rule node) {
|
---|
159 | if (lastWasSectionHeading)
|
---|
160 | newLine();
|
---|
161 |
|
---|
162 | lastWasSectionHeading=false;
|
---|
163 |
|
---|
164 | newLineOffsetCounter = 0;
|
---|
165 |
|
---|
166 | if (node.getRulenumber() > 0)
|
---|
167 | text(node.getRulenumber() + ". ");
|
---|
168 |
|
---|
169 | metaText("<a name=\"#" + node.getName() + "\">");
|
---|
170 | text(node.getName());
|
---|
171 | metaText("</a>");
|
---|
172 | text(" ::= ");
|
---|
173 |
|
---|
174 | rightHandSideRuleOffset = newLineOffsetCounter;
|
---|
175 | ruleSpacingStack.push(newLineOffsetCounter);
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected void visitAfter(Rule node) {
|
---|
179 | metaText("<br/>");
|
---|
180 | text(";");
|
---|
181 | newLine();
|
---|
182 | ruleSpacingStack.pop();
|
---|
183 | }
|
---|
184 | }
|
---|