-
Notifications
You must be signed in to change notification settings - Fork 1
/
subsume.cc
213 lines (182 loc) · 5.98 KB
/
subsume.cc
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
/*
ebnf2tikz
An optimizing compiler to convert (possibly annotated) Extended
Backus–Naur Form (EBNF) to railroad diagrams expressed as LaTeX
TikZ commands.
Copyright (C) 2021 Larry D. Pyeatt
This program 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.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Larry Pyeatt
#include <graph.hh>
#include <nodesize.hh>
using namespace std;
// ------------------------------------------------------------------------
void grammar::subsume()
{
regex_t *name;
// look for productions that are marked for subsumption
for(auto i=productions.begin();i!=productions.end();i++)
{
(*i)->dump(1);
if((name = (*i)->getSubsume()) != NULL) {
cout<<"Subsuming "<<(*i)->texName()<<". Body is\n";
(*i)->getChild(0)->dump(1);
for(auto j=productions.begin();j!=productions.end();j++)
if(j != i)
(*j)->subsume(name,(*i)->getChild(0));
cout<<endl;
}
}
}
// ------------------------------------------------------------------------
node* singlenode::subsume(regex_t* name, node *replacement){
node* tmp;
tmp = body->subsume(name,replacement);
if(tmp != body)
{
tmp->liftConcats(0);
tmp->setParent(this);
tmp->setPrevious(body->getPrevious());
tmp->setNext(body->getNext());
delete body;
body = tmp;
}
return this;
}
// ------------------------------------------------------------------------
node* multinode::subsume(regex_t* name, node *replacement){
node *tmp;
for(auto i = nodes.begin();i!=nodes.end();i++)
{
tmp = (*i)->subsume(name,replacement);
if(tmp != (*i))
{
tmp->liftConcats(0);
tmp->setParent(this);
tmp->setPrevious((*i)->getPrevious());
tmp->setNext((*i)->getNext());
tmp->setDrawToPrev((*i)->getDrawToPrev());
tmp->setBeforeSkip(0);
delete (*i);
(*i) = tmp;
}
}
return this;
}
// ------------------------------------------------------------------------
node* productionnode::subsume(regex_t* name, node *replacement) {
node *tmp;
// Production nodes always contain a concat with two leading
// nullnodes, and two trailing nullnodes. We only want to take the
// actual production, which is everything in between.
replacement = replacement->clone();
delete replacement->getChild(replacement->numChildren()-1);
replacement->forgetChild(replacement->numChildren()-1);
delete replacement->getChild(replacement->numChildren()-1);
replacement->forgetChild(replacement->numChildren()-1);
delete replacement->getChild(0);
replacement->forgetChild(0);
delete replacement->getChild(0);
replacement->forgetChild(0);
tmp = body->subsume(name,replacement);
// delete replacement;
if(tmp != body)
{
delete body;
body = tmp;
}
return this;
}
// ------------------------------------------------------------------------
#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
node* nontermnode::subsume(regex_t* name, node *replacement){
regmatch_t pmatch[1];
if(!regexec(name, str.c_str(), ARRAY_SIZE(pmatch), pmatch, 0))
{
cout<<" matched. Replacing "<<texName()<<endl;
node *tmp = replacement->clone();
cout<<" cloned "<<endl;
replacement->dump(1);
cout<<" and got "<<endl;
tmp->dump(1);
return tmp;
}
else
return this; // or pointer to this
}
// ------------------------------------------------------------------------
// fixSkips adjusts nodes that follow a newline.
void singlenode::fixSkips()
{
body->fixSkips();
}
void multinode::fixSkips()
{
for(auto i = nodes.begin(); i != nodes.end(); i++)
(*i)->fixSkips();
}
void choicenode::fixSkips()
{
multinode::fixSkips();
for(auto i = nodes.begin(); i != nodes.end(); i++)
(*i)->setBeforeSkip(0);
// if the loop follows a newline, then our rail must have a
// beforeskip. Check the loops rail. If its beforeskip is zero, then
// our rail needs a beforeskip.
if(parent != NULL &&
parent->is_concat() &&
parent->numChildren()==3 &&
this == parent->getChild(1) &&
parent->getParent() != NULL &&
(parent->getParent()->is_choice() || parent->getParent()->is_loop()) &&
parent->getParent()->getLeftRail() != NULL &&
parent->getParent()->getLeftRail()->getBeforeSkip() > 0)
previous->setBeforeSkip(0);
}
void loopnode::fixSkips()
{
multinode::fixSkips();
for(auto i = nodes.begin(); i != nodes.end(); i++)
(*i)->setBeforeSkip(0);
if(parent != NULL &&
parent->is_concat() &&
parent->numChildren()==3 &&
this == parent->getChild(1) &&
parent->getParent() != NULL &&
(parent->getParent()->is_choice() || parent->getParent()->is_loop()) &&
parent->getParent()->getLeftRail() != NULL &&
parent->getParent()->getLeftRail()->getBeforeSkip() > 0)
previous->setBeforeSkip(0);
}
void concatnode::fixSkips()
{
multinode::fixSkips();
node *gp=NULL,*ggp=NULL;
if(parent != NULL)
gp = parent->getParent();
if(gp != NULL)
ggp = gp->getParent();
if(parent != NULL && (parent->is_loop() || parent->is_choice()) &&
gp != NULL && gp->is_concat() && ggp != NULL && ggp->is_row() &&
(ggp->getPrevious() == NULL || !ggp->getPrevious()->is_newline()))
{
setBeforeSkip(0);
nodes[0]->setBeforeSkip(0);
}
if(parent != NULL && (parent->is_loop() || parent->is_choice()) &&
gp != NULL && gp->is_concat() && ggp != NULL &&
(ggp->is_loop() || ggp->is_choice()))
{
setBeforeSkip(0);
nodes[0]->setBeforeSkip(0);
}
}