Skip to content
Browse files

Fixed whitespaces

  • Loading branch information...
1 parent 241b9e9 commit 3699ddb0525e5cf0a28d41ab021c80c86c82674c @speedmax committed Feb 20, 2009
Showing with 445 additions and 446 deletions.
  1. +1 −1 README.md
  2. +84 −84 h2o/nodes.php
  3. +280 −280 h2o/parser.php
  4. +80 −81 specs/parser_spec.php
View
2 README.md
@@ -3,7 +3,7 @@ H2O template markup
-H2o template
+H2o template
------------------------
H2O is markup language for PHP that taken a lot of inspiration from django.
View
168 h2o/nodes.php
@@ -1,85 +1,85 @@
-<?php
-/*
- Nodes
-*/
-
-class H2o_Node {
- var $position;
- function __construct($argstring) {}
-
- function render($context, $stream) {}
-}
-
-class NodeList extends H2o_Node implements IteratorAggregate {
- var $parser;
- var $list;
-
- function __construct(&$parser, $initial = null, $position = 0) {
- $this->parser = $parser;
- if (is_null($initial))
- $initial = array();
- $this->list = $initial;
- $this->position = $position;
- }
-
- function render($context, $stream) {
- foreach($this->list as $node) {
- $node->render($context, $stream);
- }
- }
-
- function append($node) {
- array_push($this->list, $node);
- }
-
- function extend($nodes) {
- array_merge($this->list, $nodes);
- }
-
- function getLength() {
- return count($this->list);
- }
-
- public function getIterator() {
- return new ArrayIterator( $this->list );
- }
-}
-
-class VariableNode extends H2o_Node {
- private $filters = false;
- var $variable;
-
- function __construct($variable, $filters, $position = 0) {
- if (!empty($filters))
- $this->filters = $filters;
- $this->variable = $variable;
- }
-
- function render($context, $stream) {
- $value = $context->resolve($this->variable);
- if ($this->filters)
- $value = $context->applyFilters($value, $this->filters);
- $stream->write($value);
- }
-}
-
-class CommentNode extends H2o_Node {}
-
-class TextNode extends H2o_Node {
- var $content;
- function __construct($content, $position = 0) {
- $this->content = $content;
- $this->position = $position;
- }
-
- function render($context, $stream) {
- $stream->write($this->content);
- }
-
- function is_blank() {
- return strlen(trim($this->content));
- }
-}
-
-
+<?php
+/*
+ Nodes
+*/
+
+class H2o_Node {
+ var $position;
+ function __construct($argstring) {}
+
+ function render($context, $stream) {}
+}
+
+class NodeList extends H2o_Node implements IteratorAggregate {
+ var $parser;
+ var $list;
+
+ function __construct(&$parser, $initial = null, $position = 0) {
+ $this->parser = $parser;
+ if (is_null($initial))
+ $initial = array();
+ $this->list = $initial;
+ $this->position = $position;
+ }
+
+ function render($context, $stream) {
+ foreach($this->list as $node) {
+ $node->render($context, $stream);
+ }
+ }
+
+ function append($node) {
+ array_push($this->list, $node);
+ }
+
+ function extend($nodes) {
+ array_merge($this->list, $nodes);
+ }
+
+ function getLength() {
+ return count($this->list);
+ }
+
+ public function getIterator() {
+ return new ArrayIterator( $this->list );
+ }
+}
+
+class VariableNode extends H2o_Node {
+ private $filters = false;
+ var $variable;
+
+ function __construct($variable, $filters, $position = 0) {
+ if (!empty($filters))
+ $this->filters = $filters;
+ $this->variable = $variable;
+ }
+
+ function render($context, $stream) {
+ $value = $context->resolve($this->variable);
+ if ($this->filters)
+ $value = $context->applyFilters($value, $this->filters);
+ $stream->write($value);
+ }
+}
+
+class CommentNode extends H2o_Node {}
+
+class TextNode extends H2o_Node {
+ var $content;
+ function __construct($content, $position = 0) {
+ $this->content = $content;
+ $this->position = $position;
+ }
+
+ function render($context, $stream) {
+ $stream->write($this->content);
+ }
+
+ function is_blank() {
+ return strlen(trim($this->content));
+ }
+}
+
+
?>
View
560 h2o/parser.php
@@ -1,281 +1,281 @@
-<?php
-class H2o_Lexer {
- function __construct($options = array()) {
- $this->options = $options;
-
- if ($this->options['TRIM_TAGS'])
- $trim = '(?:\r?\n)?';
-
- $this->pattern = ('/(.*?)(?:' .
- preg_quote($this->options['BLOCK_START']). '(.*?)' .preg_quote($this->options['BLOCK_END']) . $trim . '|' .
- preg_quote($this->options['VARIABLE_START']). '(.*?)' .preg_quote($this->options['VARIABLE_END']) . '|' .
- preg_quote($this->options['COMMENT_START']). '(.*?)' .preg_quote($this->options['COMMENT_END']) . $trim . ')/sm'
- );
- }
-
- function tokenize($source) {
- $result = new TokenStream;
- $pos = 0;
- $matches = array();
- preg_match_all($this->pattern, $source, $matches, PREG_SET_ORDER);
-
- foreach ($matches as $match) {
- if ($match[1])
- $result->feed('text', $match[1], $pos);
- $tagpos = $pos + strlen($match[1]);
- if ($match[2])
- $result->feed('block', trim($match[2]), $tagpos);
- elseif ($match[3])
- $result->feed('variable', trim($match[3]), $tagpos);
- elseif ($match[4])
- $result->feed('comment', trim($match[4]), $tagpos);
- $pos += strlen($match[0]);
- }
- if ($pos < strlen($source)){
- $result->feed('text', substr($source, $pos), $pos);
- }
- $result->close();
- return $result;
- }
-}
-
-class H2o_Parser {
- var $first;
- var $storage = array();
- var $filename;
- var $runtime;
-
- function __construct($source, $filename, $runtime, $options) {
- $this->options = $options;
- //$this->source = $source;
- $this->runtime = $runtime;
- $this->filename = $filename;
- $this->first = true;
-
- $this->lexer = new H2o_Lexer($options);
- $this->tokenstream = $this->lexer->tokenize($source);
- $this->storage = array(
- 'blocks' => array(),
- 'templates' => array(),
- 'included' => array()
- );
- }
-
- function &parse() {
- $until = func_get_args();
- $nodelist = new NodeList($this);
- while($token = $this->tokenstream->next()) {
- //$token = $this->tokenstream->current();
- switch($token->type) {
- case 'text' :
- $node = new TextNode($token->content, $token->position);
- break;
- case 'variable' :
- $args = H2o_Parser::parseArguments($token->content, $token->position);
- $variable = array_shift($args);
- $filters = $args;
- $node = new VariableNode($variable, $filters, $token->position);
- break;
- case 'comment' :
- $node = new CommentNode($token->content);
- break;
- case 'block' :
- if (in_array($token->content, $until)) {
- $this->token = $token;
- return $nodelist;
- }
- @list($name, $args) = preg_split('/\s+/',$token->content, 2);
- $node = H2o::createTag($name, $args, $this, $token->position);
- $this->token = $token;
- }
- $this->searching = join(',',$until);
- $this->first = false;
- $nodelist->append($node);
- }
-
- if ($until) {
- throw new TemplateSyntaxError('Unclose tag, expecting '. $until[0]);
- }
- return $nodelist;
- }
-
- function skipTo($until) {
- $this->parse($until);
- return null;
- }
-
- # Parse arguments
- static function parseArguments($source = null, $fpos = 0){
- $parser = new ArgumentLexer($source, $fpos);
- $result = array();
- $current_buffer = &$result;
- $filter_buffer = array();
- $tokens = $parser->parse();
- foreach ($tokens as $token) {
- list($token, $data) = $token;
- if ($token == 'filter_start') {
- $filter_buffer = array();
- $current_buffer = &$filter_buffer;
- }
- elseif ($token == 'filter_end') {
- if (count($filter_buffer))
- $result[] = $filter_buffer;
- $current_buffer = &$result;
- }
- elseif ($token == 'name') {
- $current_buffer[] = symbol($data);
- }
- elseif ($token == 'number' || $token == 'string') {
- $current_buffer[] = $data;
- }
- elseif ($token == 'named_argument') {
- $last = $current_buffer[count($current_buffer) - 1];
- if (!is_array($last))
- $current_buffer[] = array();
-
- $namedArgs =& $current_buffer[count($current_buffer) - 1];
- list($name,$value) = array_map('trim', explode(':', $data, 2));
-
- # if argument value is variable mark it
- $ap = new ArgumentLexer($value);
- $t = $ap->parse();
-
- if (isset($t[0][0]) && $t[0][0] == 'name' && $value !== 'true' && $value !== 'false') {
- $value = symbol($value);
- }
- $namedArgs[$name] = $value;
- }
- elseif( $token == 'operator') {
- $current_buffer[] = array('operator'=>$data);
- }
- }
- return $result;
- }
-}
-
-class H2O_RE {
- static $whitespace, $seperator, $parentheses, $pipe, $filter_end, $operator,
- $number, $string, $i18n_string, $name, $named_args;
-
- function init() {
- $r = 'strip_regex';
-
- self::$whitespace = '/\s+/m';
- self::$parentheses = '/\(|\)/m';
- self::$filter_end = '/;/';
- self::$seperator = '/,/';
- self::$pipe = '/\|/';
- self::$operator = '/\s?(>|<|>=|<=|!=|==|!|and |not |or )\s?/i';
- self::$number = '/\d+(\.\d*)?/';
- self::$name = '/[a-zA-Z][a-zA-Z0-9-_]*(?:\.[a-zA-Z_0-9][a-zA-Z0-9_-]*)*/';
-
- self::$string = '/(?:
- "([^"\\\\]*(?:\\\\.[^"\\\\]*)*)" | # Double Quote string
- \'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\' # Single Quote String
- )/xsm';
- self::$i18n_string = "/_\({$r(self::$string)}\) | {$r(self::$string)}/xsm";
-
- self::$named_args = "{
- ({$r(self::$name)})(?:{$r(self::$whitespace)})?
- :
- (?:{$r(self::$whitespace)})?({$r(self::$i18n_string)}|{$r(self::$number)}|{$r(self::$name)})
- }x";
- }
-}
-H2O_RE::init();
-
-class ArgumentLexer {
- private $source;
- private $match;
- private $pos = 0, $fpos, $eos;
- private $operator_map = array(
- '!' => 'not', '!='=> 'ne', '==' => 'eq', '>' => 'gt', '<' => 'lt', '<=' => 'le', '>=' => 'ge'
- );
-
- function __construct($source, $fpos = 0){
- if (!is_null($source))
- $this->source = $source;
- $this->fpos=$fpos;
- }
-
- function parse(){
- $result = array();
- $filtering = false;
- while (!$this->eos()) {
- $this->scan(H2O_RE::$whitespace);
- if (!$filtering) {
- if ($this->scan(H2O_RE::$operator)){
- $operator = trim($this->match);
- if(isset($this->operator_map[$operator]))
- $operator = $this->operator_map[$operator];
- $result[] = array('operator', $operator);
- }
- elseif ($this->scan(H2O_RE::$named_args))
- $result[] = array('named_argument', $this->match);
- elseif ($this->scan(H2O_RE::$name))
- $result[] = array('name', $this->match);
- elseif ($this->scan(H2O_RE::$pipe)) {
- $filtering = true;
- $result[] = array('filter_start', $this->match);
- }
- elseif ($this->scan(H2O_RE::$seperator))
- $result[] = array('separator', null);
- elseif ($this->scan(H2O_RE::$i18n_string))
- $result[] = array('string', $this->match);
- elseif ($this->scan(H2O_RE::$number))
- $result[] = array('number', $this->match);
- else
- throw new TemplateSyntaxError('unexpected character in filters : "'. $this->source[$this->pos]. '" at '.$this->getPosition());
- }
- else {
- // parse filters, with chaining and ";" as filter end character
- if ($this->scan(H2O_RE::$pipe)) {
- $result[] = array('filter_end', null);
- $result[] = array('filter_start', null);
- }
- elseif ($this->scan(H2O_RE::$seperator))
- $result[] = array('separator', null);
- elseif ($this->scan(H2O_RE::$filter_end)) {
- $result[] = array('filter_end', null);
- $filtering = false;
- }
- elseif ($this->scan(H2O_RE::$named_args))
- $result[] = array('named_argument', $this->match);
- elseif ($this->scan(H2O_RE::$name))
- $result[] = array('name', $this->match);
- elseif ($this->scan(H2O_RE::$i18n_string))
- $result[] = array('string', $this->match);
- elseif ($this->scan(H2O_RE::$number))
- $result[] = array('number', $this->match);
- else
- throw new TemplateSyntaxError('unexpected character in filters : "'. $this->source[$this->pos]. '" at '.$this->getPosition());
- }
- }
- // if we are still in the filter state, we add a filter_end token.
- if ($filtering)
- $result[] = array('filter_end', null);
- return $result;
- }
-
- # String scanner
- function scan($regexp) {
- if (preg_match($regexp . 'A', $this->source, $match, null, $this->pos)) {
- $this->match = $match[0];
- $this->pos += strlen($this->match);
- return true;
- }
- return false;
- }
-
- function eos() {
- return $this->pos >= strlen($this->source);
- }
-
- /**
- * return the position in the template
- */
- function getPosition() {
- return $this->fpos + $this->pos;
- }
-}
+<?php
+class H2o_Lexer {
+ function __construct($options = array()) {
+ $this->options = $options;
+
+ if ($this->options['TRIM_TAGS'])
+ $trim = '(?:\r?\n)?';
+
+ $this->pattern = ('/(.*?)(?:' .
+ preg_quote($this->options['BLOCK_START']). '(.*?)' .preg_quote($this->options['BLOCK_END']) . $trim . '|' .
+ preg_quote($this->options['VARIABLE_START']). '(.*?)' .preg_quote($this->options['VARIABLE_END']) . '|' .
+ preg_quote($this->options['COMMENT_START']). '(.*?)' .preg_quote($this->options['COMMENT_END']) . $trim . ')/sm'
+ );
+ }
+
+ function tokenize($source) {
+ $result = new TokenStream;
+ $pos = 0;
+ $matches = array();
+ preg_match_all($this->pattern, $source, $matches, PREG_SET_ORDER);
+
+ foreach ($matches as $match) {
+ if ($match[1])
+ $result->feed('text', $match[1], $pos);
+ $tagpos = $pos + strlen($match[1]);
+ if ($match[2])
+ $result->feed('block', trim($match[2]), $tagpos);
+ elseif ($match[3])
+ $result->feed('variable', trim($match[3]), $tagpos);
+ elseif ($match[4])
+ $result->feed('comment', trim($match[4]), $tagpos);
+ $pos += strlen($match[0]);
+ }
+ if ($pos < strlen($source)){
+ $result->feed('text', substr($source, $pos), $pos);
+ }
+ $result->close();
+ return $result;
+ }
+}
+
+class H2o_Parser {
+ var $first;
+ var $storage = array();
+ var $filename;
+ var $runtime;
+
+ function __construct($source, $filename, $runtime, $options) {
+ $this->options = $options;
+ //$this->source = $source;
+ $this->runtime = $runtime;
+ $this->filename = $filename;
+ $this->first = true;
+
+ $this->lexer = new H2o_Lexer($options);
+ $this->tokenstream = $this->lexer->tokenize($source);
+ $this->storage = array(
+ 'blocks' => array(),
+ 'templates' => array(),
+ 'included' => array()
+ );
+ }
+
+ function &parse() {
+ $until = func_get_args();
+ $nodelist = new NodeList($this);
+ while($token = $this->tokenstream->next()) {
+ //$token = $this->tokenstream->current();
+ switch($token->type) {
+ case 'text' :
+ $node = new TextNode($token->content, $token->position);
+ break;
+ case 'variable' :
+ $args = H2o_Parser::parseArguments($token->content, $token->position);
+ $variable = array_shift($args);
+ $filters = $args;
+ $node = new VariableNode($variable, $filters, $token->position);
+ break;
+ case 'comment' :
+ $node = new CommentNode($token->content);
+ break;
+ case 'block' :
+ if (in_array($token->content, $until)) {
+ $this->token = $token;
+ return $nodelist;
+ }
+ @list($name, $args) = preg_split('/\s+/',$token->content, 2);
+ $node = H2o::createTag($name, $args, $this, $token->position);
+ $this->token = $token;
+ }
+ $this->searching = join(',',$until);
+ $this->first = false;
+ $nodelist->append($node);
+ }
+
+ if ($until) {
+ throw new TemplateSyntaxError('Unclose tag, expecting '. $until[0]);
+ }
+ return $nodelist;
+ }
+
+ function skipTo($until) {
+ $this->parse($until);
+ return null;
+ }
+
+ # Parse arguments
+ static function parseArguments($source = null, $fpos = 0){
+ $parser = new ArgumentLexer($source, $fpos);
+ $result = array();
+ $current_buffer = &$result;
+ $filter_buffer = array();
+ $tokens = $parser->parse();
+ foreach ($tokens as $token) {
+ list($token, $data) = $token;
+ if ($token == 'filter_start') {
+ $filter_buffer = array();
+ $current_buffer = &$filter_buffer;
+ }
+ elseif ($token == 'filter_end') {
+ if (count($filter_buffer))
+ $result[] = $filter_buffer;
+ $current_buffer = &$result;
+ }
+ elseif ($token == 'name') {
+ $current_buffer[] = symbol($data);
+ }
+ elseif ($token == 'number' || $token == 'string') {
+ $current_buffer[] = $data;
+ }
+ elseif ($token == 'named_argument') {
+ $last = $current_buffer[count($current_buffer) - 1];
+ if (!is_array($last))
+ $current_buffer[] = array();
+
+ $namedArgs =& $current_buffer[count($current_buffer) - 1];
+ list($name,$value) = array_map('trim', explode(':', $data, 2));
+
+ # if argument value is variable mark it
+ $ap = new ArgumentLexer($value);
+ $t = $ap->parse();
+
+ if (isset($t[0][0]) && $t[0][0] == 'name' && $value !== 'true' && $value !== 'false') {
+ $value = symbol($value);
+ }
+ $namedArgs[$name] = $value;
+ }
+ elseif( $token == 'operator') {
+ $current_buffer[] = array('operator'=>$data);
+ }
+ }
+ return $result;
+ }
+}
+
+class H2O_RE {
+ static $whitespace, $seperator, $parentheses, $pipe, $filter_end, $operator,
+ $number, $string, $i18n_string, $name, $named_args;
+
+ function init() {
+ $r = 'strip_regex';
+
+ self::$whitespace = '/\s+/m';
+ self::$parentheses = '/\(|\)/m';
+ self::$filter_end = '/;/';
+ self::$seperator = '/,/';
+ self::$pipe = '/\|/';
+ self::$operator = '/\s?(>|<|>=|<=|!=|==|!|and |not |or )\s?/i';
+ self::$number = '/\d+(\.\d*)?/';
+ self::$name = '/[a-zA-Z][a-zA-Z0-9-_]*(?:\.[a-zA-Z_0-9][a-zA-Z0-9_-]*)*/';
+
+ self::$string = '/(?:
+ "([^"\\\\]*(?:\\\\.[^"\\\\]*)*)" | # Double Quote string
+ \'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\' # Single Quote String
+ )/xsm';
+ self::$i18n_string = "/_\({$r(self::$string)}\) | {$r(self::$string)}/xsm";
+
+ self::$named_args = "{
+ ({$r(self::$name)})(?:{$r(self::$whitespace)})?
+ :
+ (?:{$r(self::$whitespace)})?({$r(self::$i18n_string)}|{$r(self::$number)}|{$r(self::$name)})
+ }x";
+ }
+}
+H2O_RE::init();
+
+class ArgumentLexer {
+ private $source;
+ private $match;
+ private $pos = 0, $fpos, $eos;
+ private $operator_map = array(
+ '!' => 'not', '!='=> 'ne', '==' => 'eq', '>' => 'gt', '<' => 'lt', '<=' => 'le', '>=' => 'ge'
+ );
+
+ function __construct($source, $fpos = 0){
+ if (!is_null($source))
+ $this->source = $source;
+ $this->fpos=$fpos;
+ }
+
+ function parse(){
+ $result = array();
+ $filtering = false;
+ while (!$this->eos()) {
+ $this->scan(H2O_RE::$whitespace);
+ if (!$filtering) {
+ if ($this->scan(H2O_RE::$operator)){
+ $operator = trim($this->match);
+ if(isset($this->operator_map[$operator]))
+ $operator = $this->operator_map[$operator];
+ $result[] = array('operator', $operator);
+ }
+ elseif ($this->scan(H2O_RE::$named_args))
+ $result[] = array('named_argument', $this->match);
+ elseif ($this->scan(H2O_RE::$name))
+ $result[] = array('name', $this->match);
+ elseif ($this->scan(H2O_RE::$pipe)) {
+ $filtering = true;
+ $result[] = array('filter_start', $this->match);
+ }
+ elseif ($this->scan(H2O_RE::$seperator))
+ $result[] = array('separator', null);
+ elseif ($this->scan(H2O_RE::$i18n_string))
+ $result[] = array('string', $this->match);
+ elseif ($this->scan(H2O_RE::$number))
+ $result[] = array('number', $this->match);
+ else
+ throw new TemplateSyntaxError('unexpected character in filters : "'. $this->source[$this->pos]. '" at '.$this->getPosition());
+ }
+ else {
+ // parse filters, with chaining and ";" as filter end character
+ if ($this->scan(H2O_RE::$pipe)) {
+ $result[] = array('filter_end', null);
+ $result[] = array('filter_start', null);
+ }
+ elseif ($this->scan(H2O_RE::$seperator))
+ $result[] = array('separator', null);
+ elseif ($this->scan(H2O_RE::$filter_end)) {
+ $result[] = array('filter_end', null);
+ $filtering = false;
+ }
+ elseif ($this->scan(H2O_RE::$named_args))
+ $result[] = array('named_argument', $this->match);
+ elseif ($this->scan(H2O_RE::$name))
+ $result[] = array('name', $this->match);
+ elseif ($this->scan(H2O_RE::$i18n_string))
+ $result[] = array('string', $this->match);
+ elseif ($this->scan(H2O_RE::$number))
+ $result[] = array('number', $this->match);
+ else
+ throw new TemplateSyntaxError('unexpected character in filters : "'. $this->source[$this->pos]. '" at '.$this->getPosition());
+ }
+ }
+ // if we are still in the filter state, we add a filter_end token.
+ if ($filtering)
+ $result[] = array('filter_end', null);
+ return $result;
+ }
+
+ # String scanner
+ function scan($regexp) {
+ if (preg_match($regexp . 'A', $this->source, $match, null, $this->pos)) {
+ $this->match = $match[0];
+ $this->pos += strlen($this->match);
+ return true;
+ }
+ return false;
+ }
+
+ function eos() {
+ return $this->pos >= strlen($this->source);
+ }
+
+ /**
+ * return the position in the template
+ */
+ function getPosition() {
+ return $this->fpos + $this->pos;
+ }
+}
?>
View
161 specs/parser_spec.php
@@ -1,82 +1,81 @@
-<?php
-require_once 'spec_helper.php';
-
-class Describe_Parser_Patterns extends SimpleSpec {
-
- function should_match_basic_operators () {
- $a = array('items'=> array(1,2,3));
-
- expects($a)->should_have(3, 'items');
- expects(H2O_RE::$whitespace)->should_match(' ');
- expects(H2O_RE::$whitespace)->should_match(' ');
- }
-
- function should_match_numeric_values() {
- expects(H2O_RE::$number)->should_match('0.555');
- expects(H2O_RE::$number)->should_match('2000');
- }
-
- function should_match_string() {
- # Double quote
- expects(H2O_RE::$string)->should_match('"this is a string"');
- expects(H2O_RE::$string)->should_match('"She has \"The thing\""');
-
- # Single Quote
- expects(H2O_RE::$string)->should_match("'the doctor is good'");
- expects(H2O_RE::$string)->should_match("'She can\'t do it'");
- }
-
- function shuold_match_i18n_string() {
- expects(H2O_RE::$i18n_string)->should_match("_('hello world')");
- }
-
- function should_match_variable_name() {
- expects(H2O_RE::$name)->should_match('something');
- expects(H2O_RE::$name)->should_match('somet-hing');
- expects('something.something')->should_match(H2O_RE::$name);
- }
-
- function should_match_named_arguments() {
- expects(H2O_RE::$named_args)->should_match("name: 'peter'");
- expects(H2O_RE::$named_args)->should_match("name: variable");
- expects(H2O_RE::$named_args)->should_match("price: 0.55 ");
- expects(H2O_RE::$named_args)->should_match("age: 199 ");
- expects(H2O_RE::$named_args)->should_match("alt: _('image alt tag')");
- }
-
- function should_match_operators() {
- $operators = array('==', '>', '<', '>=', '<=', '!=');
- $logics = array('!','not', 'and', 'or');
- foreach($operators+$logics as $op) {
- expects($op)->should_match(H2O_RE::$operator);
- }
- }
-}
-
-class Describe_Argument_Lexer extends SimpleSpec {
-
- function should_parse_named_arguments() {
- $result = $this->parse("something | filter 11, name: 'something', age: 18, var: variable, active: true");
- $expected = array(
- ':something', array(':filter', 11, array('name' => "'something'", 'age' => 18, 'var' => ':variable', 'active'=>'true'))
- );
- expects($result)->should_be($expected);
- }
-
- function should_parse_variable_contains_operators() {
- expects($this->parse("org"))->should_be(array(':org'));
- expects($this->parse("dand"))->should_be(array(':dand'));
- expects($this->parse("xor"))->should_be(array(':xor'));
- expects($this->parse("notd"))->should_be(array(':notd'));
- }
-
- private function parse($string) {
- return H2o_Parser::parseArguments($string);
- }
-}
-
-class Describe_Lexer extends SimpleSpec {}
-
-class Describe_Parser extends SimpleSpec {}
-
+<?php
+require_once 'spec_helper.php';
+
+class Describe_Parser_Patterns extends SimpleSpec {
+
+ function should_match_basic_operators () {
+ $a = array('items'=> array(1,2,3));
+
+ expects($a)->should_have(3, 'items');
+ expects(H2O_RE::$whitespace)->should_match(' ');
+ expects(H2O_RE::$whitespace)->should_match(' ');
+ }
+
+ function should_match_numeric_values() {
+ expects(H2O_RE::$number)->should_match('0.555');
+ expects(H2O_RE::$number)->should_match('2000');
+ }
+
+ function should_match_string() {
+ # Double quote
+ expects(H2O_RE::$string)->should_match('"this is a string"');
+ expects(H2O_RE::$string)->should_match('"She has \"The thing\""');
+
+ # Single Quote
+ expects(H2O_RE::$string)->should_match("'the doctor is good'");
+ expects(H2O_RE::$string)->should_match("'She can\'t do it'");
+ }
+
+ function shuold_match_i18n_string() {
+ expects(H2O_RE::$i18n_string)->should_match("_('hello world')");
+ }
+
+ function should_match_variable_name() {
+ expects(H2O_RE::$name)->should_match('something');
+ expects(H2O_RE::$name)->should_match('somet-hing');
+ expects('something.something')->should_match(H2O_RE::$name);
+ }
+
+ function should_match_named_arguments() {
+ expects(H2O_RE::$named_args)->should_match("name: 'peter'");
+ expects(H2O_RE::$named_args)->should_match("name: variable");
+ expects(H2O_RE::$named_args)->should_match("price: 0.55 ");
+ expects(H2O_RE::$named_args)->should_match("age: 199 ");
+ expects(H2O_RE::$named_args)->should_match("alt: _('image alt tag')");
+ }
+
+ function should_match_operators() {
+ $operators = array('==', '>', '<', '>=', '<=', '!=');
+ $logics = array('!','not', 'and', 'or');
+ foreach($operators+$logics as $op) {
+ expects($op)->should_match(H2O_RE::$operator);
+ }
+ }
+}
+
+class Describe_Argument_Lexer extends SimpleSpec {
+
+ function should_parse_named_arguments() {
+ $result = $this->parse("something | filter 11, name: 'something', age: 18, var: variable, active: true");
+ $expected = array(
+ ':something', array(':filter', 11, array('name' => "'something'", 'age' => 18, 'var' => ':variable', 'active'=>'true'))
+ );
+ expects($result)->should_be($expected);
+ }
+
+ function should_parse_variable_contains_operators() {
+ expects($this->parse("org"))->should_be(array(':org'));
+ expects($this->parse("dand"))->should_be(array(':dand'));
+ expects($this->parse("xor"))->should_be(array(':xor'));
+ expects($this->parse("notd"))->should_be(array(':notd'));
+ }
+
+ private function parse($string) {
+ return H2o_Parser::parseArguments($string);
+ }
+}
+
+class Describe_Lexer extends SimpleSpec {}
+
+class Describe_Parser extends SimpleSpec {}
?>

0 comments on commit 3699ddb

Please sign in to comment.
Something went wrong with that request. Please try again.