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.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;
/**
* 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 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;
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;
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 final void setFileToParse(final IFile fileToParse) {
this.fileToParse = fileToParse;
}
public PHPParser() {
}
public PHPParser(final IFile fileToParse) {
this(new StringReader(""));
this.fileToParse = fileToParse;
}
/**
* 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(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;
}
}
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);
}
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 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 = SimpleCharStream.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,
SimpleCharStream.tokenBegin,
SimpleCharStream.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);
init();
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);
}
}
/**
* 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();
}
}
PARSER_END(PHPParser)
TOKEN :
{
{PHPParser.createNewHTMLCode();} : PHPPARSING
| {PHPParser.createNewHTMLCode();} : PHPPARSING
| {PHPParser.createNewHTMLCode();} : PHPPARSING
}
TOKEN :
{
"> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : 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 :
{
|