int GetSubKey (const char* const* Keys, unsigned Count) /* Search for a subkey in a table of keywords. The current token must be an ** identifier and all keys must be in upper case. The identifier will be ** uppercased in the process. The function returns the index of the keyword, ** or -1 if the keyword was not found. */ { unsigned I; /* Must have an identifier */ PRECONDITION (CurTok.Tok == TOK_IDENT); /* If we aren't in ignore case mode, we have to uppercase the identifier */ if (!IgnoreCase) { UpcaseSVal (); } /* Do a linear search (a binary search is not worth the effort) */ for (I = 0; I < Count; ++I) { if (SB_CompareStr (&CurTok.SVal, Keys [I]) == 0) { /* Found it */ return I; } } /* Not found */ return -1; }
static token_t FindDotKeyword (void) /* Find the dot keyword in SVal. Return the corresponding token if found, ** return TOK_NONE if not found. */ { struct DotKeyword K; struct DotKeyword* R; /* Initialize K */ K.Key = SB_GetConstBuf (&CurTok.SVal); K.Tok = 0; /* If we aren't in ignore case mode, we have to uppercase the keyword */ if (!IgnoreCase) { UpcaseSVal (); } /* Search for the keyword */ R = bsearch (&K, DotKeywords, sizeof (DotKeywords) / sizeof (DotKeywords [0]), sizeof (DotKeywords [0]), CmpDotKeyword); if (R != 0) { return R->Tok; } else { return TOK_NONE; } }
static void ReadIdent (void) /* Read an identifier from the current input position into Ident. Filling SVal ** starts at the current position with the next character in C. It is assumed ** that any characters already filled in are ok, and the character in C is ** checked. */ { /* Read the identifier */ do { SB_AppendChar (&CurTok.SVal, C); NextChar (); } while (IsIdChar (C)); SB_Terminate (&CurTok.SVal); /* If we should ignore case, convert the identifier to upper case */ if (IgnoreCase) { UpcaseSVal (); } }
static token_t FindDotKeyword (void) /* Find the dot keyword in SVal. Return the corresponding token if found, ** return TOK_NONE if not found. */ { struct DotKeyword K; struct DotKeyword* R; /* Initialize K */ K.Key = SB_GetConstBuf (&CurTok.SVal); K.Tok = 0; /* If we aren't in ignore case mode, we have to uppercase the keyword */ if (!IgnoreCase) { UpcaseSVal (); } /* Search for the keyword */ R = bsearch (&K, DotKeywords, sizeof (DotKeywords) / sizeof (DotKeywords [0]), sizeof (DotKeywords [0]), CmpDotKeyword); if (R != 0) { /* By default, disable any somewhat experiemental DotKeyword. */ switch (R->Tok) { case TOK_ADDRSIZE: /* Disallow .ADDRSIZE function by default */ if (AddrSize == 0) { return TOK_NONE; } break; default: break; } return R->Tok; } else { return TOK_NONE; } }
static void FuncIdent (void) /* Handle the .IDENT function */ { StrBuf Buf = STATIC_STRBUF_INITIALIZER; token_t Id; unsigned I; /* Skip it */ NextTok (); /* Left paren expected */ ConsumeLParen (); /* The function expects a string argument */ if (!LookAtStrCon ()) { return; } /* Check that the string contains a valid identifier. While doing so, * determine if it is a cheap local, or global one. */ SB_Reset (&CurTok.SVal); /* Check for a cheap local symbol */ if (SB_Peek (&CurTok.SVal) == LocalStart) { SB_Skip (&CurTok.SVal); Id = TOK_LOCAL_IDENT; } else { Id = TOK_IDENT; } /* Next character must be a valid identifier start */ if (!IsIdStart (SB_Get (&CurTok.SVal))) { NoIdent (); return; } for (I = SB_GetIndex (&CurTok.SVal); I < SB_GetLen (&CurTok.SVal); ++I) { if (!IsIdChar (SB_AtUnchecked (&CurTok.SVal, I))) { NoIdent (); return; } } if (IgnoreCase) { UpcaseSVal (); } /* If anything is ok, save and skip the string. Check that the next token * is a right paren, then replace the token by an identifier token. */ SB_Copy (&Buf, &CurTok.SVal); NextTok (); if (CurTok.Tok != TOK_RPAREN) { Error ("`)' expected"); } else { CurTok.Tok = Id; SB_Copy (&CurTok.SVal, &Buf); SB_Terminate (&CurTok.SVal); } /* Free buffer memory */ SB_Done (&Buf); }