PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/ecmascript/EcmaScriptSideKickParser.java

#
Java | 202 lines | 96 code | 27 blank | 79 comment | 21 complexity | fb23336b881ddf179c047c59976240c0 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. Copyright (c) 2006, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the <ORGANIZATION> nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package sidekick.ecmascript;
  26. import java.io.StringReader;
  27. import java.util.*;
  28. import javax.swing.tree.DefaultMutableTreeNode;
  29. import org.gjt.sp.jedit.Buffer;
  30. import org.gjt.sp.jedit.View;
  31. import errorlist.DefaultErrorSource;
  32. import errorlist.ErrorSource;
  33. import sidekick.*;
  34. import sidekick.enhanced.*;
  35. import sidekick.util.*;
  36. import sidekick.ecmascript.parser.*;
  37. /**
  38. * @author Dale Anson
  39. * @version $Revision$
  40. */
  41. public class EcmaScriptSideKickParser extends SideKickParser implements PartialParser {
  42. private View currentView = null;
  43. public static boolean showAll = true;
  44. private int lineOffset = 0;
  45. public EcmaScriptSideKickParser() {
  46. super( "ecmascript" );
  47. }
  48. /**
  49. * If called by another parser to parse part of a file (for example, to parse
  50. * a script tag in an html document), this can be set to the offset of the
  51. * script tag so that the node locations can be set correctly.
  52. */
  53. public void setStartLine( int offset ) {
  54. if ( offset > 0 ) {
  55. lineOffset = offset;
  56. }
  57. }
  58. public void parse() {
  59. if ( currentView != null ) {
  60. parse( currentView.getBuffer(), null );
  61. }
  62. }
  63. /**
  64. * Parse the contents of the given buffer. This is the standard entry point
  65. * and will cause the entire text of the buffer to be parsed.
  66. *
  67. * @param buffer the buffer to parse
  68. * @param errorSource where to send errors
  69. * @return Description of the Returned Value
  70. */
  71. public SideKickParsedData parse( Buffer buffer, DefaultErrorSource errorSource ) {
  72. setStartLine( 0 );
  73. return parse( buffer, buffer.getText( 0, buffer.getLength() ), errorSource );
  74. }
  75. /**
  76. * Parse the contents of the given text. This is the entry point to use when
  77. * only a portion of the buffer text is to be parsed. Note that <code>setLineOffset</code>
  78. * should be called prior to calling this method, otherwise, tree node positions
  79. * may be off.
  80. *
  81. * @param buffer the buffer to parse
  82. * @param errorSource where to send errors
  83. * @return Description of the Returned Value
  84. */
  85. public SideKickParsedData parse( Buffer buffer, String text, DefaultErrorSource errorSource ) {
  86. String filename = buffer.getPath();
  87. SideKickParsedData parsedData = new SideKickParsedData( buffer.getName() );
  88. DefaultMutableTreeNode root = parsedData.root;
  89. StringReader reader = new StringReader( text );
  90. try {
  91. // create parser
  92. EcmaScript parser = new EcmaScript( reader );
  93. // set line offset, the parser uses this to adjust line numbers in the
  94. // case of a partial file, like when the javascript is embedded inside an
  95. // html document
  96. parser.setLineOffset( lineOffset );
  97. // set tab size so that the parser can accurately calculate line and
  98. // column positions
  99. parser.setTabSize( buffer.getTabSize() );
  100. // parse the text
  101. SimpleNode ss = parser.Program();
  102. // make a tree
  103. addTreeNodes( root, ss );
  104. // need to convert the nodes that are currently the user objects
  105. // in the tree nodes to SideKick Assets
  106. ElementUtil.convert( buffer, root );
  107. if ( !buffer.isDirty() && errorSource != null ) {
  108. /* only handle errors when buffer is saved. Otherwise, there will be a lot
  109. of spurious errors shown when code completion is on and the user is in the
  110. middle of typing something. */
  111. List<ParseError> parseErrors = parser.getParseErrors();
  112. for ( ParseError pe : parseErrors ) {
  113. String message = pe.message;
  114. Range range = pe.range;
  115. // addError is lame -- what if the error spans more than one line?
  116. // Need to just deal with it...
  117. if ( range.endLine != range.startLine ) {
  118. range.endColumn = range.startColumn;
  119. }
  120. errorSource.addError( ErrorSource.ERROR, filename, range.startLine, range.startColumn, range.endColumn, message );
  121. }
  122. }
  123. }
  124. catch ( Exception e ) {
  125. e.printStackTrace();
  126. }
  127. finally {
  128. reader.close();
  129. }
  130. return parsedData;
  131. }
  132. private void addTreeNodes( DefaultMutableTreeNode root, SimpleNode ss ) {
  133. if ( ss != null && ss.hasChildren() ) {
  134. Collections.sort(ss.getChildren(), nodeSorter);
  135. for ( Iterator it = ss.getChildren().iterator(); it.hasNext(); ) {
  136. SimpleNode cssChild = ( SimpleNode ) it.next();
  137. if ( cssChild != null && cssChild.isVisible()) {
  138. DefaultMutableTreeNode dmtNode = new DefaultMutableTreeNode( cssChild );
  139. root.add( dmtNode );
  140. addTreeNodeChildren( dmtNode, cssChild );
  141. }
  142. }
  143. }
  144. }
  145. private void addTreeNodeChildren( DefaultMutableTreeNode dmtNode, SimpleNode cssNode ) {
  146. if ( cssNode.hasChildren() ) {
  147. for ( Iterator it = cssNode.getChildren().iterator(); it.hasNext(); ) {
  148. SimpleNode cssChild = ( SimpleNode ) it.next();
  149. if ( cssChild != null && cssChild.isVisible() ) {
  150. DefaultMutableTreeNode dmtChild = new DefaultMutableTreeNode( cssChild );
  151. dmtNode.add( dmtChild );
  152. addTreeNodeChildren( dmtChild, cssChild );
  153. }
  154. }
  155. }
  156. }
  157. /*
  158. public boolean supportsCompletion() {
  159. return false;
  160. }
  161. public boolean canCompleteAnywhere() {
  162. return false;
  163. }
  164. public SideKickCompletion complete( EditPane editPane, int caret ) {
  165. return null;
  166. }
  167. */
  168. Comparator nodeSorter = new Comparator(){
  169. public int compare(Object a, Object b) {
  170. return a.toString().compareToIgnoreCase(b.toString());
  171. }
  172. };
  173. }