From: khartlage Date: Mon, 8 Mar 2004 22:30:30 +0000 (+0000) Subject: misc parser changes X-Git-Url: http://git.phpeclipse.com?hp=8dea0852fb960db1f239c6ea439b134addbb846c misc parser changes --- diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java index 5ea14a5..031d81e 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpdt/core/tests/util/AbstractCompilerTest.java @@ -10,84 +10,212 @@ *******************************************************************************/ package net.sourceforge.phpdt.core.tests.util; +import java.io.PrintWriter; +import java.io.StringWriter; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Locale; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import net.sourceforge.phpdt.core.compiler.IProblem; +import net.sourceforge.phpdt.internal.compiler.CompilationResult; +import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies; +import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit; +import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit; +import net.sourceforge.phpdt.internal.compiler.parser.UnitParser; +import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem; +import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory; +import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter; +import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration; //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase; //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions; public class AbstractCompilerTest extends TestCase { - public static final String COMPLIANCE_1_3 = "1.3"; - public static final String COMPLIANCE_1_4 = "1.4"; - public static final String COMPLIANCE_1_5 = "1.5"; - - public static final int F_1_3 = 0x1; - public static final int F_1_4 = 0x2; - public static final int F_1_5 = 0x4; +// public static final String COMPLIANCE_1_3 = "1.3"; +// public static final String COMPLIANCE_1_4 = "1.4"; +// public static final String COMPLIANCE_1_5 = "1.5"; +// +// public static final int F_1_3 = 0x1; +// public static final int F_1_4 = 0x2; +// public static final int F_1_5 = 0x4; private static int possibleComplianceLevels = -1; protected String complianceLevel; + public void checkParsePHP( + char[] source, + String expectedSyntaxErrorDiagnosis) { +// String testName) { - /* - * Returns the possible compliance levels this VM instance can run. - */ - public static int getPossibleComplianceLevels() { - if (possibleComplianceLevels == -1) { - String compliance = System.getProperty("compliance"); - if (compliance != null) { - if (COMPLIANCE_1_3.equals(compliance)) { - possibleComplianceLevels = F_1_3; - } else if (COMPLIANCE_1_4.equals(compliance)) { - possibleComplianceLevels = F_1_4; - } else if (COMPLIANCE_1_5.equals(compliance)) { - possibleComplianceLevels = F_1_5; - } else { - System.out.println("Invalid compliance specified (" + compliance + ")"); - System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5); - System.out.println("Defaulting to all possible compliances"); + UnitParser parser = + new UnitParser( + new ProblemReporter( + DefaultErrorHandlingPolicies.proceedWithAllProblems(), + //new CompilerOptions(getCompilerOptions()), + new DefaultProblemFactory(Locale.getDefault()))); + + ICompilationUnit sourceUnit = new CompilationUnit(source, "", null); + CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); + + CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true); +// if (computedUnit.types != null) { +// for (int i = computedUnit.types.size(); --i >= 0;){ +// ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit); +// } +// } + + StringBuffer buffer = new StringBuffer(100); + if (compilationResult.hasProblems() || compilationResult.hasTasks()) { + IProblem[] problems = compilationResult.getAllProblems(); + int count = problems.length; + int problemCount = 0; + char[] unitSource = compilationResult.compilationUnit.getContents(); + for (int i = 0; i < count; i++) { + if (problems[i] != null) { + if (problemCount == 0) + buffer.append("----------\n"); + problemCount++; + buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING")); + buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\')); + try { + buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource)); + buffer.append("\n"); + buffer.append(problems[i].getMessage()); + buffer.append("\n"); + } catch (Exception e) { + StringWriter stringWriter = new StringWriter(); + e.printStackTrace(new PrintWriter(stringWriter)); + buffer.append(stringWriter.getBuffer()); + } + buffer.append("----------\n"); } } - if (possibleComplianceLevels == -1) { - possibleComplianceLevels = F_1_3; - String specVersion = System.getProperty("java.specification.version"); - boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion); - if (canRun1_4) { - possibleComplianceLevels |= F_1_4; - } - boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion); - if (canRun1_5) { - possibleComplianceLevels |= F_1_5; + } + String computedSyntaxErrorDiagnosis = buffer.toString(); + if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) { + System.out.println(Util.displayString(computedSyntaxErrorDiagnosis)); + } + assertEquals( + "Invalid syntax error diagnosis", + expectedSyntaxErrorDiagnosis, + computedSyntaxErrorDiagnosis); + } + public void checkParseHTML( + char[] source, + String expectedSyntaxErrorDiagnosis) { +// String testName) { + + UnitParser parser = + new UnitParser( + new ProblemReporter( + DefaultErrorHandlingPolicies.proceedWithAllProblems(), + //new CompilerOptions(getCompilerOptions()), + new DefaultProblemFactory(Locale.getDefault()))); + + ICompilationUnit sourceUnit = new CompilationUnit(source, "", null); + CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); + + CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false); +// if (computedUnit.types != null) { +// for (int i = computedUnit.types.size(); --i >= 0;){ +// ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit); +// } +// } + + StringBuffer buffer = new StringBuffer(100); + if (compilationResult.hasProblems() || compilationResult.hasTasks()) { + IProblem[] problems = compilationResult.getAllProblems(); + int count = problems.length; + int problemCount = 0; + char[] unitSource = compilationResult.compilationUnit.getContents(); + for (int i = 0; i < count; i++) { + if (problems[i] != null) { + if (problemCount == 0) + buffer.append("----------\n"); + problemCount++; + buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING")); + buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\')); + try { + buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource)); + buffer.append("\n"); + buffer.append(problems[i].getMessage()); + buffer.append("\n"); + } catch (Exception e) { + StringWriter stringWriter = new StringWriter(); + e.printStackTrace(new PrintWriter(stringWriter)); + buffer.append(stringWriter.getBuffer()); + } + buffer.append("----------\n"); } } } - return possibleComplianceLevels; + String computedSyntaxErrorDiagnosis = buffer.toString(); + if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) { + System.out.println(Util.displayString(computedSyntaxErrorDiagnosis)); + } + assertEquals( + "Invalid syntax error diagnosis", + expectedSyntaxErrorDiagnosis, + computedSyntaxErrorDiagnosis); } + /* + * Returns the possible compliance levels this VM instance can run. + */ +// public static int getPossibleComplianceLevels() { +// if (possibleComplianceLevels == -1) { +// String compliance = System.getProperty("compliance"); +// if (compliance != null) { +// if (COMPLIANCE_1_3.equals(compliance)) { +// possibleComplianceLevels = F_1_3; +// } else if (COMPLIANCE_1_4.equals(compliance)) { +// possibleComplianceLevels = F_1_4; +// } else if (COMPLIANCE_1_5.equals(compliance)) { +// possibleComplianceLevels = F_1_5; +// } else { +// System.out.println("Invalid compliance specified (" + compliance + ")"); +// System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5); +// System.out.println("Defaulting to all possible compliances"); +// } +// } +// if (possibleComplianceLevels == -1) { +// possibleComplianceLevels = F_1_3; +// String specVersion = System.getProperty("java.specification.version"); +// boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion); +// if (canRun1_4) { +// possibleComplianceLevels |= F_1_4; +// } +// boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion); +// if (canRun1_5) { +// possibleComplianceLevels |= F_1_5; +// } +// } +// } +// return possibleComplianceLevels; +// } /* * Returns a test suite including the tests defined by the given classes for all possible complianceLevels * and using the given setup class (CompilerTestSetup or a subclass) */ - public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) { - TestSuite all = new TestSuite(suiteName); - int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels(); - if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) { - all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses)); - } - if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) { - all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses)); - } - if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) { - all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses)); - } - return all; - } +// public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) { +// TestSuite all = new TestSuite(suiteName); +// int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels(); +// if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) { +// all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses)); +// } +// if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) { +// all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses)); +// } +// if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) { +// all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses)); +// } +// return all; +// } /* * Returns a test suite including the tests defined by the given classes for the given complianceLevel diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/DualParseSyntaxErrorTest.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/DualParseSyntaxErrorTest.java index 09830f3..76d4a10 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/DualParseSyntaxErrorTest.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/phpeditor/php/test/DualParseSyntaxErrorTest.java @@ -35,66 +35,10 @@ public class DualParseSyntaxErrorTest extends AbstractCompilerTest { public DualParseSyntaxErrorTest(String testName){ super(testName); } - public void checkParse( - char[] source, - String expectedSyntaxErrorDiagnosis) { -// String testName) { - - UnitParser parser = - new UnitParser( - new ProblemReporter( - DefaultErrorHandlingPolicies.proceedWithAllProblems(), - //new CompilerOptions(getCompilerOptions()), - new DefaultProblemFactory(Locale.getDefault()))); - - ICompilationUnit sourceUnit = new CompilationUnit(source, "", null); - CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); - - CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true); -// if (computedUnit.types != null) { -// for (int i = computedUnit.types.size(); --i >= 0;){ -// ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit); -// } -// } - - StringBuffer buffer = new StringBuffer(100); - if (compilationResult.hasProblems() || compilationResult.hasTasks()) { - IProblem[] problems = compilationResult.getAllProblems(); - int count = problems.length; - int problemCount = 0; - char[] unitSource = compilationResult.compilationUnit.getContents(); - for (int i = 0; i < count; i++) { - if (problems[i] != null) { - if (problemCount == 0) - buffer.append("----------\n"); - problemCount++; - buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING")); - buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\')); - try { - buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource)); - buffer.append("\n"); - buffer.append(problems[i].getMessage()); - buffer.append("\n"); - } catch (Exception e) { - StringWriter stringWriter = new StringWriter(); - e.printStackTrace(new PrintWriter(stringWriter)); - buffer.append(stringWriter.getBuffer()); - } - buffer.append("----------\n"); - } - } - } - String computedSyntaxErrorDiagnosis = buffer.toString(); - if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) { - System.out.println(Util.displayString(computedSyntaxErrorDiagnosis)); - } - assertEquals( - "Invalid syntax error diagnosis", - expectedSyntaxErrorDiagnosis, - computedSyntaxErrorDiagnosis); - } + public void test01() { String s = + "$login =1; " + "final class test {\n" + "\n" + "private function f1() {\n" + @@ -110,7 +54,7 @@ public class DualParseSyntaxErrorTest extends AbstractCompilerTest { ""; String testName = ""; - checkParse( + checkParsePHP( s.toCharArray(), expectedSyntaxErrorDiagnosis); // testName); @@ -127,12 +71,42 @@ public class DualParseSyntaxErrorTest extends AbstractCompilerTest { ""; String testName = ""; - checkParse( + checkParsePHP( + s.toCharArray(), + expectedSyntaxErrorDiagnosis); +// testName); + } + public void test03() { + String s = + "$chars = ( isset($HTTP_GET_VARS['chars']) ) ? intval($HTTP_GET_VARS['chars']) : 200;"; + String expectedSyntaxErrorDiagnosis = + ""; + + checkParsePHP( + s.toCharArray(), + expectedSyntaxErrorDiagnosis); +// testName); +} + public void test96() { + String s = "$str = <<foo = 'Foo';\n" + + " $this->bar = array('Bar1', 'Bar2', 'Bar3');\n" + " }\n" + + "}\n" + "\n" + "$foo = new foo();\n" + "$name = 'MyName';\n" + "\n" + + "echo <<foo.\n" + + "Now, I am printing some {$foo->bar[1]}.\n" + + "This should print a capital 'A': \\x41\n" + "EOT;\n"; + String expectedSyntaxErrorDiagnosis = + ""; + + checkParsePHP( s.toCharArray(), expectedSyntaxErrorDiagnosis); // testName); } - public void test97() { String s = "class momo { \n"+ @@ -144,19 +118,20 @@ public class DualParseSyntaxErrorTest extends AbstractCompilerTest { String expectedSyntaxErrorDiagnosis = ""; - checkParse( + checkParsePHP( s.toCharArray(), expectedSyntaxErrorDiagnosis); // testName); } public void test98() { String s = + "return(array());"+ "if(!$result = mysql_query($sql)) return(array());\n"; String expectedSyntaxErrorDiagnosis = ""; - checkParse( + checkParsePHP( s.toCharArray(), expectedSyntaxErrorDiagnosis); // testName); @@ -172,9 +147,19 @@ public class DualParseSyntaxErrorTest extends AbstractCompilerTest { "} \n"; String expectedSyntaxErrorDiagnosis = - ""; - - checkParse( + "----------\n" + + "1. ERROR in (at line 1)\n" + + " murks; \n" + + " ^^^^^\n" + + "Parser error \"\'public\' \'private\' or \'protected\' modifier expected for field declarations.\"\n" + + "----------\n" + + "2. ERROR in (at line 1)\n" + + " } \n" + + " ^\n" + + "Parser error \"Too many closing \'}\'; end-of-file not reached.\"\n" + + "----------\n"; + + checkParsePHP( s.toCharArray(), expectedSyntaxErrorDiagnosis); // testName); diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPManualTestCase.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPManualTestCase.java index 56d3697..fb231ff 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPManualTestCase.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPManualTestCase.java @@ -5,14 +5,13 @@ package net.sourceforge.phpeclipse.tests.parser; * under the terms of the Common Public License v1.0 which accompanies this * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html ******************************************************************************/ -import junit.framework.TestCase; -import net.sourceforge.phpdt.internal.compiler.parser.Parser; +import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest; import net.sourceforge.phpdt.internal.compiler.parser.Scanner; /** * Tests the php parser */ -public class PHPManualTestCase extends TestCase { - Parser parser; +public class PHPManualTestCase extends AbstractCompilerTest { +// Parser parser; public PHPManualTestCase(String name) { super(name); } @@ -87,8 +86,8 @@ public class PHPManualTestCase extends TestCase { + " echo \"
\\n\";\n" + "}\n" + "\n" + "// ...because you can simply type\n" + "if ($show_separators) {\n" + " echo \"
\\n\";\n" + "}"); - checkPHP("echo gettype((bool) \"\"); // bool(false)\n" - + "echo gettype((bool) 1); // bool(true)\n" + checkPHP( // "echo gettype((bool) \"\"); // bool(false)\n" + "echo gettype((bool) 1); // bool(true)\n" + "echo gettype((bool) -2); // bool(true)\n" + "echo gettype((bool) \"foo\"); // bool(true)\n" + "echo gettype((bool) 2.3e5); // bool(true)\n" @@ -124,17 +123,7 @@ public class PHPManualTestCase extends TestCase { + "echo 'This will not expand: \\n a newline';\n" + "\n" + "// Outputs: Variables do not $expand $either\n" + "echo 'Variables do not $expand $either';\n"); - checkPHP("$str = <<foo = 'Foo';\n" - + " $this->bar = array('Bar1', 'Bar2', 'Bar3');\n" + " }\n" - + "}\n" + "\n" + "$foo = new foo();\n" + "$name = 'MyName';\n" + "\n" - + "echo <<foo.\n" - + "Now, I am printing some {$foo->bar[1]}.\n" - + "This should print a capital 'A': \\x41\n" + "EOT;\n"); + checkPHP("echo \"This works: \" . $arr['foo'][3];"); checkPHP("echo \"\\$foo==$foo; type is \" . gettype ($foo) . \"
\\n\";"); checkPHP("$arr = array(\"foo\" => \"bar\", 12 => true);\n" + "\n" @@ -217,19 +206,22 @@ public class PHPManualTestCase extends TestCase { System.out.println("\n------------------------------------"); System.out.println(strEval); } - parser.phpParserTester(strEval, 1); - } - private void checkHTML(String strEval) { - if (Scanner.DEBUG) { - System.out.println("\n------------------------------------"); - System.out.println(strEval); - } - parser.parse(strEval); + checkParsePHP( + strEval.toCharArray(), + ""); +// parser.phpParserTester(strEval, 1); } +// private void checkHTML(String strEval) { +// if (Scanner.DEBUG) { +// System.out.println("\n------------------------------------"); +// System.out.println(strEval); +// } +// parser.parse(strEval); +// } /** * The JUnit setup method */ - protected void setUp() { - parser = new Parser(null); - } +// protected void setUp() { +// parser = new Parser(null); +// } } diff --git a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPParserTestCase.java b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPParserTestCase.java index f0c034a..d7759ea 100644 --- a/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPParserTestCase.java +++ b/net.sourceforge.phpeclipse.tests/src/net/sourceforge/phpeclipse/tests/parser/PHPParserTestCase.java @@ -5,14 +5,14 @@ package net.sourceforge.phpeclipse.tests.parser; * under the terms of the Common Public License v1.0 which accompanies this * distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html ******************************************************************************/ -import junit.framework.TestCase; +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 */ -public class PHPParserTestCase extends TestCase { - Parser parser; +public class PHPParserTestCase extends AbstractCompilerTest { +// Parser parser; public PHPParserTestCase(String name) { super(name); } @@ -20,6 +20,11 @@ public class PHPParserTestCase extends TestCase { * Test the PHP Parser with different PHP snippets */ public void testPHPParser() { + checkHTML(""); + checkPHP("if ($topic<1) { $topic = 1;}"); + checkPHP("$this->result_field_names[$result_id][] = odbc_field_name($result_id, $i);"); + checkPHP("$db->sql_query($sql);"); + checkPHP("$val = $$add;"); // checkPHP("if(!$result = mysql_query($sql)) return(array());"); checkPHP("class test { function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) \n{ \n } \n }"); // Bugs item #690938 @@ -51,7 +56,7 @@ public class PHPParserTestCase extends TestCase { checkPHP("@$connect_function($dbhost, $user, $pw);"); checkPHP("$conn = @$connect_function($dbhost, $user, $pw);"); checkPHP("global ${$objectname}; "); - checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } "); + // checkPHP("class DB_mssql extends DB_common { var $connection; var $phptype, $dbsyntax; } "); checkPHP("unset($this->blockvariables[$block][$varname]);"); checkPHP("new IT_Error(\"The block '$block' was not found in the template.\", __FILE__, __LINE__);"); checkPHP("for ($i=156, $j=0; $i<512; $i++, $j++) $v_checksum += ord(substr($v_binary_data_last,$j,1));"); @@ -62,8 +67,8 @@ public class PHPParserTestCase extends TestCase { checkPHP("function validateAndParseResponse($code, &$arguments) { }"); checkPHP("$options = Console_Getopt::getopt($argv, \"h?v:e:p:d:\");"); checkPHP("$this->container = new $container_class($container_options);"); - checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }"); - checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }"); + // checkPHP("class Cmd extends PEAR { var $arrSetting = array(); }"); + // checkPHP("class Cmd extends PEAR { var $arrSetting = array(), $i=10; }"); checkPHP("if (isset($test)) { } elseif (isset($lang)) { }"); checkPHP("require_once(\"mainfile.php\"); "); checkPHP("if (eregi(\"footer.php\",$PHP_SELF)) {\n" @@ -100,31 +105,44 @@ public class PHPParserTestCase extends TestCase { checkPHP("do {$array[] = array(\"$myrow[uid]\" => \"$myrow[uname]\"); } while($myrow = mysql_fetch_array($result));"); checkPHP("$ol = new Overlib();"); checkPHP("$risultato = mysql_query($sql) or\n die(mysql_error());"); - checkHTML("\n\n\n\n "); - checkHTML(""); - checkHTML(""); - checkHTML(" foo "); - checkHTML(" "); - checkHTML(""); +// checkHTML("\n\n\n\n "); +// checkHTML(""); +// checkHTML(""); +// checkHTML(" foo "); +// checkHTML(" "); +// checkHTML(""); } private void checkPHP(String strEval) { if (Scanner.DEBUG) { System.out.println("\n------------------------------------"); System.out.println(strEval); } - parser.phpParserTester(strEval, 1); + checkParsePHP( + strEval.toCharArray(), + ""); +// parser.phpParserTester(strEval, 1); } private void checkHTML(String strEval) { if (Scanner.DEBUG) { System.out.println("\n------------------------------------"); System.out.println(strEval); } - parser.parse(strEval); - } - /** - * The JUnit setup method - */ - protected void setUp() { - parser = new Parser(null); + checkParseHTML( + strEval.toCharArray(), + ""); +// parser.phpParserTester(strEval, 1); } +// private void checkHTML(String strEval) { +// if (Scanner.DEBUG) { +// System.out.println("\n------------------------------------"); +// System.out.println(strEval); +// } +// parser.parse(strEval); +// } +// /** +// * The JUnit setup method +// */ +// protected void setUp() { +// parser = new Parser(null); +// } } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/compiler/ITerminalSymbols.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/compiler/ITerminalSymbols.java index cc27c47..62feaeb 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/compiler/ITerminalSymbols.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/compiler/ITerminalSymbols.java @@ -79,8 +79,13 @@ public interface ITerminalSymbols { final static int TokenNameQUESTION = 62; final static int TokenNamePAAMAYIM_NEKUDOTAYIM = 63; final static int TokenNameAT = 64; + + final static int TokenNameand = 65; + final static int TokenNameor = 66; + final static int TokenNamexor = 67; - final static int TokenNameDOLLAR_LBRACE = 127; + final static int TokenNameDOLLAR = 126; +// final static int TokenNameDOLLAR_LBRACE = 127; final static int TokenNameLPAREN = 128; final static int TokenNameRPAREN = 129; final static int TokenNameLBRACE = 130; @@ -108,9 +113,19 @@ public interface ITerminalSymbols { final static int TokenNameRIGHT_SHIFT = 156; final static int TokenNameEQUAL_EQUAL_EQUAL = 157; final static int TokenNameNOT_EQUAL_EQUAL = 158; - final static int TokenNameOR = 159; - final static int TokenNameHEREDOC = 160; + + final static int TokenNameOR = 160; + final static int TokenNameHEREDOC = 161; + final static int TokenNameintCAST = 174; + final static int TokenNameboolCAST = 175; + final static int TokenNamedoubleCAST = 176; + final static int TokenNamestringCAST = 177; + final static int TokenNamearrayCAST = 178; + final static int TokenNameobjectCAST = 179; + final static int TokenNameunsetCAST = 180; + + public final static int TokenNameKEYWORD = 1000; public final static int TokenNameif = 1001; public final static int TokenNameelseif = 1002; @@ -158,7 +173,7 @@ public interface ITerminalSymbols { // public final static int TokenNamenull = 1044; // public final static int TokenNamefalse = 1045; // public final static int TokenNametrue = 1046; - public final static int TokenNamethis = 1047; +// public final static int TokenNamethis = 1047; // public final static int TokenNameabstract = 1050; public final static int TokenNamecatch = 1051; @@ -173,7 +188,8 @@ public interface ITerminalSymbols { public final static int TokenNamesuper = 1060; public final static int TokenNamethrow = 1061; - public final static int TokenNameconst = 1063; + public final static int TokenNameconst = 1062; + public final static int TokenNameclone = 1063; public final static int TokenNamedeclare = 1064; public final static int TokenNameenddeclare = 1065; public final static int TokenNameeval = 1065; diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java index f389faa..2a0e2c1 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java @@ -1,6 +1,6 @@ /********************************************************************** Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de - All rights reserved. This program and the accompanying materials + All rights reserved. This program and the accompanying material are made available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html @@ -62,7 +62,7 @@ public class Parser //extends PHPParserSuperclass private String stringValue; /** Contains the current expression. */ // private StringBuffer expression; - private boolean phpMode; + //private boolean phpMode; protected int modifiers; protected int modifiersSourceStart; protected Parser() { @@ -554,78 +554,78 @@ public class Parser //extends PHPParserSuperclass // } catch (CoreException e) { // } // } - public void phpParserTester(String s, int rowCount) { - this.str = s; - if (s == null) { - if (phpList.size() != 0) { - this.str = ((PHPString) phpList.get(currentPHPString++)).getPHPString(); - } - } - this.token = TokenNameEOF; - // this.chIndx = 0; - // this.rowCount = rowCount; - // this.columnCount = 0; - this.phpEnd = false; - this.phpMode = true; - scanner.setSource(s.toCharArray()); - scanner.setPHPMode(true); - getNextToken(); - do { - try { - if (token != TokenNameEOF && token != TokenNameERROR) { - statementList(); - } - if (token != TokenNameEOF) { - if (token == TokenNameERROR) { - throwSyntaxError("Scanner error (Found unknown token: " - + scanner.toStringAction(token) + ")"); - } - if (token == TokenNameRPAREN) { - throwSyntaxError("Too many closing ')'; end-of-file not reached."); - } - if (token == TokenNameRBRACE) { - throwSyntaxError("Too many closing '}'; end-of-file not reached."); - } - if (token == TokenNameRBRACKET) { - throwSyntaxError("Too many closing ']'; end-of-file not reached."); - } - if (token == TokenNameLPAREN) { - throwSyntaxError("Read character '('; end-of-file not reached."); - } - if (token == TokenNameLBRACE) { - throwSyntaxError("Read character '{'; end-of-file not reached."); - } - if (token == TokenNameLBRACKET) { - throwSyntaxError("Read character '['; end-of-file not reached."); - } - throwSyntaxError("End-of-file not reached."); - } - return; - } catch (SyntaxError err) { - if (s != null) { - throw err; - } else { - // setMarker(err.getMessage(), err.getLine(), ERROR); - // setMarker(err.getMessage(), - // scanner.getCurrentTokenStartPosition(), - // scanner.getCurrentTokenEndPosition(), ERROR); - } - // if an error occured, - // try to find keywords 'class' or 'function' - // to parse the rest of the string - while (token != TokenNameEOF && token != TokenNameERROR) { - if (token == TokenNameabstract || token == TokenNamefinal - || token == TokenNameclass || token == TokenNamefunction) { - break; - } - getNextToken(); - } - if (token == TokenNameEOF || token == TokenNameERROR) { - return; - } - } - } while (true); - } +// public void phpParserTester(String s, int rowCount) { +// this.str = s; +// if (s == null) { +// if (phpList.size() != 0) { +// this.str = ((PHPString) phpList.get(currentPHPString++)).getPHPString(); +// } +// } +// this.token = TokenNameEOF; +// // this.chIndx = 0; +// // this.rowCount = rowCount; +// // this.columnCount = 0; +// this.phpEnd = false; +// this.phpMode = true; +// scanner.setSource(s.toCharArray()); +// scanner.setPHPMode(true); +// getNextToken(); +// do { +// try { +// if (token != TokenNameEOF && token != TokenNameERROR) { +// statementList(); +// } +// if (token != TokenNameEOF) { +// if (token == TokenNameERROR) { +// throwSyntaxError("Scanner error (Found unknown token: " +// + scanner.toStringAction(token) + ")"); +// } +// if (token == TokenNameRPAREN) { +// throwSyntaxError("Too many closing ')'; end-of-file not reached."); +// } +// if (token == TokenNameRBRACE) { +// throwSyntaxError("Too many closing '}'; end-of-file not reached."); +// } +// if (token == TokenNameRBRACKET) { +// throwSyntaxError("Too many closing ']'; end-of-file not reached."); +// } +// if (token == TokenNameLPAREN) { +// throwSyntaxError("Read character '('; end-of-file not reached."); +// } +// if (token == TokenNameLBRACE) { +// throwSyntaxError("Read character '{'; end-of-file not reached."); +// } +// if (token == TokenNameLBRACKET) { +// throwSyntaxError("Read character '['; end-of-file not reached."); +// } +// throwSyntaxError("End-of-file not reached."); +// } +// return; +// } catch (SyntaxError err) { +// if (s != null) { +// throw err; +// } else { +// // setMarker(err.getMessage(), err.getLine(), ERROR); +// // setMarker(err.getMessage(), +// // scanner.getCurrentTokenStartPosition(), +// // scanner.getCurrentTokenEndPosition(), ERROR); +// } +// // if an error occured, +// // try to find keywords 'class' or 'function' +// // to parse the rest of the string +// while (token != TokenNameEOF && token != TokenNameERROR) { +// if (token == TokenNameabstract || token == TokenNamefinal +// || token == TokenNameclass || token == TokenNamefunction) { +// break; +// } +// getNextToken(); +// } +// if (token == TokenNameEOF || token == TokenNameERROR) { +// return; +// } +// } +// } while (true); +// } public void init(String s) { this.str = s; this.token = TokenNameEOF; @@ -633,7 +633,7 @@ public class Parser //extends PHPParserSuperclass // this.rowCount = 1; // this.columnCount = 0; this.phpEnd = false; - this.phpMode = false; +// this.phpMode = false; /* scanner initialization */ scanner.setSource(s.toCharArray()); scanner.setPHPMode(false); @@ -647,7 +647,7 @@ public class Parser //extends PHPParserSuperclass // this.rowCount = 1; // this.columnCount = 0; this.phpEnd = false; - this.phpMode = phpMode; +// this.phpMode = phpMode; scanner.setPHPMode(phpMode); } /** @@ -746,7 +746,7 @@ public class Parser //extends PHPParserSuperclass // return outlineInfo; // } private boolean isVariable() { - return token == TokenNameVariable || token == TokenNamethis; + return token == TokenNameVariable; // || token == TokenNamethis; } // private void parseDeclarations(PHPOutlineInfo outlineInfo, // OutlineableWithChildren current, boolean goBack) { @@ -949,49 +949,51 @@ public class Parser //extends PHPParserSuperclass // TokenNamenew) { // char[] ident = scanner.getCurrentIdentifierSource(); // String keyword = new String(ident); - if (token == TokenNameAT) { - getNextToken(); - if (token != TokenNamerequire && token != TokenNamerequire_once - && token != TokenNameinclude && token != TokenNameinclude_once - && token != TokenNameIdentifier && token != TokenNameVariable - && token != TokenNamethis && token != TokenNameStringInterpolated) { - throwSyntaxError("identifier expected after '@'."); - } - } - if (token == TokenNameinclude || token == TokenNameinclude_once) { - getNextToken(); - if (token == TokenNameLPAREN) { - expr(); - if (token == TokenNameSEMICOLON) { - getNextToken(); - } else { - if (previousToken != TokenNameAT && token != TokenNameStopPHP) { - throwSyntaxError("';' expected after 'include' or 'include_once'."); - } - // getNextToken(); - } - } else { - concatenationExpression(); - } - return; - } else if (token == TokenNamerequire || token == TokenNamerequire_once) { - getNextToken(); - //constant(); - if (token == TokenNameLPAREN) { - expr(); - if (token == TokenNameSEMICOLON) { - getNextToken(); - } else { - if (previousToken != TokenNameAT && token != TokenNameStopPHP) { - throwSyntaxError("';' expected after 'require' or 'require_once'."); - } - // getNextToken(); - } - } else { - concatenationExpression(); - } - return; - } else if (token == TokenNameif) { + // if (token == TokenNameAT) { + // getNextToken(); + // if (token != TokenNamerequire && token != TokenNamerequire_once + // && token != TokenNameinclude && token != TokenNameinclude_once + // && token != TokenNameIdentifier && token != TokenNameVariable + // && token != TokenNameStringInterpolated) { + // throwSyntaxError("identifier expected after '@'."); + // } + // } + // if (token == TokenNameinclude || token == TokenNameinclude_once) { + // getNextToken(); + // if (token == TokenNameLPAREN) { + // expr(); + // if (token == TokenNameSEMICOLON) { + // getNextToken(); + // } else { + // if (previousToken != TokenNameAT && token != TokenNameStopPHP) { + // throwSyntaxError("';' expected after 'include' or 'include_once'."); + // } + // // getNextToken(); + // } + // } else { + // concatenationExpression(); + // } + // return; + // } else if (token == TokenNamerequire || token == TokenNamerequire_once) + // { + // getNextToken(); + // //constant(); + // if (token == TokenNameLPAREN) { + // expr(); + // if (token == TokenNameSEMICOLON) { + // getNextToken(); + // } else { + // if (previousToken != TokenNameAT && token != TokenNameStopPHP) { + // throwSyntaxError("';' expected after 'require' or 'require_once'."); + // } + // // getNextToken(); + // } + // } else { + // concatenationExpression(); + // } + // return; + // } else + if (token == TokenNameif) { getNextToken(); if (token == TokenNameLPAREN) { getNextToken(); @@ -1179,19 +1181,31 @@ public class Parser //extends PHPParserSuperclass // getNextToken(); // } // return; - } else if (token == TokenNameglobal || token == TokenNamestatic) { + } else if (token == TokenNameglobal) { getNextToken(); - variableList(); + global_var_list(); if (token == TokenNameSEMICOLON) { getNextToken(); } else { if (token != TokenNameStopPHP) { - throwSyntaxError("';' expected after 'global' or 'static' statement."); + throwSyntaxError("';' expected after 'global' statement."); } getNextToken(); } return; - } else if (token == TokenNameunset) { + } else if (token == TokenNamestatic) { + getNextToken(); + static_var_list(); + if (token == TokenNameSEMICOLON) { + getNextToken(); + } else { + if (token != TokenNameStopPHP) { + throwSyntaxError("';' expected after 'static' statement."); + } + getNextToken(); + } + return; + }else if (token == TokenNameunset) { getNextToken(); if (token == TokenNameLPAREN) { getNextToken(); @@ -1314,6 +1328,61 @@ public class Parser //extends PHPParserSuperclass } } } + private void global_var_list() { + // global_var_list: + // global_var_list ',' global_var + //| global_var + while (true) { + global_var(); + if (token != TokenNameCOMMA) { + break; + } + getNextToken(); + } + } + private void global_var() { + //global_var: + // T_VARIABLE + //| '$' r_variable + //| '$' '{' expr '}' + if (token == TokenNameVariable) { + getNextToken(); + } else if (token == TokenNameDOLLAR) { + getNextToken(); + if (token == TokenNameLPAREN) { + getNextToken(); + expr(); + if (token != TokenNameLPAREN) { + throwSyntaxError("')' expected in global variable."); + } + getNextToken(); + } else { + r_variable(); + } + } + } + private void static_var_list() { + //static_var_list: + // static_var_list ',' T_VARIABLE + //| static_var_list ',' T_VARIABLE '=' static_scalar + //| T_VARIABLE + //| T_VARIABLE '=' static_scalar + while (true) { + if (token == TokenNameVariable) { + getNextToken(); + if (token == TokenNameEQUAL) { + getNextToken(); + static_scalar(); + } + if (token != TokenNameCOMMA) { + break; + } + getNextToken(); + } else { + break; + } + } + } private void unset_variables() { // unset_variables: // unset_variable @@ -1634,10 +1703,11 @@ public class Parser //extends PHPParserSuperclass static_scalar(); } } else { - if (token == TokenNamethis) { - throwSyntaxError("'$this' not allowed after keyword 'public' 'protected' 'private' 'var'."); - } - throwSyntaxError("Variable expected keyword 'public' 'protected' 'private' 'var'."); + // if (token == TokenNamethis) { + // throwSyntaxError("'$this' not allowed after keyword 'public' + // 'protected' 'private' 'var'."); + // } + throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'."); } if (token != TokenNameCOMMA) { break; @@ -1738,9 +1808,10 @@ public class Parser //extends PHPParserSuperclass } return; } - if (token == TokenNamethis) { - throwSyntaxError("Reserved word '$this' not allowed in parameter declaration."); - } + // if (token == TokenNamethis) { + // throwSyntaxError("Reserved word '$this' not allowed in parameter + // declaration."); + // } } private void labeledStatementList() { if (token != TokenNamecase && token != TokenNamedefault) { @@ -2044,29 +2115,85 @@ public class Parser //extends PHPParserSuperclass } while (true); } private void expr() { - //todo: find a better way to get the expression - // expression = new StringBuffer(); - // for (int i = chIndx; i < str.length(); i++) { - // if (str.charAt(i) == ';') { - // break; - // } - // expression.append(str.charAt(i)); - // } - // if (token == TokenNameSTRING_CONSTANT || token == - // TokenNameINTERPOLATED_STRING) { - // getNextToken(); - // } else { - logicalinclusiveorExpression(); - // while (token != TokenNameSEMICOLON) { - // getNextToken(); - // // } + // r_variable + // | expr_without_variable + // if (token!=TokenNameEOF) { + if (Scanner.TRACE) { + System.out.println("TRACE: expr()"); + } + expr_without_variable(true); // } } - private void expr_without_variable() { - // String ident; - char[] ident; - boolean castFlag = false; - boolean arrayFlag = false; + private void expr_without_variable(boolean only_variable) { + // internal_functions_in_yacc + // | T_CLONE expr + // | T_PRINT expr + // | '(' expr ')' + // | '@' expr + // | '+' expr + // | '-' expr + // | '!' expr + // | '~' expr + // | T_INC rw_variable + // | T_DEC rw_variable + // | T_INT_CAST expr + // | T_DOUBLE_CAST expr + // | T_STRING_CAST expr + // | T_ARRAY_CAST expr + // | T_OBJECT_CAST expr + // | T_BOOL_CAST expr + // | T_UNSET_CAST expr + // | T_EXIT exit_expr + // | scalar + // | T_ARRAY '(' array_pair_list ')' + // | '`' encaps_list '`' + // | T_LIST '(' assignment_list ')' '=' expr + // | T_NEW class_name_reference ctor_arguments + // | variable '=' expr + // | variable '=' '&' variable + // | variable '=' '&' T_NEW class_name_reference ctor_arguments + // | variable T_PLUS_EQUAL expr + // | variable T_MINUS_EQUAL expr + // | variable T_MUL_EQUAL expr + // | variable T_DIV_EQUAL expr + // | variable T_CONCAT_EQUAL expr + // | variable T_MOD_EQUAL expr + // | variable T_AND_EQUAL expr + // | variable T_OR_EQUAL expr + // | variable T_XOR_EQUAL expr + // | variable T_SL_EQUAL expr + // | variable T_SR_EQUAL expr + // | rw_variable T_INC + // | rw_variable T_DEC + // | expr T_BOOLEAN_OR expr + // | expr T_BOOLEAN_AND expr + // | expr T_LOGICAL_OR expr + // | expr T_LOGICAL_AND expr + // | expr T_LOGICAL_XOR expr + // | expr '|' expr + // | expr '&' expr + // | expr '^' expr + // | expr '.' expr + // | expr '+' expr + // | expr '-' expr + // | expr '*' expr + // | expr '/' expr + // | expr '%' expr + // | expr T_SL expr + // | expr T_SR expr + // | expr T_IS_IDENTICAL expr + // | expr T_IS_NOT_IDENTICAL expr + // | expr T_IS_EQUAL expr + // | expr T_IS_NOT_EQUAL expr + // | expr '<' expr + // | expr T_IS_SMALLER_OR_EQUAL expr + // | expr '>' expr + // | expr T_IS_GREATER_OR_EQUAL expr + // | expr T_INSTANCEOF class_name_reference + // | expr '?' expr ':' expr + if (Scanner.TRACE) { + System.out.println("TRACE: expr_without_variable() PART 1"); + } switch (token) { case TokenNameisset : case TokenNameempty : @@ -2077,125 +2204,74 @@ public class Parser //extends PHPParserSuperclass case TokenNamerequire_once : internal_functions_in_yacc(); break; - case TokenNamenew : - getNextToken(); - expr(); - break; - // - // case TokenNamenull : - // getNextToken(); - // break; - // case TokenNamefalse : - // getNextToken(); - // break; - // case TokenNametrue : - // getNextToken(); - // break; - case TokenNameStringConstant : - getNextToken(); - break; - case TokenNameHEREDOC : - case TokenNameStringInterpolated : - case TokenNameStringLiteral : - getNextToken(); - break; + // | '(' expr ')' case TokenNameLPAREN : getNextToken(); - if (token == TokenNameIdentifier) { - // check if identifier is a type: - // ident = identifier; - ident = scanner.getCurrentIdentifierSource(); - String str = new String(ident).toLowerCase(); - for (int i = 0; i < PHP_TYPES.length; i++) { - if (PHP_TYPES[i].equals(str)) { - castFlag = true; - if (PHP_TYPES[i].equals("array")) { - arrayFlag = true; - } - break; - } - } - } - if (castFlag) { - getNextToken(); - if (arrayFlag && token == TokenNameLPAREN) { - getNextToken(); - if (token == TokenNameRPAREN) { - getNextToken(); - } else { - expr(); - if (token != TokenNameRPAREN) { - throwSyntaxError(") expected after 'array('."); - } - } - } - if (token != TokenNameRPAREN) { - throwSyntaxError(") expected after cast-type '" + str + "'."); - } + expr(); + if (token == TokenNameRPAREN) { getNextToken(); - expr(); - break; } else { - expr(); + throwSyntaxError("')' expected in expression."); } - if (token != TokenNameRPAREN) { - throwSyntaxError(") expected in postfix-expression."); - } - getNextToken(); break; - case TokenNameDoubleLiteral : - getNextToken(); - break; - case TokenNameIntegerLiteral : - getNextToken(); - break; - case TokenNameDOLLAR_LBRACE : + // | T_CLONE expr + // | T_PRINT expr + // | '@' expr + // | '+' expr + // | '-' expr + // | '!' expr + // | '~' expr + // | T_INT_CAST expr + // | T_DOUBLE_CAST expr + // | T_STRING_CAST expr + // | T_ARRAY_CAST expr + // | T_OBJECT_CAST expr + // | T_BOOL_CAST expr + // | T_UNSET_CAST expr + case TokenNameclone : + case TokenNameprint : + case TokenNameAT : + case TokenNamePLUS : + case TokenNameMINUS : + case TokenNameNOT : + case TokenNameTWIDDLE : + case TokenNameintCAST : + case TokenNamedoubleCAST : + case TokenNamestringCAST : + case TokenNamearrayCAST : + case TokenNameobjectCAST : + case TokenNameboolCAST : + case TokenNameunsetCAST : getNextToken(); expr(); - if (token != TokenNameRBRACE) { - throwSyntaxError("'}' expected after indirect variable token '${'."); - } - getNextToken(); break; - case TokenNameVariable : - case TokenNamethis : - ident = scanner.getCurrentIdentifierSource(); + case TokenNameexit : getNextToken(); - if (token == TokenNameLBRACE) { - getNextToken(); - expr(); - if (token != TokenNameRBRACE) { - throwSyntaxError("'}' expected after variable '" - + new String(ident) + "' in variable-expression."); - } - getNextToken(); - } else if (token == TokenNameLPAREN) { - getNextToken(); - if (token != TokenNameRPAREN) { - expressionList(); - if (token != TokenNameRPAREN) { - throwSyntaxError("')' expected after variable '" - + new String(ident) + "' in postfix-expression."); - } - } - getNextToken(); - } + exit_expr(); break; - case TokenNameIdentifier : - ident = scanner.getCurrentIdentifierSource(); + // scalar: + // T_STRING + //| T_STRING_VARNAME + //| class_constant + //| '"' encaps_list '"' + //| '\'' encaps_list '\'' + //| T_START_HEREDOC encaps_list T_END_HEREDOC + // | '`' encaps_list '`' + // | common_scalar + case TokenNameIntegerLiteral : + case TokenNameDoubleLiteral : + case TokenNameStringLiteral : + case TokenNameStringConstant : + case TokenNameStringInterpolated : + case TokenNameFILE : + case TokenNameLINE : + case TokenNameCLASS_C : + case TokenNameMETHOD_C : + case TokenNameFUNC_C : + common_scalar(); + break; + case TokenNameHEREDOC : getNextToken(); - if (token == TokenNameLPAREN) { - getNextToken(); - if (token != TokenNameRPAREN) { - expressionList(); - if (token != TokenNameRPAREN) { - throwSyntaxError("')' expected after identifier '" - + new String(ident) + "' in postfix-expression." - + "(Found token: " + scanner.toStringAction(token) + ")"); - } - } - getNextToken(); - } break; case TokenNamearray : // T_ARRAY '(' array_pair_list ')' @@ -2217,178 +2293,285 @@ public class Parser //extends PHPParserSuperclass + "(Found token: " + scanner.toStringAction(token) + ")"); } break; - case TokenNameprint : - getNextToken(); - expr(); - break; - case TokenNameexit : - getNextToken(); - exit_expr(); - break; case TokenNamelist : + // | T_LIST '(' assignment_list ')' '=' expr getNextToken(); if (token == TokenNameLPAREN) { getNextToken(); - if (token == TokenNameCOMMA) { - getNextToken(); - } - expressionList(); + assignment_list(); if (token != TokenNameRPAREN) { throwSyntaxError("')' expected after 'list' keyword."); } getNextToken(); - // if (token == TokenNameSET) { - // getNextToken(); - // logicalinclusiveorExpression(); - // } + if (token != TokenNameEQUAL) { + throwSyntaxError("'=' expected after 'list' keyword."); + } + getNextToken(); + expr(); } else { throwSyntaxError("'(' expected after 'list' keyword."); } break; - // case TokenNameexit : - // getNextToken(); - // if (token != TokenNameSEMICOLON) { - // exitStatus(); - // } - // if (token == TokenNameSEMICOLON) { - // getNextToken(); - // } else { - // if (token != TokenNameStopPHP) { - // throwSyntaxError("';' expected after 'exit' expression."); - // } - // getNextToken(); - // } - // break; - // case TokenNamedie : - // getNextToken(); - // if (token != TokenNameSEMICOLON) { - // exitStatus(); - // } - // if (token == TokenNameSEMICOLON) { - // getNextToken(); - // } else { - // if (token != TokenNameStopPHP) { - // throwSyntaxError("';' expected after 'die' expression."); - // } - // } - // break; - // case TokenNamearray : - // getNextToken(); - // if (token == TokenNameARGOPEN) { - // getNextToken(); - // if (token == TokenNameCOMMA) { - // getNextToken(); - // } - // expressionList(); - // if (token != TokenNameARGCLOSE) { - // throwSyntaxError("')' expected after 'list' keyword."); - // } - // getNextToken(); - // if (token == TokenNameSET) { - // getNextToken(); - // logicalinclusiveorExpression(); - // } - // } else { - // throwSyntaxError("'(' expected after 'list' keyword."); - // } - // break; + case TokenNamenew : + // | T_NEW class_name_reference ctor_arguments + getNextToken(); + class_name_reference(); + ctor_arguments(); + break; + // | T_INC rw_variable + // | T_DEC rw_variable + case TokenNamePLUS_PLUS : + case TokenNameMINUS_MINUS : + getNextToken(); + rw_variable(); + break; + // | variable '=' expr + // | variable '=' '&' variable + // | variable '=' '&' T_NEW class_name_reference ctor_arguments + // | variable T_PLUS_EQUAL expr + // | variable T_MINUS_EQUAL expr + // | variable T_MUL_EQUAL expr + // | variable T_DIV_EQUAL expr + // | variable T_CONCAT_EQUAL expr + // | variable T_MOD_EQUAL expr + // | variable T_AND_EQUAL expr + // | variable T_OR_EQUAL expr + // | variable T_XOR_EQUAL expr + // | variable T_SL_EQUAL expr + // | variable T_SR_EQUAL expr + // | rw_variable T_INC + // | rw_variable T_DEC + case TokenNameIdentifier : + case TokenNameVariable : + case TokenNameDOLLAR : + variable(); + switch (token) { + case TokenNameEQUAL : + getNextToken(); + if (token == TokenNameAND) { + getNextToken(); + if (token == TokenNamenew) { + getNextToken(); + throwSyntaxError("not yet implemented (= & new)"); + // class_name_reference(); + // ctor_arguments(); + } else { + variable(); + } + } else { + expr(); + } + break; + case TokenNamePLUS_EQUAL : + case TokenNameMINUS_EQUAL : + case TokenNameMULTIPLY_EQUAL : + case TokenNameDIVIDE_EQUAL : + case TokenNameDOT_EQUAL : + case TokenNameREMAINDER_EQUAL : + case TokenNameAND_EQUAL : + case TokenNameOR_EQUAL : + case TokenNameXOR_EQUAL : + case TokenNameRIGHT_SHIFT_EQUAL : + case TokenNameLEFT_SHIFT_EQUAL : + getNextToken(); + expr(); + break; + case TokenNamePLUS_PLUS : + case TokenNameMINUS_MINUS : + getNextToken(); + break; + default : + if (!only_variable) { + throwSyntaxError("Variable expression not allowed (found token '" + + scanner.toStringAction(token) + "')."); + } + } + break; + default : + if (token != TokenNameStopPHP) { + throwSyntaxError("Error in expression (found token '" + + scanner.toStringAction(token) + "')."); + } + return; } - boolean while_flag = true; - do { + if (Scanner.TRACE) { + System.out.println("TRACE: expr_without_variable() PART 2"); + } + // | expr T_BOOLEAN_OR expr + // | expr T_BOOLEAN_AND expr + // | expr T_LOGICAL_OR expr + // | expr T_LOGICAL_AND expr + // | expr T_LOGICAL_XOR expr + // | expr '|' expr + // | expr '&' expr + // | expr '^' expr + // | expr '.' expr + // | expr '+' expr + // | expr '-' expr + // | expr '*' expr + // | expr '/' expr + // | expr '%' expr + // | expr T_SL expr + // | expr T_SR expr + // | expr T_IS_IDENTICAL expr + // | expr T_IS_NOT_IDENTICAL expr + // | expr T_IS_EQUAL expr + // | expr T_IS_NOT_EQUAL expr + // | expr '<' expr + // | expr T_IS_SMALLER_OR_EQUAL expr + // | expr '>' expr + // | expr T_IS_GREATER_OR_EQUAL expr + while (true) { switch (token) { - case TokenNameLBRACKET : + case TokenNameOR_OR : + case TokenNameAND_AND : + case TokenNameand : + case TokenNameor : + case TokenNamexor : + case TokenNameAND : + case TokenNameOR : + case TokenNameXOR : + case TokenNameDOT : + case TokenNamePLUS : + case TokenNameMINUS : + case TokenNameMULTIPLY : + case TokenNameDIVIDE : + case TokenNameREMAINDER : + case TokenNameLEFT_SHIFT : + case TokenNameRIGHT_SHIFT : + case TokenNameEQUAL_EQUAL_EQUAL : + case TokenNameNOT_EQUAL_EQUAL : + case TokenNameEQUAL_EQUAL : + case TokenNameNOT_EQUAL : + case TokenNameLESS : + case TokenNameLESS_EQUAL : + case TokenNameGREATER : + case TokenNameGREATER_EQUAL : getNextToken(); expr(); - if (token != TokenNameRBRACKET) { - throwSyntaxError("] expected in postfix-expression."); - } + break; + // | expr T_INSTANCEOF class_name_reference + // | expr '?' expr ':' expr + case TokenNameinstanceof : getNextToken(); + throwSyntaxError("not yet implemented (class_name_reference)"); + // class_name_reference(); break; - case TokenNamePAAMAYIM_NEKUDOTAYIM : - // :: - case TokenNameMINUS_GREATER : - // -> + case TokenNameQUESTION : getNextToken(); - if (token > TokenNameKEYWORD) { - ident = scanner.getCurrentIdentifierSource(); - // setMarker( - // "Avoid using keyword '" - // + new String(ident) - // + "' as variable name.", - // rowCount, - // PHPParser.INFO); - // setMarker( - // "Avoid using keyword '" + new String(ident) + "' as - // variable name.", - // scanner.getCurrentTokenStartPosition(), - // scanner.getCurrentTokenEndPosition(), - // INFO); - } - switch (token) { - case TokenNameVariable : - ident = scanner.getCurrentIdentifierSource(); - getNextToken(); - // if (token == TokenNameARGOPEN) { - // getNextToken(); - // expressionList(); - // if (token != TokenNameARGCLOSE) { - // throwSyntaxError(") expected after variable '" + - // ident + "'."); - // } - // getNextToken(); - // } - break; - case TokenNameIdentifier : - //ident = scanner.getCurrentIdentifierSource(); - getNextToken(); - break; - case TokenNameLBRACE : - getNextToken(); - expr(); - if (token != TokenNameRBRACE) { - throwSyntaxError("} expected in postfix-expression."); - } - getNextToken(); - break; - default : - throwSyntaxError("Syntax error after '->' token."); - } - while (token == TokenNameLBRACKET || token == TokenNameLPAREN - || token == TokenNameLBRACE) { - if (token == TokenNameLBRACKET) { - getNextToken(); - expressionList(); - if (token != TokenNameRBRACKET) { - throwSyntaxError("] expected after '->'."); - } - getNextToken(); - } else if (token == TokenNameLPAREN) { - getNextToken(); - expressionList(); - if (token != TokenNameRPAREN) { - throwSyntaxError(") expected after '->'."); - } - getNextToken(); - } else if (token == TokenNameLBRACE) { - getNextToken(); - expr(); - if (token != TokenNameRBRACE) { - throwSyntaxError("} expected after '->'."); - } - getNextToken(); - } + expr(); + if (token == TokenNameCOLON) { + getNextToken(); + expr(); } break; - case TokenNamePLUS_PLUS : + default : + return; + } + } + } + private void class_name_reference() { + // class_name_reference: + // T_STRING + //| dynamic_class_name_reference + if (Scanner.TRACE) { + System.out.println("TRACE: class_name_reference()"); + } + if (token == TokenNameIdentifier) { + getNextToken(); + } else { + dynamic_class_name_reference(); + } + } + private void dynamic_class_name_reference() { + //dynamic_class_name_reference: + // base_variable T_OBJECT_OPERATOR object_property + // dynamic_class_name_variable_properties + //| base_variable + if (Scanner.TRACE) { + System.out.println("TRACE: dynamic_class_name_reference()"); + } + base_variable(); + if (token == TokenNameMINUS_GREATER) { + getNextToken(); + object_property(); + dynamic_class_name_variable_properties(); + } + } + private void dynamic_class_name_variable_properties() { + // dynamic_class_name_variable_properties: + // dynamic_class_name_variable_properties + // dynamic_class_name_variable_property + // | /* empty */ + if (Scanner.TRACE) { + System.out.println("TRACE: dynamic_class_name_variable_properties()"); + } + while (token == TokenNameMINUS_GREATER) { + dynamic_class_name_variable_property(); + } + } + private void dynamic_class_name_variable_property() { + // dynamic_class_name_variable_property: + // T_OBJECT_OPERATOR object_property + if (Scanner.TRACE) { + System.out.println("TRACE: dynamic_class_name_variable_property()"); + } + if (token == TokenNameMINUS_GREATER) { + getNextToken(); + object_property(); + } + } + private void ctor_arguments() { + // ctor_arguments: + // /* empty */ + //| '(' function_call_parameter_list ')' + if (token == TokenNameLPAREN) { + getNextToken(); + if (token == TokenNameRPAREN) { + getNextToken(); + return; + } + non_empty_function_call_parameter_list(); + if (token != TokenNameRPAREN) { + throwSyntaxError("')' expected in ctor_arguments."); + } + getNextToken(); + } + } + private void assignment_list() { + // assignment_list: + // assignment_list ',' assignment_list_element + //| assignment_list_element + while (true) { + assignment_list_element(); + if (token != TokenNameCOMMA) { + break; + } + getNextToken(); + } + } + private void assignment_list_element() { + //assignment_list_element: + // variable + //| T_LIST '(' assignment_list ')' + //| /* empty */ + if (token == TokenNameVariable || token == TokenNameDOLLAR) { + variable(); + } else { + if (token == TokenNamelist) { + getNextToken(); + if (token == TokenNameLPAREN) { getNextToken(); - break; - case TokenNameMINUS_MINUS : + assignment_list(); + if (token != TokenNameRPAREN) { + throwSyntaxError("')' expected after 'list' keyword."); + } getNextToken(); - break; - default : - while_flag = false; + } else { + throwSyntaxError("'(' expected after 'list' keyword."); + } } - } while (while_flag); + } } private void array_pair_list() { // array_pair_list: @@ -2433,309 +2616,392 @@ public class Parser //extends PHPParserSuperclass } } } - private void unaryExpression() { - switch (token) { - case TokenNamePLUS_PLUS : - getNextToken(); - unaryExpression(); - break; - case TokenNameMINUS_MINUS : - getNextToken(); - unaryExpression(); - break; - // '@' '&' '*' '+' '-' '~' '!' - case TokenNameAT : - getNextToken(); - if (token == TokenNameinclude || token == TokenNameinclude_once - || token == TokenNamerequire || token == TokenNamerequire_once) { - statement(TokenNameAT); - } else { - expr_without_variable(); // castExpression(); - } - break; - case TokenNameAND : - getNextToken(); - castExpression(); - break; - case TokenNameMULTIPLY : - getNextToken(); - castExpression(); - break; - case TokenNamePLUS : - getNextToken(); - castExpression(); - break; - case TokenNameMINUS : - getNextToken(); - castExpression(); - break; - case TokenNameTWIDDLE : - getNextToken(); - castExpression(); - break; - case TokenNameNOT : - getNextToken(); - castExpression(); - break; - default : - expr_without_variable(); +// private void variableList() { +// do { +// variable(); +// if (token == TokenNameCOMMA) { +// getNextToken(); +// } else { +// break; +// } +// } while (true); +// } + private void variable_without_objects() { + // variable_without_objects: + // reference_variable + // | simple_indirect_reference reference_variable + if (Scanner.TRACE) { + System.out.println("TRACE: variable_without_objects()"); } - } - private void castExpression() { - // if (token == TokenNameARGOPEN) { - // getNextToken(); - // typeName(); - // if (token != TokenNameARGCLOSE) { - // throwSyntaxError(") expected after cast-expression."); - // } - // getNextToken(); - // } - unaryExpression(); - } - private void assignExpression() { - castExpression(); - if (token == TokenNameEQUAL) { // = - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameDOT_EQUAL) { // .= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameEQUAL_GREATER) { // => - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNamePLUS_EQUAL) { // += - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameMINUS_EQUAL) { // -= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameMULTIPLY_EQUAL) { // *= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameDIVIDE_EQUAL) { // *= + while (token == TokenNameDOLLAR) { getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameREMAINDER_EQUAL) { // %= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameAND_EQUAL) { // &= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameOR_EQUAL) { // |= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameXOR_EQUAL) { // ^= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameLEFT_SHIFT_EQUAL) { // <<= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameRIGHT_SHIFT_EQUAL) { // >>= - getNextToken(); - logicalinclusiveorExpression(); - } else if (token == TokenNameTWIDDLE_EQUAL) { // ~= - getNextToken(); - logicalinclusiveorExpression(); } - } - private void multiplicativeExpression() { - do { - assignExpression(); - if (token != TokenNameMULTIPLY && token != TokenNameDIVIDE - && token != TokenNameREMAINDER) { - return; - } + reference_variable(); + } + private void function_call() { + // function_call: + // T_STRING '(' function_call_parameter_list ')' + //| class_constant '(' function_call_parameter_list ')' + //| static_member '(' function_call_parameter_list ')' + //| variable_without_objects '(' function_call_parameter_list ')' + if (Scanner.TRACE) { + System.out.println("TRACE: function_call()"); + } + if (token == TokenNameIdentifier) { getNextToken(); - } while (true); - } - private void concatenationExpression() { - do { - multiplicativeExpression(); - if (token != TokenNameDOT) { - return; + switch (token) { + case TokenNamePAAMAYIM_NEKUDOTAYIM : + // static member: + getNextToken(); + if (token == TokenNameIdentifier) { + // class _constant + getNextToken(); + } else { + // static member: + variable_without_objects(); + } + break; } + } else { + variable_without_objects(); + } + if (token != TokenNameLPAREN) { + // TODO is this ok ? + return; + // throwSyntaxError("'(' expected in function call."); + } + getNextToken(); + if (token == TokenNameRPAREN) { getNextToken(); - } while (true); + return; + } + non_empty_function_call_parameter_list(); + if (token != TokenNameRPAREN) { + throwSyntaxError("')' expected in function call."); + } + getNextToken(); } - private void additiveExpression() { - do { - concatenationExpression(); - if (token != TokenNamePLUS && token != TokenNameMINUS) { - return; + // private void function_call_parameter_list() { + // function_call_parameter_list: + // non_empty_function_call_parameter_list { $$ = $1; } + // | /* empty */ + // } + private void non_empty_function_call_parameter_list() { + //non_empty_function_call_parameter_list: + // expr_without_variable + // | variable + // | '&' w_variable + // | non_empty_function_call_parameter_list ',' expr_without_variable + // | non_empty_function_call_parameter_list ',' variable + // | non_empty_function_call_parameter_list ',' '&' w_variable + if (Scanner.TRACE) { + System.out.println("TRACE: non_empty_function_call_parameter_list()"); + } + while (true) { + if (token == TokenNameAND) { + getNextToken(); + w_variable(); + } else { +// if (token == TokenNameIdentifier || token == TokenNameVariable +// || token == TokenNameDOLLAR) { +// variable(); +// } else { + expr_without_variable(true); +// } } - getNextToken(); - } while (true); - } - private void shiftExpression() { - do { - additiveExpression(); - if (token != TokenNameLEFT_SHIFT && token != TokenNameRIGHT_SHIFT) { - return; + if (token != TokenNameCOMMA) { + break; } getNextToken(); - } while (true); + } } - private void relationalExpression() { - do { - shiftExpression(); - if (token != TokenNameLESS && token != TokenNameGREATER - && token != TokenNameLESS_EQUAL && token != TokenNameGREATER_EQUAL) { - return; - } + private void fully_qualified_class_name() { + if (token == TokenNameIdentifier) { getNextToken(); - } while (true); + } else { + throwSyntaxError("Class name expected."); + } } - private void identicalExpression() { - do { - relationalExpression(); - if (token != TokenNameEQUAL_EQUAL_EQUAL - && token != TokenNameNOT_EQUAL_EQUAL) { - return; - } + private void static_member() { + // static_member: + // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM + // variable_without_objects + if (Scanner.TRACE) { + System.out.println("TRACE: static_member()"); + } + fully_qualified_class_name(); + if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) { + throwSyntaxError("'::' expected after class name (static_member)."); + } + getNextToken(); + variable_without_objects(); + } + private void base_variable_with_function_calls() { + // base_variable_with_function_calls: + // base_variable + //| function_call + boolean functionCall = false; + if (Scanner.TRACE) { + System.out.println("TRACE: base_variable_with_function_calls()"); + } + if (token == TokenNameIdentifier) { + functionCall = true; + } else if (token == TokenNameVariable) { + int tempToken = token; + int tempPosition = scanner.currentPosition; getNextToken(); - } while (true); + if (token == TokenNameLPAREN) { + functionCall = true; + } + token = tempToken; + scanner.currentPosition = tempPosition; + scanner.phpMode = true; + } + if (functionCall) { + function_call(); + } else { + base_variable(); + } } - private void equalityExpression() { - do { - identicalExpression(); - if (token != TokenNameEQUAL_EQUAL && token != TokenNameNOT_EQUAL) { - return; + private void base_variable() { + // base_variable: + // reference_variable + // | simple_indirect_reference reference_variable + // | static_member + if (Scanner.TRACE) { + System.out.println("TRACE: base_variable()"); + } + if (token == TokenNameIdentifier) { + static_member(); + } else { + while (token == TokenNameDOLLAR) { + getNextToken(); } - getNextToken(); - } while (true); + reference_variable(); + } } - private void ternaryExpression() { - equalityExpression(); - if (token == TokenNameQUESTION) { - getNextToken(); - expr(); - if (token == TokenNameCOLON) { + // private void simple_indirect_reference() { + // // simple_indirect_reference: + // // '$' + // //| simple_indirect_reference '$' + // } + private void reference_variable() { + // reference_variable: + // reference_variable '[' dim_offset ']' + // | reference_variable '{' expr '}' + // | compound_variable + if (Scanner.TRACE) { + System.out.println("TRACE: reference_variable()"); + } + compound_variable(); + while (true) { + if (token == TokenNameLBRACE) { getNextToken(); expr(); + if (token != TokenNameRBRACE) { + throwSyntaxError("'}' expected in reference variable."); + } + getNextToken(); + } else if (token == TokenNameLBRACKET) { + getNextToken(); + if (token != TokenNameRBRACKET) { + expr(); + // dim_offset(); + if (token != TokenNameRBRACKET) { + throwSyntaxError("']' expected in reference variable."); + } + } + getNextToken(); } else { - throwSyntaxError("':' expected in ternary operator '? :'."); + break; } } } - private void andExpression() { - do { - ternaryExpression(); - if (token != TokenNameAND) { - return; - } - getNextToken(); - } while (true); - } - private void exclusiveorExpression() { - do { - andExpression(); - if (token != TokenNameXOR) { - return; - } - getNextToken(); - } while (true); - } - private void inclusiveorExpression() { - do { - exclusiveorExpression(); - if (token != TokenNameOR) { - return; - } - getNextToken(); - } while (true); - } - private void booleanandExpression() { - do { - inclusiveorExpression(); - if (token != TokenNameAND_AND) { - return; - } - getNextToken(); - } while (true); - } - private void booleanorExpression() { - do { - booleanandExpression(); - if (token != TokenNameOR_OR) { - return; - } + private void compound_variable() { + // compound_variable: + // T_VARIABLE + // | '$' '{' expr '}' + if (Scanner.TRACE) { + System.out.println("TRACE: compound_variable()"); + } + if (token == TokenNameVariable) { getNextToken(); - } while (true); - } - private void logicalandExpression() { - do { - booleanorExpression(); - if (token != TokenNameAND) { - return; + } else { + // because of simple_indirect_reference + while (token == TokenNameDOLLAR) { + getNextToken(); } - getNextToken(); - } while (true); - } - private void logicalexclusiveorExpression() { - do { - logicalandExpression(); - if (token != TokenNameXOR) { - return; + if (token != TokenNameLBRACE) { + throwSyntaxError("'{' expected after compound variable token '$'."); } getNextToken(); - } while (true); - } - private void logicalinclusiveorExpression() { - do { - logicalexclusiveorExpression(); - if (token != TokenNameOR) { - return; + expr(); + if (token != TokenNameRBRACE) { + throwSyntaxError("'}' expected after compound variable token '$'."); } getNextToken(); - } while (true); + } } - // public void assignmentExpression() { - // if (token == TokenNameVARIABLE) { - // getNextToken(); - // if (token == TokenNameSET) { - // getNextToken(); - // logicalinclusiveorExpression(); - // } - // } else { - // logicalinclusiveorExpression(); - // } + // private void dim_offset() { + // // dim_offset: + // // /* empty */ + // // | expr + // expr(); // } - private void variableList() { - do { - variable(); - if (token == TokenNameCOMMA) { + private void object_property() { + // object_property: + // object_dim_list + //| variable_without_objects + if (Scanner.TRACE) { + System.out.println("TRACE: object_property()"); + } + if (token == TokenNameVariable || token == TokenNameDOLLAR) { + variable_without_objects(); + } else { + object_dim_list(); + } + } + private void object_dim_list() { + //object_dim_list: + // object_dim_list '[' dim_offset ']' + //| object_dim_list '{' expr '}' + //| variable_name + if (Scanner.TRACE) { + System.out.println("TRACE: object_dim_list()"); + } + variable_name(); + while (true) { + if (token == TokenNameLBRACE) { + getNextToken(); + expr(); + if (token != TokenNameRBRACE) { + throwSyntaxError("'}' expected in object_dim_list."); + } + getNextToken(); + } else if (token == TokenNameLBRACKET) { + getNextToken(); + if (token == TokenNameRBRACKET) { + getNextToken(); + continue; + } + expr(); + if (token != TokenNameRBRACKET) { + throwSyntaxError("']' expected in object_dim_list."); + } getNextToken(); } else { break; } - } while (true); + } } - private void variable() { - if (token == TokenNameDOLLAR_LBRACE) { + private void variable_name() { + //variable_name: + // T_STRING + //| '{' expr '}' + if (Scanner.TRACE) { + System.out.println("TRACE: variable_name()"); + } + if (token == TokenNameIdentifier) { + getNextToken(); + } else { + if (token != TokenNameLBRACE) { + throwSyntaxError("'{' expected in variable name."); + } getNextToken(); expr(); - ; if (token != TokenNameRBRACE) { - throwSyntaxError("'}' expected after indirect variable token '${'."); + throwSyntaxError("'}' expected in variable name."); } + } + } + private void r_variable() { + variable(); + } + private void w_variable() { + variable(); + } + private void rw_variable() { + variable(); + } + private void variable() { + // variable: + // base_variable_with_function_calls T_OBJECT_OPERATOR + // object_property method_or_not variable_properties + // | base_variable_with_function_calls + base_variable_with_function_calls(); + if (token == TokenNameMINUS_GREATER) { + getNextToken(); + object_property(); + method_or_not(); + variable_properties(); + } + // if (token == TokenNameDOLLAR_LBRACE) { + // getNextToken(); + // expr(); + // ; + // if (token != TokenNameRBRACE) { + // throwSyntaxError("'}' expected after indirect variable token '${'."); + // } + // getNextToken(); + // } else { + // if (token == TokenNameVariable) { + // getNextToken(); + // if (token == TokenNameLBRACKET) { + // getNextToken(); + // expr(); + // if (token != TokenNameRBRACKET) { + // throwSyntaxError("']' expected in variable-list."); + // } + // getNextToken(); + // } else if (token == TokenNameEQUAL) { + // getNextToken(); + // static_scalar(); + // } + // } else { + // throwSyntaxError("$-variable expected in variable-list."); + // } + // } + } + private void variable_properties() { + // variable_properties: + // variable_properties variable_property + // | /* empty */ + while (token == TokenNameMINUS_GREATER) { + variable_property(); + } + } + private void variable_property() { + // variable_property: + // T_OBJECT_OPERATOR object_property method_or_not + if (Scanner.TRACE) { + System.out.println("TRACE: variable_property()"); + } + if (token == TokenNameMINUS_GREATER) { getNextToken(); + object_property(); + method_or_not(); } else { - if (token == TokenNameVariable) { + throwSyntaxError("'->' expected in variable_property."); + } + } + private void method_or_not() { + // method_or_not: + // '(' function_call_parameter_list ')' + // | /* empty */ + if (Scanner.TRACE) { + System.out.println("TRACE: method_or_not()"); + } + if (token == TokenNameLPAREN) { + getNextToken(); + if (token == TokenNameRPAREN) { getNextToken(); - if (token == TokenNameLBRACKET) { - getNextToken(); - expr(); - if (token != TokenNameRBRACKET) { - throwSyntaxError("']' expected in variable-list."); - } - getNextToken(); - } else if (token == TokenNameEQUAL) { - getNextToken(); - static_scalar(); - } - } else { - throwSyntaxError("$-variable expected in variable-list."); + return; + } + non_empty_function_call_parameter_list(); + if (token != TokenNameRPAREN) { + throwSyntaxError("')' expected in method_or_not."); } + getNextToken(); } } private void exit_expr() { @@ -2878,6 +3144,17 @@ public class Parser //extends PHPParserSuperclass } return false; } + private void scalar() { + // scalar: + // T_STRING + //| T_STRING_VARNAME + //| class_constant + //| common_scalar + //| '"' encaps_list '"' + //| '\'' encaps_list '\'' + //| T_START_HEREDOC encaps_list T_END_HEREDOC + throwSyntaxError("Not yet implemented (scalar)."); + } private void static_scalar() { // static_scalar: /* compile-time evaluated scalars */ // common_scalar diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java index bef873a..041fd95 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java @@ -131,6 +131,7 @@ public class Scanner implements IScanner, ITerminalSymbols { public char[][] taskTags = null; public char[][] taskPriorities = null; public static final boolean DEBUG = false; + public static final boolean TRACE = false; public Scanner() { this(false, false); } @@ -675,6 +676,133 @@ public class Scanner implements IScanner, ITerminalSymbols { return false; } } + public int getCastOrParen() { + int tempPosition = currentPosition; + char tempCharacter = currentCharacter; + int tempToken = TokenNameLPAREN; + boolean found = false; + StringBuffer buf = new StringBuffer(); + try { + do { + currentCharacter = source[currentPosition++]; + } while (currentCharacter == ' ' || currentCharacter == '\t'); + while ((currentCharacter >= 'a' && currentCharacter <= 'z') + || (currentCharacter >= 'A' && currentCharacter <= 'Z')) { + buf.append(currentCharacter); + currentCharacter = source[currentPosition++]; + } + if (buf.length() >= 3 && buf.length() <= 7) { + char[] data = buf.toString().toCharArray(); + int index = 0; + switch (data.length) { + case 3 : + // int + if ((data[index] == 'i') && (data[++index] == 'n') + && (data[++index] == 't')) { + found = true; + tempToken = TokenNameintCAST; + } + break; + case 4 : + // bool real + if ((data[index] == 'b') && (data[++index] == 'o') + && (data[++index] == 'o') && (data[++index] == 'l')) { + found = true; + tempToken = TokenNameboolCAST; + } else { + index = 0; + if ((data[index] == 'r') && (data[++index] == 'e') + && (data[++index] == 'a') && (data[++index] == 'l')) { + found = true; + tempToken = TokenNamedoubleCAST; + } + } + break; + case 5 : + // array unset float + if ((data[index] == 'a') && (data[++index] == 'r') + && (data[++index] == 'r') && (data[++index] == 'a') + && (data[++index] == 'y')) { + found = true; + tempToken = TokenNamearrayCAST; + } else { + index = 0; + if ((data[index] == 'u') && (data[++index] == 'n') + && (data[++index] == 's') && (data[++index] == 'e') + && (data[++index] == 't')) { + found = true; + tempToken = TokenNameunsetCAST; + } else { + index = 0; + if ((data[index] == 'f') && (data[++index] == 'l') + && (data[++index] == 'o') && (data[++index] == 'a') + && (data[++index] == 't')) { + found = true; + tempToken = TokenNamedoubleCAST; + } + } + } + break; + case 6 : + // object string double + if ((data[index] == 'o') && (data[++index] == 'b') + && (data[++index] == 'j') && (data[++index] == 'e') + && (data[++index] == 'c') && (data[++index] == 't')) { + found = true; + tempToken = TokenNameobjectCAST; + } else { + index = 0; + if ((data[index] == 's') && (data[++index] == 't') + && (data[++index] == 'r') && (data[++index] == 'i') + && (data[++index] == 'n') && (data[++index] == 'g')) { + found = true; + tempToken = TokenNamestringCAST; + } else { + index = 0; + if ((data[index] == 'd') && (data[++index] == 'o') + && (data[++index] == 'u') && (data[++index] == 'b') + && (data[++index] == 'l') && (data[++index] == 'e')) { + found = true; + tempToken = TokenNamedoubleCAST; + } + } + } + break; + case 7 : + // boolean integer + if ((data[index] == 'b') && (data[++index] == 'o') + && (data[++index] == 'o') && (data[++index] == 'l') + && (data[++index] == 'e') && (data[++index] == 'a') + && (data[++index] == 'n')) { + found = true; + tempToken = TokenNameboolCAST; + } else { + index = 0; + if ((data[index] == 'i') && (data[++index] == 'n') + && (data[++index] == 't') && (data[++index] == 'e') + && (data[++index] == 'g') && (data[++index] == 'e') + && (data[++index] == 'r')) { + found = true; + tempToken = TokenNameintCAST; + } + } + break; + } + if (found) { + while (currentCharacter == ' ' || currentCharacter == '\t') { + currentCharacter = source[currentPosition++]; + } + if (currentCharacter == ')') { + return tempToken; + } + } + } + } catch (IndexOutOfBoundsException e) { + } + currentCharacter = tempCharacter; + currentPosition = tempPosition; + return TokenNameLPAREN; + } public int getNextToken() throws InvalidInputException { int htmlPosition = currentPosition; try { @@ -781,7 +909,7 @@ public class Scanner implements IScanner, ITerminalSymbols { // ---------Identify the next token------------- switch (currentCharacter) { case '(' : - return TokenNameLPAREN; + return getCastOrParen(); case ')' : return TokenNameRPAREN; case '{' : @@ -797,6 +925,8 @@ public class Scanner implements IScanner, ITerminalSymbols { case ',' : return TokenNameCOMMA; case '.' : + if (getNextChar('=')) + return TokenNameDOT_EQUAL; if (getNextCharAsDigit()) return scanNumber(true); return TokenNameDOT; @@ -842,56 +972,66 @@ public class Scanner implements IScanner, ITerminalSymbols { return TokenNameREMAINDER; case '<' : { - int test; - if ((test = getNextChar('=', '<')) == 0) - return TokenNameLESS_EQUAL; - if (test > 0) { - if (getNextChar('=')) - return TokenNameLEFT_SHIFT_EQUAL; - if (getNextChar('<')) { - int heredocStart = currentPosition; - int heredocLength = 0; - currentCharacter = source[currentPosition++]; - if (isPHPIdentifierStart(currentCharacter)) { - currentCharacter = source[currentPosition++]; - } else { - return TokenNameERROR; - } - while (isPHPIdentifierPart(currentCharacter)) { + int oldPosition = currentPosition; + try { + currentCharacter = source[currentPosition++]; + } catch (IndexOutOfBoundsException e) { + currentPosition = oldPosition; + return TokenNameLESS; + } + switch (currentCharacter) { + case '=' : + return TokenNameLESS_EQUAL; + case '>' : + return TokenNameNOT_EQUAL; + case '<' : + if (getNextChar('=')) + return TokenNameLEFT_SHIFT_EQUAL; + if (getNextChar('<')) { + int heredocStart = currentPosition; + int heredocLength = 0; currentCharacter = source[currentPosition++]; - } - heredocLength = currentPosition - heredocStart - 1; - // heredoc end-tag determination - boolean endTag = true; - char ch; - do { - ch = source[currentPosition++]; - if (ch == '\r' || ch == '\n') { - if (recordLineSeparator) { - pushLineSeparator(); - } else { - currentLine = null; - } - for (int i = 0; i < heredocLength; i++) { - if (source[currentPosition + i] != source[heredocStart - + i]) { - endTag = false; - break; + if (isPHPIdentifierStart(currentCharacter)) { + currentCharacter = source[currentPosition++]; + } else { + return TokenNameERROR; + } + while (isPHPIdentifierPart(currentCharacter)) { + currentCharacter = source[currentPosition++]; + } + heredocLength = currentPosition - heredocStart - 1; + // heredoc end-tag determination + boolean endTag = true; + char ch; + do { + ch = source[currentPosition++]; + if (ch == '\r' || ch == '\n') { + if (recordLineSeparator) { + pushLineSeparator(); + } else { + currentLine = null; + } + for (int i = 0; i < heredocLength; i++) { + if (source[currentPosition + i] != source[heredocStart + + i]) { + endTag = false; + break; + } + } + if (endTag) { + currentPosition += heredocLength - 1; + currentCharacter = source[currentPosition++]; + break; // do...while loop + } else { + endTag = true; } } - if (endTag) { - currentPosition += heredocLength - 1; - currentCharacter = source[currentPosition++]; - break; // do...while loop - } else { - endTag = true; - } - } - } while (true); - return TokenNameHEREDOC; - } - return TokenNameLEFT_SHIFT; + } while (true); + return TokenNameHEREDOC; + } + return TokenNameLEFT_SHIFT; } + currentPosition = oldPosition; return TokenNameLESS; } case '>' : @@ -1330,9 +1470,12 @@ public class Scanner implements IScanner, ITerminalSymbols { case '#' : case '/' : { + char startChar = currentCharacter; + if (getNextChar('=')) { + return TokenNameDIVIDE_EQUAL; + } int test; - if ((currentCharacter == '#') - || (test = getNextChar('/', '*')) == 0) { + if ((startChar == '#') || (test = getNextChar('/', '*')) == 0) { //line comment int endPositionForLineComment = 0; try { //get the next char @@ -1546,8 +1689,6 @@ public class Scanner implements IScanner, ITerminalSymbols { } break; } - if (getNextChar('=')) - return TokenNameDIVIDE_EQUAL; return TokenNameDIVIDE; } case '\u001a' : @@ -1558,13 +1699,20 @@ public class Scanner implements IScanner, ITerminalSymbols { throw new InvalidInputException("Ctrl-Z"); //$NON-NLS-1$ default : if (currentCharacter == '$') { - while ((currentCharacter = source[currentPosition++]) == '$') { + int oldPosition = currentPosition; + try { + currentCharacter = source[currentPosition++]; + + if (isPHPIdentifierStart(currentCharacter)) { + return scanIdentifierOrKeyword(true); + } else { + currentPosition = oldPosition; + return TokenNameDOLLAR; + } + } catch (IndexOutOfBoundsException e) { + currentPosition = oldPosition; + return TokenNameDOLLAR; } - if (currentCharacter == '{') - return TokenNameDOLLAR_LBRACE; - if (isPHPIdentifierStart(currentCharacter)) - return scanIdentifierOrKeyword(true); - return TokenNameERROR; } if (isPHPIdentifierStart(currentCharacter)) return scanIdentifierOrKeyword(false); @@ -2508,9 +2656,9 @@ public class Scanner implements IScanner, ITerminalSymbols { while (getNextCharAsJavaIdentifierPart()) { }; if (isVariable) { - if (new String(getCurrentTokenSource()).equals("$this")) { - return TokenNamethis; - } + // if (new String(getCurrentTokenSource()).equals("$this")) { + // return TokenNamethis; + // } return TokenNameVariable; } int index, length; @@ -2600,7 +2748,7 @@ public class Scanner implements IScanner, ITerminalSymbols { case 3 : //and if ((data[++index] == 'n') && (data[++index] == 'd')) { - return TokenNameAND; + return TokenNameand; } else { return TokenNameIdentifier; } @@ -2635,7 +2783,7 @@ public class Scanner implements IScanner, ITerminalSymbols { return TokenNameIdentifier; } case 'c' : - //case catch class const continue + //case catch class clone const continue switch (length) { case 4 : if ((data[++index] == 'a') && (data[++index] == 's') @@ -2647,10 +2795,16 @@ public class Scanner implements IScanner, ITerminalSymbols { if ((data[++index] == 'a') && (data[++index] == 't') && (data[++index] == 'c') && (data[++index] == 'h')) return TokenNamecatch; - if ((data[index] == 'l') && (data[++index] == 'a') + index = 0; + if ((data[++index] == 'l') && (data[++index] == 'a') && (data[++index] == 's') && (data[++index] == 's')) return TokenNameclass; - if ((data[index] == 'o') && (data[++index] == 'n') + index = 0; + if ((data[++index] == 'l') && (data[++index] == 'o') + && (data[++index] == 'n') && (data[++index] == 'e')) + return TokenNameclone; + index = 0; + if ((data[++index] == 'o') && (data[++index] == 'n') && (data[++index] == 's') && (data[++index] == 't')) return TokenNameconst; else @@ -2793,9 +2947,9 @@ public class Scanner implements IScanner, ITerminalSymbols { else return TokenNameIdentifier; case 5 : -// if ((data[++index] == 'a') && (data[++index] == 'l') -// && (data[++index] == 's') && (data[++index] == 'e')) -// return TokenNamefalse; + // if ((data[++index] == 'a') && (data[++index] == 'l') + // && (data[++index] == 's') && (data[++index] == 'e')) + // return TokenNamefalse; if ((data[++index] == 'i') && (data[++index] == 'n') && (data[++index] == 'a') && (data[++index] == 'l')) return TokenNamefinal; @@ -2912,12 +3066,12 @@ public class Scanner implements IScanner, ITerminalSymbols { return TokenNamenew; else return TokenNameIdentifier; -// case 4 : -// if ((data[++index] == 'u') && (data[++index] == 'l') -// && (data[++index] == 'l')) -// return TokenNamenull; -// else -// return TokenNameIdentifier; + // case 4 : + // if ((data[++index] == 'u') && (data[++index] == 'l') + // && (data[++index] == 'l')) + // return TokenNamenull; + // else + // return TokenNameIdentifier; default : return TokenNameIdentifier; } @@ -2925,7 +3079,7 @@ public class Scanner implements IScanner, ITerminalSymbols { // or old_function if (length == 2) { if (data[++index] == 'r') { - return TokenNameOR; + return TokenNameor; } } // if (length == 12) { @@ -3029,12 +3183,12 @@ public class Scanner implements IScanner, ITerminalSymbols { return TokenNametry; else return TokenNameIdentifier; -// case 4 : -// if ((data[++index] == 'r') && (data[++index] == 'u') -// && (data[++index] == 'e')) -// return TokenNametrue; -// else -// return TokenNameIdentifier; + // case 4 : + // if ((data[++index] == 'r') && (data[++index] == 'u') + // && (data[++index] == 'e')) + // return TokenNametrue; + // else + // return TokenNameIdentifier; case 5 : if ((data[++index] == 'h') && (data[++index] == 'r') && (data[++index] == 'o') && (data[++index] == 'w')) @@ -3095,7 +3249,7 @@ public class Scanner implements IScanner, ITerminalSymbols { switch (length) { case 3 : if ((data[++index] == 'o') && (data[++index] == 'r')) - return TokenNameXOR; + return TokenNamexor; else return TokenNameIdentifier; default : @@ -3325,6 +3479,8 @@ public class Scanner implements IScanner, ITerminalSymbols { return "Variable(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$ case TokenNameabstract : return "abstract"; //$NON-NLS-1$ + case TokenNameand : + return "AND"; //$NON-NLS-1$ case TokenNamearray : return "array"; //$NON-NLS-1$ case TokenNameas : @@ -3335,6 +3491,12 @@ public class Scanner implements IScanner, ITerminalSymbols { return "case"; //$NON-NLS-1$ case TokenNameclass : return "class"; //$NON-NLS-1$ + case TokenNameclone : + //$NON-NLS-1$ + return "clone"; + case TokenNameconst : + //$NON-NLS-1$ + return "const"; case TokenNamecontinue : return "continue"; //$NON-NLS-1$ case TokenNamedefault : @@ -3361,8 +3523,8 @@ public class Scanner implements IScanner, ITerminalSymbols { return "endwhile"; //$NON-NLS-1$ case TokenNameextends : return "extends"; //$NON-NLS-1$ -// case TokenNamefalse : -// return "false"; //$NON-NLS-1$ + // case TokenNamefalse : + // return "false"; //$NON-NLS-1$ case TokenNamefinal : return "final"; //$NON-NLS-1$ case TokenNamefor : @@ -3383,12 +3545,16 @@ public class Scanner implements IScanner, ITerminalSymbols { return "include_once"; //$NON-NLS-1$ case TokenNameinterface : return "interface"; //$NON-NLS-1$ + case TokenNameisset : + return "isset"; //$NON-NLS-1$ case TokenNamelist : return "list"; //$NON-NLS-1$ case TokenNamenew : return "new"; //$NON-NLS-1$ -// case TokenNamenull : -// return "null"; //$NON-NLS-1$ + // case TokenNamenull : + // return "null"; //$NON-NLS-1$ + case TokenNameor : + return "OR"; //$NON-NLS-1$ case TokenNameprint : return "print"; //$NON-NLS-1$ case TokenNameprivate : @@ -3407,16 +3573,18 @@ public class Scanner implements IScanner, ITerminalSymbols { return "static"; //$NON-NLS-1$ case TokenNameswitch : return "switch"; //$NON-NLS-1$ -// case TokenNametrue : -// return "true"; //$NON-NLS-1$ + // case TokenNametrue : + // return "true"; //$NON-NLS-1$ case TokenNameunset : return "unset"; //$NON-NLS-1$ case TokenNamevar : return "var"; //$NON-NLS-1$ case TokenNamewhile : return "while"; //$NON-NLS-1$ - case TokenNamethis : - return "$this"; //$NON-NLS-1$ + case TokenNamexor : + return "XOR"; //$NON-NLS-1$ + // case TokenNamethis : + // return "$this"; //$NON-NLS-1$ case TokenNameIntegerLiteral : return "Integer(" + new String(getCurrentTokenSource()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$ case TokenNameDoubleLiteral : @@ -3468,6 +3636,8 @@ public class Scanner implements IScanner, ITerminalSymbols { return "^="; //$NON-NLS-1$ case TokenNameREMAINDER_EQUAL : return "%="; //$NON-NLS-1$ + case TokenNameDOT_EQUAL : + return ".="; //$NON-NLS-1$ case TokenNameLEFT_SHIFT_EQUAL : return "<<="; //$NON-NLS-1$ case TokenNameRIGHT_SHIFT_EQUAL : @@ -3530,8 +3700,10 @@ public class Scanner implements IScanner, ITerminalSymbols { return "="; //$NON-NLS-1$ case TokenNameAT : return "@"; - case TokenNameDOLLAR_LBRACE : - return "${"; + case TokenNameDOLLAR : + return "$"; + // case TokenNameDOLLAR_LBRACE : + // return "${"; case TokenNameEOF : return "EOF"; //$NON-NLS-1$ case TokenNameWHITESPACE : @@ -3553,7 +3725,17 @@ public class Scanner implements IScanner, ITerminalSymbols { case TokenNameMETHOD_C : return "__METHOD__"; //$NON-NLS-1$ case TokenNameFUNC_C : - return "__FUNCTION__"; //$NON-NLS-1$ + return "__FUNCTION__"; //$NON-NLS-1 + case TokenNameboolCAST : + return "( bool )"; //$NON-NLS-1$ + case TokenNameintCAST : + return "( int )"; //$NON-NLS-1$ + case TokenNamedoubleCAST : + return "( double )"; //$NON-NLS-1$ + case TokenNameobjectCAST : + return "( object )"; //$NON-NLS-1$ + case TokenNamestringCAST : + return "( string )"; //$NON-NLS-1$ default : return "not-a-token(" + (new Integer(act)) + ") " + new String(getCurrentTokenSource()); //$NON-NLS-1$ diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/problem/ProblemReporter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/problem/ProblemReporter.java index 7e1acce..3b07fa2 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/problem/ProblemReporter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/problem/ProblemReporter.java @@ -2697,7 +2697,7 @@ public class ProblemReporter extends ProblemHandler implements ProblemReasons { // case Scanner.TokenNamestrictfp: // case Scanner.TokenNamesynchronized: case Scanner.TokenNametry : - case Scanner.TokenNamethis : +// case Scanner.TokenNamethis : // case Scanner.TokenNametrue : case Scanner.TokenNamethrow : // case Scanner.TokenNamethrows: diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java index ecfed39..e48c07c 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java @@ -9,14 +9,12 @@ * IBM Corporation - initial API and implementation ******************************************************************************/ package net.sourceforge.phpdt.internal.formatter; - import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.util.Hashtable; import java.util.Locale; import java.util.Map; - import net.sourceforge.phpdt.core.ICodeFormatter; import net.sourceforge.phpdt.core.compiler.CharOperation; import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; @@ -25,104 +23,85 @@ import net.sourceforge.phpdt.internal.compiler.ConfigurableOption; import net.sourceforge.phpdt.internal.compiler.parser.Scanner; import net.sourceforge.phpdt.internal.formatter.impl.FormatterOptions; import net.sourceforge.phpdt.internal.formatter.impl.SplitLine; - -/**

How to format a piece of code ?

- *
  • Create an instance of CodeFormatter - *
  • Use the method void format(aString) - * on this instance to format aString. - * It will return the formatted string.
-*/ +/** + *

How to format a piece of code ?

+ *
    + *
  • Create an instance of CodeFormatter + *
  • Use the method void format(aString) on this instance to + * format aString. It will return the formatted string. + *
+ */ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { - public FormatterOptions options; - - /** + /** * Represents a block in the constructions stack. */ public static final int BLOCK = ITerminalSymbols.TokenNameLBRACE; - - /** - * Represents a block following a control statement in the constructions stack. + /** + * Represents a block following a control statement in the constructions + * stack. */ public static final int NONINDENT_BLOCK = -100; - - /** + /** * Contains the formatted output. */ StringBuffer formattedSource; - - /** - * Contains the current line.
+ /** + * Contains the current line.
* Will be dumped at the next "newline" */ StringBuffer currentLineBuffer; - - /** + /** * Used during the formatting to get each token. */ Scanner scanner; - - /** - * Contains the tokens responsible for the current indentation level - * and the blocks not closed yet. + /** + * Contains the tokens responsible for the current indentation level and the + * blocks not closed yet. */ private int[] constructions; - - /** + /** * Index in the constructions array. */ private int constructionsCount; - - /** - * Level of indentation of the current token (number of tab char put in front of it). + /** + * Level of indentation of the current token (number of tab char put in front + * of it). */ private int indentationLevel; - - /** + /** * Regular level of indentation of all the lines */ private int initialIndentationLevel; - - /** + /** * Used to split a line. */ Scanner splitScanner; - - /** - * To remember the offset between the beginning of the line and the - * beginning of the comment. + /** + * To remember the offset between the beginning of the line and the beginning + * of the comment. */ int currentCommentOffset; int currentLineIndentationLevel; int maxLineSize = 30; private boolean containsOpenCloseBraces; private int indentationLevelForOpenCloseBraces; - /** * Collections of positions to map */ private int[] positionsToMap; - /** * Collections of mapped positions */ private int[] mappedPositions; - private int indexToMap; - private int indexInMap; - private int globalDelta; - private int lineDelta; - private int splitDelta; - private int beginningOfLineIndex; - private int multipleLineCommentCounter; - - /** + /** * Creates a new instance of Code Formatter using the given settings. * * @deprecated backport 1.0 internal functionality @@ -130,47 +109,42 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { public CodeFormatter(ConfigurableOption[] settings) { this(convertConfigurableOptions(settings)); } - - /** - * Creates a new instance of Code Formatter using the FormattingOptions object - * given as argument + /** + * Creates a new instance of Code Formatter using the FormattingOptions + * object given as argument + * * @deprecated Use CodeFormatter(ConfigurableOption[]) instead */ public CodeFormatter() { this((Map) null); } - /** + /** * Creates a new instance of Code Formatter using the given settings. */ public CodeFormatter(Map settings) { - // initialize internal state constructionsCount = 0; constructions = new int[10]; currentLineIndentationLevel = indentationLevel = initialIndentationLevel; currentCommentOffset = -1; - // initialize primary and secondary scanners - scanner = new Scanner(true /*comment*/ - , true /*whitespace*/ - , false /*nls*/ - , false /*assert*/ + scanner = new Scanner(true /* comment */ + , true /* whitespace */ + , false /* nls */ + , false /* assert */ ); // regular scanner for forming lines scanner.recordLineSeparator = true; - // to remind of the position of the beginning of the line. - splitScanner = new Scanner(true /*comment*/ - , true /*whitespace*/ - , false /*nls*/ - , false /*assert*/ + splitScanner = new Scanner(true /* comment */ + , true /* whitespace */ + , false /* nls */ + , false /* assert */ ); // secondary scanner to split long lines formed by primary scanning - // initialize current line buffer currentLineBuffer = new StringBuffer(); this.options = new FormatterOptions(settings); } - /** * Returns true if a lineSeparator has to be inserted before operator * false otherwise. @@ -185,52 +159,51 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { return true; } } - - /** - * @deprecated backport 1.0 internal functionality - */ + /** + * @deprecated backport 1.0 internal functionality + */ private static Map convertConfigurableOptions(ConfigurableOption[] settings) { Hashtable options = new Hashtable(10); - for (int i = 0; i < settings.length; i++) { if (settings[i].getComponentName().equals(CodeFormatter.class.getName())) { String optionName = settings[i].getOptionName(); int valueIndex = settings[i].getCurrentValueIndex(); - if (optionName.equals("newline.openingBrace")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.newline.openingBrace", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put( + "net.sourceforge.phpdt.core.formatter.newline.openingBrace", + valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("newline.controlStatement")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.newline.controlStatement", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put( + "net.sourceforge.phpdt.core.formatter.newline.controlStatement", + valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("newline.clearAll")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.newline.clearAll", valueIndex == 0 ? "clear all" : "preserve one"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put("net.sourceforge.phpdt.core.formatter.newline.clearAll", + valueIndex == 0 ? "clear all" : "preserve one"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("newline.elseIf")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.newline.elseIf", valueIndex == 0 ? "do not insert" : "insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put("net.sourceforge.phpdt.core.formatter.newline.elseIf", + valueIndex == 0 ? "do not insert" : "insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("newline.emptyBlock")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.newline.emptyBlock", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put( + "net.sourceforge.phpdt.core.formatter.newline.emptyBlock", + valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("lineSplit")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.lineSplit", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$ - + options.put("net.sourceforge.phpdt.core.formatter.lineSplit", String + .valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$ } else if (optionName.equals("style.assignment")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.style.assignment", valueIndex == 0 ? "compact" : "normal"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put("net.sourceforge.phpdt.core.formatter.style.assignment", + valueIndex == 0 ? "compact" : "normal"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("tabulation.char")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.tabulation.char", valueIndex == 0 ? "tab" : "space"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + options.put("net.sourceforge.phpdt.core.formatter.tabulation.char", + valueIndex == 0 ? "tab" : "space"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } else if (optionName.equals("tabulation.size")) { //$NON-NLS-1$ - options.put("net.sourceforge.phpdt.core.formatter.tabulation.size", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$ + options.put("net.sourceforge.phpdt.core.formatter.tabulation.size", + String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$ } } } - return options; } - - /** + /** * Returns the end of the source code. */ private final String copyRemainingSource() { @@ -243,9 +216,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return (bufr.toString()); } - /** - * Inserts tabCount tab character or their equivalent number of spaces. + * Inserts tabCount tab character or their equivalent number + * of spaces. */ private void dumpTab(int tabCount) { if (options.indentWithTab) { @@ -260,7 +233,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } } - /** * Dumps currentLineBuffer into the formatted string. */ @@ -270,14 +242,17 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { beginningOfLineIndex = formattedSource.length(); if (containsOpenCloseBraces) { containsOpenCloseBraces = false; - outputLine(currentString, false, indentationLevelForOpenCloseBraces, 0, -1, null, 0); + outputLine(currentString, false, indentationLevelForOpenCloseBraces, 0, + -1, null, 0); indentationLevelForOpenCloseBraces = currentLineIndentationLevel; } else { - outputLine(currentString, false, currentLineIndentationLevel, 0, -1, null, 0); + outputLine(currentString, false, currentLineIndentationLevel, 0, -1, + null, 0); } int scannerSourceLength = scanner.source.length; if (scannerSourceLength > 2) { - if (scanner.source[scannerSourceLength - 1] == '\n' && scanner.source[scannerSourceLength - 2] == '\r') { + if (scanner.source[scannerSourceLength - 1] == '\n' + && scanner.source[scannerSourceLength - 2] == '\r') { formattedSource.append(options.lineSeparatorSequence); increaseGlobalDelta(options.lineSeparatorSequence.length - 2); } else if (scanner.source[scannerSourceLength - 1] == '\n') { @@ -290,8 +265,7 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } updateMappedPositions(scanner.startPosition); } - - /** + /** * Formats the input string. */ private void format() { @@ -300,7 +274,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { int previousCompilableToken = 0; int indentationOffset = 0; int newLinesInWhitespace = 0; - // number of new lines in the previous whitespace token // (used to leave blank lines before comments) int pendingNewLines = 0; @@ -315,36 +288,32 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { boolean inThrowsClause = false; boolean inClassOrInterfaceHeader = false; int dollarBraceCount = 0; - - // openBracketCount is used to count the number of open brackets not closed yet. + // openBracketCount is used to count the number of open brackets not closed + // yet. int openBracketCount = 0; int unarySignModifier = 0; - - // openParenthesis[0] is used to count the parenthesis not belonging to a condition + // openParenthesis[0] is used to count the parenthesis not belonging to a + // condition // (eg foo();). parenthesis in for (...) are count elsewhere in the array. int openParenthesisCount = 1; int[] openParenthesis = new int[10]; - - // tokenBeforeColon is used to know what token goes along with the current : + // tokenBeforeColon is used to know what token goes along with the current + // : // it can be case or ? int tokenBeforeColonCount = 0; int[] tokenBeforeColon = new int[10]; - constructionsCount = 0; // initializes the constructions count. - // contains DO if in a DO..WHILE statement, UNITIALIZED otherwise. int nlicsToken = 0; - - // fix for 1FF17XY: LFCOM:ALL - Format problem on not matching } and else + // fix for 1FF17XY: LFCOM:ALL - Format problem on not matching } and else boolean specialElse = false; - // OPTION (IndentationLevel): initial indentation level may be non-zero. currentLineIndentationLevel += constructionsCount; - - // An InvalidInputException exception might cause the termination of this loop. + // An InvalidInputException exception might cause the termination of this + // loop. try { while (true) { - // Get the next token. Catch invalid input and output it + // Get the next token. Catch invalid input and output it // with minimal formatting, also catch end of input and // exit the loop. try { @@ -352,11 +321,10 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (Scanner.DEBUG) { int currentEndPosition = scanner.getCurrentTokenEndPosition(); int currentStartPosition = scanner.getCurrentTokenStartPosition(); - - System.out.print(currentStartPosition + "," + currentEndPosition + ": "); + System.out.print(currentStartPosition + "," + currentEndPosition + + ": "); System.out.println(scanner.toStringAction(token)); } - // Patch for line comment // See PR http://dev.eclipse.org/bugs/show_bug.cgi?id=23096 if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) { @@ -380,28 +348,30 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } if (token == Scanner.TokenNameEOF) break; - - /* ## MODIFYING the indentation level before generating new lines - and indentation in the output string - */ - - // Removes all the indentations made by statements not followed by a block - // except if the current token is ELSE, CATCH or if we are in a switch/case + /* + * ## MODIFYING the indentation level before generating new lines and + * indentation in the output string + */ + // Removes all the indentations made by statements not followed by a + // block + // except if the current token is ELSE, CATCH or if we are in a + // switch/case if (clearNonBlockIndents && (token != Scanner.TokenNameWHITESPACE)) { switch (token) { case TokenNameelse : - if (constructionsCount > 0 && constructions[constructionsCount - 1] == TokenNameelse) { + if (constructionsCount > 0 + && constructions[constructionsCount - 1] == TokenNameelse) { pendingNewLines = 1; specialElse = true; } indentationLevel += popInclusiveUntil(TokenNameif); break; - // case TokenNamecatch : - // indentationLevel += popInclusiveUntil(TokenNamecatch); - // break; - // case TokenNamefinally : - // indentationLevel += popInclusiveUntil(TokenNamecatch); - // break; + // case TokenNamecatch : + // indentationLevel += popInclusiveUntil(TokenNamecatch); + // break; + // case TokenNamefinally : + // indentationLevel += popInclusiveUntil(TokenNamecatch); + // break; case TokenNamewhile : if (nlicsToken == TokenNamedo) { indentationLevel += pop(TokenNamedo); @@ -409,9 +379,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } default : indentationLevel += popExclusiveUntilBlockOrCase(); - // clear until a CASE, DEFAULT or BLOCK is encountered. - // Thus, the indentationLevel is correctly cleared either - // in a switch/case statement or in any other situation. + // clear until a CASE, DEFAULT or BLOCK is encountered. + // Thus, the indentationLevel is correctly cleared either + // in a switch/case statement or in any other situation. } clearNonBlockIndents = false; } @@ -423,15 +393,16 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // if (token == Scanner.TokenNamethrows) { // inThrowsClause = true; // } - if ((token == Scanner.TokenNameclass // || token == Scanner.TokenNameinterface - ) - && previousToken != Scanner.TokenNameDOT) { + if ((token == Scanner.TokenNameclass // || token == + // Scanner.TokenNameinterface + ) && previousToken != Scanner.TokenNameDOT) { inClassOrInterfaceHeader = true; } - - /* ## APPEND newlines and indentations to the output string - */ - // Do not add a new line between ELSE and IF, if the option elseIfOnSameLine is true. + /* + * ## APPEND newlines and indentations to the output string + */ + // Do not add a new line between ELSE and IF, if the option + // elseIfOnSameLine is true. // Fix for 1ETLWPZ: IVJCOM:ALL - incorrect "else if" formatting // if (pendingNewlineAfterParen // && previousCompilableToken == TokenNameelse @@ -447,30 +418,32 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // Add a newline & indent to the formatted source string if // a for/if-else/while statement was scanned and there is no block // following it. - pendingNewlineAfterParen = - pendingNewlineAfterParen || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE); + pendingNewlineAfterParen = pendingNewlineAfterParen + || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE); if (pendingNewlineAfterParen && token != Scanner.TokenNameWHITESPACE) { pendingNewlineAfterParen = false; - // Do to add a newline & indent sequence if the current token is an - // open brace or a period or if the current token is a semi-colon and the + // open brace or a period or if the current token is a semi-colon and + // the // previous token is a close paren. // add a new line if a parenthesis belonging to a for() statement // has been closed and the current token is not an opening brace if (token != TokenNameLBRACE - && !isComment(token) // to avoid adding new line between else and a comment - && token != TokenNameDOT - && !(previousCompilableToken == TokenNameRPAREN && token == TokenNameSEMICOLON)) { + && !isComment(token) + // to avoid adding new line between else and a comment + && token != TokenNameDOT + && !(previousCompilableToken == TokenNameRPAREN && token == TokenNameSEMICOLON)) { newLine(1); currentLineIndentationLevel = indentationLevel; pendingNewLines = 0; pendingSpace = false; } else { - if (token == TokenNameLBRACE && options.newLineBeforeOpeningBraceMode) { + if (token == TokenNameLBRACE + && options.newLineBeforeOpeningBraceMode) { newLine(1); if (constructionsCount > 0 - && constructions[constructionsCount - 1] != BLOCK - && constructions[constructionsCount - 1] != NONINDENT_BLOCK) { + && constructions[constructionsCount - 1] != BLOCK + && constructions[constructionsCount - 1] != NONINDENT_BLOCK) { currentLineIndentationLevel = indentationLevel - 1; } else { currentLineIndentationLevel = indentationLevel; @@ -480,10 +453,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } } - if (token == TokenNameLBRACE - && options.newLineBeforeOpeningBraceMode - && constructionsCount > 0 - && constructions[constructionsCount - 1] == TokenNamedo) { + if (token == TokenNameLBRACE && options.newLineBeforeOpeningBraceMode + && constructionsCount > 0 + && constructions[constructionsCount - 1] == TokenNamedo) { newLine(1); currentLineIndentationLevel = indentationLevel - 1; pendingNewLines = 0; @@ -512,59 +484,63 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // Add pending new lines to the formatted source string. // Note: pending new lines are not added if the current token // is a single line comment or whitespace. - // if the comment is between parenthesis, there is no blank line preservation + // if the comment is between parenthesis, there is no blank line + // preservation // (if it's a one-line comment, a blank line is added after it). if (((pendingNewLines > 0 && (!isComment(token))) - || (newLinesInWhitespace > 0 && (openParenthesisCount <= 1 && isComment(token))) - || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE)) - && token != Scanner.TokenNameWHITESPACE) { - + || (newLinesInWhitespace > 0 && (openParenthesisCount <= 1 && isComment(token))) || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE)) + && token != Scanner.TokenNameWHITESPACE) { // Do not add newline & indent between an adjoining close brace and - // close paren. Anonymous inner classes may use this form. - boolean closeBraceAndCloseParen = previousToken == TokenNameRBRACE && token == TokenNameRPAREN; - + // close paren. Anonymous inner classes may use this form. + boolean closeBraceAndCloseParen = previousToken == TokenNameRBRACE + && token == TokenNameRPAREN; // OPTION (NewLineInCompoundStatement): do not add newline & indent // between close brace and else, (do) while, catch, and finally if // newlineInCompoundStatement is true. - boolean nlicsOption = - previousToken == TokenNameRBRACE + boolean nlicsOption = previousToken == TokenNameRBRACE && !options.newlineInControlStatementMode && (token == TokenNameelse || (token == TokenNamewhile && nlicsToken == TokenNamedo)); // || token == TokenNamecatch // || token == TokenNamefinally); - - // Do not add a newline & indent between a close brace and semi-colon. - boolean semiColonAndCloseBrace = previousToken == TokenNameRBRACE && token == TokenNameSEMICOLON; - - // Do not add a new line & indent between a multiline comment and a opening brace - boolean commentAndOpenBrace = previousToken == Scanner.TokenNameCOMMENT_BLOCK && token == TokenNameLBRACE; - - // Do not add a newline & indent between a close brace and a colon (in array assignments, for example). - boolean commaAndCloseBrace = previousToken == TokenNameRBRACE && token == TokenNameCOMMA; - + // Do not add a newline & indent between a close brace and + // semi-colon. + boolean semiColonAndCloseBrace = previousToken == TokenNameRBRACE + && token == TokenNameSEMICOLON; + // Do not add a new line & indent between a multiline comment and a + // opening brace + boolean commentAndOpenBrace = previousToken == Scanner.TokenNameCOMMENT_BLOCK + && token == TokenNameLBRACE; + // Do not add a newline & indent between a close brace and a colon + // (in array assignments, for example). + boolean commaAndCloseBrace = previousToken == TokenNameRBRACE + && token == TokenNameCOMMA; // Add a newline and indent, if appropriate. if (specialElse - || (!commentAndOpenBrace && !closeBraceAndCloseParen && !nlicsOption && !semiColonAndCloseBrace && !commaAndCloseBrace)) { - + || (!commentAndOpenBrace && !closeBraceAndCloseParen + && !nlicsOption && !semiColonAndCloseBrace && !commaAndCloseBrace)) { // if clearAllBlankLinesMode=false, leaves the blank lines // inserted by the user // if clearAllBlankLinesMode=true, removes all of then // and insert only blank lines required by the formatting. if (!options.clearAllBlankLinesMode) { // (isComment(token)) - pendingNewLines = (pendingNewLines < newLinesInWhitespace) ? newLinesInWhitespace : pendingNewLines; + pendingNewLines = (pendingNewLines < newLinesInWhitespace) + ? newLinesInWhitespace + : pendingNewLines; pendingNewLines = (pendingNewLines > 2) ? 2 : pendingNewLines; } - if (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE) { + if (previousCompilableToken == TokenNameLBRACE + && token == TokenNameRBRACE) { containsOpenCloseBraces = true; indentationLevelForOpenCloseBraces = currentLineIndentationLevel; if (isComment(previousToken)) { newLine(pendingNewLines); } else { - /* if (!(constructionsCount > 1 - && constructions[constructionsCount-1] == NONINDENT_BLOCK - && (constructions[constructionsCount-2] == TokenNamefor - || constructions[constructionsCount-2] == TokenNamewhile))) {*/ + /* + * if (!(constructionsCount > 1 && + * constructions[constructionsCount-1] == NONINDENT_BLOCK && + * (constructions[constructionsCount-2] == TokenNamefor + */ if (options.newLineInEmptyBlockMode) { if (inArrayAssignment) { newLine(1); // array assigment with an empty block @@ -575,17 +551,16 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // } } } else { - // see PR 1FKKC3U: LFCOM:WINNT - Format problem with a comment before the ';' - if (!((previousToken == Scanner.TokenNameCOMMENT_BLOCK || previousToken == Scanner.TokenNameCOMMENT_PHPDOC) - && token == TokenNameSEMICOLON)) { + // see PR 1FKKC3U: LFCOM:WINNT - Format problem with a comment + // before the ';' + if (!((previousToken == Scanner.TokenNameCOMMENT_BLOCK || previousToken == Scanner.TokenNameCOMMENT_PHPDOC) && token == TokenNameSEMICOLON)) { newLine(pendingNewLines); } } if (((previousCompilableToken == TokenNameSEMICOLON) - || (previousCompilableToken == TokenNameLBRACE) - || (previousCompilableToken == TokenNameRBRACE) - || (isComment(previousToken))) - && (token == TokenNameRBRACE)) { + || (previousCompilableToken == TokenNameLBRACE) + || (previousCompilableToken == TokenNameRBRACE) || (isComment(previousToken))) + && (token == TokenNameRBRACE)) { indentationOffset = -1; indentationLevel += popExclusiveUntilBlock(); } @@ -593,7 +568,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // PR 1FI5IPO currentLineIndentationLevel++; } else { - currentLineIndentationLevel = indentationLevel + indentationOffset; + currentLineIndentationLevel = indentationLevel + + indentationOffset; } pendingSpace = false; indentationOffset = 0; @@ -601,15 +577,14 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingNewLines = 0; newLinesInWhitespace = 0; specialElse = false; - if (nlicsToken == TokenNamedo && token == TokenNamewhile) { nlicsToken = 0; } } switch (token) { - case TokenNameDOLLAR_LBRACE : - dollarBraceCount++; - break; + // case TokenNameDOLLAR : + // dollarBraceCount++; + // break; case TokenNameelse : // case TokenNamefinally : expectingOpenBrace = true; @@ -619,24 +594,18 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNamecase : case TokenNamedefault : if (tokenBeforeColonCount == tokenBeforeColon.length) { - System.arraycopy( - tokenBeforeColon, - 0, - (tokenBeforeColon = new int[tokenBeforeColonCount * 2]), - 0, - tokenBeforeColonCount); + System.arraycopy(tokenBeforeColon, 0, + (tokenBeforeColon = new int[tokenBeforeColonCount * 2]), 0, + tokenBeforeColonCount); } tokenBeforeColon[tokenBeforeColonCount++] = TokenNamecase; indentationLevel += pushControlStatement(TokenNamecase); break; case TokenNameQUESTION : if (tokenBeforeColonCount == tokenBeforeColon.length) { - System.arraycopy( - tokenBeforeColon, - 0, - (tokenBeforeColon = new int[tokenBeforeColonCount * 2]), - 0, - tokenBeforeColonCount); + System.arraycopy(tokenBeforeColon, 0, + (tokenBeforeColon = new int[tokenBeforeColonCount * 2]), 0, + tokenBeforeColonCount); } tokenBeforeColon[tokenBeforeColonCount++] = token; break; @@ -645,23 +614,24 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameif : case TokenNamewhile : if (openParenthesisCount == openParenthesis.length) { - System.arraycopy(openParenthesis, 0, (openParenthesis = new int[openParenthesisCount * 2]), 0, openParenthesisCount); + System.arraycopy(openParenthesis, 0, + (openParenthesis = new int[openParenthesisCount * 2]), 0, + openParenthesisCount); } openParenthesis[openParenthesisCount++] = 0; expectingOpenBrace = true; - indentationLevel += pushControlStatement(token); break; - // case TokenNametry : - // pendingNewlineAfterParen = true; - // case TokenNamecatch : - // // several CATCH statements can be contiguous. - // // a CATCH is encountered pop until first CATCH (if a CATCH follows a TRY it works the same way, - // // as CATCH and TRY are the same token in the stack). - // expectingOpenBrace = true; - // indentationLevel += pushControlStatement(TokenNamecatch); - // break; - + // case TokenNametry : + // pendingNewlineAfterParen = true; + // case TokenNamecatch : + // // several CATCH statements can be contiguous. + // // a CATCH is encountered pop until first CATCH (if a CATCH + // follows a TRY it works the same way, + // // as CATCH and TRY are the same token in the stack). + // expectingOpenBrace = true; + // indentationLevel += pushControlStatement(TokenNamecatch); + // break; case TokenNamedo : expectingOpenBrace = true; indentationLevel += pushControlStatement(token); @@ -678,14 +648,13 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // compliment (eg: !), semi-colon, open brace, close brace, // super, or this. if (previousCompilableToken != TokenNameLBRACKET - && previousToken != TokenNameIdentifier - && previousToken != 0 - && previousToken != TokenNameNOT - && previousToken != TokenNameLPAREN - && previousToken != TokenNameTWIDDLE - && previousToken != TokenNameSEMICOLON - && previousToken != TokenNameLBRACE - && previousToken != TokenNameRBRACE) { + && previousToken != TokenNameIdentifier && previousToken != 0 + && previousToken != TokenNameNOT + && previousToken != TokenNameLPAREN + && previousToken != TokenNameTWIDDLE + && previousToken != TokenNameSEMICOLON + && previousToken != TokenNameLBRACE + && previousToken != TokenNameRBRACE) { // && previousToken != TokenNamesuper // && previousToken != TokenNamethis) { space(); @@ -697,16 +666,16 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { openParenthesis[openParenthesisCount - 1]++; else openParenthesis[0]++; - pendingSpace = false; - //S } + //S } break; case TokenNameRPAREN : - // Decrease the parenthesis count // if there is no more unclosed parenthesis, - // a new line and indent may be append (depending on the next token). - if ((openParenthesisCount > 1) && (openParenthesis[openParenthesisCount - 1] > 0)) { + // a new line and indent may be append (depending on the next + // token). + if ((openParenthesisCount > 1) + && (openParenthesis[openParenthesisCount - 1] > 0)) { openParenthesis[openParenthesisCount - 1]--; if (openParenthesis[openParenthesisCount - 1] <= 0) { pendingNewlineAfterParen = true; @@ -719,7 +688,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingSpace = false; break; case TokenNameLBRACE : - if ((previousCompilableToken == TokenNameRBRACKET) || (previousCompilableToken == TokenNameEQUAL)) { + if (previousCompilableToken == TokenNameDOLLAR) { + dollarBraceCount++; + } + if ((previousCompilableToken == TokenNameRBRACKET) + || (previousCompilableToken == TokenNameEQUAL)) { // if (previousCompilableToken == TokenNameRBRACKET) { inArrayAssignment = true; inAssignment = false; @@ -747,23 +720,23 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } else { pendingNewLines = 1; indentationLevel += popInclusiveUntilBlock(); - if (previousCompilableToken == TokenNameRPAREN) { - // fix for 1FGDDV6: LFCOM:WIN98 - Weird splitting on message expression + // fix for 1FGDDV6: LFCOM:WIN98 - Weird splitting on message + // expression currentLineBuffer.append(options.lineSeparatorSequence); increaseLineDelta(options.lineSeparatorSequence.length); } if (constructionsCount > 0) { switch (constructions[constructionsCount - 1]) { case TokenNamefor : - //indentationLevel += popExclusiveUntilBlock(); - //break; + //indentationLevel += popExclusiveUntilBlock(); + //break; case TokenNameswitch : case TokenNameif : case TokenNameelse : - // case TokenNametry : - // case TokenNamecatch : - // case TokenNamefinally : + // case TokenNametry : + // case TokenNamecatch : + // case TokenNamefinally : case TokenNamewhile : case TokenNamedo : // case TokenNamesynchronized : @@ -780,7 +753,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { break; case TokenNameRBRACKET : openBracketCount -= (openBracketCount > 0) ? 1 : 0; - // if there is no left bracket to close, the right bracket is ignored. + // if there is no left bracket to close, the right bracket is + // ignored. pendingSpace = false; break; case TokenNameCOMMA : @@ -788,11 +762,12 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingSpace = false; break; case TokenNameSEMICOLON : - // Do not generate line terminators in the definition of // the for statement. - // if not in this case, jump a line and reduce indentation after the brace - // if the block it closes belongs to a conditional statement (if, while, do...). + // if not in this case, jump a line and reduce indentation after + // the brace + // if the block it closes belongs to a conditional statement (if, + // while, do...). if (openParenthesisCount <= 1) { pendingNewLines = 1; if (expectingOpenBrace) { @@ -805,21 +780,21 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { break; case TokenNamePLUS_PLUS : case TokenNameMINUS_MINUS : - // Do not put a space between a post-increment/decrement // and the identifier being modified. - if (previousToken == TokenNameIdentifier || previousToken == TokenNameRBRACKET) { + if (previousToken == TokenNameIdentifier + || previousToken == TokenNameRBRACKET) { pendingSpace = false; } break; - case TokenNamePLUS : // previously ADDITION + case TokenNamePLUS : + // previously ADDITION case TokenNameMINUS : - // Handle the unary operators plus and minus via a flag if (!isLiteralToken(previousToken) - && previousToken != TokenNameIdentifier - && previousToken != TokenNameRPAREN - && previousToken != TokenNameRBRACKET) { + && previousToken != TokenNameIdentifier + && previousToken != TokenNameRPAREN + && previousToken != TokenNameRBRACKET) { unarySignModifier = 1; } break; @@ -848,7 +823,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { pendingNewLines = 1; break; case Scanner.TokenNameWHITESPACE : - // Count the number of line terminators in the whitespace so // line spacing can be preserved near comments. char[] source = scanner.source; @@ -870,30 +844,29 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } increaseLineDelta(scanner.startPosition - scanner.currentPosition); break; - // case TokenNameHTML : - // // Add the next token to the formatted source string. - // // outputCurrentToken(token); - // int startPosition = scanner.startPosition; - // flushBuffer(); - // for (int i = startPosition, max = scanner.currentPosition; i < max; i++) { - // char currentCharacter = scanner.source[i]; - // updateMappedPositions(i); - // currentLineBuffer.append(currentCharacter); - // } - // break; + // case TokenNameHTML : + // // Add the next token to the formatted source string. + // // outputCurrentToken(token); + // int startPosition = scanner.startPosition; + // flushBuffer(); + // for (int i = startPosition, max = scanner.currentPosition; i < + // max; i++) { + // char currentCharacter = scanner.source[i]; + // updateMappedPositions(i); + // currentLineBuffer.append(currentCharacter); + // } + // break; default : if ((token == TokenNameIdentifier) || isLiteralToken(token)) { // || token == TokenNamesuper // || token == TokenNamethis) { - // Do not put a space between a unary operator // (eg: ++, --, +, -) and the identifier being modified. if (previousToken == TokenNamePLUS_PLUS - || previousToken == TokenNameMINUS_MINUS - || (previousToken == TokenNameMINUS_GREATER && - options.compactDereferencingMode) // -> - || (previousToken == TokenNamePLUS && unarySignModifier > 0) - || (previousToken == TokenNameMINUS && unarySignModifier > 0)) { + || previousToken == TokenNameMINUS_MINUS + || (previousToken == TokenNameMINUS_GREATER && options.compactDereferencingMode) // -> + || (previousToken == TokenNamePLUS && unarySignModifier > 0) + || (previousToken == TokenNameMINUS && unarySignModifier > 0)) { pendingSpace = false; } unarySignModifier = 0; @@ -902,35 +875,33 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } // Do not output whitespace tokens. if (token != Scanner.TokenNameWHITESPACE) { - - /* Add pending space to the formatted source string. - Do not output a space under the following circumstances: - 1) this is the first pass - 2) previous token is an open paren - 3) previous token is a period - 4) previous token is the logical compliment (eg: !) - 5) previous token is the bitwise compliment (eg: ~) - 6) previous token is the open bracket (eg: [) - 7) in an assignment statement, if the previous token is an - open brace or the current token is a close brace - 8) previous token is a single line comment - 9) current token is a '->' - */ - if (token == TokenNameMINUS_GREATER && - options.compactDereferencingMode) pendingSpace = false; - - boolean openAndCloseBrace = previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE; - + /* + * Add pending space to the formatted source string. Do not output a + * space under the following circumstances: 1) this is the first pass 2) + * previous token is an open paren 3) previous token is a period 4) + * previous token is the logical compliment (eg: !) 5) previous token + * is the bitwise compliment (eg: ~) 6) previous token is the open + * bracket (eg: [) 7) in an assignment statement, if the previous + * token is an open brace or the current token is a close brace 8) + * previous token is a single line comment 9) current token is a '->' + */ + if (token == TokenNameMINUS_GREATER + && options.compactDereferencingMode) + pendingSpace = false; + boolean openAndCloseBrace = previousCompilableToken == TokenNameLBRACE + && token == TokenNameRBRACE; if (pendingSpace - && insertSpaceAfter(previousToken) - && !(inAssignment && (previousToken == TokenNameLBRACE || token == TokenNameRBRACE)) - && previousToken != Scanner.TokenNameCOMMENT_LINE) { - if ((!(options.compactAssignmentMode && token == TokenNameEQUAL)) && !openAndCloseBrace) + && insertSpaceAfter(previousToken) + && !(inAssignment && (previousToken == TokenNameLBRACE || token == TokenNameRBRACE)) + && previousToken != Scanner.TokenNameCOMMENT_LINE) { + if ((!(options.compactAssignmentMode && token == TokenNameEQUAL)) + && !openAndCloseBrace) space(); } // Add the next token to the formatted source string. outputCurrentToken(token); - if (token == Scanner.TokenNameCOMMENT_LINE && openParenthesisCount > 1) { + if (token == Scanner.TokenNameCOMMENT_LINE + && openParenthesisCount > 1) { pendingNewLines = 0; currentLineBuffer.append(options.lineSeparatorSequence); increaseLineDelta(options.lineSeparatorSequence.length); @@ -941,8 +912,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (token != Scanner.TokenNameWHITESPACE) { previousToken = token; if (token != Scanner.TokenNameCOMMENT_BLOCK - && token != Scanner.TokenNameCOMMENT_LINE - && token != Scanner.TokenNameCOMMENT_PHPDOC) { + && token != Scanner.TokenNameCOMMENT_LINE + && token != Scanner.TokenNameCOMMENT_PHPDOC) { previousCompilableToken = token; } } @@ -956,10 +927,10 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // dump the last token of the source in the formatted output. } } - - /** - * Formats the char array sourceString, - * and returns a string containing the formatted version. + /** + * Formats the char array sourceString, and returns a string + * containing the formatted version. + * * @return the formatted ouput. */ public String formatSourceString(String sourceString) { @@ -969,32 +940,37 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { format(); return formattedSource.toString(); } - - /** - * Formats the char array sourceString, - * and returns a string containing the formatted version. - * @param string the string to format - * @param indentationLevel the initial indentation level + /** + * Formats the char array sourceString, and returns a string + * containing the formatted version. + * + * @param string + * the string to format + * @param indentationLevel + * the initial indentation level * @return the formatted ouput. */ public String format(String string, int indentationLevel) { return format(string, indentationLevel, (int[]) null); } - - /** - * Formats the char array sourceString, - * and returns a string containing the formatted version. - * The positions array is modified to contain the mapped positions. - * @param string the string to format - * @param indentationLevel the initial indentation level - * @param positions the array of positions to map + /** + * Formats the char array sourceString, and returns a string + * containing the formatted version. The positions array is modified to + * contain the mapped positions. + * + * @param string + * the string to format + * @param indentationLevel + * the initial indentation level + * @param positions + * the array of positions to map * @return the formatted ouput. */ public String format(String string, int indentationLevel, int[] positions) { return this.format(string, indentationLevel, positions, null); } - - public String format(String string, int indentationLevel, int[] positions, String lineSeparator) { + public String format(String string, int indentationLevel, int[] positions, + String lineSeparator) { if (lineSeparator != null) { this.options.setLineSeparator(lineSeparator); } @@ -1010,31 +986,32 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { return this.formatSourceString(string); } } - /** - * Formats the char array sourceString, - * and returns a string containing the formatted version. The initial indentation level is 0. - * @param string the string to format + /** + * Formats the char array sourceString, and returns a string + * containing the formatted version. The initial indentation level is 0. + * + * @param string + * the string to format * @return the formatted ouput. */ public String format(String string) { return this.format(string, 0, (int[]) null); } - - /** - * Formats a given source string, starting indenting it at a particular - * depth and using the given options + /** + * Formats a given source string, starting indenting it at a particular depth + * and using the given options * * @deprecated backport 1.0 internal functionality */ - public static String format(String sourceString, int initialIndentationLevel, ConfigurableOption[] options) { + public static String format(String sourceString, int initialIndentationLevel, + ConfigurableOption[] options) { CodeFormatter formatter = new CodeFormatter(options); formatter.setInitialIndentationLevel(initialIndentationLevel); return formatter.formatSourceString(sourceString); } - /** - * Returns the number of characters and tab char between the beginning of the line - * and the beginning of the comment. + * Returns the number of characters and tab char between the beginning of the + * line and the beginning of the comment. */ private int getCurrentCommentOffset() { int linePtr = scanner.linePtr; @@ -1045,7 +1022,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { int beginningOfLine = scanner.lineEnds[linePtr]; int currentStartPosition = scanner.startPosition; char[] source = scanner.source; - // find the position of the beginning of the line containing the comment while (beginningOfLine > currentStartPosition) { if (linePtr > 0) { @@ -1073,44 +1049,60 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return offset; } - /** - * Returns an array of descriptions for the configurable options. - * The descriptions may be changed and passed back to a different - * compiler. + * Returns an array of descriptions for the configurable options. The + * descriptions may be changed and passed back to a different compiler. * * @deprecated backport 1.0 internal functionality */ public static ConfigurableOption[] getDefaultOptions(Locale locale) { String componentName = CodeFormatter.class.getName(); FormatterOptions options = new FormatterOptions(); - return new ConfigurableOption[] { new ConfigurableOption(componentName, "newline.openingBrace", locale, options.newLineBeforeOpeningBraceMode ? 0 : 1), //$NON-NLS-1$ - new ConfigurableOption(componentName, "newline.controlStatement", locale, options.newlineInControlStatementMode ? 0 : 1), //$NON-NLS-1$ - new ConfigurableOption(componentName, "newline.clearAll", locale, options.clearAllBlankLinesMode ? 0 : 1), //$NON-NLS-1$ - // new ConfigurableOption(componentName, "newline.elseIf", locale, options.compactElseIfMode ? 0 : 1), //$NON-NLS-1$ - new ConfigurableOption(componentName, "newline.emptyBlock", locale, options.newLineInEmptyBlockMode ? 0 : 1), //$NON-NLS-1$ - new ConfigurableOption(componentName, "line.split", locale, options.maxLineLength), //$NON-NLS-1$ - new ConfigurableOption(componentName, "style.compactAssignment", locale, options.compactAssignmentMode ? 0 : 1), //$NON-NLS-1$ - new ConfigurableOption(componentName, "tabulation.char", locale, options.indentWithTab ? 0 : 1), //$NON-NLS-1$ - new ConfigurableOption(componentName, "tabulation.size", locale, options.tabSize) //$NON-NLS-1$ + return new ConfigurableOption[]{ + new ConfigurableOption(componentName, "newline.openingBrace", locale, + options.newLineBeforeOpeningBraceMode ? 0 : 1), + //$NON-NLS-1$ + new ConfigurableOption(componentName, "newline.controlStatement", + locale, options.newlineInControlStatementMode ? 0 : 1), + //$NON-NLS-1$ + new ConfigurableOption(componentName, "newline.clearAll", locale, + options.clearAllBlankLinesMode ? 0 : 1), + //$NON-NLS-1$ + // new ConfigurableOption(componentName, "newline.elseIf", locale, + // options.compactElseIfMode ? 0 : 1), //$NON-NLS-1$ + new ConfigurableOption(componentName, "newline.emptyBlock", locale, + options.newLineInEmptyBlockMode ? 0 : 1), + //$NON-NLS-1$ + new ConfigurableOption(componentName, "line.split", locale, + options.maxLineLength), + //$NON-NLS-1$ + new ConfigurableOption(componentName, "style.compactAssignment", + locale, options.compactAssignmentMode ? 0 : 1), + //$NON-NLS-1$ + new ConfigurableOption(componentName, "tabulation.char", locale, + options.indentWithTab ? 0 : 1), + //$NON-NLS-1$ + new ConfigurableOption(componentName, "tabulation.size", locale, + options.tabSize) //$NON-NLS-1$ }; } - /** - * Returns the array of mapped positions. - * Returns null is no positions have been set. + * Returns the array of mapped positions. Returns null is no positions have + * been set. + * * @return int[] * @deprecated There is no need to retrieve the mapped positions anymore. */ public int[] getMappedPositions() { return mappedPositions; } - /** - * Returns the priority of the token given as argument
+ * Returns the priority of the token given as argument
* The most prioritary the token is, the smallest the return value is. + * * @return the priority of token - * @param token the token of which the priority is requested + * @param token + * the token of which the priority is requested */ private static int getTokenPriority(int token) { switch (token) { @@ -1118,88 +1110,119 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // case TokenNameimplements : // case TokenNamethrows : return 10; - case TokenNameSEMICOLON : // ; + case TokenNameSEMICOLON : + // ; return 20; - case TokenNameCOMMA : // , + case TokenNameCOMMA : + // , return 25; - case TokenNameEQUAL : // = + case TokenNameEQUAL : + // = return 30; - case TokenNameAND_AND : // && - case TokenNameOR_OR : // || + case TokenNameAND_AND : + // && + case TokenNameOR_OR : + // || return 40; - case TokenNameQUESTION : // ? - case TokenNameCOLON : // : + case TokenNameQUESTION : + // ? + case TokenNameCOLON : + // : return 50; // it's better cutting on ?: than on ; - case TokenNameEQUAL_EQUAL : // == - case TokenNameEQUAL_EQUAL_EQUAL : // === - case TokenNameNOT_EQUAL : // != - case TokenNameNOT_EQUAL_EQUAL : // != + case TokenNameEQUAL_EQUAL : + // == + case TokenNameEQUAL_EQUAL_EQUAL : + // === + case TokenNameNOT_EQUAL : + // != + case TokenNameNOT_EQUAL_EQUAL : + // != return 60; - case TokenNameLESS : // < - case TokenNameLESS_EQUAL : // <= - case TokenNameGREATER : // > - case TokenNameGREATER_EQUAL : // >= + case TokenNameLESS : + // < + case TokenNameLESS_EQUAL : + // <= + case TokenNameGREATER : + // > + case TokenNameGREATER_EQUAL : + // >= // case TokenNameinstanceof : // instanceof return 70; - case TokenNamePLUS : // + - case TokenNameMINUS : // - + case TokenNamePLUS : + // + + case TokenNameMINUS : + // - return 80; - case TokenNameMULTIPLY : // * - case TokenNameDIVIDE : // / - case TokenNameREMAINDER : // % + case TokenNameMULTIPLY : + // * + case TokenNameDIVIDE : + // / + case TokenNameREMAINDER : + // % return 90; - case TokenNameLEFT_SHIFT : // << - case TokenNameRIGHT_SHIFT : // >> - // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> + case TokenNameLEFT_SHIFT : + // << + case TokenNameRIGHT_SHIFT : + // >> + // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> return 100; - case TokenNameAND : // & - case TokenNameOR : // | - case TokenNameXOR : // ^ + case TokenNameAND : + // & + case TokenNameOR : + // | + case TokenNameXOR : + // ^ return 110; - case TokenNameMULTIPLY_EQUAL : // *= - case TokenNameDIVIDE_EQUAL : // /= - case TokenNameREMAINDER_EQUAL : // %= - case TokenNamePLUS_EQUAL : // += - case TokenNameMINUS_EQUAL : // -= - case TokenNameLEFT_SHIFT_EQUAL : // <<= - case TokenNameRIGHT_SHIFT_EQUAL : // >>= - // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= - case TokenNameAND_EQUAL : // &= - case TokenNameXOR_EQUAL : // ^= - case TokenNameOR_EQUAL : // |= + case TokenNameMULTIPLY_EQUAL : + // *= + case TokenNameDIVIDE_EQUAL : + // /= + case TokenNameREMAINDER_EQUAL : + // %= + case TokenNamePLUS_EQUAL : + // += + case TokenNameMINUS_EQUAL : + // -= + case TokenNameLEFT_SHIFT_EQUAL : + // <<= + case TokenNameRIGHT_SHIFT_EQUAL : + // >>= + // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= + case TokenNameAND_EQUAL : + // &= + case TokenNameXOR_EQUAL : + // ^= + case TokenNameOR_EQUAL : + // |= return 120; - case TokenNameDOT : // . + case TokenNameDOT : + // . return 130; default : return Integer.MAX_VALUE; } } - /** - * Handles the exception raised when an invalid token is encountered. - * Returns true if the exception has been handled, false otherwise. + * Handles the exception raised when an invalid token is encountered. Returns + * true if the exception has been handled, false otherwise. */ private boolean handleInvalidToken(Exception e) { if (e.getMessage().equals(Scanner.INVALID_CHARACTER_CONSTANT) - || e.getMessage().equals(Scanner.INVALID_CHAR_IN_STRING) - || e.getMessage().equals(Scanner.INVALID_ESCAPE)) { + || e.getMessage().equals(Scanner.INVALID_CHAR_IN_STRING) + || e.getMessage().equals(Scanner.INVALID_ESCAPE)) { return true; } return false; } - private final void increaseGlobalDelta(int offset) { globalDelta += offset; } - private final void increaseLineDelta(int offset) { lineDelta += offset; } - private final void increaseSplitDelta(int offset) { splitDelta += offset; } - /** * Returns true if a space has to be inserted after operator * false otherwise. @@ -1210,7 +1233,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameNOT : case TokenNameTWIDDLE : case TokenNameDOT : - case 0 : // no token + case 0 : + // no token case TokenNameLBRACKET : case Scanner.TokenNameCOMMENT_LINE : return false; @@ -1218,12 +1242,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { return true; } } - /** * Returns true if a space has to be inserted before operator - * false otherwise.
- * Cannot be static as it uses the code formatter options - * (to know if the compact assignment mode is on). + * false otherwise.
+ * Cannot be static as it uses the code formatter options (to know if the + * compact assignment mode is on). */ private boolean insertSpaceBefore(int token) { switch (token) { @@ -1233,40 +1256,41 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { return false; } } - private static boolean isComment(int token) { - boolean result = - token == Scanner.TokenNameCOMMENT_BLOCK || token == Scanner.TokenNameCOMMENT_LINE || token == Scanner.TokenNameCOMMENT_PHPDOC; + boolean result = token == Scanner.TokenNameCOMMENT_BLOCK + || token == Scanner.TokenNameCOMMENT_LINE + || token == Scanner.TokenNameCOMMENT_PHPDOC; return result; } - private static boolean isLiteralToken(int token) { boolean result = token == TokenNameIntegerLiteral - // || token == TokenNameLongLiteral - // || token == TokenNameFloatingPointLiteral - || token == TokenNameDoubleLiteral - // || token == TokenNameCharacterLiteral - || token == TokenNameStringLiteral; + // || token == TokenNameLongLiteral + // || token == TokenNameFloatingPointLiteral + || token == TokenNameDoubleLiteral + // || token == TokenNameCharacterLiteral + || token == TokenNameStringLiteral; return result; } - /** * If the length of oneLineBuffer exceeds maxLineLength, * it is split and the result is dumped in formattedSource - * @param newLineCount the number of new lines to append + * + * @param newLineCount + * the number of new lines to append */ private void newLine(int newLineCount) { - // format current line splitDelta = 0; beginningOfLineIndex = formattedSource.length(); String currentLine = currentLineBuffer.toString(); if (containsOpenCloseBraces) { containsOpenCloseBraces = false; - outputLine(currentLine, false, indentationLevelForOpenCloseBraces, 0, -1, null, 0); + outputLine(currentLine, false, indentationLevelForOpenCloseBraces, 0, -1, + null, 0); indentationLevelForOpenCloseBraces = currentLineIndentationLevel; } else { - outputLine(currentLine, false, currentLineIndentationLevel, 0, -1, null, 0); + outputLine(currentLine, false, currentLineIndentationLevel, 0, -1, null, + 0); } // dump line break(s) for (int i = 0; i < newLineCount; i++) { @@ -1275,158 +1299,152 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } // reset formatter for next line int currentLength = currentLine.length(); - currentLineBuffer = new StringBuffer(currentLength > maxLineSize ? maxLineSize = currentLength : maxLineSize); - + currentLineBuffer = new StringBuffer(currentLength > maxLineSize + ? maxLineSize = currentLength + : maxLineSize); increaseGlobalDelta(splitDelta); increaseGlobalDelta(lineDelta); lineDelta = 0; currentLineIndentationLevel = initialIndentationLevel; } - private String operatorString(int operator) { switch (operator) { case TokenNameextends : return "extends"; //$NON-NLS-1$ - - // case TokenNameimplements : - // return "implements"; //$NON-NLS-1$ - // - // case TokenNamethrows : - // return "throws"; //$NON-NLS-1$ - - case TokenNameSEMICOLON : // ; + // case TokenNameimplements : + // return "implements"; //$NON-NLS-1$ + // + // case TokenNamethrows : + // return "throws"; //$NON-NLS-1$ + case TokenNameSEMICOLON : + // ; return ";"; //$NON-NLS-1$ - - case TokenNameCOMMA : // , + case TokenNameCOMMA : + // , return ","; //$NON-NLS-1$ - - case TokenNameEQUAL : // = + case TokenNameEQUAL : + // = return "="; //$NON-NLS-1$ - - case TokenNameAND_AND : // && (15.22) + case TokenNameAND_AND : + // && (15.22) return "&&"; //$NON-NLS-1$ - - case TokenNameOR_OR : // || (15.23) + case TokenNameOR_OR : + // || (15.23) return "||"; //$NON-NLS-1$ - - case TokenNameQUESTION : // ? (15.24) + case TokenNameQUESTION : + // ? (15.24) return "?"; //$NON-NLS-1$ - - case TokenNameCOLON : // : (15.24) + case TokenNameCOLON : + // : (15.24) return ":"; //$NON-NLS-1$ - - case TokenNamePAAMAYIM_NEKUDOTAYIM : // : (15.24) + case TokenNamePAAMAYIM_NEKUDOTAYIM : + // : (15.24) return "::"; //$NON-NLS-1$ - - case TokenNameEQUAL_EQUAL : // == (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameEQUAL_EQUAL : + // == (15.20, 15.20.1, 15.20.2, 15.20.3) return "=="; //$NON-NLS-1$ - - case TokenNameEQUAL_EQUAL_EQUAL : // == (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameEQUAL_EQUAL_EQUAL : + // == (15.20, 15.20.1, 15.20.2, 15.20.3) return "==="; //$NON-NLS-1$ - - case TokenNameEQUAL_GREATER : // -= (15.25.2) + case TokenNameEQUAL_GREATER : + // -= (15.25.2) return "=>"; //$NON-NLS-1$ - - case TokenNameNOT_EQUAL : // != (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameNOT_EQUAL : + // != (15.20, 15.20.1, 15.20.2, 15.20.3) return "!="; //$NON-NLS-1$ - - case TokenNameNOT_EQUAL_EQUAL : // != (15.20, 15.20.1, 15.20.2, 15.20.3) - return "!=="; //$NON-NLS-1$ - - case TokenNameLESS : // < (15.19.1) + case TokenNameNOT_EQUAL_EQUAL : + // != (15.20, 15.20.1, 15.20.2, 15.20.3) + return "!=="; //$NON-NLS-1$ + case TokenNameLESS : + // < (15.19.1) return "<"; //$NON-NLS-1$ - - case TokenNameLESS_EQUAL : // <= (15.19.1) + case TokenNameLESS_EQUAL : + // <= (15.19.1) return "<="; //$NON-NLS-1$ - - case TokenNameGREATER : // > (15.19.1) + case TokenNameGREATER : + // > (15.19.1) return ">"; //$NON-NLS-1$ - - case TokenNameGREATER_EQUAL : // >= (15.19.1) + case TokenNameGREATER_EQUAL : + // >= (15.19.1) return ">="; //$NON-NLS-1$ - - // case TokenNameinstanceof : // instanceof - // return "instanceof"; //$NON-NLS-1$ - - case TokenNamePLUS : // + (15.17, 15.17.2) + // case TokenNameinstanceof : // instanceof + // return "instanceof"; //$NON-NLS-1$ + case TokenNamePLUS : + // + (15.17, 15.17.2) return "+"; //$NON-NLS-1$ - - case TokenNameMINUS : // - (15.17.2) + case TokenNameMINUS : + // - (15.17.2) return "-"; //$NON-NLS-1$ - - case TokenNameMULTIPLY : // * (15.16.1) + case TokenNameMULTIPLY : + // * (15.16.1) return "*"; //$NON-NLS-1$ - - case TokenNameDIVIDE : // / (15.16.2) + case TokenNameDIVIDE : + // / (15.16.2) return "/"; //$NON-NLS-1$ - - case TokenNameREMAINDER : // % (15.16.3) + case TokenNameREMAINDER : + // % (15.16.3) return "%"; //$NON-NLS-1$ - - case TokenNameLEFT_SHIFT : // << (15.18) + case TokenNameLEFT_SHIFT : + // << (15.18) return "<<"; //$NON-NLS-1$ - - case TokenNameRIGHT_SHIFT : // >> (15.18) + case TokenNameRIGHT_SHIFT : + // >> (15.18) return ">>"; //$NON-NLS-1$ - - // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18) - // return ">>>"; //$NON-NLS-1$ - - case TokenNameAND : // & (15.21, 15.21.1, 15.21.2) + // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18) + // return ">>>"; //$NON-NLS-1$ + case TokenNameAND : + // & (15.21, 15.21.1, 15.21.2) return "&"; //$NON-NLS-1$ - - case TokenNameOR : // | (15.21, 15.21.1, 15.21.2) + case TokenNameOR : + // | (15.21, 15.21.1, 15.21.2) return "|"; //$NON-NLS-1$ - - case TokenNameXOR : // ^ (15.21, 15.21.1, 15.21.2) + case TokenNameXOR : + // ^ (15.21, 15.21.1, 15.21.2) return "^"; //$NON-NLS-1$ - - case TokenNameMULTIPLY_EQUAL : // *= (15.25.2) + case TokenNameMULTIPLY_EQUAL : + // *= (15.25.2) return "*="; //$NON-NLS-1$ - - case TokenNameDIVIDE_EQUAL : // /= (15.25.2) + case TokenNameDIVIDE_EQUAL : + // /= (15.25.2) return "/="; //$NON-NLS-1$ - - case TokenNameREMAINDER_EQUAL : // %= (15.25.2) + case TokenNameREMAINDER_EQUAL : + // %= (15.25.2) return "%="; //$NON-NLS-1$ - - case TokenNamePLUS_EQUAL : // += (15.25.2) + case TokenNamePLUS_EQUAL : + // += (15.25.2) return "+="; //$NON-NLS-1$ - - case TokenNameMINUS_EQUAL : // -= (15.25.2) + case TokenNameMINUS_EQUAL : + // -= (15.25.2) return "-="; //$NON-NLS-1$ - - case TokenNameMINUS_GREATER : // -= (15.25.2) + case TokenNameMINUS_GREATER : + // -= (15.25.2) return "->"; //$NON-NLS-1$ - - case TokenNameLEFT_SHIFT_EQUAL : // <<= (15.25.2) + case TokenNameLEFT_SHIFT_EQUAL : + // <<= (15.25.2) return "<<="; //$NON-NLS-1$ - - case TokenNameRIGHT_SHIFT_EQUAL : // >>= (15.25.2) + case TokenNameRIGHT_SHIFT_EQUAL : + // >>= (15.25.2) return ">>="; //$NON-NLS-1$ - - // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2) - // return ">>>="; //$NON-NLS-1$ - - case TokenNameAND_EQUAL : // &= (15.25.2) + // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2) + // return ">>>="; //$NON-NLS-1$ + case TokenNameAND_EQUAL : + // &= (15.25.2) return "&="; //$NON-NLS-1$ - - case TokenNameXOR_EQUAL : // ^= (15.25.2) + case TokenNameXOR_EQUAL : + // ^= (15.25.2) return "^="; //$NON-NLS-1$ - - case TokenNameOR_EQUAL : // |= (15.25.2) + case TokenNameOR_EQUAL : + // |= (15.25.2) return "|="; //$NON-NLS-1$ - - case TokenNameDOT : // . + case TokenNameDOT : + // . return "."; //$NON-NLS-1$ - default : return ""; //$NON-NLS-1$ } } - - /** - * Appends stringToOutput to the formatted output.
+ /** + * Appends stringToOutput to the formatted output.
* If it contains \n, append a LINE_SEPARATOR and indent after it. */ private void output(String stringToOutput) { @@ -1438,15 +1456,14 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } } - - /** - * Appends token to the formatted output.
- * If it contains \n, append a LINE_SEPARATOR and indent after it. + /** + * Appends token to the formatted output.
+ * If it contains \n, append a LINE_SEPARATOR and indent + * after it. */ private void outputCurrentToken(int token) { char[] source = scanner.source; int startPosition = scanner.startPosition; - switch (token) { case Scanner.TokenNameCOMMENT_PHPDOC : case Scanner.TokenNameCOMMENT_BLOCK : @@ -1486,7 +1503,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { endOfLine = true; } if (endOfLine) { - // we remove a maximum of currentCommentOffset characters (tabs are converted to space numbers). + // we remove a maximum of currentCommentOffset characters (tabs + // are converted to space numbers). beginningOfLineSpaces += options.tabSize; if (beginningOfLineSpaces > currentCommentOffset) { currentLineBuffer.append(currentCharacter); @@ -1506,7 +1524,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { endOfLine = true; } if (endOfLine) { - // we remove a maximum of currentCommentOffset characters (tabs are converted to space numbers). + // we remove a maximum of currentCommentOffset characters (tabs + // are converted to space numbers). beginningOfLineSpaces++; if (beginningOfLineSpaces > currentCommentOffset) { currentLineBuffer.append(currentCharacter); @@ -1542,30 +1561,29 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } } - /** * Outputs currentString:
- *
  • If its length is < maxLineLength, output - *
  • Otherwise it is split.
- * @param currentString string to output - * @param preIndented whether the string to output was pre-indented - * @param depth number of indentation to put in front of currentString - * @param operator value of the operator belonging to currentString. + *
    + *
  • If its length is < maxLineLength, output + *
  • Otherwise it is split. + *
+ * + * @param currentString + * string to output + * @param preIndented + * whether the string to output was pre-indented + * @param depth + * number of indentation to put in front of currentString + * @param operator + * value of the operator belonging to currentString. */ - private void outputLine( - String currentString, - boolean preIndented, - int depth, - int operator, - int substringIndex, - int[] startSubstringIndexes, - int offsetInGlobalLine) { - + private void outputLine(String currentString, boolean preIndented, int depth, + int operator, int substringIndex, int[] startSubstringIndexes, + int offsetInGlobalLine) { boolean emptyFirstSubString = false; String operatorString = operatorString(operator); boolean placeOperatorBehind = !breakLineBeforeOperator(operator); boolean placeOperatorAhead = !placeOperatorBehind; - // dump prefix operator? if (placeOperatorAhead) { if (!preIndented) { @@ -1579,9 +1597,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } formattedSource.append(operatorString); increaseSplitDelta(operatorString.length()); - if (insertSpaceAfter(operator) // && operator != TokenNameimplements - && operator != TokenNameextends) { + && operator != TokenNameextends) { // && operator != TokenNamethrows) { formattedSource.append(' '); increaseSplitDelta(1); @@ -1590,10 +1607,10 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } SplitLine splitLine = null; if (options.maxLineLength == 0 - || getLength(currentString, depth) < options.maxLineLength - || (splitLine = split(currentString, offsetInGlobalLine)) == null) { - - // depending on the type of operator, outputs new line before of after dumping it + || getLength(currentString, depth) < options.maxLineLength + || (splitLine = split(currentString, offsetInGlobalLine)) == null) { + // depending on the type of operator, outputs new line before of after + // dumping it // indent before postfix operator // indent also when the line cannot be split if (operator == TokenNameextends) { @@ -1610,12 +1627,13 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { int max = currentString.length(); if (multipleLineCommentCounter != 0) { try { - BufferedReader reader = new BufferedReader(new StringReader(currentString)); + BufferedReader reader = new BufferedReader(new StringReader( + currentString)); String line = reader.readLine(); while (line != null) { - updateMappedPositionsWhileSplitting( - beginningOfLineIndex, - beginningOfLineIndex + line.length() + options.lineSeparatorSequence.length); + updateMappedPositionsWhileSplitting(beginningOfLineIndex, + beginningOfLineIndex + line.length() + + options.lineSeparatorSequence.length); formattedSource.append(line); beginningOfLineIndex = beginningOfLineIndex + line.length(); if ((line = reader.readLine()) != null) { @@ -1629,7 +1647,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { e.printStackTrace(); } } else { - updateMappedPositionsWhileSplitting(beginningOfLineIndex, beginningOfLineIndex + max); + updateMappedPositionsWhileSplitting(beginningOfLineIndex, + beginningOfLineIndex + max); for (int i = 0; i < max; i++) { char currentChar = currentString.charAt(i); switch (currentChar) { @@ -1637,11 +1656,12 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { break; case '\n' : if (i != max - 1) { - // fix for 1FFYL5C: LFCOM:ALL - Incorrect indentation when split with a comment inside a condition + // fix for 1FFYL5C: LFCOM:ALL - Incorrect indentation when + // split with a comment inside a condition // a substring cannot end with a lineSeparatorSequence, - // except if it has been added by format() after a one-line comment + // except if it has been added by format() after a one-line + // comment formattedSource.append(options.lineSeparatorSequence); - // 1FGDDV6: LFCOM:WIN98 - Weird splitting on message expression dumpTab(depth - 1); } @@ -1654,13 +1674,15 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // update positions inside the mappedPositions table if (substringIndex != -1) { if (multipleLineCommentCounter == 0) { - int startPosition = beginningOfLineIndex + startSubstringIndexes[substringIndex]; - updateMappedPositionsWhileSplitting(startPosition, startPosition + max); + int startPosition = beginningOfLineIndex + + startSubstringIndexes[substringIndex]; + updateMappedPositionsWhileSplitting(startPosition, startPosition + + max); } - // compute the splitDelta resulting with the operator and blank removal if (substringIndex + 1 != startSubstringIndexes.length) { - increaseSplitDelta(startSubstringIndexes[substringIndex] + max - startSubstringIndexes[substringIndex + 1]); + increaseSplitDelta(startSubstringIndexes[substringIndex] + max + - startSubstringIndexes[substringIndex + 1]); } } // dump postfix operator? @@ -1680,7 +1702,8 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } // fix for 1FG0BA3: LFCOM:WIN98 - Weird splitting on interfaces // extends has to stand alone on a line when currentString has been split. - if (options.maxLineLength != 0 && splitLine != null && (operator == TokenNameextends)) { + if (options.maxLineLength != 0 && splitLine != null + && (operator == TokenNameextends)) { // || operator == TokenNameimplements // || operator == TokenNamethrows)) { formattedSource.append(options.lineSeparatorSequence); @@ -1697,33 +1720,31 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // perform actual splitting String result[] = splitLine.substrings; int[] splitOperators = splitLine.operators; - if (result[0].length() == 0) { // when the substring 0 is null, the substring 1 is correctly indented. depth--; emptyFirstSubString = true; } - // the operator going in front of the result[0] string is the operator parameter + // the operator going in front of the result[0] string is the operator + // parameter for (int i = 0, max = result.length; i < max; i++) { // the new depth is the current one if this is the first substring, // the current one + 1 otherwise. - // if the substring is a comment, use the current indentation Level instead of the depth + // if the substring is a comment, use the current indentation Level + // instead of the depth // (-1 because the ouputline increases depth). - // (fix for 1FFC72R: LFCOM:ALL - Incorrect line split in presence of line comments) + // (fix for 1FFC72R: LFCOM:ALL - Incorrect line split in presence of line + // comments) String currentResult = result[i]; - if (currentResult.length() != 0 || splitOperators[i] != 0) { - int newDepth = (currentResult.startsWith("/*") //$NON-NLS-1$ - || currentResult.startsWith("//")) //$NON-NLS-1$ - ? indentationLevel - 1 : depth; - outputLine( - currentResult, - i == 0 || (i == 1 && emptyFirstSubString) ? preIndented : false, - i == 0 ? newDepth : newDepth + 1, - splitOperators[i], - i, - splitLine.startSubstringsIndexes, - currentString.indexOf(currentResult)); + int newDepth = (currentResult.startsWith("/*") //$NON-NLS-1$ + || currentResult.startsWith("//")) //$NON-NLS-1$ + ? indentationLevel - 1 : depth; + outputLine(currentResult, i == 0 || (i == 1 && emptyFirstSubString) + ? preIndented + : false, i == 0 ? newDepth : newDepth + 1, splitOperators[i], i, + splitLine.startSubstringsIndexes, currentString + .indexOf(currentResult)); if (i != max - 1) { formattedSource.append(options.lineSeparatorSequence); increaseSplitDelta(options.lineSeparatorSequence.length); @@ -1735,7 +1756,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { String lastOperatorString = operatorString(lastOperator); formattedSource.append(options.lineSeparatorSequence); increaseSplitDelta(options.lineSeparatorSequence.length); - if (breakLineBeforeOperator(lastOperator)) { dumpTab(depth + 1); if (lastOperator != 0) { @@ -1745,9 +1765,9 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } formattedSource.append(lastOperatorString); increaseSplitDelta(lastOperatorString.length()); - - if (insertSpaceAfter(lastOperator) // && lastOperator != TokenNameimplements - && lastOperator != TokenNameextends) { + if (insertSpaceAfter(lastOperator) // && lastOperator != + // TokenNameimplements + && lastOperator != TokenNameextends) { // && lastOperator != TokenNamethrows) { formattedSource.append(' '); increaseSplitDelta(1); @@ -1764,37 +1784,39 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { //increaseSplitDelta(operatorString.length()); } } - /** * Pops the top statement of the stack if it is token */ private int pop(int token) { int delta = 0; - if ((constructionsCount > 0) && (constructions[constructionsCount - 1] == token)) { + if ((constructionsCount > 0) + && (constructions[constructionsCount - 1] == token)) { delta--; constructionsCount--; } return delta; } - /** - * Pops the top statement of the stack if it is a BLOCK or a NONINDENT_BLOCK. + * Pops the top statement of the stack if it is a BLOCK or a + * NONINDENT_BLOCK. */ private int popBlock() { int delta = 0; if ((constructionsCount > 0) - && ((constructions[constructionsCount - 1] == BLOCK) || (constructions[constructionsCount - 1] == NONINDENT_BLOCK))) { + && ((constructions[constructionsCount - 1] == BLOCK) || (constructions[constructionsCount - 1] == NONINDENT_BLOCK))) { if (constructions[constructionsCount - 1] == BLOCK) delta--; constructionsCount--; } return delta; } - /** - * Pops elements until the stack is empty or the top element is token.
+ * Pops elements until the stack is empty or the top element is token. + *
* Does not remove token from the stack. - * @param token the token to be left as the top of the stack + * + * @param token + * the token to be left as the top of the stack */ private int popExclusiveUntil(int token) { int delta = 0; @@ -1806,43 +1828,44 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return delta; } - /** - * Pops elements until the stack is empty or the top element is - * a BLOCK or a NONINDENT_BLOCK.
+ * Pops elements until the stack is empty or the top element is a BLOCK + * or a NONINDENT_BLOCK.
* Does not remove it from the stack. */ private int popExclusiveUntilBlock() { int startCount = constructionsCount; int delta = 0; - for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK; i--) { + for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK + && constructions[i] != NONINDENT_BLOCK; i--) { constructionsCount--; delta--; } return delta; } - /** - * Pops elements until the stack is empty or the top element is - * a BLOCK, a NONINDENT_BLOCK or a CASE.
+ * Pops elements until the stack is empty or the top element is a BLOCK, + * a NONINDENT_BLOCK or a CASE.
* Does not remove it from the stack. */ private int popExclusiveUntilBlockOrCase() { int startCount = constructionsCount; int delta = 0; - for (int i = startCount - 1; - i >= 0 && constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK && constructions[i] != TokenNamecase; - i--) { + for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK + && constructions[i] != NONINDENT_BLOCK + && constructions[i] != TokenNamecase; i--) { constructionsCount--; delta--; } return delta; } - /** - * Pops elements until the stack is empty or the top element is token.
+ * Pops elements until the stack is empty or the top element is token. + *
* Removes token from the stack too. - * @param token the token to remove from the stack + * + * @param token + * the token to remove from the stack */ private int popInclusiveUntil(int token) { int startCount = constructionsCount; @@ -1859,16 +1882,16 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return delta; } - /** - * Pops elements until the stack is empty or the top element is - * a BLOCK or a NONINDENT_BLOCK.
+ * Pops elements until the stack is empty or the top element is a BLOCK + * or a NONINDENT_BLOCK.
* Does not remove it from the stack. */ private int popInclusiveUntilBlock() { int startCount = constructionsCount; int delta = 0; - for (int i = startCount - 1; i >= 0 && (constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK); i--) { + for (int i = startCount - 1; i >= 0 + && (constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK); i--) { delta--; constructionsCount--; } @@ -1879,22 +1902,22 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return delta; } - - /** - * Pushes a block in the stack.
- * Pushes a BLOCK if the stack is empty or if the top element is a BLOCK, - * pushes NONINDENT_BLOCK otherwise. - * Creates a new bigger array if the current one is full. + /** + * Pushes a block in the stack.
+ * Pushes a BLOCK if the stack is empty or if the top element + * is a BLOCK, pushes NONINDENT_BLOCK + * otherwise. Creates a new bigger array if the current one is full. */ private int pushBlock() { int delta = 0; if (constructionsCount == constructions.length) - System.arraycopy(constructions, 0, (constructions = new int[constructionsCount * 2]), 0, constructionsCount); - + System.arraycopy(constructions, 0, + (constructions = new int[constructionsCount * 2]), 0, + constructionsCount); if ((constructionsCount == 0) - || (constructions[constructionsCount - 1] == BLOCK) - || (constructions[constructionsCount - 1] == NONINDENT_BLOCK) - || (constructions[constructionsCount - 1] == TokenNamecase)) { + || (constructions[constructionsCount - 1] == BLOCK) + || (constructions[constructionsCount - 1] == NONINDENT_BLOCK) + || (constructions[constructionsCount - 1] == TokenNamecase)) { delta++; constructions[constructionsCount++] = BLOCK; } else { @@ -1902,33 +1925,33 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return delta; } - - /** + /** * Pushes token.
* Creates a new bigger array if the current one is full. */ private int pushControlStatement(int token) { if (constructionsCount == constructions.length) - System.arraycopy(constructions, 0, (constructions = new int[constructionsCount * 2]), 0, constructionsCount); + System.arraycopy(constructions, 0, + (constructions = new int[constructionsCount * 2]), 0, + constructionsCount); constructions[constructionsCount++] = token; return 1; } - private static boolean separateFirstArgumentOn(int currentToken) { - //return (currentToken == TokenNameCOMMA || currentToken == TokenNameSEMICOLON); - return currentToken != TokenNameif - && currentToken != TokenNameLPAREN - && currentToken != TokenNameNOT - && currentToken != TokenNamewhile - && currentToken != TokenNamefor - && currentToken != TokenNameswitch; + //return (currentToken == TokenNameCOMMA || currentToken == + // TokenNameSEMICOLON); + return currentToken != TokenNameif && currentToken != TokenNameLPAREN + && currentToken != TokenNameNOT && currentToken != TokenNamewhile + && currentToken != TokenNamefor && currentToken != TokenNameswitch; } - /** - * Set the positions to map. The mapped positions should be retrieved using the - * getMappedPositions() method. - * @param positions int[] - * @deprecated Set the positions to map using the format(String, int, int[]) method. + * Set the positions to map. The mapped positions should be retrieved using + * the getMappedPositions() method. + * + * @param positions + * int[] + * @deprecated Set the positions to map using the format(String, int, int[]) + * method. * * @see #getMappedPositions() */ @@ -1938,37 +1961,36 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { globalDelta = 0; mappedPositions = new int[positions.length]; } - - /** + /** * Appends a space character to the current line buffer. */ private void space() { currentLineBuffer.append(' '); increaseLineDelta(1); } - /** - * Splits stringToSplit on the top level token
- * If there are several identical token at the same level, - * the string is cut into many pieces. - * @return an object containing the operator and all the substrings - * or null if the string cannot be split + * Splits stringToSplit on the top level token
+ * If there are several identical token at the same level, the string is cut + * into many pieces. + * + * @return an object containing the operator and all the substrings or null + * if the string cannot be split */ public SplitLine split(String stringToSplit) { return split(stringToSplit, 0); } - /** - * Splits stringToSplit on the top level token
- * If there are several identical token at the same level, - * the string is cut into many pieces. - * @return an object containing the operator and all the substrings - * or null if the string cannot be split + * Splits stringToSplit on the top level token
+ * If there are several identical token at the same level, the string is cut + * into many pieces. + * + * @return an object containing the operator and all the substrings or null + * if the string cannot be split */ public SplitLine split(String stringToSplit, int offsetInGlobalLine) { /* * See http://dev.eclipse.org/bugs/show_bug.cgi?id=12540 and - * http://dev.eclipse.org/bugs/show_bug.cgi?id=14387 + * http://dev.eclipse.org/bugs/show_bug.cgi?id=14387 */ if (stringToSplit.indexOf("//$NON-NLS") != -1) { //$NON-NLS-1$ return null; @@ -1978,7 +2000,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { int splitTokenType = 0; int splitTokenDepth = Integer.MAX_VALUE; int splitTokenPriority = Integer.MAX_VALUE; - int[] substringsStartPositions = new int[10]; // contains the start position of substrings int[] substringsEndPositions = new int[10]; @@ -2001,7 +2022,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { int previousToken = -1; // to remember the previous token. splitScanner.setSource(stringToSplit.toCharArray()); - try { // start the loop while (true) { @@ -2012,9 +2032,10 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { currentToken = splitScanner.getNextToken(); if (Scanner.DEBUG) { int currentEndPosition = splitScanner.getCurrentTokenEndPosition(); - int currentStartPosition = splitScanner.getCurrentTokenStartPosition(); - - System.out.print(currentStartPosition + "," + currentEndPosition + ": "); + int currentStartPosition = splitScanner + .getCurrentTokenStartPosition(); + System.out.print(currentStartPosition + "," + currentEndPosition + + ": "); System.out.println(scanner.toStringAction(currentToken)); } } catch (InvalidInputException e) { @@ -2025,7 +2046,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } if (currentToken == TokenNameEOF) break; - if (firstTokenOnLine == -1) { firstTokenOnLine = currentToken; } @@ -2033,21 +2053,23 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameRBRACE : case TokenNameRPAREN : if (openParenthesisPositionCount > 0) { - if (openParenthesisPositionCount == 1 && lastOpenParenthesisPosition < openParenthesisPosition[0]) { + if (openParenthesisPositionCount == 1 + && lastOpenParenthesisPosition < openParenthesisPosition[0]) { lastOpenParenthesisPosition = openParenthesisPosition[0]; - } else if ( - (splitTokenDepth == Integer.MAX_VALUE) + } else if ((splitTokenDepth == Integer.MAX_VALUE) || (splitTokenDepth > openParenthesisPositionCount && openParenthesisPositionCount == 1)) { splitTokenType = 0; splitTokenDepth = openParenthesisPositionCount; splitTokenPriority = Integer.MAX_VALUE; substringsStartPositions[0] = 0; - // better token means the whole line until now is the first substring + // better token means the whole line until now is the first + // substring substringsCount = 1; // resets the count of substrings substringsEndPositions[0] = openParenthesisPosition[0]; // substring ends on operator start position = openParenthesisPosition[0]; - // the string mustn't be cut before the closing parenthesis but after the opening one. + // the string mustn't be cut before the closing parenthesis but + // after the opening one. splitOperatorsCount = 1; // resets the count of split operators splitOperators[0] = 0; } @@ -2057,23 +2079,27 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { case TokenNameLBRACE : case TokenNameLPAREN : if (openParenthesisPositionCount == openParenthesisPosition.length) { - System.arraycopy( - openParenthesisPosition, - 0, - (openParenthesisPosition = new int[openParenthesisPositionCount * 2]), - 0, - openParenthesisPositionCount); + System + .arraycopy( + openParenthesisPosition, + 0, + (openParenthesisPosition = new int[openParenthesisPositionCount * 2]), + 0, openParenthesisPositionCount); } openParenthesisPosition[openParenthesisPositionCount++] = splitScanner.currentPosition; - if (currentToken == TokenNameLPAREN && previousToken == TokenNameRPAREN) { + if (currentToken == TokenNameLPAREN + && previousToken == TokenNameRPAREN) { openParenthesisPosition[openParenthesisPositionCount - 1] = splitScanner.startPosition; } break; - case TokenNameSEMICOLON : // ; - case TokenNameCOMMA : // , - case TokenNameEQUAL : // = + case TokenNameSEMICOLON : + // ; + case TokenNameCOMMA : + // , + case TokenNameEQUAL : + // = if (openParenthesisPositionCount < splitTokenDepth - || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) { + || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) { // the current token is better than the one we currently have // (in level or in priority if same level) // reset the substringsCount @@ -2081,11 +2107,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { splitTokenType = currentToken; splitTokenPriority = getTokenPriority(currentToken); substringsStartPositions[0] = 0; - // better token means the whole line until now is the first substring - - if (separateFirstArgumentOn(firstTokenOnLine) && openParenthesisPositionCount > 0) { + // better token means the whole line until now is the first + // substring + if (separateFirstArgumentOn(firstTokenOnLine) + && openParenthesisPositionCount > 0) { substringsCount = 2; // resets the count of substrings - substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1]; substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1]; substringsEndPositions[1] = splitScanner.startPosition; @@ -2096,7 +2122,6 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // next substring will start from operator end } else { substringsCount = 1; // resets the count of substrings - substringsEndPositions[0] = splitScanner.startPosition; // substring ends on operator start position = splitScanner.currentPosition; @@ -2106,30 +2131,30 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } else { if ((openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken)) - && splitTokenType != TokenNameEQUAL - && currentToken != TokenNameEQUAL) { - // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after split + && splitTokenType != TokenNameEQUAL + && currentToken != TokenNameEQUAL) { + // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after + // split // take only the 1st = into account. // if another token with the same priority is found, // push the start position of the substring and // push the token into the stack. // create a new array object if the current one is full. if (substringsCount == substringsStartPositions.length) { - System.arraycopy( - substringsStartPositions, - 0, - (substringsStartPositions = new int[substringsCount * 2]), - 0, - substringsCount); - System.arraycopy( - substringsEndPositions, - 0, - (substringsEndPositions = new int[substringsCount * 2]), - 0, - substringsCount); + System + .arraycopy( + substringsStartPositions, + 0, + (substringsStartPositions = new int[substringsCount * 2]), + 0, substringsCount); + System.arraycopy(substringsEndPositions, 0, + (substringsEndPositions = new int[substringsCount * 2]), + 0, substringsCount); } if (splitOperatorsCount == splitOperators.length) { - System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount * 2]), 0, splitOperatorsCount); + System.arraycopy(splitOperators, 0, + (splitOperators = new int[splitOperatorsCount * 2]), 0, + splitOperatorsCount); } substringsStartPositions[substringsCount] = position; substringsEndPositions[substringsCount++] = splitScanner.startPosition; @@ -2140,57 +2165,89 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } } break; - - case TokenNameCOLON : // : (15.24) - // see 1FK7C5R, we only split on a colon, when it is associated with a question-mark. - // indeed it might appear also behind a case statement, and we do not to break at this point. - if ((splitOperatorsCount == 0) || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) { + case TokenNameCOLON : + // : (15.24) + // see 1FK7C5R, we only split on a colon, when it is associated + // with a question-mark. + // indeed it might appear also behind a case statement, and we do + // not to break at this point. + if ((splitOperatorsCount == 0) + || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) { break; } case TokenNameextends : - // case TokenNameimplements : - // case TokenNamethrows : - - case TokenNameDOT : // . - case TokenNameMULTIPLY : // * (15.16.1) - case TokenNameDIVIDE : // / (15.16.2) - case TokenNameREMAINDER : // % (15.16.3) - case TokenNamePLUS : // + (15.17, 15.17.2) - case TokenNameMINUS : // - (15.17.2) - case TokenNameLEFT_SHIFT : // << (15.18) - case TokenNameRIGHT_SHIFT : // >> (15.18) - // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18) - case TokenNameLESS : // < (15.19.1) - case TokenNameLESS_EQUAL : // <= (15.19.1) - case TokenNameGREATER : // > (15.19.1) - case TokenNameGREATER_EQUAL : // >= (15.19.1) - // case TokenNameinstanceof : // instanceof - case TokenNameEQUAL_EQUAL : // == (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameEQUAL_EQUAL_EQUAL : // == (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameNOT_EQUAL : // != (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameNOT_EQUAL_EQUAL : // != (15.20, 15.20.1, 15.20.2, 15.20.3) - case TokenNameAND : // & (15.21, 15.21.1, 15.21.2) - case TokenNameOR : // | (15.21, 15.21.1, 15.21.2) - case TokenNameXOR : // ^ (15.21, 15.21.1, 15.21.2) - case TokenNameAND_AND : // && (15.22) - case TokenNameOR_OR : // || (15.23) - case TokenNameQUESTION : // ? (15.24) - case TokenNameMULTIPLY_EQUAL : // *= (15.25.2) - case TokenNameDIVIDE_EQUAL : // /= (15.25.2) - case TokenNameREMAINDER_EQUAL : // %= (15.25.2) - case TokenNamePLUS_EQUAL : // += (15.25.2) - case TokenNameMINUS_EQUAL : // -= (15.25.2) - case TokenNameLEFT_SHIFT_EQUAL : // <<= (15.25.2) - case TokenNameRIGHT_SHIFT_EQUAL : // >>= (15.25.2) - // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2) - case TokenNameAND_EQUAL : // &= (15.25.2) - case TokenNameXOR_EQUAL : // ^= (15.25.2) - case TokenNameOR_EQUAL : // |= (15.25.2) - - if ((openParenthesisPositionCount < splitTokenDepth - || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) - && !((currentToken == TokenNamePLUS || currentToken == TokenNameMINUS) - && (previousToken == TokenNameLBRACE || previousToken == TokenNameLBRACKET || splitScanner.startPosition == 0))) { + // case TokenNameimplements : + // case TokenNamethrows : + case TokenNameDOT : + // . + case TokenNameMULTIPLY : + // * (15.16.1) + case TokenNameDIVIDE : + // / (15.16.2) + case TokenNameREMAINDER : + // % (15.16.3) + case TokenNamePLUS : + // + (15.17, 15.17.2) + case TokenNameMINUS : + // - (15.17.2) + case TokenNameLEFT_SHIFT : + // << (15.18) + case TokenNameRIGHT_SHIFT : + // >> (15.18) + // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18) + case TokenNameLESS : + // < (15.19.1) + case TokenNameLESS_EQUAL : + // <= (15.19.1) + case TokenNameGREATER : + // > (15.19.1) + case TokenNameGREATER_EQUAL : + // >= (15.19.1) + // case TokenNameinstanceof : // instanceof + case TokenNameEQUAL_EQUAL : + // == (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameEQUAL_EQUAL_EQUAL : + // == (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameNOT_EQUAL : + // != (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameNOT_EQUAL_EQUAL : + // != (15.20, 15.20.1, 15.20.2, 15.20.3) + case TokenNameAND : + // & (15.21, 15.21.1, 15.21.2) + case TokenNameOR : + // | (15.21, 15.21.1, 15.21.2) + case TokenNameXOR : + // ^ (15.21, 15.21.1, 15.21.2) + case TokenNameAND_AND : + // && (15.22) + case TokenNameOR_OR : + // || (15.23) + case TokenNameQUESTION : + // ? (15.24) + case TokenNameMULTIPLY_EQUAL : + // *= (15.25.2) + case TokenNameDIVIDE_EQUAL : + // /= (15.25.2) + case TokenNameREMAINDER_EQUAL : + // %= (15.25.2) + case TokenNamePLUS_EQUAL : + // += (15.25.2) + case TokenNameMINUS_EQUAL : + // -= (15.25.2) + case TokenNameLEFT_SHIFT_EQUAL : + // <<= (15.25.2) + case TokenNameRIGHT_SHIFT_EQUAL : + // >>= (15.25.2) + // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2) + case TokenNameAND_EQUAL : + // &= (15.25.2) + case TokenNameXOR_EQUAL : + // ^= (15.25.2) + case TokenNameOR_EQUAL : + // |= (15.25.2) + if ((openParenthesisPositionCount < splitTokenDepth || (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority > getTokenPriority(currentToken))) + && !((currentToken == TokenNamePLUS || currentToken == TokenNameMINUS) && (previousToken == TokenNameLBRACE + || previousToken == TokenNameLBRACKET || splitScanner.startPosition == 0))) { // the current token is better than the one we currently have // (in level or in priority if same level) // reset the substringsCount @@ -2198,11 +2255,11 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { splitTokenType = currentToken; splitTokenPriority = getTokenPriority(currentToken); substringsStartPositions[0] = 0; - // better token means the whole line until now is the first substring - - if (separateFirstArgumentOn(firstTokenOnLine) && openParenthesisPositionCount > 0) { + // better token means the whole line until now is the first + // substring + if (separateFirstArgumentOn(firstTokenOnLine) + && openParenthesisPositionCount > 0) { substringsCount = 2; // resets the count of substrings - substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth - 1]; substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth - 1]; substringsEndPositions[1] = splitScanner.startPosition; @@ -2214,39 +2271,38 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // next substring will start from operator end } else { substringsCount = 1; // resets the count of substrings - substringsEndPositions[0] = splitScanner.startPosition; // substring ends on operator start position = splitScanner.currentPosition; // next substring will start from operator end splitOperatorsCount = 2; // resets the count of split operators splitOperators[0] = 0; - // nothing for first operand since operator will be inserted in front of the second operand + // nothing for first operand since operator will be inserted in + // front of the second operand splitOperators[1] = currentToken; - } } else { - if (openParenthesisPositionCount == splitTokenDepth && splitTokenPriority == getTokenPriority(currentToken)) { + if (openParenthesisPositionCount == splitTokenDepth + && splitTokenPriority == getTokenPriority(currentToken)) { // if another token with the same priority is found, // push the start position of the substring and // push the token into the stack. // create a new array object if the current one is full. if (substringsCount == substringsStartPositions.length) { - System.arraycopy( - substringsStartPositions, - 0, - (substringsStartPositions = new int[substringsCount * 2]), - 0, - substringsCount); - System.arraycopy( - substringsEndPositions, - 0, - (substringsEndPositions = new int[substringsCount * 2]), - 0, - substringsCount); + System + .arraycopy( + substringsStartPositions, + 0, + (substringsStartPositions = new int[substringsCount * 2]), + 0, substringsCount); + System.arraycopy(substringsEndPositions, 0, + (substringsEndPositions = new int[substringsCount * 2]), + 0, substringsCount); } if (splitOperatorsCount == splitOperators.length) { - System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount * 2]), 0, splitOperatorsCount); + System.arraycopy(splitOperators, 0, + (splitOperators = new int[splitOperatorsCount * 2]), 0, + splitOperatorsCount); } substringsStartPositions[substringsCount] = position; substringsEndPositions[substringsCount++] = splitScanner.startPosition; @@ -2271,32 +2327,30 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { // if the string cannot be split, return null. if (splitOperatorsCount == 0) return null; - // ## SPECIAL CASES BEGIN - if (((splitOperatorsCount == 2 - && splitOperators[1] == TokenNameDOT - && splitTokenDepth == 0 - && lastOpenParenthesisPosition > -1) - || (splitOperatorsCount > 2 - && splitOperators[1] == TokenNameDOT - && splitTokenDepth == 0 - && lastOpenParenthesisPosition > -1 - && lastOpenParenthesisPosition <= options.maxLineLength) - || (separateFirstArgumentOn(firstTokenOnLine) && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1)) - && (lastOpenParenthesisPosition < splitScanner.source.length && splitScanner.source[lastOpenParenthesisPosition] != ')')) { - // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis should not be broken on two lines + if (((splitOperatorsCount == 2 && splitOperators[1] == TokenNameDOT + && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1) + || (splitOperatorsCount > 2 && splitOperators[1] == TokenNameDOT + && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1 && lastOpenParenthesisPosition <= options.maxLineLength) || (separateFirstArgumentOn(firstTokenOnLine) + && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1)) + && (lastOpenParenthesisPosition < splitScanner.source.length && splitScanner.source[lastOpenParenthesisPosition] != ')')) { + // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis should + // not be broken on two lines // only one split on a top level . - // or more than one split on . and substring before open parenthesis fits one line. + // or more than one split on . and substring before open parenthesis fits + // one line. // or split inside parenthesis and first token is not a for/while/if - SplitLine sl = split(stringToSplit.substring(lastOpenParenthesisPosition), lastOpenParenthesisPosition); + SplitLine sl = split( + stringToSplit.substring(lastOpenParenthesisPosition), + lastOpenParenthesisPosition); if (sl == null || sl.operators[0] != TokenNameCOMMA) { - // trim() is used to remove the extra blanks at the end of the substring. See PR 1FGYPI1 - return new SplitLine( - new int[] { 0, 0 }, - new String[] { + // trim() is used to remove the extra blanks at the end of the + // substring. See PR 1FGYPI1 + return new SplitLine(new int[]{0, 0}, new String[]{ stringToSplit.substring(0, lastOpenParenthesisPosition).trim(), - stringToSplit.substring(lastOpenParenthesisPosition)}, - new int[] { offsetInGlobalLine, lastOpenParenthesisPosition + offsetInGlobalLine }); + stringToSplit.substring(lastOpenParenthesisPosition)}, new int[]{ + offsetInGlobalLine, + lastOpenParenthesisPosition + offsetInGlobalLine}); } else { // right substring can be split and is split on comma // copy substrings and operators @@ -2307,23 +2361,26 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { int[] startIndexes = new int[subStringsLength]; int operatorsLength = sl.operators.length + 1 - startIndex; int[] operators = new int[operatorsLength]; - result[0] = stringToSplit.substring(0, lastOpenParenthesisPosition); operators[0] = 0; - - System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes, 1, subStringsLength - 1); + System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes, + 1, subStringsLength - 1); for (int i = subStringsLength - 1; i >= 0; i--) { startIndexes[i] += offsetInGlobalLine; } - System.arraycopy(sl.substrings, startIndex, result, 1, subStringsLength - 1); - System.arraycopy(sl.operators, startIndex, operators, 1, operatorsLength - 1); - + System.arraycopy(sl.substrings, startIndex, result, 1, + subStringsLength - 1); + System.arraycopy(sl.operators, startIndex, operators, 1, + operatorsLength - 1); return new SplitLine(operators, result, startIndexes); } } - // if the last token is a comment and the substring before the comment fits on a line, + // if the last token is a comment and the substring before the comment fits + // on a line, // split before the comment and return the result. - if (lastCommentStartPosition > -1 && lastCommentStartPosition < options.maxLineLength && splitTokenPriority > 50) { + if (lastCommentStartPosition > -1 + && lastCommentStartPosition < options.maxLineLength + && splitTokenPriority > 50) { int end = lastCommentStartPosition; int start = lastCommentStartPosition; if (stringToSplit.charAt(end - 1) == ' ') { @@ -2332,31 +2389,31 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { if (start != end && stringToSplit.charAt(start) == ' ') { start++; } - return new SplitLine( - new int[] { 0, 0 }, - new String[] { stringToSplit.substring(0, end), stringToSplit.substring(start)}, - new int[] { 0, start }); + return new SplitLine(new int[]{0, 0}, new String[]{ + stringToSplit.substring(0, end), stringToSplit.substring(start)}, + new int[]{0, start}); } if (position != stringToSplit.length()) { if (substringsCount == substringsStartPositions.length) { - System.arraycopy( - substringsStartPositions, - 0, - (substringsStartPositions = new int[substringsCount * 2]), - 0, - substringsCount); - System.arraycopy(substringsEndPositions, 0, (substringsEndPositions = new int[substringsCount * 2]), 0, substringsCount); + System.arraycopy(substringsStartPositions, 0, + (substringsStartPositions = new int[substringsCount * 2]), 0, + substringsCount); + System.arraycopy(substringsEndPositions, 0, + (substringsEndPositions = new int[substringsCount * 2]), 0, + substringsCount); } // avoid empty extra substring, e.g. line terminated with a semi-colon substringsStartPositions[substringsCount] = position; substringsEndPositions[substringsCount++] = stringToSplit.length(); } if (splitOperatorsCount == splitOperators.length) { - System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount * 2]), 0, splitOperatorsCount); + System.arraycopy(splitOperators, 0, + (splitOperators = new int[splitOperatorsCount * 2]), 0, + splitOperatorsCount); } splitOperators[splitOperatorsCount] = 0; - - // the last element of the stack is the position of the end of StringToSPlit + // the last element of the stack is the position of the end of + // StringToSPlit // +1 because the substring method excludes the last character String[] result = new String[substringsCount]; for (int i = 0; i < substringsCount; i++) { @@ -2373,29 +2430,41 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { substringsStartPositions[i] += offsetInGlobalLine; } if (splitOperatorsCount > substringsCount) { - System.arraycopy(substringsStartPositions, 0, (substringsStartPositions = new int[splitOperatorsCount]), 0, substringsCount); - System.arraycopy(substringsEndPositions, 0, (substringsEndPositions = new int[splitOperatorsCount]), 0, substringsCount); + System.arraycopy(substringsStartPositions, 0, + (substringsStartPositions = new int[splitOperatorsCount]), 0, + substringsCount); + System.arraycopy(substringsEndPositions, 0, + (substringsEndPositions = new int[splitOperatorsCount]), 0, + substringsCount); for (int i = substringsCount; i < splitOperatorsCount; i++) { substringsStartPositions[i] = position; substringsEndPositions[i] = position; } - System.arraycopy(splitOperators, 0, (splitOperators = new int[splitOperatorsCount]), 0, splitOperatorsCount); + System.arraycopy(splitOperators, 0, + (splitOperators = new int[splitOperatorsCount]), 0, + splitOperatorsCount); } else { - System.arraycopy(substringsStartPositions, 0, (substringsStartPositions = new int[substringsCount]), 0, substringsCount); - System.arraycopy(substringsEndPositions, 0, (substringsEndPositions = new int[substringsCount]), 0, substringsCount); - System.arraycopy(splitOperators, 0, (splitOperators = new int[substringsCount]), 0, substringsCount); + System.arraycopy(substringsStartPositions, 0, + (substringsStartPositions = new int[substringsCount]), 0, + substringsCount); + System.arraycopy(substringsEndPositions, 0, + (substringsEndPositions = new int[substringsCount]), 0, + substringsCount); + System.arraycopy(splitOperators, 0, + (splitOperators = new int[substringsCount]), 0, substringsCount); } - SplitLine splitLine = new SplitLine(splitOperators, result, substringsStartPositions); + SplitLine splitLine = new SplitLine(splitOperators, result, + substringsStartPositions); return splitLine; } - private void updateMappedPositions(int startPosition) { if (positionsToMap == null) { return; } char[] source = scanner.source; int sourceLength = source.length; - while (indexToMap < positionsToMap.length && positionsToMap[indexToMap] <= startPosition) { + while (indexToMap < positionsToMap.length + && positionsToMap[indexToMap] <= startPosition) { int posToMap = positionsToMap[indexToMap]; if (posToMap < 0 || posToMap >= sourceLength) { // protection against out of bounds position @@ -2417,20 +2486,17 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { indexToMap++; } } - - private void updateMappedPositionsWhileSplitting(int startPosition, int endPosition) { + private void updateMappedPositionsWhileSplitting(int startPosition, + int endPosition) { if (mappedPositions == null || mappedPositions.length == indexInMap) return; - while (indexInMap < mappedPositions.length - && startPosition <= mappedPositions[indexInMap] - && mappedPositions[indexInMap] < endPosition - && indexInMap < indexToMap) { + && startPosition <= mappedPositions[indexInMap] + && mappedPositions[indexInMap] < endPosition && indexInMap < indexToMap) { mappedPositions[indexInMap] += splitDelta; indexInMap++; } } - private int getLength(String s, int tabDepth) { int length = 0; for (int i = 0; i < tabDepth; i++) { @@ -2448,13 +2514,14 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { } return length; } - - /** - * Sets the initial indentation level - * @param indentationLevel new indentation level - * - * @deprecated - */ + /** + * Sets the initial indentation level + * + * @param indentationLevel + * new indentation level + * + * @deprecated + */ public void setInitialIndentationLevel(int newIndentationLevel) { this.initialIndentationLevel = currentLineIndentationLevel = indentationLevel = newIndentationLevel; } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java index 91ce5e7..f3f9132 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java @@ -124,7 +124,7 @@ public class DeclarationEngine { continue; // for loop } break; - case ITerminalSymbols.TokenNamethis : + case ITerminalSymbols.TokenNameVariable : if (type != PHPIdentifierLocation.METHOD && type != PHPIdentifierLocation.VARIABLE) { continue; // for loop } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java index 056edbe..fbabccb 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/builder/IdentifierIndexManager.java @@ -172,8 +172,7 @@ public class IdentifierIndexManager { } } } - } else if ((fToken == TokenNameLBRACE) - || (fToken == TokenNameDOLLAR_LBRACE)) { + } else if (fToken == TokenNameLBRACE) { getNextToken(); counter++; } else if (fToken == TokenNameRBRACE) { diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/obfuscator/ObfuscatorPass1Exporter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/obfuscator/ObfuscatorPass1Exporter.java index dcd9a6e..ab2d8f9 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/obfuscator/ObfuscatorPass1Exporter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/obfuscator/ObfuscatorPass1Exporter.java @@ -142,7 +142,7 @@ public class ObfuscatorPass1Exporter implements ITerminalSymbols { } getNextToken(); - } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) { + } else if (fToken == TokenNameLBRACE) { getNextToken(); counter++; } else if (fToken == TokenNameRBRACE) { diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java index b0ba0b8..de0473e 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPCompletionProcessor.java @@ -224,8 +224,8 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { case ITerminalSymbols.TokenNameMINUS_GREATER : // dereferencing operator '->' found lastSignificantToken = ITerminalSymbols.TokenNameMINUS_GREATER; - if (beforeLastToken == ITerminalSymbols.TokenNamethis) { - lastSignificantToken = ITerminalSymbols.TokenNamethis; + if (beforeLastToken == ITerminalSymbols.TokenNameVariable) { + lastSignificantToken = ITerminalSymbols.TokenNameVariable; } break; case ITerminalSymbols.TokenNamenew : @@ -270,7 +270,7 @@ public class PHPCompletionProcessor implements IContentAssistProcessor { int lastSignificantToken = getLastToken(viewer, offset, context); boolean useClassMembers = (lastSignificantToken == ITerminalSymbols.TokenNameMINUS_GREATER) || - (lastSignificantToken == ITerminalSymbols.TokenNamethis) || + (lastSignificantToken == ITerminalSymbols.TokenNameVariable) || (lastSignificantToken == ITerminalSymbols.TokenNamenew); boolean emptyPrefix = prefix == null || prefix.equals("");