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 2de24e2..e8ab5f2 100644
--- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj
+++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj
@@ -1,3 +1,4 @@
+
options {
LOOKAHEAD = 1;
CHOICE_AMBIGUITY_CHECK = 2;
@@ -17,6 +18,7 @@ options {
BUILD_TOKEN_MANAGER = true;
SANITY_CHECK = true;
FORCE_LA_CHECK = false;
+ COMMON_TOKEN_ACTION = true;
}
PARSER_BEGIN(PHPParser)
@@ -29,79 +31,159 @@ import org.eclipse.ui.texteditor.MarkerUtilities;
import org.eclipse.jface.preference.IPreferenceStore;
import java.util.Hashtable;
+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.Outlineable;
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;
+import net.sourceforge.phpdt.internal.corext.Assert;
/**
* 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 final class PHPParser extends PHPParserSuperclass {
- private static IFile fileToParse;
+//todo : fix the variables names bug
+//todo : handle tilde operator
+
- /** 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;
+
+ /** 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;
+
+ private static final String SYNTAX_ERROR_CHAR = "syntax error";
+ /**
+ * 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 static final boolean PARSER_DEBUG = false;
public final void setFileToParse(final IFile fileToParse) {
- this.fileToParse = fileToParse;
+ PHPParser.fileToParse = fileToParse;
+ }
+
+ public PHPParser() {
}
public PHPParser(final IFile fileToParse) {
this(new StringReader(""));
- this.fileToParse = fileToParse;
+ PHPParser.fileToParse = fileToParse;
}
- public static final void phpParserTester(final String strEval) throws CoreException, ParseException {
- PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
+ public static final void phpParserTester(final String strEval) throws ParseException {
final StringReader stream = new StringReader(strEval);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(new StringReader(strEval));
+ init();
+ phpDocument = new PHPDocument(null,"_root".toCharArray());
+ currentSegment = phpDocument;
+ outlineInfo = new PHPOutlineInfo(null, currentSegment);
+ PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
phpTest();
}
- public static final void htmlParserTester(final String strEval) throws CoreException, ParseException {
+ public static final void htmlParserTester(final File fileName) throws FileNotFoundException, ParseException {
+ final Reader stream = new FileReader(fileName);
+ if (jj_input_stream == null) {
+ jj_input_stream = new SimpleCharStream(stream, 1, 1);
+ }
+ ReInit(stream);
+ init();
+ phpDocument = new PHPDocument(null,"_root".toCharArray());
+ currentSegment = phpDocument;
+ outlineInfo = new PHPOutlineInfo(null, currentSegment);
+ phpFile();
+ }
+
+ public static final void htmlParserTester(final String strEval) throws ParseException {
final StringReader stream = new StringReader(strEval);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
+ init();
+ phpDocument = new PHPDocument(null,"_root".toCharArray());
+ currentSegment = phpDocument;
+ outlineInfo = new PHPOutlineInfo(null, currentSegment);
phpFile();
}
+ /**
+ * Reinitialize the parser.
+ */
+ private static final void init() {
+ nodes = new AstNode[AstStackIncrement];
+ nodePtr = -1;
+ htmlStart = 0;
+ }
+
+ /**
+ * Add an php node on the stack.
+ * @param node the node that will be added to the stack
+ */
+ private static final void pushOnAstNodes(final AstNode node) {
+ try {
+ nodes[++nodePtr] = node;
+ } catch (IndexOutOfBoundsException e) {
+ final int oldStackLength = nodes.length;
+ final AstNode[] oldStack = nodes;
+ nodes = new AstNode[oldStackLength + AstStackIncrement];
+ System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
+ nodePtr = oldStackLength;
+ nodes[nodePtr] = node;
+ }
+ }
+
public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
- outlineInfo = new PHPOutlineInfo(parent);
- currentSegment = outlineInfo.getDeclarations();
+ phpDocument = new PHPDocument(parent,"_root".toCharArray());
+ currentSegment = phpDocument;
+ outlineInfo = new PHPOutlineInfo(parent, currentSegment);
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.nodes = new AstNode[nodes.length];
+ System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
+ if (PHPeclipsePlugin.DEBUG) {
+ PHPeclipsePlugin.log(1,phpDocument.toString());
+ }
} catch (ParseException e) {
processParseException(e);
}
@@ -109,6 +191,19 @@ public final class PHPParser extends PHPParserSuperclass {
}
/**
+ * This function will throw the exception if we are in debug mode
+ * and process it if we are in production mode.
+ * this should be fast since the PARSER_DEBUG is static final so the difference will be at compile time
+ * @param e the exception
+ * @throws ParseException the thrown exception
+ */
+ private static void processParseExceptionDebug(final ParseException e) throws ParseException {
+ if (PARSER_DEBUG) {
+ throw e;
+ }
+ processParseException(e);
+ }
+ /**
* This method will process the parse exception.
* If the error message is null, the parse exception wasn't catched and a trace is written in the log
* @param e the ParseException
@@ -117,64 +212,53 @@ public final 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 = e.currentToken.sourceStart;
+ errorEnd = e.currentToken.sourceEnd;
}
setMarker(e);
errorMessage = null;
+ // if (PHPeclipsePlugin.DEBUG) PHPeclipsePlugin.log(e);
}
/**
- * Create marker for the parse error
+ * Create marker for the parse error.
* @param e the ParseException
*/
private static void setMarker(final ParseException e) {
try {
- setMarker(fileToParse,
- errorMessage,
- jj_input_stream.tokenBegin,
- jj_input_stream.tokenBegin + e.currentToken.image.length(),
- errorLevel,
- "Line " + e.currentToken.beginLine);
+ if (errorStart == -1) {
+ setMarker(fileToParse,
+ errorMessage,
+ e.currentToken.sourceStart,
+ e.currentToken.sourceEnd,
+ errorLevel,
+ "Line " + e.currentToken.beginLine+", "+e.currentToken.sourceStart+":"+e.currentToken.sourceEnd);
+ } else {
+ setMarker(fileToParse,
+ errorMessage,
+ errorStart,
+ errorEnd,
+ errorLevel,
+ "Line " + e.currentToken.beginLine+", "+errorStart+":"+errorEnd);
+ errorStart = -1;
+ errorEnd = -1;
+ }
} catch (CoreException e2) {
PHPeclipsePlugin.log(e2);
}
}
- /**
- * Create markers according to the external parser output
- */
- 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;
- boolean flag = true;
- while ((brIndx = output.indexOf("
", indx)) != -1) {
- // newer php error output (tested with 4.2.3)
- scanLine(output, file, indx, brIndx);
- indx = brIndx + 6;
- flag = false;
- }
- if (flag) {
- while ((brIndx = output.indexOf("
", indx)) != -1) {
- // older php error output (tested with 4.2.3)
- scanLine(output, file, indx, brIndx);
- indx = brIndx + 4;
- }
- }
- }
-
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);
+ final StringBuffer lineNumberBuffer = new StringBuffer(10);
char ch;
current = output.substring(indx, brIndx);
if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
- int onLine = current.indexOf("on line ");
+ final int onLine = current.indexOf("on line ");
if (onLine != -1) {
lineNumberBuffer.delete(0, lineNumberBuffer.length());
for (int i = onLine; i < current.length(); i++) {
@@ -184,9 +268,9 @@ public final class PHPParser extends PHPParserSuperclass {
}
}
- int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
+ final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
- Hashtable attributes = new Hashtable();
+ final Hashtable attributes = new Hashtable();
current = current.replaceAll("\n", "");
current = current.replaceAll("", "");
@@ -205,12 +289,13 @@ public final class PHPParser extends PHPParserSuperclass {
}
}
- public final void parse(final String s) throws CoreException {
+ public final void parse(final String 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();
} catch (ParseException e) {
@@ -240,25 +325,70 @@ public final class PHPParser extends PHPParserSuperclass {
}
}
- public static final void parse() throws ParseException {
+ /**
+ * Put a new html block in the stack.
+ */
+ public static final void createNewHTMLCode() {
+ final int currentPosition = token.sourceStart;
+ if (currentPosition == htmlStart ||
+ currentPosition > SimpleCharStream.currentBuffer.length()) {
+ return;
+ }
+ final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
+ pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
+ }
+
+ /** Create a new task. */
+ public static final void createNewTask() {
+ final int currentPosition = token.sourceStart;
+ final String todo = SimpleCharStream.currentBuffer.substring(currentPosition-3,
+ SimpleCharStream.currentBuffer.indexOf("\n",
+ currentPosition)-1);
+ if (!PARSER_DEBUG) {
+ try {
+ setMarker(fileToParse,
+ todo,
+ SimpleCharStream.getBeginLine(),
+ TASK,
+ "Line "+SimpleCharStream.getBeginLine());
+ } catch (CoreException e) {
+ PHPeclipsePlugin.log(e);
+ }
+ }
+ }
+
+ private static final void parse() throws ParseException {
phpFile();
}
}
PARSER_END(PHPParser)
+TOKEN_MGR_DECLS:
+{
+ // CommonTokenAction: use the begins/ends fields added to the Jack
+ // CharStream class to set corresponding fields in each Token (which was
+ // also extended with new fields). By default Jack doesn't supply absolute
+ // offsets, just line/column offsets
+ static void CommonTokenAction(Token t) {
+ t.sourceStart = input_stream.beginOffset;
+ t.sourceEnd = input_stream.endOffset;
+ } // CommonTokenAction
+} // TOKEN_MGR_DECLS
+
TOKEN :
{
- : PHPPARSING
-| : PHPPARSING
-| : PHPPARSING
+ {PHPParser.createNewHTMLCode();} : PHPPARSING
+| {PHPParser.createNewHTMLCode();} : PHPPARSING
+| {PHPParser.createNewHTMLCode();} : PHPPARSING
}
- TOKEN :
+ TOKEN :
{
- "> : DEFAULT
+ "> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT
}
+/* Skip any character if we are not in php mode */
SKIP :
{
< ~[] >
@@ -266,7 +396,6 @@ PARSER_END(PHPParser)
/* WHITE SPACE */
-
SKIP :
{
" "
@@ -277,36 +406,33 @@ PARSER_END(PHPParser)
}
/* COMMENTS */
-
SPECIAL_TOKEN :
{
"//" : IN_SINGLE_LINE_COMMENT
-|
- <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
-|
- "/*" : IN_MULTI_LINE_COMMENT
+| "#" : IN_SINGLE_LINE_COMMENT
+| <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
+| "/*" : IN_MULTI_LINE_COMMENT
}
SPECIAL_TOKEN :
{
: PHPPARSING
+| < ~[] >
}
- SPECIAL_TOKEN :
+ SPECIAL_TOKEN :
{
- " > : DEFAULT
+ "todo" {PHPParser.createNewTask();}
}
-
-SPECIAL_TOKEN :
+ SPECIAL_TOKEN :
{
- : PHPPARSING
+ "*/" : PHPPARSING
}
-
-SPECIAL_TOKEN :
+ SPECIAL_TOKEN :
{
- : PHPPARSING
+ "*/" : PHPPARSING
}
@@ -326,6 +452,7 @@ MORE :
|
|
|
+|
}
/* LANGUAGE CONSTRUCT */
@@ -338,16 +465,13 @@ MORE :
|
|
|
+|
|
| ">
|
| ">
}
- TOKEN :
-{
-
-}
/* RESERVED WORDS AND LITERALS */
TOKEN :
@@ -370,6 +494,7 @@ MORE :
|
|
|
+|
|
|
|
@@ -377,7 +502,6 @@ MORE :
}
/* TYPES */
-
TOKEN :
{
@@ -391,73 +515,74 @@ MORE :
|
}
+//Misc token
TOKEN :
{
- <_ORL : "OR">
-| <_ANDL : "AND">
+
+|
+|
+|
+|
+|
}
-/* LITERALS */
+/* OPERATORS */
+ TOKEN :
+{
+
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+|
+| >">
+| >>">
+| <_ORL : "OR">
+| <_ANDL : "AND">
+}
+/* LITERALS */
TOKEN :
{
- < INTEGER_LITERAL:
+ (["l","L"])?
| (["l","L"])?
| (["l","L"])?
>
|
- < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
+ <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
|
- < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
+ <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
|
- < #OCTAL_LITERAL: "0" (["0"-"7"])* >
+ <#OCTAL_LITERAL: "0" (["0"-"7"])* >
|
- < FLOATING_POINT_LITERAL:
+ )? (["f","F","d","D"])?
| "." (["0"-"9"])+ ()? (["f","F","d","D"])?
| (["0"-"9"])+ (["f","F","d","D"])?
| (["0"-"9"])+ ()? ["f","F","d","D"]
>
|
- < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
+ <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
|
- < STRING_LITERAL: ( | | )>
-| < STRING_1:
- "\""
- (
- ~["\""]
- |
- "\\\""
- )*
- "\""
- >
-| < STRING_2:
- "'"
- (
- ~["'"]
- |
- "\\'"
- )*
-
- "'"
- >
-| < STRING_3:
- "`"
- (
- ~["`"]
- |
- "\\`"
- )*
- "`"
- >
+ | | )>
+|
+|
+|
}
/* IDENTIFIERS */
TOKEN :
{
- < IDENTIFIER: (|) (||)* >
+ |) (||)* >
|
< #LETTER:
["a"-"z"] | ["A"-"Z"]
@@ -493,10 +618,10 @@ MORE :
{
">
|
-|
+|
|
| =">
-|
+|
| ">
|
|
@@ -516,38 +641,13 @@ MORE :
|
|
|
-}
-
-/* OPERATORS */
- TOKEN :
-{
-
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-|
-| >">
-| >>">
|
| >=">
}
TOKEN :
{
- < DOLLAR_ID: >
+ >
}
void phpTest() :
@@ -562,28 +662,38 @@ void phpFile() :
{
try {
(PhpBlock())*
-
+ {PHPParser.createNewHTMLCode();}
} catch (TokenMgrError e) {
+ PHPeclipsePlugin.log(e);
+ errorStart = SimpleCharStream.beginOffset;
+ errorEnd = SimpleCharStream.endOffset;
errorMessage = e.getMessage();
errorLevel = ERROR;
throw generateParseException();
}
}
+/**
+ * A php block is a = expression [;]?>
+ * or
+ * or somephpcode ?>
+ */
void PhpBlock() :
{
- final int start = jj_input_stream.bufpos;
+ final PHPEchoBlock phpEchoBlock;
+ final Token token;
}
{
- Expression() [ ]
+ phpEchoBlock = phpEchoBlock()
+ {pushOnAstNodes(phpEchoBlock);}
|
- [
- |
+ [
+ | token =
{try {
setMarker(fileToParse,
"You should use '' expected";
errorLevel = ERROR;
- throw e;
+ errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = SimpleCharStream.getPosition() + 1;
+ processParseExceptionDebug(e);
}
}
+PHPEchoBlock phpEchoBlock() :
+{
+ final Expression expr;
+ final PHPEchoBlock echoBlock;
+ final Token token, token2;
+}
+{
+ token = expr = Expression() [ ] token2 =
+ {
+ echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
+ pushOnAstNodes(echoBlock);
+ return echoBlock;}
+}
+
void Php() :
{}
{
(BlockStatement())*
}
-void ClassDeclaration() :
+ClassDeclaration ClassDeclaration() :
{
- final PHPClassDeclaration classDeclaration;
- final Token className;
- final int pos = jj_input_stream.bufpos;
+ final ClassDeclaration classDeclaration;
+ Token className = null;
+ final Token superclassName, token, extendsToken;
+ String classNameImage = SYNTAX_ERROR_CHAR;
+ String superclassNameImage = null;
}
{
- className = [ ]
- {
- if (currentSegment != null) {
- classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos);
- currentSegment.add(classDeclaration);
- currentSegment = classDeclaration;
- }
+ token =
+ try {
+ className =
+ {classNameImage = className.image;}
+ } catch (ParseException e) {
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
+ errorLevel = ERROR;
+ errorStart = token.sourceEnd+1;
+ errorEnd = token.sourceEnd+1;
+ processParseExceptionDebug(e);
}
- ClassBody()
+ [
+ extendsToken =
+ try {
+ superclassName =
+ {superclassNameImage = superclassName.image;}
+ } catch (ParseException e) {
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
+ errorLevel = ERROR;
+ errorStart = extendsToken.sourceEnd+1;
+ errorEnd = extendsToken.sourceEnd+1;
+ processParseExceptionDebug(e);
+ superclassNameImage = SYNTAX_ERROR_CHAR;
+ }
+ ]
{
- if (currentSegment != null) {
- currentSegment = (PHPSegmentWithChildren) currentSegment.getParent();
+ int start, end;
+ if (className == null) {
+ start = token.sourceStart;
+ end = token.sourceEnd;
+ } else {
+ start = className.sourceStart;
+ end = className.sourceEnd;
}
+ if (superclassNameImage == null) {
+
+ classDeclaration = new ClassDeclaration(currentSegment,
+ classNameImage,
+ start,
+ end);
+ } else {
+ classDeclaration = new ClassDeclaration(currentSegment,
+ classNameImage,
+ superclassNameImage,
+ start,
+ end);
+ }
+ currentSegment.add(classDeclaration);
+ currentSegment = classDeclaration;
}
+ ClassBody(classDeclaration)
+ {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
+ classDeclaration.sourceEnd = SimpleCharStream.getPosition();
+ pushOnAstNodes(classDeclaration);
+ return classDeclaration;}
}
-void ClassBody() :
+void ClassBody(final ClassDeclaration classDeclaration) :
{}
{
try {
} catch (ParseException e) {
- errorMessage = "'{' expected";
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
errorLevel = ERROR;
- throw e;
+ errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = SimpleCharStream.getPosition() + 1;
+ processParseExceptionDebug(e);
}
- ( ClassBodyDeclaration() )*
+ ( ClassBodyDeclaration(classDeclaration) )*
try {
} catch (ParseException e) {
- errorMessage = "'var', 'function' or '}' expected";
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
errorLevel = ERROR;
- throw e;
+ errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = SimpleCharStream.getPosition() + 1;
+ processParseExceptionDebug(e);
}
}
-void ClassBodyDeclaration() :
-{}
+/**
+ * A class can contain only methods and fields.
+ */
+void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
{
- MethodDeclaration()
-|
- FieldDeclaration()
+ final MethodDeclaration method;
+ final FieldDeclaration field;
+}
+{
+ method = MethodDeclaration() {method.analyzeCode();
+ classDeclaration.addMethod(method);}
+| field = FieldDeclaration() {classDeclaration.addField(field);}
}
-void FieldDeclaration() :
+/**
+ * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
+ * it is only used by ClassBodyDeclaration()
+ */
+FieldDeclaration FieldDeclaration() :
{
- PHPVarDeclaration variableDeclaration;
+ VariableDeclaration variableDeclaration;
+ final VariableDeclaration[] list;
+ final ArrayList arrayList = new ArrayList();
+ final Token token;
+ Token token2 = null;
+ int pos;
}
{
- variableDeclaration = VariableDeclarator()
+ token = variableDeclaration = VariableDeclaratorNoSuffix()
{
- if (currentSegment != null) {
- currentSegment.add(variableDeclaration);
- }
+ arrayList.add(variableDeclaration);
+ outlineInfo.addVariable(variableDeclaration.name());
+ pos = variableDeclaration.sourceEnd;
}
- (
- variableDeclaration = VariableDeclarator()
+ (
+ variableDeclaration = VariableDeclaratorNoSuffix()
{
- if (currentSegment != null) {
- currentSegment.add(variableDeclaration);
- }
+ arrayList.add(variableDeclaration);
+ outlineInfo.addVariable(variableDeclaration.name());
+ pos = variableDeclaration.sourceEnd;
}
)*
try {
-
+ token2 =
} catch (ParseException e) {
- errorMessage = "';' expected after variable declaration";
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
errorLevel = ERROR;
- throw e;
+ errorStart = pos+1;
+ errorEnd = pos+1;
+ processParseExceptionDebug(e);
}
+
+ {list = new VariableDeclaration[arrayList.size()];
+ arrayList.toArray(list);
+ int end;
+ if (token2 == null) {
+ end = list[list.length-1].sourceEnd;
+ } else {
+ end = token2.sourceEnd;
+ }
+ return new FieldDeclaration(list,
+ token.sourceStart,
+ end,
+ currentSegment);}
}
-PHPVarDeclaration VariableDeclarator() :
+/**
+ * a strict variable declarator : there cannot be a suffix here.
+ * It will be used by fields and formal parameters
+ */
+VariableDeclaration VariableDeclaratorNoSuffix() :
{
- final String varName;
- String varValue;
- final int pos = jj_input_stream.bufpos;
+ final Token varName;
+ Expression initializer = null;
+ Token assignToken;
}
{
- varName = VariableDeclaratorId()
+ varName =
[
-
+ assignToken =
try {
- varValue = VariableInitializer()
- {return new PHPVarDeclaration(currentSegment,varName,pos,varValue);}
+ initializer = VariableInitializer()
} catch (ParseException e) {
errorMessage = "Literal expression expected in variable initializer";
errorLevel = ERROR;
- throw e;
+ errorStart = assignToken.sourceEnd +1;
+ errorEnd = assignToken.sourceEnd +1;
+ processParseExceptionDebug(e);
+ }
+ ]
+ {
+ if (initializer == null) {
+ return new VariableDeclaration(currentSegment,
+ new Variable(varName.image.substring(1),
+ varName.sourceStart+1,
+ varName.sourceEnd+1),
+ varName.sourceStart+1,
+ varName.sourceEnd+1);
+ }
+ return new VariableDeclaration(currentSegment,
+ new Variable(varName.image.substring(1),
+ varName.sourceStart+1,
+ varName.sourceEnd+1),
+ initializer,
+ VariableDeclaration.EQUAL,
+ varName.sourceStart+1);
+ }
+}
+
+/**
+ * this will be used by static statement
+ */
+VariableDeclaration VariableDeclarator() :
+{
+ final AbstractVariable variable;
+ Expression initializer = null;
+ final Token token;
+}
+{
+ variable = VariableDeclaratorId()
+ [
+ token =
+ try {
+ initializer = VariableInitializer()
+ } catch (ParseException e) {
+ errorMessage = "Literal expression expected in variable initializer";
+ errorLevel = ERROR;
+ errorStart = token.sourceEnd+1;
+ errorEnd = token.sourceEnd+1;
+ processParseExceptionDebug(e);
}
]
- {return new PHPVarDeclaration(currentSegment,varName,pos);}
+ {
+ if (initializer == null) {
+ return new VariableDeclaration(currentSegment,
+ variable,
+ variable.sourceStart,
+ variable.sourceEnd);
+ }
+ return new VariableDeclaration(currentSegment,
+ variable,
+ initializer,
+ VariableDeclaration.EQUAL,
+ variable.sourceStart);
+ }
}
-String VariableDeclaratorId() :
+/**
+ * A Variable name.
+ * @return the variable name (with suffix)
+ */
+AbstractVariable VariableDeclaratorId() :
{
- String expr;
- final StringBuffer buff = new StringBuffer();
+ final Variable var;
+ AbstractVariable expression = null;
}
{
try {
- expr = Variable()
- {buff.append(expr);}
- ( LOOKAHEAD(2) expr = VariableSuffix()
- {buff.append(expr);}
+ var = Variable()
+ (
+ LOOKAHEAD(2)
+ expression = VariableSuffix(var)
)*
- {return buff.toString();}
+ {
+ if (expression == null) {
+ return var;
+ }
+ return expression;
+ }
} catch (ParseException e) {
errorMessage = "'$' expected for variable identifier";
errorLevel = ERROR;
+ errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = SimpleCharStream.getPosition() + 1;
throw e;
}
}
-String Variable():
+/**
+ * Return a variablename without the $.
+ * @return a variable name
+ *//*
+Variable Variable():
{
- String expr = null;
+ final StringBuffer buff;
+ Expression expression = null;
final Token token;
+ Variable expr;
+ final int pos;
}
{
- token = [ expr = Expression() ]
+ token =
+ [ expression = Expression() ]
{
- if (expr == null) {
- return token.image;
+ if (expression == null) {
+ return new Variable(token.image.substring(1),
+ token.sourceStart+1,
+ token.sourceEnd+1);
}
- return token + "{" + expr + "}";
+ String s = expression.toStringExpression();
+ buff = new StringBuffer(token.image.length()+s.length()+2);
+ buff.append(token.image);
+ buff.append("{");
+ buff.append(s);
+ buff.append("}");
+ s = buff.toString();
+ return new Variable(s,token.sourceStart+1,token.sourceEnd+1);
}
|
- expr = VariableName()
- {return "$" + expr;}
-}
+ token =
+ expr = VariableName()
+ {return new Variable(expr,token.sourceStart,expr.sourceEnd);}
+} */
-String VariableName():
+Variable Variable() :
{
-String expr = null;
-final Token token;
+ Variable variable = null;
+ final Token token;
}
{
- expr = Expression()
- {return "{"+expr+"}";}
-|
- token = [ expr = Expression() ]
+ token = [variable = Var(token)]
{
- if (expr == null) {
- return token.image;
+ if (variable == null) {
+ return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
}
- return token + "{" + expr + "}";
+ final StringBuffer buff = new StringBuffer();
+ buff.append(token.image.substring(1));
+ buff.append(variable.toStringExpression());
+ return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1);
}
|
- expr = VariableName()
- {return "$" + expr;}
-|
- token = [expr = VariableName()]
+ token = variable = Var(token)
{
- if (expr == null) {
- return token.image;
- }
- return token.image + expr;
+ return new Variable(variable,token.sourceStart,variable.sourceEnd);
}
}
-String VariableInitializer() :
+Variable Var(final Token dollar) :
{
- final String expr;
+ Variable variable = null;
final Token token;
+ ConstantIdentifier constant;
+}
+{
+ token = [variable = Var(token)]
+ {if (variable == null) {
+ return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
+ }
+ final StringBuffer buff = new StringBuffer();
+ buff.append(token.image.substring(1));
+ buff.append(variable.toStringExpression());
+ return new Variable(buff.toString(),dollar.sourceStart,variable.sourceEnd);
+ }
+|
+ LOOKAHEAD( )
+ token = variable = Var(token)
+ {return new Variable(variable,dollar.sourceStart,variable.sourceEnd);}
+|
+ constant = VariableName()
+ {return new Variable(constant.name,dollar.sourceStart,constant.sourceEnd);}
+}
+
+/**
+ * A Variable name (without the $)
+ * @return a variable name String
+ */
+ConstantIdentifier VariableName():
+{
+ final StringBuffer buff;
+ String expr;
+ Expression expression = null;
+ final Token token;
+ Token token2 = null;
+}
+{
+ token = expression = Expression() token2 =
+ {expr = expression.toStringExpression();
+ buff = new StringBuffer(expr.length()+2);
+ buff.append("{");
+ buff.append(expr);
+ buff.append("}");
+ expr = buff.toString();
+ return new ConstantIdentifier(expr,
+ token.sourceStart,
+ token2.sourceEnd);
+
+ }
+|
+ token =
+ [ expression = Expression() token2 = ]
+ {
+ if (expression == null) {
+ return new ConstantIdentifier(token.image,
+ token.sourceStart,
+ token.sourceEnd);
+ }
+ expr = expression.toStringExpression();
+ buff = new StringBuffer(token.image.length()+expr.length()+2);
+ buff.append(token.image);
+ buff.append("{");
+ buff.append(expr);
+ buff.append("}");
+ expr = buff.toString();
+ return new ConstantIdentifier(expr,
+ token.sourceStart,
+ token2.sourceEnd);
+ }
+/*|
+
+ var = VariableName()
+ {
+ return new Variable(var,
+ var.sourceStart-1,
+ var.sourceEnd);
+ }
+|
+ token =
+ {
+ return new Variable(token.image,
+ token.sourceStart+1,
+ token.sourceEnd+1);
+ } */
+}
+
+Expression VariableInitializer() :
+{
+ final Expression expr;
+ final Token token, token2;
}
{
expr = Literal()
{return expr;}
|
- (token = | token = )
- {return "-" + token.image;}
+ token2 = (token = | token = )
+ {return new PrefixedUnaryExpression(new NumberLiteral(token),
+ OperatorIds.MINUS,
+ token2.sourceStart);}
|
- (token = | token = )
- {return "+" + token.image;}
+ token2 = (token = | token = )
+ {return new PrefixedUnaryExpression(new NumberLiteral(token),
+ OperatorIds.PLUS,
+ token2.sourceStart);}
|
expr = ArrayDeclarator()
{return expr;}
|
token =
- {return token.image;}
+ {return new ConstantIdentifier(token);}
}
-String ArrayVariable() :
+ArrayVariableDeclaration ArrayVariable() :
{
-String expr;
-final StringBuffer buff = new StringBuffer();
+final Expression expr,expr2;
}
{
expr = Expression()
- {buff.append(expr);}
- [ expr = Expression()
- {buff.append("=>").append(expr);}]
- {return buff.toString();}
+ [
+ expr2 = Expression()
+ {return new ArrayVariableDeclaration(expr,expr2);}
+ ]
+ {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
}
-String ArrayInitializer() :
+ArrayVariableDeclaration[] ArrayInitializer() :
{
-String expr;
-final StringBuffer buff = new StringBuffer("(");
+ ArrayVariableDeclaration expr;
+ final ArrayList list = new ArrayList();
}
{
- [ expr = ArrayVariable()
- {buff.append(expr);}
- ( LOOKAHEAD(2) expr = ArrayVariable()
- {buff.append(",").append(expr);}
- )* ]
+
+ [
+ expr = ArrayVariable()
+ {list.add(expr);}
+ ( LOOKAHEAD(2) expr = ArrayVariable()
+ {list.add(expr);}
+ )*
+ ]
+ [
+ {list.add(null);}
+ ]
{
- buff.append(")");
- return buff.toString();
- }
+ final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
+ list.toArray(vars);
+ return vars;}
}
-void MethodDeclaration() :
+/**
+ * A Method Declaration.
+ * function MetodDeclarator() Block()
+ */
+MethodDeclaration MethodDeclaration() :
{
- final PHPFunctionDeclaration functionDeclaration;
+ final MethodDeclaration functionDeclaration;
+ final Block block;
+ final OutlineableWithChildren seg = currentSegment;
+ final Token token;
}
{
- functionDeclaration = MethodDeclarator()
- {
- if (currentSegment != null) {
- currentSegment.add(functionDeclaration);
- currentSegment = functionDeclaration;
- }
- }
- Block()
- {
- if (currentSegment != null) {
- currentSegment = (PHPSegmentWithChildren) currentSegment.getParent();
- }
+ token =
+ try {
+ functionDeclaration = MethodDeclarator(token.sourceStart)
+ {outlineInfo.addVariable(functionDeclaration.name);}
+ } catch (ParseException e) {
+ if (errorMessage != null) throw e;
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
+ errorLevel = ERROR;
+ errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = SimpleCharStream.getPosition() + 1;
+ throw e;
}
+ {currentSegment = functionDeclaration;}
+ block = Block()
+ {functionDeclaration.statements = block.statements;
+ currentSegment = seg;
+ return functionDeclaration;}
}
-PHPFunctionDeclaration MethodDeclarator() :
+/**
+ * A MethodDeclarator.
+ * [&] IDENTIFIER(parameters ...).
+ * @return a function description for the outline
+ */
+MethodDeclaration MethodDeclarator(final int start) :
{
- final Token identifier;
- final StringBuffer methodDeclaration = new StringBuffer();
- final String formalParameters;
- final int pos = jj_input_stream.bufpos;
+ Token identifier = null;
+ Token reference = null;
+ final Hashtable formalParameters = new Hashtable();
+ String identifierChar = SYNTAX_ERROR_CHAR;
+ int end = start;
}
{
- [ {methodDeclaration.append("&");} ]
- identifier =
- {methodDeclaration.append(identifier);}
- formalParameters = FormalParameters()
+ [reference = {end = reference.sourceEnd;}]
+ try {
+ identifier =
+ {
+ identifierChar = identifier.image;
+ end = identifier.sourceEnd;
+ }
+ } catch (ParseException e) {
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
+ errorLevel = ERROR;
+ errorStart = e.currentToken.sourceEnd;
+ errorEnd = e.currentToken.next.sourceStart;
+ processParseExceptionDebug(e);
+ }
+ end = FormalParameters(formalParameters)
{
- methodDeclaration.append(formalParameters);
- return new PHPFunctionDeclaration(currentSegment,methodDeclaration.toString(),pos);
+ int nameStart, nameEnd;
+ if (identifier == null) {
+ if (reference == null) {
+ nameStart = start + 9;
+ nameEnd = start + 10;
+ } else {
+ nameStart = reference.sourceEnd + 1;
+ nameEnd = reference.sourceEnd + 2;
+ }
+ } else {
+ nameStart = identifier.sourceStart;
+ nameEnd = identifier.sourceEnd;
+ }
+ return new MethodDeclaration(currentSegment,
+ identifierChar,
+ formalParameters,
+ reference != null,
+ nameStart,
+ nameEnd,
+ start,
+ end);
}
}
-String FormalParameters() :
+/**
+ * FormalParameters follows method identifier.
+ * (FormalParameter())
+ */
+int FormalParameters(final Hashtable parameters) :
{
- String expr;
- final StringBuffer buff = new StringBuffer("(");
+ VariableDeclaration var;
+ final Token token;
+ Token tok = PHPParser.token;
+ int end = tok.sourceEnd;
}
{
try {
-
+ tok =
+ {end = tok.sourceEnd;}
} catch (ParseException e) {
- errorMessage = "Formal parameter expected after function identifier";
+ errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
errorLevel = ERROR;
- jj_consume_token(token.kind);
- }
- [ expr = FormalParameter()
- {buff.append(expr);}
- (
- expr = FormalParameter()
- {buff.append(",").append(expr);}
- )*
- ]
+ errorStart = e.currentToken.next.sourceStart;
+ errorEnd = e.currentToken.next.sourceEnd;
+ processParseExceptionDebug(e);
+ }
+ [
+ var = FormalParameter()
+ {parameters.put(var.name(),var);end = var.sourceEnd;}
+ (
+ var = FormalParameter()
+ {parameters.put(var.name(),var);end = var.sourceEnd;}
+ )*
+ ]
try {
-
+ token =
+ {end = token.sourceEnd;}
} catch (ParseException e) {
errorMessage = "')' expected";
errorLevel = ERROR;
- throw e;
+ errorStart = e.currentToken.next.sourceStart;
+ errorEnd = e.currentToken.next.sourceEnd;
+ processParseExceptionDebug(e);
}
- {
- buff.append(")");
- return buff.toString();
- }
+ {return end;}
}
-String FormalParameter() :
+/**
+ * A formal parameter.
+ * $varname[=value] (,$varname[=value])
+ */
+VariableDeclaration FormalParameter() :
{
- final PHPVarDeclaration variableDeclaration;
- final StringBuffer buff = new StringBuffer();
+ final VariableDeclaration variableDeclaration;
+ Token token = null;
}
{
- [ {buff.append("&");}] variableDeclaration = VariableDeclarator()
+ [token = ] variableDeclaration = VariableDeclaratorNoSuffix()
{
- buff.append(variableDeclaration.toString());
- return buff.toString();
- }
+ if (token != null) {
+ variableDeclaration.setReference(true);
+ }
+ return variableDeclaration;}
}
-String Type() :
-{}
+ConstantIdentifier Type() :
+{final Token token;}
{
-
- {return "string";}
-|
-
- {return "bool";}
-|
-
- {return "boolean";}
-|
-
- {return "real";}
-|
-
- {return "double";}
-|
-
- {return "float";}
-|
-
- {return "int";}
-|
-
- {return "integer";}
-|
-