#include <string.h>
+/* common */
+#include "strbuf.h"
+
/* ca65 */
#include "error.h"
#include "nexttok.h"
/*****************************************************************************/
-/* Data */
-/*****************************************************************************/
-
-
-
-/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
-SymEntry* ParseScopedSymName (int AllocNew)
-/* Parse a (possibly scoped) symbol name, search for it in the symbol table
- * and return the symbol table entry.
+SymTable* ParseScopedIdent (char* Name, StrBuf* ScopeName)
+/* Parse a (possibly scoped) identifer. Name must point to a buffer big enough
+ * to hold such an identifier. The scope of the name must exist and is returned
+ * as function result, while the last part (the identifier) which may be either
+ * a symbol or a scope depending on the context is returned in Name. ScopeName
+ * is a string buffer that is used to store the name of the scope, the
+ * identifier lives in. It does contain anything but the identifier itself, so
+ * if ScopeName is empty on return, no explicit scope was specified. The full
+ * name of the identifier (including the scope) is ScopeName+Name.
*/
{
/* Get the starting table */
SymTable* Scope;
if (Tok == TOK_NAMESPACE) {
+
+ /* Start from the root scope */
Scope = RootScope;
+
+ } else if (Tok == TOK_IDENT) {
+
+ /* Remember the name and skip it */
+ strcpy (Name, SVal);
NextTok ();
+
+ /* If no namespace symbol follows, we're already done */
+ if (Tok != TOK_NAMESPACE) {
+ SB_Terminate (ScopeName);
+ return CurrentScope;
+ }
+
+ /* Pass the scope back to the caller */
+ SB_AppendStr (ScopeName, Name);
+
+ /* The scope must exist, so search for it starting with the current
+ * scope.
+ */
+ Scope = SymFindAnyScope (CurrentScope, Name);
+ if (Scope == 0) {
+ /* Scope not found */
+ SB_Terminate (ScopeName);
+ Error ("No such scope: `%s'", SB_GetConstBuf (ScopeName));
+ return 0;
+ }
+
} else {
- Scope = CurrentScope;
+
+ /* Invalid token */
+ Error ("Identifier expected");
+ SB_Terminate (ScopeName);
+ Name[0] = '\0';
+ return 0;
+
}
- /* Resolve scopes */
+ /* Skip the namespace token that follows */
+ SB_AppendStr (ScopeName, "::");
+ NextTok ();
+
+ /* Resolve scopes. */
while (1) {
- /* An identifier must follow. Remember and skip it. */
- char Name[sizeof (SVal)];
+ /* Next token must be an identifier. */
if (Tok != TOK_IDENT) {
Error ("Identifier expected");
+ SB_Terminate (ScopeName);
+ Name[0] = '\0';
return 0;
}
+
+ /* Remember and skip the identifier */
strcpy (Name, SVal);
NextTok ();
- /* If the next token is a namespace token, handle the name as the
- * name of a scope, otherwise it's the name of a symbol in that
- * scope.
+ /* If a namespace token follows, we search for another scope, otherwise
+ * the name is a symbol and we're done.
*/
+ if (Tok != TOK_NAMESPACE) {
+ /* Symbol */
+ SB_Terminate (ScopeName);
+ return Scope;
+ }
- if (Tok == TOK_NAMESPACE) {
+ /* Pass the scope back to the caller */
+ SB_AppendStr (ScopeName, Name);
- /* Search for the child scope */
- Scope = SymFindScope (Scope, Name, AllocNew);
+ /* Search for the child scope */
+ Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
+ if (Scope == 0) {
+ /* Scope not found */
+ SB_Terminate (ScopeName);
+ Error ("No such scope: `%s'", SB_GetConstBuf (ScopeName));
+ return 0;
+ }
- /* Skip the namespace token */
- NextTok ();
+ /* Skip the namespace token that follows */
+ SB_AppendStr (ScopeName, "::");
+ NextTok ();
+ }
+}
- /* If we didn't find the scope, bail out */
- if (Scope == 0) {
- return 0;
- }
+
+SymEntry* ParseScopedSymName (int AllocNew)
+/* Parse a (possibly scoped) symbol name, search for it in the symbol table
+ * and return the symbol table entry.
+ */
+{
+ StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
+ char Ident[sizeof (SVal)];
+ int NoScope;
+ SymEntry* Sym;
+
+ /* Parse the scoped symbol name */
+ SymTable* Scope = ParseScopedIdent (Ident, &ScopeName);
+
+ /* If ScopeName is empty, no scope was specified */
+ NoScope = SB_IsEmpty (&ScopeName);
+
+ /* We don't need ScopeName any longer */
+ DoneStrBuf (&ScopeName);
+
+ /* Check if the scope is valid. Errors have already been diagnosed by
+ * the routine, so just exit.
+ */
+ if (Scope) {
+ /* Search for the symbol and return it. If no scope was specified,
+ * search also in the upper levels.
+ */
+ if (NoScope && !AllocNew) {
+ Sym = SymFindAny (Scope, Ident);
+ } else {
+ Sym = SymFind (Scope, Ident, AllocNew);
+ }
+ } else {
+ /* No scope ==> no symbol. To avoid errors in the calling routine that
+ * may not expect NULL to be returned if AllocNew is true, create a new
+ * symbol.
+ */
+ if (AllocNew) {
+ Sym = NewSymEntry (Ident, SF_NONE);
} else {
+ Sym = 0;
+ }
+ }
+
+ /* Return the symbol found */
+ return Sym;
+}
+
+
- /* Search for the symbol and return it */
- return SymFind (Scope, Name, AllocNew);
+SymTable* ParseScopedSymTable (void)
+/* Parse a (possibly scoped) symbol table (scope) name, search for it in the
+ * symbol space and return the symbol table struct.
+ */
+{
+ StrBuf ScopeName = AUTO_STRBUF_INITIALIZER;
+ char Name[sizeof (SVal)];
+ int NoScope;
+
+
+ /* Parse the scoped symbol name */
+ SymTable* Scope = ParseScopedIdent (Name, &ScopeName);
+ /* If ScopeName is empty, no scope was specified */
+ NoScope = SB_IsEmpty (&ScopeName);
+
+ /* We don't need FullName any longer */
+ DoneStrBuf (&ScopeName);
+
+ /* If we got no error, search for the child scope withint the enclosing one.
+ * Beware: If no explicit parent scope was specified, search in all upper
+ * levels.
+ */
+ if (Scope) {
+ /* Search for the last scope */
+ if (NoScope) {
+ Scope = SymFindAnyScope (Scope, Name);
+ } else {
+ Scope = SymFindScope (Scope, Name, SYM_FIND_EXISTING);
}
}
+ return Scope;
}