mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-04-22 01:11:42 +00:00
[script] Add Script_GetLine and handle BOM
Both came about while working on KSP modding and eventually found there way into script.py in qfmap. This gets the changes into the C implementation.
This commit is contained in:
parent
79bebde069
commit
7314ebde0c
2 changed files with 42 additions and 3 deletions
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue