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