diff --git a/include/QF/script.h b/include/QF/script.h index 540fb8db2..6f0841f05 100644 --- a/include/QF/script.h +++ b/include/QF/script.h @@ -44,8 +44,10 @@ typedef struct script_s { /// contains last error message or null if no error /// if set, no tokens will be parsed. const char *error; - /// if set, multi line quoted tokens will be treated as errors - int no_quote_lines; + /// if true, multi line quoted tokens will be treated as errors + bool no_quote_lines; + /// if true, quotes do not delimit strips + bool no_quotes; /// if set, characters in this string will always be lexed as single /// character tokens. If not set, defaults to "{}()':". Set to "" /// (empty string) to disable. Not set by default. @@ -89,6 +91,16 @@ bool Script_TokenAvailable (script_t *script, bool crossline); */ bool Script_GetToken (script_t *script, bool crossline); +/** Get the remainder of the current line as a token + + Parses the rest of the line (minus // style comments) as the token. + Trailing whitespace is not stripped. + If a token has been pushed back, it will be included in the new token. + \param script The script_t object being parsed + \return true if there is more to parse (EOF not hit) +*/ +bool Script_GetLine (script_t *script); + /** Unget the current token. Only one level of unget is supported. \param script The script_t object being parsed */ diff --git a/libs/util/script.c b/libs/util/script.c index 1f9d5c037..188efa860 100644 --- a/libs/util/script.c +++ b/libs/util/script.c @@ -55,6 +55,10 @@ Script_Start (script_t *script, const char *file, const char *data) script->line = 1; script->file = file; script->p = data; + // skip over the dreaded BOM if it's present + if (strncmp (script->p, "\xef\xbb\xbf", 3) == 0) { + script->p += 3; + } script->unget = false; script->error = 0; } @@ -120,7 +124,7 @@ Script_GetToken (script_t *script, bool crossline) } // copy token - if (*script->p == '"') { + if (!script->no_quotes && *script->p == '"') { int start_line = script->line; script->p++; token_p = script->p; @@ -160,6 +164,29 @@ Script_GetToken (script_t *script, bool crossline) return true; } +VISIBLE bool +Script_GetLine (script_t *script) +{ + const char *token_p = script->p; + while (*script->p) { + if (script->p[0] == '\n') { + script->line++; + script->p++; + break; + } + if (script->p[0] == '/' && script->p[1] == '/') { + break; + } + script->p++; + } + if (script->unget) { + dstring_appendsubstr (script->token, token_p, script->p - token_p); + } else { + dstring_copysubstr (script->token, token_p, script->p - token_p); + } + return *script->p; +} + VISIBLE void Script_UngetToken (script_t *script) {