Joey Mazzarelli avatar Joey Mazzarelli committed dbc8a56

simplifying lexer and parser

Comments (0)

Files changed (2)

src/phplemon/Trekel/Parser.y

 %declare_class {class Trekel_Parser}
 %include_class {
 
-  public $column = 0;
   private $selector;
   private $tree = null;
 
     if ($this->selector) {
       $lexer = new Trekel_Lexer($this->selector);
       while ($lexer->yylex()) {
-        $this->column = $lexer->column;
         $this->doParse($lexer->token, $lexer->value);
       }
       $this->doParse(0, 0);
     $expect[] = self::$yyTokenName[$token];
   }
   $found = $this->tokenName($yymajor);
-
-  // lines are 0-indexed
-  $column = $this->column;
-
-  $message = "Could not compile query: error at character {$column}\n";
-  $message .= "Unexpected {$this->tokenName($yymajor)}({$TOKEN}), ";
+  $message = "Unexpected {$this->tokenName($yymajor)}({$TOKEN}), ";
   if (count($expect) == 1) {
     $message .= "expected: {$expect[0]}\n";
   } else {

src/plex/Trekel/Lexer.plex

   public $token;
   public $value;
   public $line;
-  private $val_width = 0;
-  public $column;
-  private $_string;
-  private $debug = 0;
-
-  private function updateColumn ($val = null) {
-    if (null == $val) {
-      $val = $this->value;
-    }
-    if ("\n" == $val) {
-      $this->val_width = 0;
-      $this->column = 1;
-    } else {
-      $this->column += $this->val_width;
-      $this->val_width = strlen($val);
-    }
-  }
 
   function __construct ($data) {
-    $this->input = str_replace("\r", "\n", str_replace("\r\n", "\n", $data));
+    $this->input = $data;
     $this->N = 0;
   }
 
 */
 
 /*!lex2php
-'*' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_IDENTIFIER;
-}
 
-'[' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_LBRACKET;
-}
+// === Literal Tokens ===
+'*' { $this->token = Trekel_Parser::T_IDENTIFIER; }
+'[' { $this->token = Trekel_Parser::T_LBRACKET; }
+']' { $this->token = Trekel_Parser::T_RBRACKET; }
+'=' { $this->token = Trekel_Parser::T_EQ; }
+'^=' { $this->token = Trekel_Parser::T_STARTSWITH; }
+'$=' { $this->token = Trekel_Parser::T_ENDSWITH; }
+'*=' { $this->token = Trekel_Parser::T_CONTAINS; }
+'!=' { $this->token = Trekel_Parser::T_NEQ; }
+'@=' { $this->token = Trekel_Parser::T_REGEX; }
+'>' { $this->token = Trekel_Parser::T_CHILD; }
+'+' { $this->token = Trekel_Parser::T_ADJACENT; }
+'~' { $this->token = Trekel_Parser::T_SIBLING; }
 
-']' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_RBRACKET;
-}
-
-'=' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_EQ;
-}
-
-'^=' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_STARTSWITH;
-}
-
-'$=' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_ENDSWITH;
-}
-
-'*=' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_CONTAINS;
-}
-
-'!=' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_NEQ;
-}
-
-'@=' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_REGEX;
-}
-
-'>' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_CHILD;
-}
-
-'+' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_ADJACENT;
-}
-
-'~' {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_SIBLING;
-}
-
-// Regular expression tokens
-
-GETTER {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_GETTER;
-}
-
-IDENTIFIER {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_IDENTIFIER;
-}
-
-PSEUDO {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_PSEUDO;
-}
-
-DQUOTESTRING {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_STRING;
-}
-
-SQUOTESTRING {
-  $this->updateColumn();
-  $this->token = Trekel_Parser::T_STRING;
-}
-
-WHITESPACE {
-  $this->updateColumn();
-  return false;
-}
+// === Regular expression tokens ===
+GETTER { $this->token = Trekel_Parser::T_GETTER; }
+IDENTIFIER { $this->token = Trekel_Parser::T_IDENTIFIER; }
+PSEUDO { $this->token = Trekel_Parser::T_PSEUDO; }
+DQUOTESTRING { $this->token = Trekel_Parser::T_STRING; }
+SQUOTESTRING { $this->token = Trekel_Parser::T_STRING; }
+WHITESPACE { return false; }
 
 */
 
-  /**
-   * return something useful, when a parse error occurs.
-   *
-   * used to build error messages if the parser fails, and needs to
-   * know the line number..
-   *
-   * @return   string
-   * @access   public
-   */
-  function parseError () {
-    return "Error at line {$this->yyline}";
-  }
-
 } // end Trekel_Lexer
 
 // vim: set ft=php et ts=2:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.