HandlebarsParser

Handlebars parser (based on mustache).

Defined (2)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Parser.php  
  1. class Parser 
  2. /** 
  3. * Process array of tokens and convert them into parse tree 
  4. * @param array $tokens Set of 
  5. * @return array Token parse tree 
  6. */ 
  7. public function parse(array $tokens = array()) 
  8. return $this->_buildTree(new \ArrayIterator($tokens)); 
  9.  
  10. /** 
  11. * Helper method for recursively building a parse tree. 
  12. * Trim right and trim left is a bit tricky here. 
  13. * {{#begin~}}{{TOKEN}}, TOKEN.. {{LAST}}{{~/begin}} is translated to: 
  14. * {{#begin}}{{~TOKEN}}, TOKEN.. {{LAST~}}{{/begin}} 
  15. * @param \ArrayIterator $tokens Stream of tokens 
  16. * @throws \LogicException when nesting errors or mismatched section tags 
  17. * are encountered. 
  18. * @return array Token parse tree 
  19. */ 
  20. private function _buildTree(\ArrayIterator $tokens) 
  21. $stack = array(); 
  22.  
  23. do { 
  24. $token = $tokens->current(); 
  25. $tokens->next(); 
  26.  
  27. if ($token !== null) { 
  28. switch ($token[Tokenizer::TYPE]) { 
  29. case Tokenizer::T_END_SECTION: 
  30. $newNodes = array($token); 
  31. do { 
  32. $result = array_pop($stack); 
  33. if ($result === null) { 
  34. throw new \LogicException( 
  35. 'Unexpected closing tag: /' . $token[Tokenizer::NAME] 
  36. ); 
  37.  
  38. if (!array_key_exists(Tokenizer::NODES, $result) 
  39. && isset($result[Tokenizer::NAME]) 
  40. && $result[Tokenizer::NAME] == $token[Tokenizer::NAME] 
  41. ) { 
  42. if (isset($result[Tokenizer::TRIM_RIGHT]) && $result[Tokenizer::TRIM_RIGHT]) { 
  43. // If the start node has trim right, then its equal with the first item in the loop with 
  44. // Trim left 
  45. $newNodes[0][Tokenizer::TRIM_LEFT] = true; 
  46.  
  47. if (isset($token[Tokenizer::TRIM_RIGHT]) && $token[Tokenizer::TRIM_RIGHT]) { 
  48. //OK, if we have trim right here, we should pass it to the upper level. 
  49. $result[Tokenizer::TRIM_RIGHT] = true; 
  50.  
  51. $result[Tokenizer::NODES] = $newNodes; 
  52. $result[Tokenizer::END] = $token[Tokenizer::INDEX]; 
  53. array_push($stack, $result); 
  54. break; 
  55. } else { 
  56. array_unshift($newNodes, $result); 
  57. } while (true); 
  58. break; 
  59. default: 
  60. array_push($stack, $token); 
  61.  
  62. } while ($tokens->valid()); 
  63.  
  64. return $stack; 
  65.  
/vendor/xamin/handlebars.php/src/Handlebars/Parser.php  
  1. class Parser 
  2. /** 
  3. * Process array of tokens and convert them into parse tree 
  4. * @param array $tokens Set of 
  5. * @return array Token parse tree 
  6. */ 
  7. public function parse(array $tokens = array()) 
  8. return $this->_buildTree(new \ArrayIterator($tokens)); 
  9.  
  10. /** 
  11. * Helper method for recursively building a parse tree. 
  12. * Trim right and trim left is a bit tricky here. 
  13. * {{#begin~}}{{TOKEN}}, TOKEN.. {{LAST}}{{~/begin}} is translated to: 
  14. * {{#begin}}{{~TOKEN}}, TOKEN.. {{LAST~}}{{/begin}} 
  15. * @param \ArrayIterator $tokens Stream of tokens 
  16. * @throws \LogicException when nesting errors or mismatched section tags 
  17. * are encountered. 
  18. * @return array Token parse tree 
  19. */ 
  20. private function _buildTree(\ArrayIterator $tokens) 
  21. $stack = array(); 
  22.  
  23. do { 
  24. $token = $tokens->current(); 
  25. $tokens->next(); 
  26.  
  27. if ($token !== null) { 
  28. switch ($token[Tokenizer::TYPE]) { 
  29. case Tokenizer::T_END_SECTION: 
  30. $newNodes = array($token); 
  31. do { 
  32. $result = array_pop($stack); 
  33. if ($result === null) { 
  34. throw new \LogicException( 
  35. 'Unexpected closing tag: /' . $token[Tokenizer::NAME] 
  36. ); 
  37.  
  38. if (!array_key_exists(Tokenizer::NODES, $result) 
  39. && isset($result[Tokenizer::NAME]) 
  40. && $result[Tokenizer::NAME] == $token[Tokenizer::NAME] 
  41. ) { 
  42. if (isset($result[Tokenizer::TRIM_RIGHT]) && $result[Tokenizer::TRIM_RIGHT]) { 
  43. // If the start node has trim right, then its equal with the first item in the loop with 
  44. // Trim left 
  45. $newNodes[0][Tokenizer::TRIM_LEFT] = true; 
  46.  
  47. if (isset($token[Tokenizer::TRIM_RIGHT]) && $token[Tokenizer::TRIM_RIGHT]) { 
  48. //OK, if we have trim right here, we should pass it to the upper level. 
  49. $result[Tokenizer::TRIM_RIGHT] = true; 
  50.  
  51. $result[Tokenizer::NODES] = $newNodes; 
  52. $result[Tokenizer::END] = $token[Tokenizer::INDEX]; 
  53. array_push($stack, $result); 
  54. break; 
  55. } else { 
  56. array_unshift($newNodes, $result); 
  57. } while (true); 
  58. break; 
  59. default: 
  60. array_push($stack, $token); 
  61.  
  62. } while ($tokens->valid()); 
  63.  
  64. return $stack; 
  65.