-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJMForm.parser.php
More file actions
211 lines (203 loc) · 5.21 KB
/
SJMForm.parser.php
File metadata and controls
211 lines (203 loc) · 5.21 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Simple fml parser based on http://lisperator.net/pltut/parser/
*/
class SJMFormParser {
public function parse($fml) {
$is = new FMLInputStream($fml);
$ts = new FMLTokenStream($is);
$p = new FMLParser($ts);
$ast = $p->parse();
return $ast;
}
}
class FMLInputStream {
public function __construct($input) {
$this->input = $input;
$this->len = strlen($input);
$this->pos = 0;
$this->line = 1;
$this->col = 1;
}
public function next() {
$ch = substr($this->input, $this->pos, 1);
$this->pos += 1;
if ($ch == "\n") {
$this->line += 1;
$this->col = 1;
} else {
$this->col += 1;
}
return $ch;
}
public function peek() {
return substr($this->input, $this->pos, 1);
}
public function eof() {
return $this->pos >= $this->len;
}
public function pos() {
return [$this->pos, $this->line, $this->col];
}
}
class FMLTokenStream {
private $current = null;
public function __construct($input) {
$this->input = $input;
$this->pos = $input->pos();
}
public function peek() {
if (!$this->current) {
$this->current = $this->readNext();
}
return $this->current;
}
public function next() {
$tok = $this->peek();
$this->current = null;
return $tok;
}
public function eof() {
return $this->peek() === null;
}
public function croak($msg) {
list($pos, $line, $col) = $this->pos;
$error = $msg . " @ (" . $line . ":" . $col . ")";
throw new Exception($error);
}
private function readNext() {
$in = $this->input;
$this->readWhile('ctype_space');
if ($in->eof()) return null;
$ch = $in->peek();
if ($ch == "#") {
$this->readWhile(function($ch) {return $ch != "\n";});
$in->next();
return $this->readNext();
}
$this->pos = $this->input->pos();
if ($ch == '"') return $this->readString('"');
if (ctype_punct($ch)) return (object)['type' => 'punct', 'value' => $in->next()];
if (ctype_alnum($ch)) return (object)['type' => 'id', 'value' => $this->readWhile([$this, 'isIdChar'])];
// return (object)['type' => 'debug', 'value' => $in->next()];
$this->croak("Unexpected character ".json_encode($ch));
}
private function readWhile($predicate) {
$in = $this->input;
$str = '';
while (!$in->eof() && call_user_func($predicate, $in->peek())) {
$str .= $in->next();
}
return $str;
}
private function readString($end) {
$escaped = false;
$str = "";
$this->input->next();
while (!$this->input->eof()) {
$ch = $this->input->next();
if ($escaped) {
$str .= stripcslashes("\\$ch");
$escaped = false;
} elseif ($ch == "\\") {
$escaped = true;
} elseif ($ch == $end) {
break;
} else {
$str .= $ch;
}
}
return (object)['type' => 'str', 'value' => $str];
}
private function isIdChar($ch) {
// also handles numbers
return ctype_alnum($ch) || $ch == '.';
}
}
class FMLParser {
public $literals = [
"true" => true,
"false" => false,
"null" => null,
];
public function __construct($input) {
$this->input = $input;
}
public function parse() {
$a = [];
while (!$this->input->eof()) {
if ($this->testPeek('punct', ';')) {
$this->next(); // ';' are optional at the top level
} else {
$a []= $this->parseDef();
}
}
return $a;
}
public function parseDef() {
$type = $this->next('id');
$name = $this->next('id');
$args = $this->delimited('(', ')', ',', [$this, 'parseLiteral']);
$def = (object)['type' => $type->value, 'name' => $name->value, 'args' => $args];
if ($this->testPeek('punct', '{')) {
$def->childNodes = $this->delimited('{', '}', ';', [$this, 'parseDef']);
}
return $def;
}
private function parseLiteral() {
$tok = $this->input->next();
if ($tok->type == 'str') return $tok->value;
if ($tok->type == 'id') {
$val = $tok->value;
if (array_key_exists($val, $this->literals)) {
return $this->literals[$val];
}
if (is_numeric($val)) {
return $val + 0; // int or float
}
}
$this->input->croak("Invalid literal ".json_encode($tok->value));
}
private function delimited($start, $stop, $separator, $parser) {
$a = [];
$first = true;
$this->next('punct', $start);
while (!$this->input->eof()) {
if ($this->testPeek('punct', $stop)) break;
if ($first) {
$first = false;
} else {
$this->next('punct', $separator);
}
if ($this->testPeek('punct', $stop)) break;
$a []= call_user_func($parser);
}
$this->next('punct', $stop);
return $a;
}
private function testPeek($type, $value = null) {
$tok = $this->input->peek();
if (!$tok) return false;
if ($tok->type !== $type) return false;
if ($value !== null && $tok->value !== $value) return false;
return true;
}
private function next($type = null, $value = null) {
$tok = $this->input->next();
if ($value !== null) {
if (!is_array($value)) $value = [$value];
if (!in_array($tok->value, $value)) {
$values = implode(' or ', array_map('json_encode', $value));
$this->input->croak("Unexpected value ".json_encode($tok->value)." when expecting $values");
}
}
if ($type !== null) {
if (!is_array($type)) $type = [$type];
if (!in_array($tok->type, $type)) {
$types = implode(' or ', array_map('json_encode', $type));
$this->input->croak("Unexpected type ".json_encode($tok->type)." when expecting $types");
}
}
return $tok;
}
}