Changeset 37 in default for trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src
- Timestamp:
- 11/04/10 11:24:11 (14 years ago)
- Location:
- trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java
r34 r37 69 69 } 70 70 71 pr ivateboolean isCommentNode(LeafNode node) {71 protected boolean isCommentNode(LeafNode node) { 72 72 if ((node.getText().trim().startsWith("//") || node.getText().trim().startsWith("/*")) && (node.isHidden())) 73 73 return true; … … 75 75 } 76 76 77 pr ivatevoid collectAllComments(CompositeNode node) {77 protected void collectAllComments(CompositeNode node) { 78 78 for (int i=0; i < node.getChildren().size(); i++) { 79 79 AbstractNode currentNode = node.getChildren().get(i); … … 91 91 } 92 92 93 pr ivateboolean isSingleLineComment(String str) {93 protected boolean isSingleLineComment(String str) { 94 94 if (str.startsWith("//")) 95 95 return true; … … 97 97 } 98 98 99 pr ivateboolean isMultiLineComment(String str) {99 protected boolean isMultiLineComment(String str) { 100 100 if (str.startsWith("/*")) 101 101 return true; … … 103 103 } 104 104 105 pr ivateboolean isWhitespace(char ch) {105 protected boolean isWhitespace(char ch) { 106 106 if ((ch==' ') || (ch == '\t') || (ch == '\n') || (ch == '\r')) 107 107 return true; … … 109 109 } 110 110 111 pr ivatevoid skipWhitespacesOriginalText() {111 protected void skipWhitespacesOriginalText() { 112 112 while (bufferPositionOriginalText < originalText.length() && isWhitespace(originalText.charAt(bufferPositionOriginalText))) { 113 113 bufferPositionOriginalText++; … … 115 115 } 116 116 117 pr ivatevoid skipWhitespacesFormattedText(StringBuffer result) {117 protected void skipWhitespacesFormattedText(StringBuffer result) { 118 118 while (bufferPositionFormattedText < formattedText.length() && isWhitespace(formattedText.charAt(bufferPositionFormattedText))) { 119 119 result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1)); … … 122 122 } 123 123 124 pr ivateboolean isSingleLineCommentNext(String str, int position) {124 protected boolean isSingleLineCommentNext(String str, int position) { 125 125 if ((str.charAt(position) == '/') && (str.charAt(position) == '/')) 126 126 return true; … … 128 128 } 129 129 130 pr ivateboolean isMultiLineCommentNext(String str, int position) {130 protected boolean isMultiLineCommentNext(String str, int position) { 131 131 if ((str.charAt(position) == '/') && (str.charAt(position) == '*')) 132 132 return true; … … 134 134 } 135 135 136 pr ivateboolean isCommentNext(String str, int position) {136 protected boolean isCommentNext(String str, int position) { 137 137 if (isSingleLineCommentNext(str, position) || isMultiLineCommentNext(str, position)) 138 138 return true; … … 141 141 } 142 142 143 pr ivateString scanBackWhitespaces(String str, int position) {143 protected String scanBackWhitespaces(String str, int position) { 144 144 StringBuffer whiteSpaces = new StringBuffer(); 145 145 int currentPosition = position; … … 151 151 } 152 152 153 pr ivateString stripEndingNewline(String str) {153 protected String stripEndingNewline(String str) { 154 154 int position = str.length() - 1; 155 155 while ((str.charAt(position) == '\n') || (str.charAt(position) == '\r')) { … … 159 159 } 160 160 161 pr ivateint scanBackNewlinesCount(String str, int position) {161 protected int scanBackNewlinesCount(String str, int position) { 162 162 int newLinesCount = 0; 163 163 int currentPosition = position; … … 179 179 } 180 180 181 pr ivatevoid weaveComments() {181 protected void weaveComments() { 182 182 bufferPositionOriginalText = 0; 183 183 bufferPositionFormattedTextNoWhitespaces = 0; … … 255 255 } 256 256 257 pr ivatevoid newLine() {257 protected void newLine() { 258 258 buf.append("\n"); 259 259 if ((ruleSpacingStack != null) && (!ruleSpacingStack.empty())) { … … 264 264 } 265 265 266 pr ivatevoid text(String str) {266 protected void text(String str) { 267 267 buf.append(str); 268 268 newLineOffsetCounter += str.length(); 269 269 } 270 270 271 pr ivatevoid space() {271 protected void space() { 272 272 buf.append(" "); 273 273 newLineOffsetCounter++; 274 274 } 275 275 276 pr ivatevoid spaces(int count) {276 protected void spaces(int count) { 277 277 for (int i=0; i < count; i++) { 278 278 buf.append(" "); … … 280 280 } 281 281 282 pr ivateboolean lastIsClosingParentheses() {282 protected boolean lastIsClosingParentheses() { 283 283 char ch = buf.toString().charAt(buf.toString().length()-1); 284 284 if ((ch == ')') || (ch == ']') || (ch == '}')) … … 287 287 } 288 288 289 pr ivatevoid wrap() {289 protected void wrap() { 290 290 if ((config.isWrapAfterThreshold()) && (newLineOffsetCounter > config.getWrapThreshold())) { 291 291 char last = buf.toString().charAt(buf.toString().length()-1); -
trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfHtmlFormatterVisitor.java
r34 r37 1 1 package de.ugoe.cs.swe.bnftools.ui.formatter; 2 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 3 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.SectionHeading; 4 11 5 12 public class EbnfHtmlFormatterVisitor extends EbnfFormatterVisitor { 6 13 14 private ArrayList<MetaTextEntry> metaTexts = new ArrayList<MetaTextEntry>(); 15 7 16 public EbnfHtmlFormatterVisitor(EObject rootNode, FormatterConfig config) { 8 17 super(rootNode, config); … … 11 20 } 12 21 22 protected void weaveComments() { 23 bufferPositionOriginalText = 0; 24 bufferPositionFormattedTextNoWhitespaces = 0; 25 bufferPositionFormattedText = 0; 26 27 StringBuffer result = new StringBuffer(); 28 formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", ""); 29 formattedText = buf.toString(); 13 30 31 MetaTextEntry currentMetaText = null; 32 if (metaTexts.iterator().hasNext()) 33 currentMetaText = metaTexts.iterator().next(); 34 35 while (bufferPositionFormattedTextNoWhitespaces <= formattedTextNoWhitespaces.length()) { 36 skipWhitespacesOriginalText(); 37 skipWhitespacesFormattedText(result); 38 39 if (!(bufferPositionOriginalText < originalText.length())) 40 break; 41 42 char formattedPositionNoWhitespaces; 43 if (bufferPositionFormattedTextNoWhitespaces == formattedTextNoWhitespaces.length()) { 44 formattedPositionNoWhitespaces = ' '; 45 } else { 46 formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces); 47 } 48 char originalPosition = originalText.charAt(bufferPositionOriginalText); 49 50 if (formattedPositionNoWhitespaces != originalPosition) { 51 if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary 52 bufferPositionFormattedTextNoWhitespaces++; 53 bufferPositionFormattedText++; 54 } else if (isCommentNext(originalText, bufferPositionOriginalText)) { 55 LeafNode currentComment = allComments.get(allCommentsPosition); 56 if (currentComment.getTotalOffset() == bufferPositionOriginalText) { 57 if (isMultiLineComment(currentComment.getText())) { 58 int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1); 59 if (newLinesCount > 0) { 60 if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) { 61 result.append("\n\n"); 62 } 63 64 result.append(currentComment.getText()); 65 result.append("\n"); 66 } else { 67 String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1); 68 result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length()); 69 result.append(" " + stripEndingNewline(currentComment.getText())); 70 result.append(lastWhiteSpaces); 71 } 72 } else if (isSingleLineComment(currentComment.getText())) { 73 int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1); 74 String lastWhiteSpaces = scanBackWhitespaces(result.toString(), result.toString().length()-1); 75 result.delete(result.toString().length() - lastWhiteSpaces.length(), result.toString().length()); 76 if (newLinesCount > 0) { 77 result.append("\n\n" + stripEndingNewline(currentComment.getText())); 78 } else { 79 result.append(" " + stripEndingNewline(currentComment.getText())); 80 } 81 result.append(lastWhiteSpaces); 82 } 83 bufferPositionOriginalText+=currentComment.getLength(); 84 allCommentsPosition++; 85 } 86 } else { // disaster handling: return original unformatted text! 87 System.err.println("Disaster Recovery: returning original text!!"); 88 buf = new StringBuffer(); 89 buf.append(originalText); 90 return; 91 } 92 } else { 93 if ((currentMetaText != null) && (bufferPositionFormattedText >= currentMetaText.getOffset())) { 94 result.append(currentMetaText.getText()); 95 metaTexts.remove(currentMetaText); 96 if (metaTexts.iterator().hasNext()) 97 currentMetaText = metaTexts.iterator().next(); 98 else 99 currentMetaText = null; 100 } 101 102 result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1)); 103 bufferPositionOriginalText++; 104 bufferPositionFormattedText++; 105 bufferPositionFormattedTextNoWhitespaces++; 106 107 if ((currentMetaText != null) && (bufferPositionFormattedText >= currentMetaText.getOffset())) { 108 result.append(currentMetaText.getText()); 109 metaTexts.remove(currentMetaText); 110 if (metaTexts.iterator().hasNext()) 111 currentMetaText = metaTexts.iterator().next(); 112 else 113 currentMetaText = null; 114 } 115 } 116 } 117 buf = result; 118 } 119 120 protected void metaText(String str) { 121 MetaTextEntry metaTextEntry = new MetaTextEntry(str, buf.length()); 122 metaTexts.add(metaTextEntry); 123 } 124 125 // ----------------------------------------------------------------------------- 126 127 protected void visitBefore(SectionHeading node) { 128 if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n")) 129 newLine(); 130 131 lastWasSectionHeading=true; 132 133 metaText("<h1>"); 134 text(node.getSectionHeader().replaceAll("[\n\r]", "")); 135 metaText("</h1>"); 136 } 137 138 protected void visitAfter(SectionHeading node) { 139 } 140 141 protected void visitAfter(EtsiBnf node) { 142 weaveComments(); 143 } 14 144 }
Note: See TracChangeset
for help on using the changeset viewer.