/* Generated By:JavaCC: Do not edit this line. PHPParser.java */
package test;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
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.corext.Assert;
/**
* A new php parser.
* 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 implements PHPParserConstants {
//todo : fix the variables names bug
//todo : handle tilde operator
/** 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$
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;
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) {
PHPParser.fileToParse = fileToParse;
}
public PHPParser() {
}
public PHPParser(final IFile fileToParse) {
this(new StringReader(""));
PHPParser.fileToParse = fileToParse;
}
public 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);
token_source = new PHPParserTokenManager(jj_input_stream);
}
ReInit(new StringReader(strEval));
init();
phpDocument = new PHPDocument(null,"_root".toCharArray());
currentSegment = phpDocument;
outlineInfo = new PHPOutlineInfo(null, currentSegment);
token_source.SwitchTo(PHPParserTokenManager.PHPPARSING);
phpTest();
}
public 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);
token_source = new PHPParserTokenManager(jj_input_stream);
}
ReInit(stream);
init();
phpDocument = new PHPDocument(null,"_root".toCharArray());
currentSegment = phpDocument;
outlineInfo = new PHPOutlineInfo(null, currentSegment);
phpFile();
}
public 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);
token_source = new PHPParserTokenManager(jj_input_stream);
}
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) {
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);
token_source = new PHPParserTokenManager(jj_input_stream);
}
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);
}
return outlineInfo;
}
/**
* 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
*/
private static void processParseException(final ParseException e) {
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.
* @param e the ParseException
*/
private static void setMarker(final ParseException e) {
try {
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;
- 1
- 2
前往页