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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
//@beginlicenses@
//@license{chiba_tokyo}{}@
//@license{xerox}{}@
//@license{contributors}{}@
//
// Permission to use, copy, distribute and modify this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both that copyright
// notice and this permission notice appear in supporting documentation.
//
// 1997-2001 Shigeru Chiba, Tokyo Institute of Technology. make(s) no representations about the suitability of this
// software for any purpose. It is provided "as is" without express or implied
// warranty.
//
// Copyright (C) 1997-2001 Shigeru Chiba, Tokyo Institute of Technology.
//
// -----------------------------------------------------------------
//
//
// Copyright (c) 1995, 1996 Xerox Corporation.
// All Rights Reserved.
//
// Use and copying of this software and preparation of derivative works
// based upon this software are permitted. Any copy of this software or
// of any derivative work must include the above copyright notice of
// Xerox Corporation, this paragraph and the one after it. Any
// distribution of this software or derivative works must comply with all
// applicable United States export control laws.
//
// This software is made available AS IS, and XEROX CORPORATION DISCLAIMS
// ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE, AND NOTWITHSTANDING ANY OTHER PROVISION CONTAINED HEREIN, ANY
// LIABILITY FOR DAMAGES RESULTING FROM THE SOFTWARE OR ITS USE IS
// EXPRESSLY DISCLAIMED, WHETHER ARISING IN CONTRACT, TORT (INCLUDING
// NEGLIGENCE) OR STRICT LIABILITY, EVEN IF XEROX CORPORATION IS ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGES.
//
// -----------------------------------------------------------------
//
// Permission to use, copy, distribute and modify this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both that copyright
// notice and this permission notice appear in supporting documentation.
//
// Other Contributors (see file AUTHORS) make(s) no representations about the suitability of this
// software for any purpose. It is provided "as is" without express or implied
// warranty.
//
// Copyright (C) Other Contributors (see file AUTHORS)
//
//@endlicenses@
#include <iostream>
#include <opencxx/parser/Program.h>
#include <opencxx/parser/Ptree.h>
#include <opencxx/parser/auxil.h>
#include <opencxx/parser/token-names.h>
#include <opencxx/parser/PtreeUtil.h>
namespace Opencxx
{
char Program::Get()
{
if(buf[index] == '\0')
return buf[index];
else
return buf[index++];
}
void Program::Subst(Ptree* newtext, Ptree* oldtext)
{
Replace(PtreeUtil::LeftMost(oldtext), PtreeUtil::RightMost(oldtext), newtext);
}
void Program::Insert(Ptree* pos, Ptree* before_text, Ptree* after_text)
{
char* p;
if(before_text != 0){
p = PtreeUtil::LeftMost(pos);
Replace(p, p, before_text);
}
if(after_text != 0){
p = PtreeUtil::RightMost(pos);
Replace(p, p, after_text);
}
}
void Program::MinimumSubst(Ptree* newtext, Ptree* oldtext)
{
if(MinimumSubst2(newtext, oldtext))
Subst(newtext, oldtext);
}
bool Program::MinimumSubst2(Ptree* newtext, Ptree* oldtext)
{
int what;
if(oldtext == newtext) {
return false;
}
else if(oldtext == 0 || newtext == 0) {
return true;
}
else if(what = newtext->What(),
(what == ntExprStatement || what == ntTypedef)) {
return true;
}
else if(oldtext->IsLeaf() || newtext->IsLeaf()) {
return true;
}
else if(oldtext->Car() == 0 && oldtext->Cdr() == 0) {
return true;
}
else if(oldtext == newtext->Cdr()) {
Insert(oldtext, newtext->Car(), 0);
return false;
}
else if(oldtext->Car() != 0 && oldtext->Car() == PtreeUtil::Second(newtext)) {
Insert(oldtext->Car(), newtext->Car(), 0);
newtext = PtreeUtil::ListTail(newtext, 2);
if(MinimumSubst2(newtext, oldtext->Cdr())) {
if(oldtext->Cdr() == 0) {
Insert(oldtext->Car(), 0, newtext);
}
else {
Subst(newtext, oldtext->Cdr());
}
}
return false;
}
else{
bool dirty1 = MinimumSubst2(newtext->Car(), oldtext->Car());
bool dirty2 = MinimumSubst2(newtext->Cdr(), oldtext->Cdr());
if(dirty1 == dirty2) {
return dirty1;
}
else if(dirty1) {
if(oldtext->Cdr() == 0 && newtext->Cdr() == 0) {
return true;
}
else if(oldtext->Car() == 0) {
Insert(oldtext->Cdr(), newtext->Car(), 0);
}
else {
Subst(newtext->Car(), oldtext->Car());
}
}
else {
if(oldtext->Car() == 0 && newtext->Car() == 0) {
return true;
}
else if(oldtext->Cdr() == 0) {
Insert(oldtext->Car(), 0, newtext->Cdr());
}
else {
Subst(newtext->Cdr(), oldtext->Cdr());
}
}
return false;
}
}
void Program::Replace(char* startpos, char* endpos, Ptree* text)
{
if(startpos == 0 || endpos == 0)
return;
unsigned start = unsigned(startpos - buf);
unsigned end = unsigned(endpos - buf);
Replacement* p = replacement;
if(p == 0)
replacement = new Replacement(0, start, end, text);
else if (start < p->startpos)
replacement = new Replacement(p, start, end, text);
else if(p->next == 0)
p->next = new Replacement(0, start, end, text);
else{
for(; p->next != 0; p = p->next)
if(start < p->next->startpos)
break;
p->next = new Replacement(p->next, start, end, text);
}
}
/*
LineNumber() returns the line number of the line pointed to by PTR.
*/
unsigned Program::LineNumber(char* ptr, char*& filename, int& filename_length)
{
int n;
int len;
unsigned name;
int nline = 0;
unsigned pos = unsigned(ptr - buf);
if(pos > size){
// error?
filename = defaultname;
filename_length = strlen(defaultname);
return 0;
}
int line_number = -1;
filename_length = 0;
while(pos > 0){
switch(Ref(--pos)){
case '\n' :
++nline;
break;
case '#' :
len = 0;
n = ReadLineDirective(pos, -1, name, len);
if(n >= 0){ // unless #pragma
if(line_number < 0)
line_number = n + nline;
if(len > 0 && filename_length == 0){
filename = (char*)Read(name);
filename_length = len;
}
}
break;
}
if(line_number >= 0 && filename_length > 0)
return line_number;
}
if(filename_length == 0){
filename = defaultname;
filename_length = strlen(defaultname);
}
if(line_number < 0)
line_number = nline + 1;
return line_number;
}
/*
Write() saves the program as a file named FILE_NAME.
This assumes that the first line of the program is
a # line directive.
*/
void Program::Write(std::ostream& out, const char* file_name)
{
Replacement* rep = replacement;
unsigned pos;
unsigned nlines = 1;
unsigned line_number = 1;
unsigned i = 0;
char c;
unsigned filename = 0;
int filename_length = 0;
if(Ref(i) == '#')
line_number = ReadLineDirective(i, (int)line_number,
filename, filename_length);
for(; rep != 0; rep = rep->next){
pos = rep->startpos;
while(i < pos){
c = Ref(i++);
if(c == '\0'){
--i;
break;
}
out << c;
if(c == '\n'){
++nlines;
++line_number;
if(Ref(i) == '#')
line_number = ReadLineDirective(i, (int)line_number,
filename, filename_length);
}
}
if(i > 0 && Ref(i - 1) != '\n'){
out << '\n';
++nlines;
}
#if defined(_MSC_VER) || defined(IRIX_CC)
out << "#line " << nlines + 1 << " \"" << file_name << "\"\n";
#else
out << "# " << nlines + 1 << " \"" << file_name << "\"\n";
#endif
++nlines;
nlines += rep->text->Write(out);
pos = rep->endpos;
if(rep->next != 0 && rep->next->startpos <= pos){
rep = rep->next;
out << '\n';
++nlines;
nlines += rep->text->Write(out);
if(rep->endpos > pos)
pos = rep->endpos;
}
while(i < pos){
c = Ref(i++);
if(c == '\0'){
--i;
break;
}
else if(c == '\n'){
++line_number;
if(Ref(i) == '#')
line_number = ReadLineDirective(i, (int)line_number,
filename, filename_length);
}
}
#if defined(_MSC_VER) || defined(IRIX_CC)
out << "\n#line " << line_number << ' ';
#else
out << "\n# " << line_number << ' ';
#endif
++nlines;
if(filename_length > 0)
for(int j = 0; j < filename_length; ++j)
out << (char)Ref(filename + j);
else
out << '"' << defaultname << '"';
out << '\n';
++nlines;
}
while((c = Ref(i++)) != '\0'){
out << c;
if(c == '\n')
++nlines;
}
#if defined(_MSC_VER) || defined(IRIX_CC)
out << "\n#line " << nlines + 2 << " \"" << file_name << "\"\n";
#else
out << "\n# " << nlines + 2 << " \"" << file_name << "\"\n";
#endif
}
int Program::ReadLineDirective(unsigned i, int line_number,
unsigned& filename, int& filename_length)
{
char c;
do{
c = Ref(++i);
}while(is_blank(c));
#if defined(_MSC_VER) || defined(IRIX_CC)
if(i + 4 <= GetSize() && strncmp(Read(i), "line", 4) == 0){
i += 3;
do{
c = Ref(++i);
}while(is_blank(c));
}
#endif
if(is_digit(c)){ /* # <line> <file> */
unsigned num = c - '0';
for(;;){
c = Ref(++i);
if(is_digit(c))
num = num * 10 + c - '0';
else
break;
}
line_number = num - 1; /* line_number'll be incremented soon */
if(is_blank(c)){
do{
c = Ref(++i);
}while(is_blank(c));
if(c == '"'){
unsigned fname_start = i;
do{
c = Ref(++i);
} while(c != '"');
if(i > fname_start + 2){
filename = fname_start;
filename_length = int(i - fname_start + 1);
}
}
}
}
return line_number;
}
// class Program::Replacement
Program::Replacement::Replacement(Replacement* n, unsigned st,
unsigned ed, Ptree* t)
{
next = n;
startpos = st;
endpos = ed;
text = t;
}
}
|