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\Llk\Rule;
38
39use Hoa\Consistency;
40
41/**
42 * Class \Hoa\Compiler\Llk\Rule.
43 *
44 * Rule parent.
45 *
46 * @copyright  Copyright © 2007-2017 Hoa community
47 * @license    New BSD License
48 */
49abstract class Rule
50{
51    /**
52     * Rule name.
53     *
54     * @var string
55     */
56    protected $_name           = null;
57
58    /**
59     * Rule's children. Can be an array of names or a single name.
60     *
61     * @var mixed
62     */
63    protected $_children       = null;
64
65    /**
66     * Node ID.
67     *
68     * @var string
69     */
70    protected $_nodeId         = null;
71
72    /**
73     * Node options.
74     *
75     * @var array
76     */
77    protected $_nodeOptions    = [];
78
79    /**
80     * Default ID.
81     *
82     * @var string
83     */
84    protected $_defaultId      = null;
85
86    /**
87     * Default options.
88     *
89     * @var array
90     */
91    protected $_defaultOptions = [];
92
93    /**
94     * For non-transitional rule: PP representation.
95     *
96     * @var string
97     */
98    protected $_pp             = null;
99
100    /**
101     * Whether the rule is transitional or not (i.e. not declared in the grammar
102     * but created by the analyzer).
103     *
104     * @var bool
105     */
106    protected $_transitional   = true;
107
108
109
110    /**
111     * Constructor.
112     *
113     * @param   string  $name        Rule name.
114     * @param   mixed   $children    Children.
115     * @param   string  $nodeId      Node ID.
116     */
117    public function __construct($name, $children, $nodeId = null)
118    {
119        $this->setName($name);
120        $this->setChildren($children);
121        $this->setNodeId($nodeId);
122
123        return;
124    }
125
126    /**
127     * Set rule name.
128     *
129     * @param   string  $name    Rule name.
130     * @return  string
131     */
132    public function setName($name)
133    {
134        $old         = $this->_name;
135        $this->_name = $name;
136
137        return $old;
138    }
139
140    /**
141     * Get rule name.
142     *
143     * @return  string
144     */
145    public function getName()
146    {
147        return $this->_name;
148    }
149
150    /**
151     * Set rule's children.
152     *
153     * @param   mixed  $children    Children.
154     * @return  mixed
155     */
156    protected function setChildren($children)
157    {
158        $old             = $this->_children;
159        $this->_children = $children;
160
161        return $old;
162    }
163
164    /**
165     * Get rule's children.
166     *
167     * @return  mixed
168     */
169    public function getChildren()
170    {
171        return $this->_children;
172    }
173
174    /**
175     * Set node ID.
176     *
177     * @param   string  $nodeId    Node ID.
178     * @return  string
179     */
180    public function setNodeId($nodeId)
181    {
182        $old = $this->_nodeId;
183
184        if (false !== $pos = strpos($nodeId, ':')) {
185            $this->_nodeId      = substr($nodeId, 0, $pos);
186            $this->_nodeOptions = str_split(substr($nodeId, $pos + 1));
187        } else {
188            $this->_nodeId      = $nodeId;
189            $this->_nodeOptions = [];
190        }
191
192        return $old;
193    }
194
195    /**
196     * Get node ID.
197     *
198     * @return  string
199     */
200    public function getNodeId()
201    {
202        return $this->_nodeId;
203    }
204
205    /**
206     * Get node options.
207     *
208     * @retrun  array
209     */
210    public function getNodeOptions()
211    {
212        return $this->_nodeOptions;
213    }
214
215    /**
216     * Set default ID.
217     *
218     * @param   string  $defaultId    Default ID.
219     * @return  string
220     */
221    public function setDefaultId($defaultId)
222    {
223        $old = $this->_defaultId;
224
225        if (false !== $pos = strpos($defaultId, ':')) {
226            $this->_defaultId      = substr($defaultId, 0, $pos);
227            $this->_defaultOptions = str_split(substr($defaultId, $pos + 1));
228        } else {
229            $this->_defaultId      = $defaultId;
230            $this->_defaultOptions = [];
231        }
232
233        return $old;
234    }
235
236    /**
237     * Get default ID.
238     *
239     * @return  string
240     */
241    public function getDefaultId()
242    {
243        return $this->_defaultId;
244    }
245
246    /**
247     * Get default options.
248     *
249     * @return  array
250     */
251    public function getDefaultOptions()
252    {
253        return $this->_defaultOptions;
254    }
255
256    /**
257     * Set PP representation of the rule.
258     *
259     * @param   string  $pp    PP representation.
260     * @return  string
261     */
262    public function setPPRepresentation($pp)
263    {
264        $old                 = $this->_pp;
265        $this->_pp           = $pp;
266        $this->_transitional = false;
267
268        return $old;
269    }
270
271    /**
272     * Get PP representation of the rule.
273     *
274     * @return  string
275     */
276    public function getPPRepresentation()
277    {
278        return $this->_pp;
279    }
280
281    /**
282     * Check whether the rule is transitional or not.
283     *
284     * @return  bool
285     */
286    public function isTransitional()
287    {
288        return $this->_transitional;
289    }
290}
291
292/**
293 * Flex entity.
294 */
295Consistency::flexEntity('Hoa\Compiler\Llk\Rule\Rule');
296