1 /*****************************************************************************/
5 /* Parse a symbol name and search for it */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
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 (StrBuf* Name, StrBuf* FullName)
57 /* Parse a (possibly scoped) identifer. The scope of the name must exist and
58 * is returned as function result, while the last part (the identifier) which
59 * may be either a symbol or a scope depending on the context is returned in
60 * Name. FullName is a string buffer that is used to store the full name of
61 * the identifier including the scope. It is used internally and may be used
62 * by the caller for error messages or similar.
67 /* Clear both passed string buffers */
71 /* Get the starting table */
72 if (CurTok.Tok == TOK_NAMESPACE) {
74 /* Start from the root scope */
77 } else if (CurTok.Tok == TOK_IDENT) {
79 /* Remember the name and skip it */
80 SB_Copy (Name, &CurTok.SVal);
83 /* If no namespace symbol follows, we're already done */
84 if (CurTok.Tok != TOK_NAMESPACE) {
85 SB_Terminate (FullName);
89 /* Pass the scope back to the caller */
90 SB_Append (FullName, Name);
92 /* The scope must exist, so search for it starting with the current
95 Scope = SymFindAnyScope (CurrentScope, Name);
98 SB_Terminate (FullName);
99 Error ("No such scope: `%m%p'", FullName);
106 Error ("Identifier expected");
111 /* Skip the namespace token that follows */
112 SB_AppendStr (FullName, "::");
115 /* Resolve scopes. */
118 /* Next token must be an identifier. */
119 if (CurTok.Tok != TOK_IDENT) {
120 Error ("Identifier expected");
124 /* Remember and skip the identifier */
125 SB_Copy (Name, &CurTok.SVal);
128 /* If a namespace token follows, we search for another scope, otherwise
129 * the name is a symbol and we're done.
131 if (CurTok.Tok != TOK_NAMESPACE) {
136 /* Pass the scope back to the caller */
137 SB_Append (FullName, Name);
139 /* Search for the child scope */
140 Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
142 /* Scope not found */
143 Error ("No such scope: `%m%p'", FullName);
147 /* Skip the namespace token that follows */
148 SB_AppendStr (FullName, "::");
155 SymEntry* ParseScopedSymName (int AllocNew)
156 /* Parse a (possibly scoped) symbol name, search for it in the symbol table
157 * and return the symbol table entry.
160 StrBuf ScopeName = STATIC_STRBUF_INITIALIZER;
161 StrBuf Ident = STATIC_STRBUF_INITIALIZER;
165 /* Parse the scoped symbol name */
166 SymTable* Scope = ParseScopedIdent (&Ident, &ScopeName);
168 /* If ScopeName is empty, no scope was specified */
169 NoScope = SB_IsEmpty (&ScopeName);
171 /* We don't need ScopeName any longer */
172 SB_Done (&ScopeName);
174 /* Check if the scope is valid. Errors have already been diagnosed by
175 * the routine, so just exit.
178 /* Search for the symbol and return it. If no scope was specified,
179 * search also in the upper levels.
181 if (NoScope && !AllocNew) {
182 Sym = SymFindAny (Scope, &Ident);
184 Sym = SymFind (Scope, &Ident, AllocNew);
187 /* No scope ==> no symbol. To avoid errors in the calling routine that
188 * may not expect NULL to be returned if AllocNew is true, create a new
192 Sym = NewSymEntry (&Ident, SF_NONE);
198 /* Deallocate memory for ident */
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 = STATIC_STRBUF_INITIALIZER;
213 StrBuf Name = STATIC_STRBUF_INITIALIZER;
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 SB_Done (&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);
239 /* Free memory for name */
242 /* Return the scope found */
248 SymEntry* ParseAnySymName (int AllocNew)
249 /* Parse a cheap local symbol or a a (possibly scoped) symbol name, search
250 * for it in the symbol table and return the symbol table entry.
255 /* Distinguish cheap locals and other symbols */
256 if (CurTok.Tok == TOK_LOCAL_IDENT) {
257 Sym = SymFindLocal (SymLast, &CurTok.SVal, AllocNew);
260 Sym = ParseScopedSymName (AllocNew);
263 /* Return the symbol found */