summaryrefslogtreecommitdiffstats
path: root/Mustache/Parser.php
blob: 39911d6b80babca619edac6229045f0a2d2c3f72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

/*
 * This file is part of Mustache.php.
 *
 * (c) 2012 Justin Hileman
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Mustache Parser class.
 *
 * This class is responsible for turning a set of Mustache tokens into a parse tree.
 */
class Mustache_Parser
{

    /**
     * Process an array of Mustache tokens and convert them into a parse tree.
     *
     * @param array $tokens Set of Mustache tokens
     *
     * @return array Mustache token parse tree
     */
    public function parse(array $tokens = array())
    {
        return $this->buildTree(new ArrayIterator($tokens));
    }

    /**
     * Helper method for recursively building a parse tree.
     *
     * @param ArrayIterator $tokens Stream of Mustache tokens
     * @param array         $parent Parent token (default: null)
     *
     * @return array Mustache Token parse tree
     *
     * @throws LogicException when nesting errors or mismatched section tags are encountered.
     */
    private function buildTree(ArrayIterator $tokens, array $parent = null)
    {
        $nodes = array();

        do {
            $token = $tokens->current();
            $tokens->next();

            if ($token === null) {
                continue;
            } else {
                switch ($token[Mustache_Tokenizer::TYPE]) {
                    case Mustache_Tokenizer::T_SECTION:
                    case Mustache_Tokenizer::T_INVERTED:
                        $nodes[] = $this->buildTree($tokens, $token);
                        break;

                    case Mustache_Tokenizer::T_END_SECTION:
                        if (!isset($parent)) {
                            throw new LogicException('Unexpected closing tag: /'. $token[Mustache_Tokenizer::NAME]);
                        }

                        if ($token[Mustache_Tokenizer::NAME] !== $parent[Mustache_Tokenizer::NAME]) {
                            throw new LogicException('Nesting error: ' . $parent[Mustache_Tokenizer::NAME] . ' vs. ' . $token[Mustache_Tokenizer::NAME]);
                        }

                        $parent[Mustache_Tokenizer::END]   = $token[Mustache_Tokenizer::INDEX];
                        $parent[Mustache_Tokenizer::NODES] = $nodes;

                        return $parent;
                        break;

                    default:
                        $nodes[] = $token;
                        break;
                }
            }

        } while ($tokens->valid());

        if (isset($parent)) {
            throw new LogicException('Missing closing tag: ' . $parent[Mustache_Tokenizer::NAME]);
        }

        return $nodes;
    }
}