Changeset 21 in default


Ignore:
Timestamp:
11/01/10 17:17:05 (14 years ago)
Author:
zeiss
Message:
 
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  
    11package de.ugoe.cs.swe.bnftools.ui.formatter; 
     2 
     3import java.util.ArrayList; 
    24 
    35import org.eclipse.emf.common.util.EList; 
    46import org.eclipse.emf.ecore.EObject; 
     7import org.eclipse.xtext.parsetree.AbstractNode; 
     8import org.eclipse.xtext.parsetree.CompositeNode; 
     9import org.eclipse.xtext.parsetree.LeafNode; 
     10import org.eclipse.xtext.parsetree.NodeAdapter; 
     11import org.eclipse.xtext.parsetree.NodeUtil; 
    512 
    613import de.ugoe.cs.swe.bnftools.ebnf.Atom; 
     
    3138        private StringBuffer buf; 
    3239        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         
    3447        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; 
    3554         
    3655        public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) { 
     
    4968        } 
    5069 
     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         
    51217        // ----------------------------------------------------------------------------- 
    52218 
    53219        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()); 
    54226                buf.append("grammar " + node.getName()); 
    55227                if (node.getType() != null) 
    56228                        buf.append(node.getType()); 
    57229                buf.append(";"); 
    58                 buf.append("\n"); 
     230 
     231                appendComments(node); 
     232                 
     233                buf.append("\n\n"); 
    59234        } 
    60235 
    61236        protected void visitAfter(EtsiBnf node) { 
     237                weaveComments(); 
    62238        } 
    63239 
    64240        protected void visitBefore(ImportSection node) { 
    65                 buf.append("\n"); 
    66241        } 
    67242 
     
    135310 
    136311        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                 
    138316        } 
    139317 
     
    178356 
    179357        protected void visitAfter(Rule node) { 
    180                 buf.append(";\n"); 
     358                buf.append(";"); 
     359//              appendComments(node); 
     360                buf.append("\n"); 
    181361        } 
    182362 
     
    204384                 
    205385                buf.append(node.getSectionHeader()); 
     386                 
     387                appendComments(node); 
    206388        } 
    207389 
Note: See TracChangeset for help on using the changeset viewer.