-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathast_handler.php
216 lines (194 loc) · 6.01 KB
/
ast_handler.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
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
209
210
211
212
213
214
215
216
<?php
// This file is part of Correct Writing question type - https://code.google.com/p/oasychev-moodle-plugins/
//
// Correct Writing question type is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Correct Writing is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains definition of ast handler, which can go through items of tree
*
* @package qtype_correctwriting
* @subpackage hints
* @copyright 2012 Oleg Sychev, Volgograd State Technical University
* @author Oleg Sychev <oasychev@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class qtype_correctwriting_ast_handler {
/**
* Visits array of parent nodes
* @param array $nodes
*/
public function visit_array($nodes) {
if (count($nodes)) {
foreach($nodes as $node) {
$this->visit($node);
}
}
}
/**
* Visits node, performing needed operations
* @param block_formal_langs_ast_node_base $node
* @return mixed
*/
public function visit($node) {
$children = $this->children($node);
if (count($children)) {
foreach($children as $child) {
$this->visit($child);
}
}
}
/**
* Returns children data
* @param block_formal_langs_ast_node_base $node node data
* @return mixed
*/
public function children($node) {
return $node->childs();
}
}
class qtype_correctwriting_marked_tree_builder extends qtype_correctwriting_ast_handler {
/**
* A tree as array of nodes
* @var array
*/
public $tree = array();
/**
* An array of arrays with keys as names of marked items and groups as numbers
* @var array
*/
public $markers = array();
/**
* Data for parent node
* @var block_formal_langs_ast_node_base
*/
public $parentnode;
/**
* Visits array of parent nodes
* @param array $nodes
*/
public function visit_array($nodes) {
if (count($nodes)) {
foreach($nodes as $node) {
$this->tree[] = $this->visit($node);
}
}
}
/**
* Visits node, performing needed operations
* @param block_formal_langs_ast_node_base $node
* @return mixed
*/
public function visit($node) {
$result = new stdClass();
$result->item = $node;
$result->marker = $this->marker_for_node($node);
$result->children = array();
$children = $this->children($node);
if (count($children)) {
foreach($children as $child) {
$result->children[] = $this->visit($child);
}
}
return $result;
}
/**
* Returns marker for node
* @var block_formal_langs_ast_node_base $node node data
* @return array|null data
*/
protected function marker_for_node($node) {
$number = $node->number();
$result = null;
foreach($this->markers as $firstpart => $group) {
foreach($group as $key => $list) {
if (in_array($number, $list)) {
$result = array($firstpart, $key);
}
}
}
return $result;
}
}
class qtype_correctwriting_marked_tree_remarker extends qtype_correctwriting_ast_handler {
public $changed = false;
public static function mark_tree($nodes) {
$changed = true;
while($changed) {
$r = new qtype_correctwriting_marked_tree_remarker();
$r->changed = false;
$r->visit_array($nodes);
$changed = $r->changed;
}
}
/**
* Visits node, performing needed operations
* @param stdClass $node
* @return mixed
*/
public function visit($node) {
if ($node->marker !== null) {
return;
}
$children = $this->children($node);
if (count($children)) {
$marker = null;
$first = true;
$allmarkersequal = true;
foreach($children as $child) {
if ($first) {
$marker = $child->marker;
$first = false;
} else {
$allmarkersequal = $allmarkersequal && ($child->marker == $marker && $marker !== null);
}
}
if ($allmarkersequal && $marker != NULL) {
$node->marker = $marker;
$this->changed = true;
} else {
parent::visit($node);
}
}
}
/**
* Returns children data
* @param stdClass $node node data
* @return mixed
*/
public function children($node) {
return $node->children;
}
}
class qtype_correctwriting_find_top_marked_nodes extends qtype_correctwriting_ast_handler {
public $result = array();
/**
* Visits node, performing needed operations
* @param stdClass $node
* @return mixed
*/
public function visit($node) {
if ($node->marker === null) {
parent::visit($node);
} else {
$this->result[] = $node;
}
}
/**
* Returns children data
* @param stdClass $node node data
* @return mixed
*/
public function children($node) {
return $node->children;
}
}