1 /*****************************************************************************/
5 /* Parse a symbol name and search for it */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 SymTable* ParseScopedIdent (char* Name, StrBuf* ScopeName)
57 /* Parse a (possibly scoped) identifer. Name must point to a buffer big enough
58 * to hold such an identifier. The scope of the name must exist and is returned
59 * as function result, while the last part (the identifier) which may be either
60 * a symbol or a scope depending on the context is returned in Name. ScopeName
61 * is a string buffer that is used to store the name of the scope, the
62 * identifier lives in. It does contain anything but the identifier itself, so
63 * if ScopeName is empty on return, no explicit scope was specified. The full
64 * name of the identifier (including the scope) is ScopeName+Name.
67 /* Get the starting table */
69 if (Tok == TOK_NAMESPACE) {
71 /* Start from the root scope */
74 } else if (Tok == TOK_IDENT) {
76 /* Remember the name and skip it */
80 /* If no namespace symbol follows, we're already done */
81 if (Tok != TOK_NAMESPACE) {
82 SB_Terminate (ScopeName);
86 /* Pass the scope back to the caller */
87 SB_AppendStr (ScopeName, Name);
89 /* The scope must exist, so search for it starting with the current
92 Scope = SymFindAnyScope (CurrentScope, Name);
95 SB_Terminate (ScopeName);
96 Error ("No such scope: `%s'", SB_GetConstBuf (ScopeName));
103 Error ("Identifier expected");
104 SB_Terminate (ScopeName);
110 /* Skip the namespace token that follows */
111 SB_AppendStr (ScopeName, "::");
114 /* Resolve scopes. */
117 /* Next token must be an identifier. */
118 if (Tok != TOK_IDENT) {
119 Error ("Identifier expected");
120 SB_Terminate (ScopeName);
125 /* Remember and skip the identifier */
129 /* If a namespace token follows, we search for another scope, otherwise
130 * the name is a symbol and we're done.
132 if (Tok != TOK_NAMESPACE) {
134 SB_Terminate (ScopeName);
138 /* Pass the scope back to the caller */
139 SB_AppendStr (ScopeName, Name);
141 /* Search for the child scope */
142 Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
144 /* Scope not found */
145 SB_Terminate (ScopeName);
146 Error ("No such scope: `%s'", SB_GetConstBuf (ScopeName));
150 /* Skip the namespace token that follows */
151 SB_AppendStr (ScopeName, "::");
158 SymEntry* ParseScopedSymName (int AllocNew)
159 /* Parse a (possibly scoped) symbol name, search for it in the symbol table
160 * and return the symbol table entry.
163 StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
164 char Ident[sizeof (SVal)];
168 /* Parse the scoped symbol name */
169 SymTable* Scope = ParseScopedIdent (Ident, &ScopeName);
171 /* If ScopeName is empty, no scope was specified */
172 NoScope = SB_IsEmpty (&ScopeName);
174 /* We don't need ScopeName any longer */
175 DoneStrBuf (&ScopeName);
177 /* Check if the scope is valid. Errors have already been diagnosed by
178 * the routine, so just exit.
181 /* Search for the symbol and return it. If no scope was specified,
182 * search also in the upper levels.
184 if (NoScope && !AllocNew) {
185 Sym = SymFindAny (Scope, Ident);
187 Sym = SymFind (Scope, Ident, AllocNew);
190 /* No scope ==> no symbol. To avoid errors in the calling routine that
191 * may not expect NULL to be returned if AllocNew is true, create a new
195 Sym = NewSymEntry (Ident, SF_NONE);
201 /* Return the symbol found */
207 SymTable* ParseScopedSymTable (void)
208 /* Parse a (possibly scoped) symbol table (scope) name, search for it in the
209 * symbol space and return the symbol table struct.
212 StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
213 char Name[sizeof (SVal)];
217 /* Parse the scoped symbol name */
218 SymTable* Scope = ParseScopedIdent (Name, &ScopeName);
220 /* If ScopeName is empty, no scope was specified */
221 NoScope = SB_IsEmpty (&ScopeName);
223 /* We don't need FullName any longer */
224 DoneStrBuf (&ScopeName);
226 /* If we got no error, search for the child scope withint the enclosing one.
227 * Beware: If no explicit parent scope was specified, search in all upper
231 /* Search for the last scope */
233 Scope = SymFindAnyScope (Scope, Name);
235 Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);