Mercurial > cpython-lru_cache-weakref
annotate Parser/node.c @ 13778:f6e4a9dd67cd legacy-trunk
Rename the "parser" package to "parsers" to be compatible with the PyXML
project.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Mon, 31 Jul 2000 23:59:54 +0000 |
| parents | e78153e179fb |
| children | 21ec69a56228 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
2 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
5 All rights reserved. |
| 145 | 6 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
7 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12970
diff
changeset
|
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 145 | 9 ******************************************************************/ |
| 10 | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
11 #include <limits.h> |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
12 |
| 6 | 13 /* Parse tree node implementation */ |
| 14 | |
| 86 | 15 #include "pgenheaders.h" |
| 6 | 16 #include "node.h" |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
17 #include "errcode.h" |
| 6 | 18 |
| 19 node * | |
| 13659 | 20 PyNode_New(int type) |
| 6 | 21 { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
22 node *n = PyMem_NEW(node, 1); |
| 6 | 23 if (n == NULL) |
| 24 return NULL; | |
| 25 n->n_type = type; | |
| 26 n->n_str = NULL; | |
| 86 | 27 n->n_lineno = 0; |
| 6 | 28 n->n_nchildren = 0; |
| 29 n->n_child = NULL; | |
| 30 return n; | |
| 31 } | |
| 32 | |
| 33 #define XXX 3 /* Node alignment factor to speed up realloc */ | |
| 34 #define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX) | |
| 35 | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
36 int |
| 13659 | 37 PyNode_AddChild(register node *n1, int type, char *str, int lineno) |
| 6 | 38 { |
| 39 register int nch = n1->n_nchildren; | |
| 40 register int nch1 = nch+1; | |
| 41 register node *n; | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
42 if (nch == SHRT_MAX || nch < 0) |
|
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
43 return E_OVERFLOW; |
| 6 | 44 if (XXXROUNDUP(nch) < nch1) { |
| 45 n = n1->n_child; | |
| 46 nch1 = XXXROUNDUP(nch1); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
47 PyMem_RESIZE(n, node, nch1); |
| 6 | 48 if (n == NULL) |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
49 return E_NOMEM; |
| 6 | 50 n1->n_child = n; |
| 51 } | |
| 52 n = &n1->n_child[n1->n_nchildren++]; | |
| 53 n->n_type = type; | |
| 54 n->n_str = str; | |
| 86 | 55 n->n_lineno = lineno; |
| 6 | 56 n->n_nchildren = 0; |
| 57 n->n_child = NULL; | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
58 return 0; |
| 6 | 59 } |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
60 |
| 86 | 61 /* Forward */ |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
62 static void freechildren(node *); |
| 86 | 63 |
| 64 | |
| 65 void | |
| 13659 | 66 PyNode_Free(node *n) |
| 86 | 67 { |
| 68 if (n != NULL) { | |
| 69 freechildren(n); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
70 PyMem_DEL(n); |
| 86 | 71 } |
| 72 } | |
| 73 | |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
74 static void |
| 13659 | 75 freechildren(node *n) |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
76 { |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
77 int i; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
78 for (i = NCH(n); --i >= 0; ) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
79 freechildren(CHILD(n, i)); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
80 if (n->n_child != NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
81 PyMem_DEL(n->n_child); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
82 if (STR(n) != NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
83 PyMem_DEL(STR(n)); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
84 } |
