options {
LOOKAHEAD = 1;
CHOICE_AMBIGUITY_CHECK = 2;
OTHER_AMBIGUITY_CHECK = 1;
STATIC = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
OPTIMIZE_TOKEN_MANAGER = false;
ERROR_REPORTING = true;
JAVA_UNICODE_ESCAPE = false;
UNICODE_INPUT = false;
IGNORE_CASE = true;
USER_TOKEN_MANAGER = false;
USER_CHAR_STREAM = false;
BUILD_PARSER = true;
BUILD_TOKEN_MANAGER = true;
SANITY_CHECK = true;
FORCE_LA_CHECK = false;
}
PARSER_BEGIN(PHPParser)
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.Enumeration;
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.parser.*;
import net.sourceforge.phpdt.internal.compiler.ast.*;
/**
* 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 {
/** The file that is parsed. */
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$
static PHPOutlineInfo outlineInfo;
private static PHPFunctionDeclaration 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;
private static int errorStart = -1;
private static int errorEnd = -1;
//ast stack
private final static int AstStackIncrement = 100;
/** The stack of node. */
private static AstNode[] astStack;
/** The cursor in expression stack. */
private static int expressionPtr;
public final void setFileToParse(final IFile fileToParse) {
this.fileToParse = fileToParse;
}
public PHPParser() {
}
public PHPParser(final IFile fileToParse) {
this(new StringReader(""));
this.fileToParse = fileToParse;
}
public static final void phpParserTester(final String strEval) throws CoreException, ParseException {
PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
final StringReader stream = new StringReader(strEval);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(new StringReader(strEval));
astStack = new AstNode[AstStackIncrement];
phpTest();
}
public static final void htmlParserTester(final File fileName) throws CoreException, ParseException {
try {
final Reader stream = new FileReader(fileName);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
astStack = new AstNode[AstStackIncrement];
phpFile();
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
public static final void htmlParserTester(final String strEval) throws CoreException, ParseException {
final StringReader stream = new StringReader(strEval);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
astStack = new AstNode[AstStackIncrement];
phpFile();
}
public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
outlineInfo = new PHPOutlineInfo(parent);
currentSegment = outlineInfo.getDeclarations();
final StringReader stream = new StringReader(s);
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
astStack = new AstNode[AstStackIncrement];
try {
parse();
} catch (ParseException e) {
processParseException(e);
}
return outlineInfo;
}
/**
* 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 = jj_input_stream.getPosition();
errorEnd = errorStart + 1;
}
setMarker(e);
errorMessage = null;
}
/**
* Create marker for the parse error
* @param e the ParseException
*/
private static void setMarker(final ParseException e) {
try {
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);
}
}
/**
* 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);
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 ");
if (onLine != -1) {
lineNumberBuffer.delete(0, lineNumberBuffer.length());
for (int i = onLine; i < current.length(); i++) {
ch = current.charAt(i);
if ('0' <= ch && '9' >= ch) {
lineNumberBuffer.append(ch);
}
}
int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
Hashtable attributes = new Hashtable();
current = current.replaceAll("\n", "");
current = current.replaceAll("", "");
current = current.replaceAll("", "");
MarkerUtilities.setMessage(attributes, current);
if (current.indexOf(PARSE_ERROR_STRING) != -1)
attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
else if (current.indexOf(PARSE_WARNING_STRING) != -1)
attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
else
attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
MarkerUtilities.setLineNumber(attributes, lineNumber);
MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
}
}
}
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);
astStack = new AstNode[AstStackIncrement];
try {
parse();
} catch (ParseException e) {
processParseException(e);
}
}
/**
* Call the php parse command ( php -l -f <filename> )
* and create markers according to the external parser output
*/
public static void phpExternalParse(final IFile file) {
final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
final String filename = file.getLocation().toString();
final String[] arguments = { filename };
final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
final String command = form.format(arguments);
final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
try {
// parse the buffer to find the errors and warnings
createMarkers(parserResult, file);
} catch (CoreException e) {
PHPeclipsePlugin.log(e);
}
}
private static final void parse() throws ParseException {
phpFile();
}
}
PARSER_END(PHPParser)
TOKEN :
{
: PHPPARSING
| : PHPPARSING
| : PHPPARSING
}
TOKEN :
{
"> : DEFAULT
}
/* Skip any character if we are not in php mode */
SKIP :
{
< ~[] >
}
/* WHITE SPACE */
SKIP :
{
" "
| "\t"
| "\n"
| "\r"
| "\f"
}
/* COMMENTS */
SPECIAL_TOKEN :
{
"//" : IN_SINGLE_LINE_COMMENT
|
"#" : IN_SINGLE_LINE_COMMENT
|
<"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
|
"/*" : IN_MULTI_LINE_COMMENT
}
SPECIAL_TOKEN :
{
: PHPPARSING
}
SPECIAL_TOKEN :
{
" > : DEFAULT
}
SPECIAL_TOKEN :
{
: PHPPARSING
}
SPECIAL_TOKEN :
{
: PHPPARSING
}
MORE :
{
< ~[] >
}
/* KEYWORDS */
TOKEN :
{
|
|
|
|
|
|
|
|
}
/* LANGUAGE CONSTRUCT */
TOKEN :
{
|
|
|
|
|
|
|
| ">
|
| ">
}
/* RESERVED WORDS AND LITERALS */
TOKEN :
{
|
|
| <_DEFAULT : "default">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
/* TYPES */
TOKEN :
{
|