import org.eclipse.ui.texteditor.MarkerUtilities;
import org.eclipse.jface.preference.IPreferenceStore;
-import java.io.CharArrayReader;
import java.util.Hashtable;
import java.io.StringReader;
import java.text.MessageFormat;
import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
/**
* A new php parser.
*/
public class PHPParser extends PHPParserSuperclass {
- private static PHPParser me;
-
private static IFile fileToParse;
+ /** The current segment */
+ private static PHPSegmentWithChildren currentSegment;
+
private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
public static final int ERROR = 2;
public PHPParser() {
}
- public static PHPParser getInstance(IFile fileToParse) {
- if (me == null) {
- me = new PHPParser(fileToParse);
- } else {
- me.setFileToParse(fileToParse);
- }
- return me;
- }
-
public void setFileToParse(IFile fileToParse) {
this.fileToParse = fileToParse;
}
- public static PHPParser getInstance(java.io.Reader stream) {
- if (me == null) {
- me = new PHPParser(stream);
- } else {
- me.ReInit(stream);
- }
- return me;
- }
-
public PHPParser(IFile fileToParse) {
this(new StringReader(""));
this.fileToParse = fileToParse;
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
- phpTest();
+ phpFile();
}
public PHPOutlineInfo parseInfo(Object parent, String s) {
outlineInfo = new PHPOutlineInfo(parent);
+ currentSegment = outlineInfo.getDeclarations();
StringReader stream = new StringReader(s);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
try {
parse();
} catch (ParseException e) {
- PHPeclipsePlugin.log(e);
+ if (errorMessage == null) {
+ PHPeclipsePlugin.log(e);
+ } else {
+ setMarker(errorMessage, e.currentToken.beginLine, errorLevel);
+ errorMessage = null;
+ }
}
}
<DEFAULT> TOKEN :
{
- "<?php" : PHPPARSING
-| "<?" : PHPPARSING
+ <PHPSTART : "<?php" | "<?"> : PHPPARSING
}
-<DEFAULT> SKIP :
+<PHPPARSING> TOKEN :
{
- < ~[] >
+ <PHPEND :"?>"> : DEFAULT
}
-<PHPPARSING> TOKEN :
+<DEFAULT> SKIP :
{
- "?>" : DEFAULT
+ < ~[] >
}
+
/* WHITE SPACE */
<PHPPARSING> SKIP :
void phpFile() :
{}
{
- ("<?php" Php() "?>")*
+ (<PHPSTART> Php() <PHPEND>)*
<EOF>
}
}
void ClassDeclaration() :
-{}
{
- <CLASS> <IDENTIFIER> [ <EXTENDS> <IDENTIFIER> ]
+ PHPClassDeclaration classDeclaration;
+ Token className;
+ int pos = jj_input_stream.bufpos;
+}
+{
+ <CLASS> className = <IDENTIFIER> [ <EXTENDS> <IDENTIFIER> ]
+ {
+ classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos);
+ currentSegment.add(classDeclaration);
+ currentSegment = classDeclaration;
+ }
ClassBody()
+ {
+ currentSegment = (PHPSegmentWithChildren) currentSegment.getParent();
+ }
}
void ClassBody() :
<DOLLAR> VariableName()
}
-void VariableName():
+String VariableName():
{}
{
<LBRACE> Expression() <RBRACE>
}
void MethodDeclaration() :
-{}
{
- <FUNCTION> MethodDeclarator()
+ PHPFunctionDeclaration functionDeclaration;
+}
+{
+ <FUNCTION> functionDeclaration = MethodDeclarator()
+ {
+ currentSegment.add(functionDeclaration);
+ currentSegment = functionDeclaration;
+ }
( Block() | <SEMICOLON> )
+ {
+ currentSegment = (PHPSegmentWithChildren) currentSegment.getParent();
+ }
}
-void MethodDeclarator() :
-{}
+PHPFunctionDeclaration MethodDeclarator() :
{
- [<BIT_AND>] <IDENTIFIER> FormalParameters()
+ Token bit_and = null;
+ Token identifier;
+ StringBuffer methodDeclaration = new StringBuffer();
+ String formalParameters;
+ int pos = jj_input_stream.bufpos;
+}
+{
+ [ bit_and = <BIT_AND>]
+ identifier = <IDENTIFIER> FormalParameters()
+ {
+ if (bit_and != null) {
+ methodDeclaration.append("&");
+ }
+ methodDeclaration.append(identifier);
+ return new PHPFunctionDeclaration(currentSegment,methodDeclaration.toString(),pos);
+ }
}
void FormalParameters() :
<INTEGER>
}
-/*
- * Expression syntax follows.
- */
-
void Expression() :
-/*
- * This expansion has been written this way instead of:
- * Assignment() | ConditionalExpression()
- * for performance reasons.
- * However, it is a weakening of the grammar for it allows the LHS of
- * assignments to be any conditional expression whereas it can only be
- * a primary expression. Consider adding a semantic predicate to work
- * around this.
- */
{}
{
PrintExpression()
{
<IDENTIFIER>
|
- <NEW> ClassIdentifier()
+ [<BIT_AND>] <NEW> ClassIdentifier()
|
VariableDeclaratorId()
}
|
<FLOATING_POINT_LITERAL>
|
- <STRING_LITERAL>
+ try {
+ <STRING_LITERAL>
+ } catch (TokenMgrError e) {
+ errorMessage = "unterminated string";
+ errorLevel = ERROR;
+ throw generateParseException();
+ }
|
BooleanLiteral()
|
void Arguments() :
{}
{
- <LPAREN> [ ArgumentList() ] <RPAREN>
+ <LPAREN> [ ArgumentList() ]
+ try {
+ <RPAREN>
+ } catch (ParseException e) {
+ errorMessage = "')' expected to close the argument list";
+ errorLevel = ERROR;
+ throw e;
+ }
}
void ArgumentList() :
{}
{
- Expression() ( <COMMA> Expression() )*
+ Expression()
+ ( <COMMA>
+ try {
+ Expression()
+ } catch (ParseException e) {
+ errorMessage = "expression expected after a comma in argument list";
+ errorLevel = ERROR;
+ throw e;
+ }
+ )*
}
/*
*/
{}
{
- <IF> Condition("if") Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) <ELSE> Statement() ]
+ <IF> Condition("if") Statement() ( LOOKAHEAD(1) ElseIfStatement() )* [ LOOKAHEAD(1) <ELSE> Statement() ]
}
void Condition(String keyword) :