|
| 1 | +/*********************************************************** |
| 2 | +Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | +Amsterdam, The Netherlands. |
| 4 | +
|
| 5 | + All Rights Reserved |
| 6 | +
|
| 7 | +Permission to use, copy, modify, and distribute this software and its |
| 8 | +documentation for any purpose and without fee is hereby granted, |
| 9 | +provided that the above copyright notice appear in all copies and that |
| 10 | +both that copyright notice and this permission notice appear in |
| 11 | +supporting documentation, and that the names of Stichting Mathematisch |
| 12 | +Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | +distribution of the software without specific, written prior permission. |
| 14 | +
|
| 15 | +STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | +THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | +FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | +FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | +
|
| 23 | +******************************************************************/ |
| 24 | + |
| 25 | +#include "allobjects.h" |
| 26 | +#include "node.h" |
| 27 | +#include "token.h" |
| 28 | +#include "pythonrun.h" |
| 29 | +#include "graminit.h" |
| 30 | +#include "errcode.h" |
| 31 | + |
| 32 | +object * |
| 33 | +node2tuple(n) |
| 34 | + node *n; |
| 35 | +{ |
| 36 | + if (n == NULL) { |
| 37 | + INCREF(None); |
| 38 | + return None; |
| 39 | + } |
| 40 | + if (ISNONTERMINAL(TYPE(n))) { |
| 41 | + int i; |
| 42 | + object *v, *w; |
| 43 | + v = newtupleobject(1 + NCH(n)); |
| 44 | + if (v == NULL) |
| 45 | + return v; |
| 46 | + w = newintobject(TYPE(n)); |
| 47 | + if (w == NULL) { |
| 48 | + DECREF(v); |
| 49 | + return NULL; |
| 50 | + } |
| 51 | + settupleitem(v, 0, w); |
| 52 | + for (i = 0; i < NCH(n); i++) { |
| 53 | + w = node2tuple(CHILD(n, i)); |
| 54 | + if (w == NULL) { |
| 55 | + DECREF(v); |
| 56 | + return NULL; |
| 57 | + } |
| 58 | + settupleitem(v, i+1, w); |
| 59 | + } |
| 60 | + return v; |
| 61 | + } |
| 62 | + else if (ISTERMINAL(TYPE(n))) { |
| 63 | + return mkvalue("(is)", TYPE(n), STR(n)); |
| 64 | + } |
| 65 | + else { |
| 66 | + err_setstr(SystemError, "unrecognized parse tree node type"); |
| 67 | + return NULL; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +static object * |
| 72 | +parser_parsefile(self, args) |
| 73 | + object *self; |
| 74 | + object *args; |
| 75 | +{ |
| 76 | + char *filename; |
| 77 | + FILE *fp; |
| 78 | + node *n = NULL; |
| 79 | + int err; |
| 80 | + object *res; |
| 81 | + if (!getargs(args, "s", &filename)) |
| 82 | + return NULL; |
| 83 | + fp = fopen(filename, "r"); |
| 84 | + if (fp == NULL) { |
| 85 | + err_errno(IOError); |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + err = parse_file(fp, filename, file_input, &n); |
| 89 | + fclose(fp); |
| 90 | + if (err != E_DONE) { |
| 91 | + err_input(err); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + res = node2tuple(n); |
| 95 | + freetree(n); |
| 96 | + return res; |
| 97 | +} |
| 98 | + |
| 99 | +static struct methodlist parser_methods[] = { |
| 100 | + {"parsefile", parser_parsefile}, |
| 101 | + {0, 0} /* Sentinel */ |
| 102 | +}; |
| 103 | + |
| 104 | +void |
| 105 | +initparser() |
| 106 | +{ |
| 107 | + initmodule("parser", parser_methods); |
| 108 | +} |
0 commit comments