Parser: parse a PHP expression after <?= ; statements aren't allowed
authoraxelcl <axelcl>
Sun, 8 May 2005 14:30:53 +0000 (14:30 +0000)
committeraxelcl <axelcl>
Sun, 8 May 2005 14:30:53 +0000 (14:30 +0000)
Code formatter: don't select the text "visually" in the editor anymore

net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Scanner.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java

index 50dc61f..8770f4a 100644 (file)
@@ -194,8 +194,14 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
    */
   private void throwSyntaxError(String error) {
     int problemStartPosition = scanner.getCurrentTokenStartPosition();
-    int problemEndPosition = scanner.getCurrentTokenEndPosition();
-    throwSyntaxError(error, problemStartPosition, problemEndPosition + 1);
+    int problemEndPosition = scanner.getCurrentTokenEndPosition() + 1;
+    if (scanner.source.length <= problemEndPosition && problemEndPosition > 0) {
+      problemEndPosition = scanner.source.length - 1;
+      if (problemStartPosition > 0 && problemStartPosition >= problemEndPosition && problemEndPosition > 0) {
+        problemStartPosition = problemEndPosition - 1;
+      }
+    }
+    throwSyntaxError(error, problemStartPosition, problemEndPosition);
   }
 
   /**
@@ -356,30 +362,9 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
           throwSyntaxError("End-of-file not reached.");
         }
         break;
-      } catch (SyntaxError sytaxErr1) {
+      } catch (SyntaxError syntaxError) {
+        //          syntaxError.printStackTrace();
         break;
-        //        // if an error occured,
-        //        // try to find keywords 'abstract' 'final' 'class' or 'function'
-        //        // to parse the rest of the string
-        //        boolean tokenize = scanner.tokenizeStrings;
-        //        if (!tokenize) {
-        //          scanner.tokenizeStrings = true;
-        //        }
-        //        try {
-        //          while (token != TokenNameEOF) {
-        //            if (token == TokenNameabstract || token == TokenNamefinal || token == TokenNameclass || token == TokenNamefunction) {
-        //              break;
-        //            }
-        //            getNextToken();
-        //          }
-        //          if (token == TokenNameEOF) {
-        //            break;
-        //          }
-        //        } catch (SyntaxError sytaxErr2) {
-        //          break;
-        //        } finally {
-        //          scanner.tokenizeStrings = tokenize;
-        //        }
       }
     } while (true);
 
@@ -798,7 +783,19 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
       }
       return statement;
     } else if (token == TokenNameINLINE_HTML) {
-      getNextToken();
+      if (scanner.phpExpressionTag) {
+        // start of <?= ... ?> block
+        getNextToken();
+        expr();
+        if (token == TokenNameSEMICOLON) {
+          getNextToken();
+        } 
+        if (token != TokenNameINLINE_HTML) {
+          throwSyntaxError("Missing '?>' for open PHP expression block ('<?=').");
+        }
+      } else {
+        getNextToken();
+      }
       return statement;
       //    } else if (token == TokenNameprint) {
       //      getNextToken();
@@ -2520,7 +2517,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
       case TokenNameIdentifier:
       case TokenNameVariable:
       case TokenNameDOLLAR:
-        boolean rememberedVar = false; 
+        boolean rememberedVar = false;
         Expression lhs = variable(true, true);
         if (lhs != null && lhs instanceof FieldReference && token != TokenNameEQUAL && token != TokenNamePLUS_EQUAL
             && token != TokenNameMINUS_EQUAL && token != TokenNameMULTIPLY_EQUAL && token != TokenNameDIVIDE_EQUAL
@@ -2654,6 +2651,9 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
             getNextToken();
             break;
           } else {
+            //            System.out.println(scanner.getCurrentTokenStartPosition());
+            //            System.out.println(scanner.getCurrentTokenEndPosition());
+
             throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
           }
         }
index a483ee4..2965347 100644 (file)
@@ -38,7 +38,9 @@ public class Scanner implements IScanner, ITerminalSymbols {
   public boolean ignorePHPOneLiner = false;
 
   public boolean phpMode = false;
-
+  
+  public boolean phpExpressionTag = false;
+  
   public Stack encapsedStringStack = null;
 
   public char currentCharacter;
@@ -1200,8 +1202,9 @@ public class Scanner implements IScanner, ITerminalSymbols {
   }
 
   public int getNextToken() throws InvalidInputException {
+    phpExpressionTag = false;
     if (!phpMode) {
-      return getInlinedHTML(currentPosition);
+      return getInlinedHTMLToken(currentPosition);
     }
     if (phpMode) {
       this.wasAcr = false;
@@ -1553,7 +1556,7 @@ public class Scanner implements IScanner, ITerminalSymbols {
                 phpMode = true;
                 return TokenNameINLINE_HTML;
               }
-              return getInlinedHTML(currentPosition - 2);
+              return getInlinedHTMLToken(currentPosition - 2);
             }
             return TokenNameQUESTION;
           case ':':
@@ -1852,39 +1855,6 @@ public class Scanner implements IScanner, ITerminalSymbols {
     return TokenNameEOF;
   }
 
-  private int getInlinedHTML(int start) throws InvalidInputException {
-    int token = getInlinedHTMLToken(start);
-    if (token == TokenNameINLINE_HTML) {
-      //               Stack stack = new Stack();
-      //               // scan html for errors
-      //               Source inlinedHTMLSource = new Source(new String(source, startPosition, currentPosition - startPosition));
-      //               int lastPHPEndPos=0;
-      //               for (Iterator i=inlinedHTMLSource.getNextTagIterator(0); i.hasNext();) {
-      //                   Tag tag=(Tag)i.next();
-      //                   
-      //                   if (tag instanceof StartTag) {
-      //                       StartTag startTag=(StartTag)tag;
-      //                     // System.out.println("startTag: "+tag);
-      //                       if (startTag.isServerTag()) {
-      //                         // TODO : what to do with a server tag ?
-      //                       } else {
-      //                           // do whatever with HTML start tag
-      //                           // use startTag.getElement() to find the element corresponding
-      //                           // to this start tag which may be useful if you implement code
-      //                           // folding etc
-      //                               stack.push(startTag);
-      //                       }
-      //                   } else {
-      //                       EndTag endTag=(EndTag)tag;
-      //                       StartTag stag = (StartTag) stack.peek();
-      //// System.out.println("endTag: "+tag);
-      //                       // do whatever with HTML end tag.
-      //                   }
-      //               }
-    }
-    return token;
-  }
-
   /**
    * @return
    * @throws InvalidInputException
@@ -1904,9 +1874,11 @@ public class Scanner implements IScanner, ITerminalSymbols {
             if ((currentCharacter != 'P') && (currentCharacter != 'p')) {
               if (currentCharacter != '=') { // <?=
                 currentPosition--;
+              } else {
+                phpExpressionTag = true; 
               }
               // <?
-              if (ignorePHPOneLiner) {
+              if (ignorePHPOneLiner) { // for CodeFormatter
                 if (lookAheadLinePHPTag() == TokenNameINLINE_HTML) {
                   phpMode = true;
                   return TokenNameINLINE_HTML;
@@ -1982,11 +1954,15 @@ public class Scanner implements IScanner, ITerminalSymbols {
             return TokenNameEOF;
           }
           break;
+        case '\\':
+          if (doubleQuotedStringActive) {
+            // ignore escaped characters in double quoted strings
+            previousCharInLine = currentCharInLine;
+            currentCharInLine = source[currentPositionInLine++];
+          }
         case '\"':
           if (doubleQuotedStringActive) {
-            if (previousCharInLine != '\\') {
-              doubleQuotedStringActive = false;
-            }
+            doubleQuotedStringActive = false;
           } else {
             if (!singleQuotedStringActive) {
               doubleQuotedStringActive = true;
index 7727311..030c7d5 100644 (file)
@@ -139,7 +139,8 @@ public class JavaSourceViewer extends ProjectionViewer implements IPropertyChang
                    case FORMAT:
                      Point point = getSelectedRange();
                      if (point.y==0) {
-                       setSelectedRange(0, getDocument().getLength());
+//                     setSelectedRange(0, getDocument().getLength());
+                       revealRange(0, getDocument().getLength());
                      }
                      break;
                }