port scalar scanners
authorChuck Hagenbuch <chuck@horde.org>
Wed, 28 Jan 2009 21:26:44 +0000 (16:26 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 28 Jan 2009 22:13:38 +0000 (17:13 -0500)
framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base/Scalar.php

index b08cfee..7ae6046 100644 (file)
-module Chronic
+<?php
+class Horde_Date_Parser_Locale_Base_Scalar extends Horde_Date_Parser_Tag
+{
+    public $scalarRegex = '/^\d*$/';
+    public $dayRegex = '/^\d\d?$/';
+    public $monthRegex = '/^\d\d?$/';
+    public $yearRegex = '/^([1-9]\d)?\d\d?$/';
+    public $timeSignifiers = array('am', 'pm', 'morning', 'afternoon', 'evening', 'night');
 
-  class Scalar < Tag #:nodoc:
-    def self.scan(tokens)
-      # for each token
-      tokens.each_index do |i|
-        if t = self.scan_for_scalars(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
-        if t = self.scan_for_days(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
-        if t = self.scan_for_months(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
-        if t = self.scan_for_years(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
-      end
-      tokens
-    end
-  
-    def self.scan_for_scalars(token, post_token)
-      if token.word =~ /^\d*$/
-        unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
-          return Scalar.new(token.word.to_i)
-        end
-      end
-      return nil
-    end
-    
-    def self.scan_for_days(token, post_token)
-      if token.word =~ /^\d\d?$/
-        unless token.word.to_i > 31 || (post_token && %w{am pm morning afternoon evening night}.include?(post_token))
-          return ScalarDay.new(token.word.to_i)
-        end
-      end
-      return nil
-    end
-    
-    def self.scan_for_months(token, post_token)
-      if token.word =~ /^\d\d?$/
-        unless token.word.to_i > 12 || (post_token && %w{am pm morning afternoon evening night}.include?(post_token))
-          return ScalarMonth.new(token.word.to_i)
-        end
-      end
-      return nil
-    end
-    
-    def self.scan_for_years(token, post_token)
-      if token.word =~ /^([1-9]\d)?\d\d?$/
-        unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
-          return ScalarYear.new(token.word.to_i)
-        end
-      end
-      return nil
-    end
-    
-    def to_s
-      'scalar'
-    end
-  end
-  
-  class ScalarDay < Scalar #:nodoc:
-    def to_s
-      super << '-day-' << @type.to_s
-    end
-  end
-  
-  class ScalarMonth < Scalar #:nodoc:
-    def to_s
-      super << '-month-' << @type.to_s
-    end
-  end
-  
-  class ScalarYear < Scalar #:nodoc:
-    def to_s
-      super << '-year-' << @type.to_s
-    end
-  end
+    public function scan($tokens)
+    {
+        foreach ($tokens as $i => &$token) {
+            $postToken = isset($tokens[$i + 1]) ? $tokens[$i + 1] : null;
+            if ($t = $this->scanForScalars($token, $postToken)) {
+                $token->tag($t);
+            }
+            if ($t = $this->scanForDays($token, $postToken)) {
+                $token->tag($t);
+            }
+            if ($t = $this->scanForMonths($token, $postToken)) {
+                $token->tag($t);
+            }
+            if ($t = $this->scanForYears($token, $postToken)) {
+                $token->tag($t);
+            }
+        }
+        return $tokens;
+    }
 
-end
\ No newline at end of file
+    public function scanForScalars($token, $postToken)
+    {
+        if (preg_match($this->scalarRegex, $token->word)) {
+            if (!in_array($postToken, $this->timeSignifiers)) {
+                return new self((int)$token->word);
+            }
+        }
+        return null;
+    }
+
+    public function scanForDays($token, $postToken)
+    {
+        if (preg_match($this->dayRegex, $token->word)) {
+            if ((int)$token->word <= 31 && !in_array($postToken, $this->timeSignifiers)) {
+                return new Horde_Date_Parser_Locale_Base_ScalarDay((int)$token->word);
+            }
+        }
+        return null;
+    }
+
+    public function scanForMonths($token, $postToken)
+    {
+        if (preg_match($this->monthRegex, $token->word)) {
+            if ((int)$token->word <= 12 && !in_array($postToken, $this->timeSignifiers)) {
+                return new Horde_Date_Parser_Locale_Base_ScalarMonth((int)$token->word);
+            }
+        }
+        return null;
+    }
+
+    public function scanForYears($token, $postToken)
+    {
+        if (preg_match($this->yearRegex, $token->word)) {
+            if (!in_array($postToken, $this->timeSignifiers)) {
+                return new Horde_Date_Parser_Locale_Base_ScalarYear((int)$token->word);
+            }
+        }
+        return null;
+    }
+
+    public function __toString()
+    {
+        return 'scalar';
+    }
+
+}
+
+class Horde_Date_Parser_Locale_Base_ScalarDay extends Horde_Date_Parser_Locale_Base_Scalar
+{
+    public function __toString()
+    {
+        return parent::__toString() . '-day-' . $this->type;
+    }
+
+}
+
+class Horde_Date_Parser_Locale_Base_ScalarMonth extends Horde_Date_Parser_Locale_Base_Scalar
+{
+    public function __toString()
+    {
+        return parent::__toString() . '-month-' . $this->type;
+    }
+
+}
+
+class Horde_Date_Parser_Locale_Base_ScalarYear extends Horde_Date_Parser_Locale_Base_Scalar
+{
+    public function __toString()
+    {
+        return parent::__toString() . '-year-' . $this->type;
+    }
+
+}