X-Git-Url: http://git.phpeclipse.com
diff --git a/net.sourceforge.phpeclipse/src/test/PHPParser.jj b/net.sourceforge.phpeclipse/src/test/PHPParser.jj
index 7c68a46..438fa59 100644
--- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj
+++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj
@@ -29,79 +29,114 @@ import org.eclipse.ui.texteditor.MarkerUtilities;
import org.eclipse.jface.preference.IPreferenceStore;
import java.util.Hashtable;
+import java.util.Enumeration;
+import java.util.ArrayList;
import java.io.StringReader;
+import java.io.*;
import java.text.MessageFormat;
import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpdt.internal.compiler.ast.*;
+import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
-import net.sourceforge.phpdt.internal.compiler.parser.PHPSegmentWithChildren;
-import net.sourceforge.phpdt.internal.compiler.parser.PHPFunctionDeclaration;
-import net.sourceforge.phpdt.internal.compiler.parser.PHPClassDeclaration;
-import net.sourceforge.phpdt.internal.compiler.parser.PHPVarDeclaration;
-import net.sourceforge.phpdt.internal.compiler.parser.PHPReqIncDeclaration;
/**
* A new php parser.
- * This php parser is inspired by the Java 1.2 grammar example
+ * This php parser is inspired by the Java 1.2 grammar example
* given with JavaCC. You can get JavaCC at http://www.webgain.com
* You can test the parser with the PHPParserTestCase2.java
* @author Matthieu Casanova
*/
-public class PHPParser extends PHPParserSuperclass {
+public final class PHPParser extends PHPParserSuperclass {
+ /** The file that is parsed. */
private static IFile fileToParse;
- /** The current segment */
- private static PHPSegmentWithChildren currentSegment;
+ /** The current segment. */
+ private static OutlineableWithChildren currentSegment;
private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
- PHPOutlineInfo outlineInfo;
+ static PHPOutlineInfo outlineInfo;
+
+ public static MethodDeclaration currentFunction;
+ private static boolean assigning;
+
+ /** The error level of the current ParseException. */
private static int errorLevel = ERROR;
+ /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */
private static String errorMessage;
- public PHPParser() {
- }
+ private static int errorStart = -1;
+ private static int errorEnd = -1;
+ private static PHPDocument phpDocument;
+ /**
+ * The point where html starts.
+ * It will be used by the token manager to create HTMLCode objects
+ */
+ public static int htmlStart;
+
+ //ast stack
+ private final static int AstStackIncrement = 100;
+ /** The stack of node. */
+ private static AstNode[] nodes;
+ /** The cursor in expression stack. */
+ private static int nodePtr;
- public void setFileToParse(IFile fileToParse) {
+ public final void setFileToParse(final IFile fileToParse) {
this.fileToParse = fileToParse;
}
- public PHPParser(IFile fileToParse) {
+ public PHPParser() {
+ }
+
+ public PHPParser(final IFile fileToParse) {
this(new StringReader(""));
this.fileToParse = fileToParse;
}
- public void phpParserTester(String strEval) throws CoreException, ParseException {
- PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
- StringReader stream = new StringReader(strEval);
- if (jj_input_stream == null) {
- jj_input_stream = new SimpleCharStream(stream, 1, 1);
- }
- ReInit(new StringReader(strEval));
- phpTest();
+ /**
+ * Reinitialize the parser.
+ */
+ private static final void init() {
+ nodes = new AstNode[AstStackIncrement];
+ nodePtr = -1;
+ htmlStart = 0;
}
- public void htmlParserTester(String strEval) throws CoreException, ParseException {
- StringReader stream = new StringReader(strEval);
- if (jj_input_stream == null) {
- jj_input_stream = new SimpleCharStream(stream, 1, 1);
+ /**
+ * Add an php node on the stack.
+ * @param node the node that will be added to the stack
+ */
+ private static final void pushOnAstNodes(AstNode node) {
+ try {
+ nodes[++nodePtr] = node;
+ } catch (IndexOutOfBoundsException e) {
+ int oldStackLength = nodes.length;
+ AstNode[] oldStack = nodes;
+ nodes = new AstNode[oldStackLength + AstStackIncrement];
+ System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
+ nodePtr = oldStackLength;
+ nodes[nodePtr] = node;
}
- ReInit(stream);
- phpFile();
}
- public PHPOutlineInfo parseInfo(Object parent, String s) {
+ public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
+ currentSegment = new PHPDocument(parent);
outlineInfo = new PHPOutlineInfo(parent);
- currentSegment = outlineInfo.getDeclarations();
- StringReader stream = new StringReader(s);
+ final StringReader stream = new StringReader(s);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
+ init();
try {
parse();
+ phpDocument = new PHPDocument(null);
+ phpDocument.nodes = new AstNode[nodes.length];
+ System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
+ PHPeclipsePlugin.log(1,phpDocument.toString());
} catch (ParseException e) {
processParseException(e);
}
@@ -117,6 +152,8 @@ public class PHPParser extends PHPParserSuperclass {
if (errorMessage == null) {
PHPeclipsePlugin.log(e);
errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
+ errorStart = jj_input_stream.getPosition();
+ errorEnd = errorStart + 1;
}
setMarker(e);
errorMessage = null;
@@ -124,10 +161,27 @@ public class PHPParser extends PHPParserSuperclass {
/**
* Create marker for the parse error
+ * @param e the ParseException
*/
- private static void setMarker(ParseException e) {
+ private static void setMarker(final ParseException e) {
try {
- setMarker(fileToParse, errorMessage, jj_input_stream.tokenBegin,jj_input_stream.tokenBegin+e.currentToken.image.length(), errorLevel);
+ if (errorStart == -1) {
+ setMarker(fileToParse,
+ errorMessage,
+ jj_input_stream.tokenBegin,
+ jj_input_stream.tokenBegin + e.currentToken.image.length(),
+ errorLevel,
+ "Line " + e.currentToken.beginLine);
+ } else {
+ setMarker(fileToParse,
+ errorMessage,
+ errorStart,
+ errorEnd,
+ errorLevel,
+ "Line " + e.currentToken.beginLine);
+ errorStart = -1;
+ errorEnd = -1;
+ }
} catch (CoreException e2) {
PHPeclipsePlugin.log(e2);
}
@@ -136,12 +190,12 @@ public class PHPParser extends PHPParserSuperclass {
/**
* Create markers according to the external parser output
*/
- private static void createMarkers(String output, IFile file) throws CoreException {
+ private static void createMarkers(final String output, final IFile file) throws CoreException {
// delete all markers
file.deleteMarkers(IMarker.PROBLEM, false, 0);
int indx = 0;
- int brIndx = 0;
+ int brIndx;
boolean flag = true;
while ((brIndx = output.indexOf("
", indx)) != -1) {
// newer php error output (tested with 4.2.3)
@@ -158,7 +212,10 @@ public class PHPParser extends PHPParserSuperclass {
}
}
- private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
+ private static void scanLine(final String output,
+ final IFile file,
+ final int indx,
+ final int brIndx) throws CoreException {
String current;
StringBuffer lineNumberBuffer = new StringBuffer(10);
char ch;
@@ -196,8 +253,13 @@ public class PHPParser extends PHPParserSuperclass {
}
}
- public void parse(String s) throws CoreException {
- ReInit(new StringReader(s));
+ public final void parse(final String s) throws CoreException {
+ final StringReader stream = new StringReader(s);
+ if (jj_input_stream == null) {
+ jj_input_stream = new SimpleCharStream(stream, 1, 1);
+ }
+ ReInit(stream);
+ init();
try {
parse();
} catch (ParseException e) {
@@ -209,15 +271,15 @@ public class PHPParser extends PHPParserSuperclass {
* Call the php parse command ( php -l -f <filename> )
* and create markers according to the external parser output
*/
- public static void phpExternalParse(IFile file) {
- IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
- String filename = file.getLocation().toString();
+ public static void phpExternalParse(final IFile file) {
+ final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+ final String filename = file.getLocation().toString();
- String[] arguments = { filename };
- MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
- String command = form.format(arguments);
+ final String[] arguments = { filename };
+ final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
+ final String command = form.format(arguments);
- String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
+ final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
try {
// parse the buffer to find the errors and warnings
@@ -227,7 +289,19 @@ public class PHPParser extends PHPParserSuperclass {
}
}
- public void parse() throws ParseException {
+ /**
+ * Put a new html block in the stack.
+ */
+ public static final void createNewHTMLCode() {
+ final int currentPosition = SimpleCharStream.getPosition();
+ if (currentPosition == htmlStart) {
+ return;
+ }
+ final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
+ pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
+ }
+
+ private static final void parse() throws ParseException {
phpFile();
}
}
@@ -236,14 +310,17 @@ PARSER_END(PHPParser)
TOKEN :
{
- : PHPPARSING
+ {PHPParser.createNewHTMLCode();} : PHPPARSING
+| {PHPParser.createNewHTMLCode();} : PHPPARSING
+| {PHPParser.createNewHTMLCode();} : PHPPARSING
}
TOKEN :
{
- "> : DEFAULT
+ "> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : DEFAULT
}
+/* Skip any character if we are not in php mode */
SKIP :
{
< ~[] >
@@ -251,7 +328,6 @@ PARSER_END(PHPParser)
/* WHITE SPACE */
-
SKIP :
{
" "
@@ -262,20 +338,25 @@ PARSER_END(PHPParser)
}
/* COMMENTS */
-
- MORE :
+ SPECIAL_TOKEN :
{
"//" : IN_SINGLE_LINE_COMMENT
|
+ "#" : IN_SINGLE_LINE_COMMENT
+|
<"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
|
"/*" : IN_MULTI_LINE_COMMENT
}
-
-SPECIAL_TOKEN :
+ SPECIAL_TOKEN :
+{
+ : PHPPARSING
+}
+
+ SPECIAL_TOKEN :
{
- " > : PHPPARSING
+ " > : DEFAULT
}
@@ -306,112 +387,151 @@ MORE :
|
|
|
+|
+|
}
/* LANGUAGE CONSTRUCT */
TOKEN :
{
-
-|
-|
-|
-|
-|
-|
-|
-| ">
-|
-| ">
+
+|
+|
+|
+|
+|
+|
+|
+| ">
+|
+| ">
}
/* RESERVED WORDS AND LITERALS */
TOKEN :
{
- < BREAK: "break" >
-| < CASE: "case" >
-| < CONST: "const" >
-| < CONTINUE: "continue" >
-| < _DEFAULT: "default" >
-| < DO: "do" >
-| < EXTENDS: "extends" >
-| < FALSE: "false" >
-| < FOR: "for" >
-| < GOTO: "goto" >
-| < NEW: "new" >
-| < NULL: "null" >
-| < RETURN: "return" >
-| < SUPER: "super" >
-| < SWITCH: "switch" >
-| < THIS: "this" >
-| < TRUE: "true" >
-| < WHILE: "while" >
-| < ENDWHILE : "endwhile" >
+
+|
+|
+| <_DEFAULT : "default">
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
}
/* TYPES */
-
TOKEN :
{
-
-|