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\Lexers;
9:
10: /**
11: * Base for a lexer that extract and generates definition
12: * files.
13: */
14: abstract class Base extends \Peg\Lib\Signals\Signal
15: {
16:
17: /**
18: * The path where resides the files with definitions of a c/c++ library.
19: * @var string
20: */
21: public $definitions_path;
22:
23: /**
24: * Full path where the definition c/c++ header files reside, this is
25: * used to correctly resolve a header file name by stripping this path out
26: * and only leaving the real header name. This may not be required in all lexers.
27: * @var string
28: */
29: public $headers_path;
30:
31: /**
32: * The symbols object used to store definitions.
33: * @var \Peg\Lib\Definitions\Symbols
34: */
35: public $symbols;
36:
37: /**
38: * Object used to create the symbols cache files.
39: * @var \Peg\Lib\Definitions\Exporter
40: */
41: public $exporter;
42:
43: /**
44: * Object used to send message signals.
45: * @var \Peg\Lib\Signals\Data\Lexers\Message
46: */
47: protected $signal_data;
48:
49: /**
50: * Constructor.
51: * @param type $definitions_path
52: * @param type $headers_path
53: * @throws \Exception If the definitions path does not exists.
54: */
55: public function __construct($definitions_path, $headers_path="")
56: {
57: if(!file_exists($definitions_path))
58: throw new \Exception(
59: t("Definitions directory does not exists.")
60: );
61:
62: $this->definitions_path = $definitions_path;
63:
64: $this->headers_path = $headers_path;
65:
66: $this->symbols = new \Peg\Lib\Definitions\Symbols();
67:
68: $this->exporter = new \Peg\Lib\Definitions\Exporter($this->symbols);
69:
70: $this->signal_data = new \Peg\Lib\Signals\Data\Lexers\Message();
71: }
72:
73: /**
74: * Generates definition files in a specified path.
75: * Can generate definitions of a specific type if the $type is specified
76: * using one of the values from \Peg\Lib\Definitions\Type
77: * @param string $path
78: * @param string $type The type of definitions file to generate.
79: */
80: public function SaveDefinitions(
81: $path = null,
82: $type=\Peg\Lib\Definitions\Type::JSON
83: )
84: {
85: $this->exporter->definitions_path = $path;
86: $this->exporter->export_type = $type;
87:
88: $this->exporter->Start();
89: }
90:
91: /**
92: * Sends a signal with message of current task being performed.
93: * @param string $message
94: */
95: protected function SendMessage($message)
96: {
97: $this->signal_data->message = $message;
98:
99: $this->Send(
100: \Peg\Lib\Signals\Type\Lexers::LEXER_MESSAGE,
101: $this->signal_data
102: );
103: }
104:
105: /**
106: * Needs to be implemented by classes extending this one in order to
107: * begin the parsing process.
108: */
109: abstract public function Start();
110: }