import java.util.Map;
import junit.framework.TestCase;
+import net.sourceforge.phpdt.core.JavaCore;
import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
import net.sourceforge.phpdt.internal.corext.util.Strings;
import net.sourceforge.phpdt.internal.formatter.CodeFormatter;
import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage;
-import net.sourceforge.phpeclipse.PHPCore;
import org.eclipse.jface.text.IDocument;
/**
* Testcase for the PHP CodeFormatter
* @author Stefan Langer
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class PHPFormatterTest extends TestCase {
private CodeFormatter fFormatter;
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
- Map options = PHPCore.getOptions();
+ Map options = JavaCore.getOptions();
fFormatter = new CodeFormatter(options);
fDocument = new DummyDocument();
}
* distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html
******************************************************************************/
import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
-import net.sourceforge.phpdt.internal.compiler.parser.Parser;
import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
/**
* Tests the php parser
* Test the PHP Parser with different PHP snippets
*/
public void testPHPParser() {
- checkHTML("<?php echo $bgcolor2?>");
+ checkHTML("<?php if ($a==$b) {\n" +
+ "}\n" +
+ ""+
+ "?> ");
+ checkHTML("<?php if ($a==$b) {?>\n" +
+ "<b>test <?php echo \"test\";?> me</b>\n" +
+ "<?php } \n" +
+ "echo $bgcolor2 ?>");
+
+ checkHTML("<?php echo $bgcolor2 ?>");
+ checkPHP("function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)\r\n" +
+ "{ static $drop_char_match = array(\'^\', \'$\'); }");
+
checkPHP("if ($topic<1) { $topic = 1;}");
checkPHP("$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);");
checkPHP("$db->sql_query($sql);");
return TokenNameLPAREN;
}
public int getNextToken() throws InvalidInputException {
- int htmlPosition = currentPosition;
- try {
- while (!phpMode) {
- currentCharacter = source[currentPosition++];
- if (currentCharacter == '<') {
- if (getNextChar('?')) {
- currentCharacter = source[currentPosition++];
- if ((currentCharacter == ' ')
- || Character.isWhitespace(currentCharacter)) {
- // <?
- startPosition = currentPosition;
- phpMode = true;
- if (tokenizeWhiteSpace) {
- // && (whiteStart != currentPosition - 1)) {
- // reposition scanner in case we are interested by spaces as
- // tokens
- startPosition = htmlPosition;
- return TokenNameHTML;
- }
- } else {
- boolean phpStart = (currentCharacter == 'P')
- || (currentCharacter == 'p');
- if (phpStart) {
- int test = getNextChar('H', 'h');
- if (test >= 0) {
- test = getNextChar('P', 'p');
- if (test >= 0) {
- // <?PHP <?php
- startPosition = currentPosition;
- phpMode = true;
- if (tokenizeWhiteSpace) {
- // && (whiteStart != currentPosition - 1)) {
- // reposition scanner in case we are interested by spaces
- // as tokens
- startPosition = htmlPosition;
- return TokenNameHTML;
- }
- }
- }
- }
- }
- }
- }
- if ((currentCharacter == '\r') || (currentCharacter == '\n')) {
- if (recordLineSeparator) {
- pushLineSeparator();
- } else {
- currentLine = null;
- }
- }
- }
- } //-----------------end switch while try--------------------
- catch (IndexOutOfBoundsException e) {
- if (tokenizeWhiteSpace) {
- // && (whiteStart != currentPosition - 1)) {
- // reposition scanner in case we are interested by spaces as tokens
- startPosition = htmlPosition;
- }
- return TokenNameEOF;
+ if (!phpMode) {
+ return getInlinedHTML(currentPosition);
}
if (phpMode) {
this.wasAcr = false;
case '?' :
if (getNextChar('>')) {
phpMode = false;
- return TokenNameINLINE_HTML;
+ if (currentPosition==source.length) {
+ phpMode = true;
+ return TokenNameINLINE_HTML;
+ }
+ return getInlinedHTML(currentPosition - 2);
}
return TokenNameQUESTION;
case ':' :
int oldPosition = currentPosition;
try {
currentCharacter = source[currentPosition++];
-
if (isPHPIdentifierStart(currentCharacter)) {
return scanIdentifierOrKeyword(true);
} else {
}
return TokenNameEOF;
}
+ /**
+ * @return @throws
+ * InvalidInputException
+ */
+ private int getInlinedHTML(int start) throws InvalidInputException {
+ // int htmlPosition = start;
+ if (currentPosition>source.length) {
+ currentPosition = source.length;
+ return TokenNameEOF;
+ }
+ startPosition = start;
+ try {
+ while (!phpMode) {
+ currentCharacter = source[currentPosition++];
+ if (currentCharacter == '<') {
+ if (getNextChar('?')) {
+ currentCharacter = source[currentPosition++];
+ if ((currentCharacter == ' ')
+ || Character.isWhitespace(currentCharacter)) {
+ // <?
+ phpMode = true;
+ return TokenNameINLINE_HTML;
+ } else {
+ boolean phpStart = (currentCharacter == 'P')
+ || (currentCharacter == 'p');
+ if (phpStart) {
+ int test = getNextChar('H', 'h');
+ if (test >= 0) {
+ test = getNextChar('P', 'p');
+ if (test >= 0) {
+ // <?PHP <?php
+ phpMode = true;
+ return TokenNameINLINE_HTML;
+ }
+ }
+ }
+ }
+ }
+ }
+ if ((currentCharacter == '\r') || (currentCharacter == '\n')) {
+ if (recordLineSeparator) {
+ pushLineSeparator();
+ } else {
+ currentLine = null;
+ }
+ }
+ } //-----------------while--------------------
+ phpMode = true;
+ return TokenNameINLINE_HTML;
+ } //-----------------try--------------------
+ catch (IndexOutOfBoundsException e) {
+ startPosition = start;
+ currentPosition--;
+ }
+ phpMode = true;
+ return TokenNameINLINE_HTML;
+ }
// public final void getNextUnicodeChar()
// throws IndexOutOfBoundsException, InvalidInputException {
// //VOID
return "ScannerError"; // + new String(getCurrentTokenSource()) + ")";
// //$NON-NLS-1$
case TokenNameINLINE_HTML :
- return "StopPHP(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
+ return "Inline-HTML(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
case TokenNameIdentifier :
return "Identifier(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
case TokenNameVariable :
return "COMMENT_BLOCK(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$
case TokenNameCOMMENT_PHPDOC :
return "COMMENT_PHPDOC(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$
- case TokenNameHTML :
- return "HTML(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$
+ // case TokenNameHTML :
+ // return "HTML(" + new String(getCurrentTokenSource()) + ")";
+ // //$NON-NLS-1$
case TokenNameFILE :
return "__FILE__"; //$NON-NLS-1$
case TokenNameLINE :