1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Compiler\Test\Integration;
38
39use Hoa\Compiler as LUT;
40use Hoa\File;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Compiler\Test\Integration\Documentation.
45 *
46 * Test suite of the examples in the documentation.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Documentation extends Test\Integration\Suite implements Test\Decorrelated
52{
53    public function case_whole_process()
54    {
55        $_grammar = <<<GRAMMAR
56%skip   space          \s
57// Scalars.
58%token  true           true
59%token  false          false
60%token  null           null
61// Strings.
62%token  quote_         "        -> string
63%token  string:string  [^"]+
64%token  string:_quote  "        -> default
65// Objects.
66%token  brace_         {
67%token _brace          }
68// Arrays.
69%token  bracket_       \[
70%token _bracket        \]
71// Rest.
72%token  colon          :
73%token  comma          ,
74%token  number         \d+
75
76value:
77    <true> | <false> | <null> | string() | object() | array() | number()
78
79string:
80    ::quote_:: <string> ::_quote::
81
82number:
83    <number>
84
85#object:
86    ::brace_:: pair() ( ::comma:: pair() )* ::_brace::
87
88#pair:
89    string() ::colon:: value()
90
91#array:
92    ::bracket_:: value() ( ::comma:: value() )* ::_bracket::
93GRAMMAR;
94        $_result = <<<GRAMMAR
95>  #object
96>  >  #pair
97>  >  >  token(string:string, foo)
98>  >  >  token(true, true)
99>  >  #pair
100>  >  >  token(string:string, bar)
101>  >  >  #array
102>  >  >  >  token(null, null)
103>  >  >  >  token(number, 42)
104
105GRAMMAR;
106
107        $this
108            ->given(
109                $grammar = new File\ReadWrite('hoa://Test/Vfs/Json.pp?type=file'),
110                $grammar->writeAll($_grammar),
111                $compiler = LUT\Llk::load($grammar)
112            )
113            ->when($ast = $compiler->parse('{"foo": true, "bar": [null, 42]}'))
114            ->then
115                ->object($ast)
116
117            ->given($dump = new LUT\Visitor\Dump())
118            ->when($result = $dump->visit($ast))
119            ->then
120                ->string($result)
121                    ->isEqualTo($_result);
122    }
123}
124