Skip to content

Commit

Permalink
Export FScanner parser to ZScript as ScriptScanner
Browse files Browse the repository at this point in the history
  • Loading branch information
Cacodemon345 committed Dec 8, 2023
1 parent 3caa624 commit 507540b
Show file tree
Hide file tree
Showing 3 changed files with 372 additions and 2 deletions.
311 changes: 309 additions & 2 deletions src/common/engine/sc_man.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#include <inttypes.h>
#include "filesystem.h"

#include "vm.h"

// MACROS ------------------------------------------------------------------

// TYPES -------------------------------------------------------------------
Expand Down Expand Up @@ -1089,13 +1091,14 @@ void FScanner::ScriptError (const char *message, ...)
va_end (arglist);
}

ParseError = true;
if (NoFatalErrors)
{
Printf(TEXTCOLOR_RED "Script error, \"%s\"" TEXTCOLOR_RED " line %d:\n" TEXTCOLOR_RED "%s\n", ScriptName.GetChars(),
AlreadyGot ? AlreadyGotLine : Line, composed.GetChars());
return;
}
I_Error ("Script error, \"%s\" line %d:\n%s\n", ScriptName.GetChars(),
I_Error ("%sScript error, \"%s\" line %d:\n%s\n", PrependMessage.GetChars(), ScriptName.GetChars(),
AlreadyGot? AlreadyGotLine : Line, composed.GetChars());
}

Expand All @@ -1121,7 +1124,8 @@ void FScanner::ScriptMessage (const char *message, ...)
va_end (arglist);
}

Printf (TEXTCOLOR_RED "Script error, \"%s\"" TEXTCOLOR_RED " line %d:\n" TEXTCOLOR_RED "%s\n", ScriptName.GetChars(),
ParseError = true;
Printf (TEXTCOLOR_RED "%sScript error, \"%s\"" TEXTCOLOR_RED " line %d:\n" TEXTCOLOR_RED "%s\n", PrependMessage.GetChars(), ScriptName.GetChars(),
AlreadyGot? AlreadyGotLine : Line, composed.GetChars());
}

Expand Down Expand Up @@ -1383,4 +1387,307 @@ int ParseHex(const char* hex, FScriptPosition* sc)
return num;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, Create)
{
PARAM_PROLOGUE;
PARAM_INT(lump);

auto sc = new FScanner(lump);

ACTION_RETURN_POINTER(sc);
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, CreateFromString)
{
PARAM_PROLOGUE;
PARAM_STRING(name);
PARAM_STRING_VAL(script);

auto sc = new FScanner;
sc->OpenString(name.GetChars(), script);
ACTION_RETURN_POINTER(sc);
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, SavePos)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_POINTER(pos, FScanner::SavedPos);

*pos = self->SavePos();
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, RestorePos)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_POINTER(pos, FScanner::SavedPos);

self->RestorePos(*pos);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, GetStringContents)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

ACTION_RETURN_STRING(FString(self->String));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, Destroy)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
self->Close();
delete self;
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, UnGet)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
self->UnGet();
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, isText)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
ACTION_RETURN_BOOL(self->isText());
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, GetMessageLine)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
ACTION_RETURN_INT(self->GetMessageLine());
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, AddSymbol)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_STRING(name);
PARAM_INT(value);

self->AddSymbol(name.GetChars(), value);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, AddSymbolUnsigned)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_STRING(name);
PARAM_UINT(value);

self->AddSymbol(name.GetChars(), value);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, AddSymbolFloat)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_STRING(name);
PARAM_FLOAT(value);

self->AddSymbol(name.GetChars(), value);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, GetString)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

ACTION_RETURN_BOOL(self->GetString());
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, GetNumber)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(evaluate);

ACTION_RETURN_BOOL(self->GetNumber(evaluate));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, GetFloat)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(evaluate);

ACTION_RETURN_BOOL(self->GetFloat(evaluate));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, CheckValue)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(allowfloat);
PARAM_BOOL(evaluate);

ACTION_RETURN_BOOL(self->CheckValue(allowfloat, evaluate));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, CheckBoolToken)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

ACTION_RETURN_BOOL(self->CheckBoolToken());
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, CheckNumber)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(evaluate);

ACTION_RETURN_BOOL(self->CheckNumber(evaluate));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, CheckString)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_STRING(name);

ACTION_RETURN_STRING(self->CheckString(name.GetChars()));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, CheckFloat)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(evaluate);

ACTION_RETURN_BOOL(self->CheckFloat(evaluate));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, SetPrependMessage)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_STRING(message);

self->SetPrependMessage(message);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, SetCMode)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(cmode);

self->SetCMode(cmode);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, SetNoOctals)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(cmode);

self->SetNoOctals(cmode);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, SetEscape)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(esc);

self->SetEscape(esc);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, SkipToEndOfBlock)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

self->SkipToEndOfBlock();
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, StartBraces)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_POINTER(braceend, FScanner::SavedPos);

(void)self->StartBraces(braceend);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, FoundEndBrace)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_POINTER(braceend, FScanner::SavedPos);

ACTION_RETURN_BOOL(self->FoundEndBrace(*braceend));
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, ScriptError)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

FString s = FStringFormat(VM_ARGS_NAMES);
self->ScriptError("%s", s.GetChars());
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, ScriptMessage)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

FString s = FStringFormat(VM_ARGS_NAMES);
self->ScriptMessage("%s", s.GetChars());
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, MustGetValue)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(allowfloat);
PARAM_BOOL(evaluate);

self->MustGetValue(allowfloat, evaluate);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, MustGetNumber)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(evaluate);

self->MustGetNumber(evaluate);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, MustGetFloat)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_BOOL(evaluate);

self->MustGetFloat(evaluate);
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, MustGetString)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

self->MustGetString();
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, MustGetStringName)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);
PARAM_STRING(name);

self->MustGetStringName(name.GetChars());
return 0;
}

DEFINE_ACTION_FUNCTION(_ScriptScanner, MustGetBoolToken)
{
PARAM_SELF_STRUCT_PROLOGUE(FScanner);

self->MustGetBoolToken();
return 0;
}

DEFINE_FIELD_X(ScriptScanner, FScanner, Line);
DEFINE_FIELD_X(ScriptScanner, FScanner, Float);
DEFINE_FIELD_X(ScriptScanner, FScanner, Number);
DEFINE_FIELD_X(ScriptScanner, FScanner, End);
DEFINE_FIELD_X(ScriptScanner, FScanner, Crossed);
DEFINE_FIELD_X(ScriptScanner, FScanner, ParseError);
2 changes: 2 additions & 0 deletions src/common/engine/sc_man.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class FScanner

void ScriptError(const char *message, ...) GCCPRINTF(2,3);
void ScriptMessage(const char *message, ...) GCCPRINTF(2,3);
void SetPrependMessage(const FString& message) { PrependMessage = message; }

bool isText();

Expand Down Expand Up @@ -240,6 +241,7 @@ class FScanner
bool StateOptions;
bool Escape;
VersionInfo ParseVersion = { 0, 0, 0 }; // no ZScript extensions by default
FString PrependMessage = "";


bool ScanValue(bool allowfloat, bool evaluate);
Expand Down
Loading

0 comments on commit 507540b

Please sign in to comment.