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* FullName)
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. FullName
61 * is a string buffer that is used to store the full name of the identifier
62 * including the scope. It is used internally and may be used by the caller
63 * for error messages or similar.
66 /* Get the starting table */
68 if (Tok == TOK_NAMESPACE) {
70 /* Start from the root scope */
73 } else if (Tok == TOK_IDENT) {
75 /* Remember the name and skip it */
76 SB_AppendStr (FullName, strcpy (Name, SVal));
79 /* If no namespace symbol follows, we're already done */
80 if (Tok != TOK_NAMESPACE) {
81 SB_Terminate (FullName);
85 /* The scope must exist, so search for it starting with the current
88 Scope = SymFindAnyScope (CurrentScope, Name);
91 SB_Terminate (FullName);
92 Error ("No such scope: `%s'", SB_GetConstBuf (FullName));
99 Error ("Identifier expected");
100 SB_Terminate (FullName);
106 /* Skip the namespace token that follows */
107 SB_AppendStr (FullName, "::");
110 /* Resolve scopes. */
113 /* Next token must be an identifier. */
114 if (Tok != TOK_IDENT) {
115 Error ("Identifier expected");
116 SB_Terminate (FullName);
121 /* Remember and skip the identifier */
122 SB_AppendStr (FullName, strcpy (Name, SVal));
125 /* If a namespace token follows, we search for another scope, otherwise
126 * the name is a symbol and we're done.
128 if (Tok != TOK_NAMESPACE) {
130 SB_Terminate (FullName);
134 /* Search for the child scope */
135 Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
137 /* Scope not found */
138 SB_Terminate (FullName);
139 Error ("No such scope: `%s'", SB_GetConstBuf (FullName));
143 /* Skip the namespace token that follows */
144 SB_AppendStr (FullName, "::");
151 SymEntry* ParseScopedSymName (int AllocNew)
152 /* Parse a (possibly scoped) symbol name, search for it in the symbol table
153 * and return the symbol table entry.
156 StrBuf FullName = AUTO_STRBUF_INITIALIZER;
157 char Ident[sizeof (SVal)];
159 /* Parse the scoped symbol name */
160 SymTable* Scope = ParseScopedIdent (Ident, &FullName);
162 /* We don't need FullName any longer */
163 DoneStrBuf (&FullName);
165 /* Check if the scope is valid. Errors have already been diagnosed by
166 * the routine, so just exit.
169 /* Search for the symbol and return it */
170 return SymFind (Scope, Ident, AllocNew);
172 /* No scope ==> no symbol. To avoid errors in the calling routine that
173 * may not expect NULL to be returned if AllocNew is true, create a new
177 return NewSymEntry (Ident, SF_NONE);
186 SymTable* ParseScopedSymTable (void)
187 /* Parse a (possibly scoped) symbol table (scope) name, search for it in the
188 * symbol space and return the symbol table struct.
191 StrBuf FullName = AUTO_STRBUF_INITIALIZER;
192 char Ident[sizeof (SVal)];
194 /* Parse the scoped symbol name */
195 SymTable* Scope = ParseScopedIdent (Ident, &FullName);
197 /* We don't need FullName any longer */
198 DoneStrBuf (&FullName);
200 /* Check if the scope is valid. Errors have already been diagnosed by
201 * the routine, so just exit.
204 /* Search for the last scope */
205 Scope = SymFindScope (Scope, Ident, SYM_FIND_EXISTING);