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