-
Notifications
You must be signed in to change notification settings - Fork 0
/
FomlParseTree.php
executable file
·127 lines (112 loc) · 3.81 KB
/
FomlParseTree.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
<?php
class FomlParseTree
{
public $text; // text for this node
public $indent;
public $lineNo;
public $children = array(); // array of child nodes
function __construct($Text=null)
{
$this->text = $Text;
}
/*
* Converts the string in $Text into a parse
* tree with a FomlParseTree for each line, and
* all child-lines stored as an array of FomlParseTree's
* in $tree->children[]
*/
static function Parse($Text)
{
// stack keeps the ordered list of all open objects
// the top item has a magic negative indent so it can't be popped
$tree = new FomlParseTree();
$tree->indent = -1; // unbeatable indent!
$stack = array($tree);
$top = $tree;
foreach (FomlParseTree::ParseLines($Text) as $line) {
// pop items from stack with greater indents - they are closed
while ($top->indent >= $line->indent) {
array_pop($stack);
$top = $stack[count($stack)-1];
}
// add self as child of the last open item
if ($top) {
$top->children[] = $line;
}
// push self to the stack
$stack[] = $top = $line;
}
return $tree;
}
static function ParseLines($Text)
{
$continuation = "/(.*)\s+\|$/";
//$lines = preg_split('/$\R?^/m', $Text);
$lines = preg_split('/\r?\n/m', $Text);
//Dump($lines);
$nodes = array();
while (!empty($lines)) {
$line = array_shift($lines);
// join subsequent lines ending in "\s+|"
// What if only a single line ends in \s+|?
// we treat it as a 1 line continuation (ie drop the |)
if (preg_match($continuation, $line, $matches)) {
$line = $matches[1];
while (!empty($lines) && preg_match($continuation, $lines[0], $matches)) {
$line .= $matches[1];
array_shift($lines);
}
}
// requires at least one non-white character to match
// so pure whitespace lines will be skipped here
if (preg_match("/^(\s*)([^\s].*)/", $line, $matches)) {
$tree = new FomlParseTree();
$tree->indent = strlen($matches[1]);
$tree->text = $matches[2];
$nodes[] = $tree;
}
}
//Dump($nodes);
return $nodes;
}
/*
* Flatten this node and all of its children to plain text
*/
function ToText()
{
$text = $this->text;
foreach ($this->children as $child) {
$text.=" ".$child->ToText();
}
return $text;
}
/*
* Generates a FomlDoc from the parse tree.
* Returns a FomlDoc(). Walks the FomlParseTree
* and generates a corresponding tree of FomlNode
* objects.
*/
function Generate()
{
// The top node of the tree is distinguised by having $text==null
// and that causes it to be a FomlDoc which is really just a container
// for the child nodes.
if ($this->text === null) {
$node = new FomlDoc();
} else {
$node = null;
// try each class in order and keep the first one that returns a node
// since the last one is a text node which will match anything, this should always match
foreach (FomlParser::$NODE_CLASSES as $nodeClass) {
if (preg_match($nodeClass::MATCH_RE, $this->text, $matches)) {
$node = new $nodeClass($matches);
break;
}
}
}
// TODO : assert $node is not null here
$node->AddChildren($this->children);
return $node;
}
}
?>