1: <?php
2: /**
3: * @author Jefferson González
4: * @license MIT
5: * @link http://github.com/peg-org/peg-src Source code.
6: */
7:
8: namespace Peg\Lib\Definitions\Element;
9:
10: /**
11: * Represents a variable type.
12: */
13: class VariableType
14: {
15:
16: /**
17: * Type of the variable without modifiers (eg: int, double, char, etc...)
18: * @var string
19: */
20: public $type;
21:
22: /**
23: * Original type of the variable with all modifiers as passed on constructor.
24: * @var string
25: */
26: public $original_type;
27:
28: /**
29: * Flag that indicates if the parameter is constant (const).
30: * @var bool
31: */
32: public $is_const;
33:
34: /**
35: * Flag that indicates if the variable is unsigned.
36: * @var bool
37: */
38: public $is_unsigned;
39:
40: /**
41: * Flag that indicates if the variable is a reference (&).
42: * @var bool
43: */
44: public $is_reference;
45:
46: /**
47: * Flag that indicates if the variable is a pointer reference (*).
48: * @var bool
49: */
50: public $is_pointer;
51:
52: /**
53: * Amount of pointers indirection (*).
54: * @var int
55: */
56: public $indirection_level;
57:
58: /**
59: * Flag that indicates if the variable is an array ([]).
60: * @var bool
61: */
62: public $is_array;
63:
64: /**
65: * Description of the element.
66: * @var string
67: */
68: public $description;
69:
70:
71: /**
72: * Create a variable type.
73: * @param string $type Parameter type by specification, eg: const int*
74: * @param string $description
75: */
76: public function __construct($type, $description="")
77: {
78: $this->original_type = $type;
79:
80: $this->type = str_replace(
81: array("const ", "&", "*"),
82: "",
83: $type
84: );
85:
86: $this->description = $description;
87:
88: if(substr_count($type, "const ") > 0)
89: {
90: $this->is_const = true;
91: }
92: else
93: {
94: $this->is_const = false;
95: }
96:
97: if(substr_count($type, "unsigned ") > 0)
98: {
99: $this->is_unsigned = true;
100: }
101: else
102: {
103: $this->is_unsigned = false;
104: }
105:
106: if(substr_count($type, "&") > 0)
107: {
108: $this->is_reference = true;
109: }
110: else
111: {
112: $this->is_reference = false;
113: }
114:
115: if(($indirection = substr_count($type, "*")) > 0)
116: {
117: $this->is_pointer = true;
118: $this->indirection_level = $indirection;
119: }
120: else
121: {
122: $this->is_pointer = false;
123: $this->indirection_level = 0;
124: }
125:
126: if(substr_count($type, "[]") > 0)
127: {
128: $this->is_array = true;
129: }
130: else
131: {
132: $this->is_array = false;
133: }
134: }
135:
136: /**
137: * Converts the type specifications to c/c++ code that can be
138: * added when generating code.
139: * eg: const int*
140: * @return string
141: */
142: public function GetDeclarationCode()
143: {
144: $code = "";
145:
146: if($this->is_const)
147: $code .= "const ";
148:
149: if($this->is_unsigned)
150: $code .= "unsigned ";
151:
152: $code .= $this->type;
153:
154: if($this->is_reference)
155: $code .= "&";
156:
157: if($this->is_pointer)
158: {
159: for($i=0; $i<$this->indirection_level; $i++)
160: {
161: $code .= "*";
162: }
163: }
164:
165: if($this->is_array)
166: $code .= "[]";
167: }
168:
169: }