1 package net.sourceforge.phpeclipse.phpeditor;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Hashtable;
20 import java.util.List;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IMarker;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.Position;
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IFileEditorInput;
30 import org.eclipse.ui.texteditor.ITextEditor;
31 import org.eclipse.ui.texteditor.MarkerUtilities;
32 import org.eclipse.ui.texteditor.TextEditorAction;
35 * Class that defines the action for parsing the current PHP file
37 public class PHPParserAction extends TextEditorAction {
39 // public static final String EXE = "exe"; //$NON-NLS-1$
40 // public static final String WINEXE = "winexe"; //$NON-NLS-1$
41 // public static final String LIBRARY = "library"; //$NON-NLS-1$
42 // public static final String MODULE = "module"; //$NON-NLS-1$
44 private static final String ERROR = "error"; //$NON-NLS-1$
45 private static final String WARNING = "warning"; //$NON-NLS-1$
47 private static PHPParserAction instance = new PHPParserAction();
49 protected IFile fileToParse;
50 protected List fVariables = new ArrayList(100);
53 * Constructs and updates the action.
55 private PHPParserAction() {
56 super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
60 public static PHPParserAction getInstance() {
65 * Code called when the action is fired.
69 fileToParse = getPHPFile();
70 if (fileToParse == null) {
71 // should never happen
72 System.err.println("Error : no file in the editor");
73 // should throw an exception
76 // first delete all the previous markers
77 fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
78 //IDocument document = getTextEditor().getDocumentProvider().getDocument(null);
79 //String text = document.get();
84 InputStream iStream = fileToParse.getContents();
85 // int c = iStream.read();
88 } catch (IOException e) {
91 // String message = "Test error";
92 // int lineNumber = 1;
94 // // create marker for the error
96 // setMarker(message, lineNumber, fileToParse);
97 } catch (CoreException e) {
103 * Finds the file that's currently opened in the PHP Text Editor
105 protected IFile getPHPFile() {
106 ITextEditor editor = getTextEditor();
108 IEditorInput editorInput = null;
109 if (editor != null) {
110 editorInput = editor.getEditorInput();
113 if (editorInput instanceof IFileEditorInput)
114 return ((IFileEditorInput) editorInput).getFile();
116 // if nothing was found, which should never happen
121 * Create marker for the parse error
123 protected void setMarker(String message, int lineNumber) throws CoreException {
125 Hashtable attributes = new Hashtable();
126 MarkerUtilities.setMessage(attributes, message);
127 if (message.startsWith(ERROR))
128 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
129 else if (message.startsWith(WARNING))
130 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
132 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
133 MarkerUtilities.setLineNumber(attributes, lineNumber);
134 MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
137 // private String getIdentifier(InputStream iStream, int c) {
140 // // int textLength = text.length();
141 // StringBuffer identifier = new StringBuffer();
142 // identifier.append((char) c);
144 // while ((c = iStream.read()) != (-1)) {
145 // if (Character.isJavaIdentifierPart((char) c)) {
146 // identifier.append((char) c);
147 // // } else if ((i == 0) && (c == '$')) {
148 // // identifier.append((char)c);
150 // return identifier.toString();
154 // } catch (IOException e) {
156 // return identifier.toString();
159 protected void parse(InputStream iStream) {
160 boolean lineCommentMode = false;
161 boolean multiLineCommentMode = false;
162 boolean stringMode = false;
164 // ArrayList phpList = new ArrayList();
165 StringBuffer buf = new StringBuffer();
168 while ((c0 = iStream.read()) != (-1)) {
169 buf.append((char) c0);
171 } catch (IOException e) {
174 String input = buf.toString();
176 int startLineNumber = 1;
180 boolean phpMode = false;
181 boolean phpFound = false;
185 while (i < input.length()) {
186 ch = input.charAt(i++);
190 if ( (! phpMode) && ch == '<') {
191 ch2 = input.charAt(i++);
193 ch2 = input.charAt(i++);
194 if (Character.isWhitespace(ch2)) {
199 startLineNumber = lineNumber;
201 } else if (ch2 == 'p') {
202 ch2 = input.charAt(i++);
204 ch2 = input.charAt(i++);
209 startLineNumber = lineNumber;
215 } else if (ch2 == 'P') {
216 ch2 = input.charAt(i++);
218 ch2 = input.charAt(i++);
223 startLineNumber = lineNumber;
237 if (lineCommentMode && (ch == '\n')) {
238 lineCommentMode = false;
239 // read until end of line
240 } else if ((!stringMode) && (ch == '#')) {
241 // read until end of line
242 lineCommentMode = true;
244 } else if ((!stringMode) && (!multiLineCommentMode) && (ch == '/')) {
245 ch2 = input.charAt(i++);
247 lineCommentMode = true;
249 } else if (ch2 == '*') {
250 multiLineCommentMode = true;
255 } else if (ch == '*' && multiLineCommentMode) {
256 ch2 = input.charAt(i++);
258 multiLineCommentMode = false;
263 } else if (ch == '\\' && stringMode) {
264 ch2 = input.charAt(i++);
270 } else if ((!lineCommentMode) && (!multiLineCommentMode) && (ch == '"')) {
278 if (lineCommentMode || multiLineCommentMode || stringMode) {
283 ch2 = input.charAt(i++);
287 // phpList.add(input.substring(startIndex, i-2));
289 PHPParser parser = new PHPParser();
290 parser.start(input.substring(startIndex, i - 2), startLineNumber);
291 } catch (SyntaxError err) {
292 setMarker(err.getMessage(), err.getLine());
302 setMarker("No PHP source code found.", lineNumber);
304 } catch (CoreException e) {