-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleDSLParser.php
53 lines (53 loc) · 1.37 KB
/
SimpleDSLParser.php
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
<?php
/**
* @author vglebov
* @date 2010-03-29
*/
class SimpleDSLParser
{
function __construct() {
$this->actions = array();
$this->addPattern("^\#", null); // comment
$this->addPattern("^\//", null); // comment
$this->addPattern("^\s*$", null); // blank line
}
/**
* @param String $pattern regex
* @param function $function callback
* @param array $params params for calback
*/
public function addPattern($pattern, $function, $params = array()) {
$this->actions[] = array('pattern' => $pattern, 'function' => $function, 'params' => $params);
}
/**
* @param string $text
*/
public function parse($text) {
$lines = split("[\n|\r]", $text);
foreach($lines as $line) {
$action = $this->_recognize($line);
if (empty($action)) {
throw new Exception("Can't recognize line [$line]");
}
if (isset($action['function'])) {
call_user_func_array($action['function'], $action['params']);
}
}
}
/**
* @param text $line command text
* @return array prepared callback
*/
private function _recognize($line) {
$matches = array();
foreach($this->actions as $action) {
$pattern = "#{$action['pattern']}#i";
if (preg_match($pattern, $line, $matches)) {
array_shift($matches);
$params = array_merge($action['params'], $matches);
return array('function' => $action['function'], 'params' => $params);
}
}
return null;
}
}