mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-24 03:52:08 +00:00
Reuse startsIdentifier and continuesIdentifier functions (#1695)
This commit is contained in:
@@ -136,9 +136,8 @@ struct CaseInsensitive {
|
||||
}
|
||||
};
|
||||
|
||||
// This map lists all RGBASM keywords which `yylex_NORMAL` lexes as identifiers
|
||||
// (see `startsIdentifier` and `continuesIdentifier` below). All non-identifier
|
||||
// tokens are lexed separately.
|
||||
// This map lists all RGBASM keywords which `yylex_NORMAL` lexes as identifiers.
|
||||
// All non-identifier tokens are lexed separately.
|
||||
static std::unordered_map<std::string, int, CaseInsensitive, CaseInsensitive> keywordDict = {
|
||||
{"ADC", T_(SM83_ADC) },
|
||||
{"ADD", T_(SM83_ADD) },
|
||||
@@ -612,8 +611,6 @@ static bool isMacroChar(char c) {
|
||||
static int peek();
|
||||
static void shiftChar();
|
||||
static uint32_t readNumber(int radix, uint32_t baseValue);
|
||||
static bool startsIdentifier(int c);
|
||||
static bool continuesIdentifier(int c);
|
||||
|
||||
static uint32_t readBracketedMacroArgNum() {
|
||||
bool disableMacroArgs = lexerState->disableMacroArgs;
|
||||
@@ -1215,15 +1212,6 @@ static uint32_t readGfxConstant() {
|
||||
|
||||
// Functions to read identifiers and keywords
|
||||
|
||||
static bool startsIdentifier(int c) {
|
||||
// Anonymous labels internally start with '!'
|
||||
return (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a') || c == '.' || c == '_';
|
||||
}
|
||||
|
||||
static bool continuesIdentifier(int c) {
|
||||
return startsIdentifier(c) || (c <= '9' && c >= '0') || c == '#' || c == '$' || c == '@';
|
||||
}
|
||||
|
||||
static Token readIdentifier(char firstChar, bool raw) {
|
||||
std::string identifier(1, firstChar);
|
||||
int tokenType = firstChar == '.' ? T_(LOCAL) : T_(SYMBOL);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "asm/symbol.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <unordered_map>
|
||||
@@ -9,6 +10,7 @@
|
||||
|
||||
#include "error.hpp"
|
||||
#include "helpers.hpp" // assume
|
||||
#include "util.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
#include "asm/fstack.hpp"
|
||||
@@ -130,6 +132,11 @@ static void updateSymbolFilename(Symbol &sym) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool isValidIdentifier(std::string const &s) {
|
||||
return !s.empty() && startsIdentifier(s[0])
|
||||
&& std::all_of(s.begin() + 1, s.end(), [](char c) { return continuesIdentifier(c); });
|
||||
}
|
||||
|
||||
static void alreadyDefinedError(Symbol const &sym, char const *asType) {
|
||||
if (sym.isBuiltin && !sym_FindScopedValidSymbol(sym.name)) {
|
||||
// `DEF()` would return false, so we should not claim the symbol is already defined
|
||||
@@ -142,11 +149,13 @@ static void alreadyDefinedError(Symbol const &sym, char const *asType) {
|
||||
fputs(" at ", stderr);
|
||||
dumpFilename(sym);
|
||||
if (sym.type == SYM_EQUS) {
|
||||
fprintf(
|
||||
stderr,
|
||||
" (should it be {interpolated} to define its contents \"%s\"?)\n",
|
||||
sym.getEqus()->c_str()
|
||||
);
|
||||
if (std::string const &contents = *sym.getEqus(); isValidIdentifier(contents)) {
|
||||
fprintf(
|
||||
stderr,
|
||||
" (should it be {interpolated} to define its contents \"%s\"?)\n",
|
||||
contents.c_str()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user