Mercurial > ~dholland > hg > ag > index.cgi
comparison examples/mpp/token.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, a System for Syntax Directed Programming | |
3 * C Macro preprocessor | |
4 * Token handling module | |
5 * | |
6 * Copyright 1993 Parsifal Software. All Rights Reserved. | |
7 * | |
8 * This software is provided 'as-is', without any express or implied | |
9 * warranty. In no event will the authors be held liable for any damages | |
10 * arising from the use of this software. | |
11 * | |
12 * Permission is granted to anyone to use this software for any purpose, | |
13 * including commercial applications, and to alter it and redistribute it | |
14 * freely, subject to the following restrictions: | |
15 * | |
16 * 1. The origin of this software must not be misrepresented; you must not | |
17 * claim that you wrote the original software. If you use this software | |
18 * in a product, an acknowledgment in the product documentation would be | |
19 * appreciated but is not required. | |
20 * 2. Altered source versions must be plainly marked as such, and must not be | |
21 * misrepresented as being the original software. | |
22 * 3. This notice may not be removed or altered from any source distribution. | |
23 */ | |
24 | |
25 #include "token.h" | |
26 | |
27 | |
28 // Constructor for token_accumulator | |
29 | |
30 token_accumulator::token_accumulator(int nc, int nx) { | |
31 cs = new token[nc]; // token storage | |
32 xs = new unsigned[nx]; // index storage (for levels) | |
33 csx = 0; // next free token | |
34 csmax = nc - 1; // leave room for termination eof | |
35 xsx = xsmax = nx; // pre-decrement, post-increment | |
36 xs[--xsx] = csx; // initial token string | |
37 } | |
38 | |
39 | |
40 // Destructor for token_accumulator | |
41 | |
42 token_accumulator::~token_accumulator() { | |
43 delete[] cs; | |
44 delete[] xs; | |
45 } | |
46 | |
47 | |
48 // Reset token_accumulator | |
49 | |
50 token_accumulator &reset(token_accumulator &t) { | |
51 t.csx = 0; | |
52 t.xsx = t.xsmax; | |
53 t.xs[--t.xsx] = t.csx; | |
54 return t; | |
55 } | |
56 | |
57 // Push data on token_accumulator | |
58 | |
59 token_sink &token_accumulator::operator << (token *tp) { | |
60 while (tp->id != END_OF_FILE) *this << *tp++; | |
61 return *this; | |
62 } | |
63 | |
64 | |
65 // Pop data from token_accumulator | |
66 | |
67 token_accumulator &token_accumulator::operator >> (token &c) { | |
68 assert(csx > xs[xsx]); // a token to pop | |
69 c = cs[--csx]; // pop token | |
70 return *this; | |
71 } | |
72 | |
73 | |
74 // Access data in token_accumulator | |
75 /* | |
76 token &token_accumulator::operator [](unsigned i) { | |
77 assert(i < csx - xs[xsx]); // index in bounds | |
78 return cs[csx - i - 1]; // identify token | |
79 } | |
80 */ | |
81 | |
82 // Concatenate token strings in token_accumulator | |
83 | |
84 token_accumulator &concat(token_accumulator &s) { | |
85 assert(s.xsx < s.xsmax - 1); // at least two strings | |
86 s.xsx++; | |
87 return s; | |
88 } | |
89 | |
90 | |
91 // Make permanent copy of token string | |
92 | |
93 token *copy(token_accumulator &s) { | |
94 int n = s.csx - s.xs[s.xsx] + 1; | |
95 token *c = new token[n]; | |
96 token null; | |
97 null.id = END_OF_FILE; | |
98 null.handle = 0; | |
99 s.cs[s.csx] = null; | |
100 memcpy(c, (token *) s, n*sizeof(token)); | |
101 return c; | |
102 } | |
103 | |
104 | |
105 // Send token string to token translator | |
106 | |
107 token_sink &token_translator::operator << (token *tp) { | |
108 while (tp->id != END_OF_FILE) *this << *tp++; | |
109 return *this; | |
110 } |