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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
<?php
namespace NXP;
class nyaml
{
/**
* Состояния
*/
const PARSE_KEY = 0;
const PARSE_VALUE = 1;
const PARSE_INDENT = 2;
const PARSE_KEY_IN_QUOTE = 3;
const PARSE_KEY_IN_DQUOTE = 4;
const PARSE_VALUE_IN_QUOTE = 5;
const PARSE_VALUE_IN_DQUOTE = 6;
/**
* Термы
*/
const T_SPACE = 0;
const T_NEWLINE = 1;
const T_CHAR = 2;
const T_QUOTE = 3;
const T_DQUOTE = 4;
private $state = 0, $result = array();
/**
* Термы
* @var array
*/
private $terms = array(
self::T_SPACE => '/[\s|\t]/',
self::T_NEWLINE => '/[\n]/',
self::T_CHAR => '/[a-zA-Zа-яА-Я0-9]/',
self::T_QUOTE => '/[\']/',
self::T_DQUOTE => '/[\"]/'
);
/**
* Таблица переходов состояния
* @var array
*/
private $actions = array(
array(self::PARSE_KEY, self::T_CHAR, self::PARSE_KEY),
array(self::PARSE_KEY, self::T_SPACE, self::PARSE_VALUE),
array(self::PARSE_KEY, self::T_NEWLINE, self::PARSE_VALUE),
array(self::PARSE_VALUE, self::T_NEWLINE, self::PARSE_KEY),
array(self::PARSE_VALUE, self::T_SPACE, self::PARSE_VALUE),
array(self::PARSE_VALUE, self::T_CHAR, self::PARSE_VALUE),
array(self::PARSE_KEY, self::T_QUOTE, self::PARSE_KEY_IN_QUOTE),
array(self::PARSE_KEY, self::T_DQUOTE, self::PARSE_KEY_IN_DQUOTE),
array(self::PARSE_VALUE, self::T_QUOTE, self::PARSE_VALUE_IN_QUOTE),
array(self::PARSE_VALUE, self::T_DQUOTE, self::PARSE_VALUE_IN_DQUOTE),
array(self::PARSE_KEY_IN_QUOTE, self::T_QUOTE, self::PARSE_KEY),
array(self::PARSE_KEY_IN_DQUOTE, self::T_DQUOTE, self::PARSE_KEY),
array(self::PARSE_VALUE_IN_QUOTE, self::T_QUOTE, self::PARSE_VALUE),
array(self::PARSE_VALUE_IN_DQUOTE, self::T_DQUOTE, self::PARSE_VALUE),
array(self::PARSE_KEY_IN_QUOTE, self::T_CHAR, self::PARSE_KEY_IN_QUOTE),
array(self::PARSE_KEY_IN_DQUOTE, self::T_CHAR, self::PARSE_KEY_IN_DQUOTE),
array(self::PARSE_VALUE_IN_QUOTE, self::T_CHAR, self::PARSE_VALUE_IN_QUOTE),
array(self::PARSE_VALUE_IN_DQUOTE, self::T_CHAR, self::PARSE_VALUE_IN_DQUOTE),
array(self::PARSE_KEY_IN_QUOTE, self::T_SPACE, self::PARSE_KEY_IN_QUOTE),
array(self::PARSE_KEY_IN_DQUOTE, self::T_SPACE, self::PARSE_KEY_IN_DQUOTE),
array(self::PARSE_VALUE_IN_QUOTE, self::T_SPACE, self::PARSE_VALUE_IN_QUOTE),
array(self::PARSE_VALUE_IN_DQUOTE, self::T_SPACE, self::PARSE_VALUE_IN_DQUOTE),
array(self::PARSE_KEY_IN_QUOTE, self::T_NEWLINE, self::PARSE_KEY_IN_QUOTE),
array(self::PARSE_KEY_IN_DQUOTE, self::T_NEWLINE, self::PARSE_KEY_IN_DQUOTE),
array(self::PARSE_VALUE_IN_QUOTE, self::T_NEWLINE, self::PARSE_VALUE_IN_QUOTE),
array(self::PARSE_VALUE_IN_DQUOTE, self::T_NEWLINE, self::PARSE_VALUE_IN_DQUOTE),
);
public function file($fileName)
{
//Parse file
return $this->string(file_get_contents($fileName));
}
public function string($nyaml)
{
$result = array();
$key = '';
$value = '';
for ($i = 0; $i < strlen($nyaml); $i++) {
$char = substr($nyaml, $i, 1);
$oldState = $this->state;
$newState = $this->getState($char);
$this->state = $newState;
if (($newState == self::PARSE_KEY) && ($oldState == self::PARSE_VALUE)) {
$result[$key] = $value;
$key = '';
$value = '';
}
if (($newState == self::PARSE_KEY) && ($oldState == self::PARSE_KEY) && ($oldState != self::PARSE_VALUE_IN_QUOTE) && ($oldState != self::PARSE_VALUE_IN_DQUOTE)) {
$key.=$char;
}
if ( ($newState == self::PARSE_VALUE) && ($oldState != self::PARSE_VALUE_IN_QUOTE) && ($oldState != self::PARSE_VALUE_IN_DQUOTE) ) {
$value.=$char;
}
if (($newState == self::PARSE_KEY_IN_QUOTE) && ($oldState != self::PARSE_KEY)) {
$key.=$char;
}
if (($newState == self::PARSE_KEY_IN_DQUOTE) && ($oldState != self::PARSE_KEY)) {
$key.=$char;
}
if (($newState == self::PARSE_VALUE_IN_QUOTE) && ($oldState != self::PARSE_VALUE)) {
$value.=$char;
}
if (($newState == self::PARSE_VALUE_IN_DQUOTE) && ($oldState != self::PARSE_VALUE)) {
$value.=$char;
}
}
$result[$key] = $value;
return $result;
}
public function getState($char)
{
$currentTerm = null;
foreach ($this->terms as $key => $term) {
if (preg_match($term, $char)) {
$currentTerm = $key;
}
}
if ($currentTerm === null) {
throw new Exception('Parse error');
}
foreach ($this->actions as $action) {
if (( $action[0] == $this->state ) && ( $action[1] == $currentTerm )) {
return $action[2];
}
}
}
}
|