Changeset 21 in default
- Timestamp:
- 11/01/10 17:17:05 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java
r17 r21 1 1 package de.ugoe.cs.swe.bnftools.ui.formatter; 2 3 import java.util.ArrayList; 2 4 3 5 import org.eclipse.emf.common.util.EList; 4 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.NodeAdapter; 11 import org.eclipse.xtext.parsetree.NodeUtil; 5 12 6 13 import de.ugoe.cs.swe.bnftools.ebnf.Atom; … … 31 38 private StringBuffer buf; 32 39 private FormatterConfig config; 33 40 private int bufferPosition = 0; 41 private int bufferPositionOriginalText = 0; 42 private int commentPosition = 0; 43 private int allCommentsPosition = 0; 44 private ArrayList<LeafNode> allComments = new ArrayList<LeafNode>(); 45 private ArrayList<String> collectedComments = new ArrayList<String>(); 46 34 47 private boolean lastWasSectionHeading=false; 48 private CompositeNode parserEtsiBnfNode; 49 private String originalText; 50 private String originalTextNoWhitespaces; 51 private int bufferPositionFormattedText; 52 private String formattedText; 53 private String bufNoWhitespaces; 35 54 36 55 public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) { … … 49 68 } 50 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 ArrayList<String> collectComments(CompositeNode node) { 93 // collectedComments.clear(); 94 //// System.out.println(node); 95 //// System.out.println(node.serialize()); 96 //// searchSubTree(node); 97 // return collectedComments; 98 // } 99 100 private boolean isSingleLineComment(String str) { 101 if (str.startsWith("//")) 102 return true; 103 return false; 104 } 105 106 private boolean isMultiLineComment(String str) { 107 if (str.startsWith("/*")) 108 return true; 109 return false; 110 } 111 112 private boolean isWhitespace(char ch) { 113 if ((ch==' ') || (ch == '\t') || (ch == '\n') || (ch == '\r')) 114 return true; 115 return false; 116 } 117 118 private void skipWhitespacesOriginalText() { 119 while (isWhitespace(originalText.charAt(bufferPositionOriginalText))) { 120 bufferPositionOriginalText++; 121 } 122 } 123 124 private void skipWhitespacesFormattedText(StringBuffer result) { 125 while (isWhitespace(formattedText.charAt(bufferPositionFormattedText))) { 126 result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1)); 127 bufferPositionFormattedText++; 128 } 129 } 130 131 private boolean isSingleLineCommentNext(String str, int position) { 132 if ((str.charAt(position) == '/') && (str.charAt(position) == '/')) 133 return true; 134 return false; 135 } 136 137 private boolean isMultiLineCommentNext(String str, int position) { 138 if ((str.charAt(position) == '/') && (str.charAt(position) == '*')) 139 return true; 140 return false; 141 } 142 143 private boolean isCommentNext(String str, int position) { 144 if (isSingleLineCommentNext(str, position) || isMultiLineCommentNext(str, position)) 145 return true; 146 else 147 return false; 148 } 149 150 private void weaveComments() { 151 bufferPosition = 0; 152 bufferPositionOriginalText = 0; 153 bufferPositionFormattedText = 0; 154 155 StringBuffer result = new StringBuffer(); 156 bufNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", ""); 157 formattedText = buf.toString(); 158 159 while (bufferPosition < bufNoWhitespaces.length()) { 160 skipWhitespacesOriginalText(); 161 skipWhitespacesFormattedText(result); 162 163 if (bufNoWhitespaces.charAt(bufferPosition) != originalText.charAt(bufferPositionOriginalText)) { 164 if (isCommentNext(originalText, bufferPositionOriginalText)) { 165 LeafNode currentComment = allComments.get(allCommentsPosition); 166 if (currentComment.getTotalOffset() == bufferPositionOriginalText) { 167 result.append(currentComment.getText()); 168 169 if (isMultiLineComment(currentComment.getText())) { 170 result.append("\n"); 171 } 172 bufferPositionOriginalText+=currentComment.getLength(); 173 allCommentsPosition++; 174 } 175 } 176 177 } else { 178 result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1)); 179 180 bufferPositionOriginalText++; 181 bufferPositionFormattedText++; 182 bufferPosition++; 183 } 184 } 185 buf = result; 186 187 } 188 189 private void appendComments(EObject node) { 190 // String currentBuf = buf.toString().replaceAll("[ \t\n\r]", ""); 191 // while (bufferPosition < currentBuf.length()) { 192 // skipWhitespacesOriginalText(); 193 // System.out.print(currentBuf.charAt(bufferPosition)); 194 // System.out.print(originalText.charAt(bufferPositionOriginalText)); 195 // bufferPositionOriginalText++; 196 // bufferPosition++; 197 // } 198 199 // 200 // ArrayList<String> comments = collectComments(parseTreeNode); 201 // for (int i=0; i < comments.size(); i++) { 202 // if (isSingleLineComment(comments.get(i))) { 203 // buf.append(" "); 204 // buf.append(comments.get(i).trim()); 205 // if (i+1 < comments.size()) 206 // buf.append("\n"); 207 // } else if (isMultiLineComment(comments.get(i))) { 208 // buf.append("\n"); 209 // buf.append(comments.get(i)); 210 // if (i+1 < comments.size()) 211 // buf.append("\n"); 212 // } 213 // } 214 215 } 216 51 217 // ----------------------------------------------------------------------------- 52 218 53 219 protected void visitBefore(EtsiBnf node) { 220 parserEtsiBnfNode = NodeUtil.getNodeAdapter(node).getParserNode(); 221 collectAllComments(parserEtsiBnfNode); 222 originalText = NodeUtil.getNodeAdapter(node).getParserNode().serialize(); 223 originalTextNoWhitespaces = originalText.replaceAll("[ \t\n\r]", ""); 224 225 //System.out.println(allComments.toString()); 54 226 buf.append("grammar " + node.getName()); 55 227 if (node.getType() != null) 56 228 buf.append(node.getType()); 57 229 buf.append(";"); 58 buf.append("\n"); 230 231 appendComments(node); 232 233 buf.append("\n\n"); 59 234 } 60 235 61 236 protected void visitAfter(EtsiBnf node) { 237 weaveComments(); 62 238 } 63 239 64 240 protected void visitBefore(ImportSection node) { 65 buf.append("\n");66 241 } 67 242 … … 135 310 136 311 protected void visitBefore(Import node) { 137 buf.append("import \"" + node.getImportURI() + "\";\n"); 312 buf.append("import \"" + node.getImportURI() + "\";"); 313 appendComments(node); 314 buf.append("\n"); 315 138 316 } 139 317 … … 178 356 179 357 protected void visitAfter(Rule node) { 180 buf.append(";\n"); 358 buf.append(";"); 359 // appendComments(node); 360 buf.append("\n"); 181 361 } 182 362 … … 204 384 205 385 buf.append(node.getSectionHeader()); 386 387 appendComments(node); 206 388 } 207 389
Note: See TracChangeset
for help on using the changeset viewer.