Skip to content

Commit

Permalink
libcommon|HexLex: Continued cleaning up HexLex
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jan 23, 2014
1 parent fd2559e commit e0d0882
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 92 deletions.
3 changes: 2 additions & 1 deletion doomsday/client/src/m_misc.cpp
Expand Up @@ -274,7 +274,7 @@ DENG_EXTERN_C AutoStr* M_ReadFileIntoString(ddstring_t const *path, dd_bool *isC

if(isCustom) *isCustom = W_LumpIsCustom(lumpNum);

// Ignore zero-length scripts.
// Ignore zero-length lumps.
size_t lumpLen = W_LumpLength(lumpNum);
if(!lumpLen) return 0;

Expand All @@ -295,6 +295,7 @@ DENG_EXTERN_C AutoStr* M_ReadFileIntoString(ddstring_t const *path, dd_bool *isC
AutoStr *string = Str_PartAppend(AutoStr_New(), readBuf, 0, int(bytesRead));
Z_Free(readBuf);

// Ignore zero-length files.
if(Str_IsEmpty(string))
return 0;

Expand Down
4 changes: 2 additions & 2 deletions doomsday/libdeng1/include/de/str.h
Expand Up @@ -300,8 +300,8 @@ DENG_PUBLIC Str *Str_Strip(Str *ds);
DENG_PUBLIC Str *Str_ReplaceAll(Str *ds, char from, char to);

/**
* Determines if the string starts starts with the given substring. The comparison
* is done case sensitively.
* Determines if the string starts with the given substring. The comparison is done
* case sensitively.
*
* @param ds String instance.
* @param text Text to look for at the start of @a ds.
Expand Down
13 changes: 9 additions & 4 deletions doomsday/plugins/common/include/hexlex.h
@@ -1,4 +1,4 @@
/** @file hexlex.h Laxical analyzer for Hexen definition/script syntax.
/** @file hexlex.h Lexical analyzer for Hexen definition/script syntax.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
Expand Down Expand Up @@ -43,13 +43,18 @@ class HexLex
void parse(Str const *script, Str const *sourcePath);

bool readToken();

Str const *token();

void unreadToken();

Str const *mustGetString();
int mustGetNumber();
Str const *readString();
int readNumber();

AutoStr *readLumpName();
int readSoundId();
int readSoundIndex();
uint readMapNumber();
Uri *readTextureUri(char const *defaultScheme);

int lineNumber() const;

Expand Down
56 changes: 48 additions & 8 deletions doomsday/plugins/common/src/hexlex.cpp
@@ -1,4 +1,4 @@
/** @file hexlex.cpp Hexen definition/script syntax.
/** @file hexlex.cpp HLexical analyzer for Hexen definition/script syntax.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
Expand Down Expand Up @@ -29,7 +29,7 @@

void HexLex::checkOpen()
{
if(!_script) Con_Error("RavHexParser: No script to parse!");
if(!_script) Con_Error("HexLex: No script to parse!");
}

bool HexLex::atEnd()
Expand Down Expand Up @@ -166,7 +166,7 @@ bool HexLex::readToken()
return true;
}

Str const *HexLex::mustGetString()
Str const *HexLex::readString()
{
if(!readToken())
{
Expand All @@ -175,7 +175,7 @@ Str const *HexLex::mustGetString()
return &_token;
}

int HexLex::mustGetNumber()
int HexLex::readNumber()
{
checkOpen();

Expand All @@ -188,15 +188,55 @@ int HexLex::mustGetNumber()
_tokenAsNumber = strtol(Str_Text(&_token), &stopper, 0);
if(*stopper != 0)
{
Con_Error("RavHexParser: Bad numeric constant \"%s\".\n"
"File: \"%s\", Line: %i",
Con_Error("HexLex: Non-numeric constant '%s' in \"%s\" on line #%i",
Str_Text(&_token), F_PrettyPath(Str_Text(&_sourcePath)), _lineNumber);
}

return _tokenAsNumber;
}

/// @note Assumes there is a valid string in sc_String.
Uri *HexLex::readTextureUri(char const *defaultScheme)
{
if(!readToken()) // Name.
{
scriptError("Missing texture Uri");
}

Uri *uri = Uri_SetScheme(Uri_New(), defaultScheme);
AutoStr *path = Str_PercentEncode(Str_Copy(AutoStr_NewStd(), &_token));
Uri_SetPath(uri, Str_Text(path));
return uri;
}

AutoStr *HexLex::readLumpName()
{
return AutoStr_FromText(Str_Text(readString()));
}

uint HexLex::readMapNumber()
{
uint num = readNumber();
return num > 0? num - 1 : num;
}

int HexLex::readSoundId()
{
return Def_Get(DD_DEF_SOUND_BY_NAME, Str_Text(readString()), 0);
}

int HexLex::readSoundIndex()
{
char const *name = Str_Text(readString());
int i = Def_Get(DD_DEF_SOUND_BY_NAME, name, 0);
if(!i)
{
AutoStr *msg = Str_Appendf(AutoStr_New(), "Unknown sound '%s'", name);
scriptError(Str_Text(msg));
}
return i;
}

// @note Assumes there is a valid string in sc_String.
void HexLex::unreadToken()
{
_alreadyGot = true;
Expand All @@ -214,7 +254,7 @@ int HexLex::lineNumber() const

void HexLex::scriptError(char const *message)
{
Con_Error("RavHexParser: Error in script \"%s\" on line #%i.\n%s",
Con_Error("HexLex: Error in \"%s\" on line #%i.\n%s",
F_PrettyPath(Str_Text(&_sourcePath)), _lineNumber, message);
}

Expand Down
27 changes: 7 additions & 20 deletions doomsday/plugins/common/src/p_sound.cpp
Expand Up @@ -93,21 +93,6 @@ int S_GetSoundID(char const *name)
return Def_Get(DD_DEF_SOUND_BY_NAME, name, 0);
}

static AutoStr *parseLumpName(HexLex &lexer)
{
return AutoStr_FromText(Str_Text(lexer.mustGetString()));
}

static uint parseMapNumber(HexLex &lexer)
{
return lexer.mustGetNumber() - 1;
}

static int parseSoundId(HexLex &lexer)
{
return Def_Get(DD_DEF_SOUND_BY_NAME, Str_Text(lexer.token()), 0);
}

void SndInfoParser(Str const *path)
{
AutoStr *script = M_ReadFileIntoString(path, 0);
Expand All @@ -127,15 +112,15 @@ void SndInfoParser(Str const *path)
// Used in combination with the -devsnd command line argument to
// redirect the loading of sounds to a directory in the local file
// system.
lexer.mustGetString();
lexer.readString();
continue;
}
if(!Str_CompareIgnoreCase(lexer.token(), "$map"))
{
// $map int(map-number) string(lump-name)
// Associate a music lump to a map.
uint const map = parseMapNumber(lexer);
AutoStr const *lumpName = parseLumpName(lexer);
uint const map = lexer.readMapNumber();
AutoStr const *lumpName = lexer.readLumpName();

if(map >= 0 && map >= P_MapInfoCount())
{
Expand All @@ -150,10 +135,12 @@ void SndInfoParser(Str const *path)
continue;
}

lexer.unreadToken();

// string(sound-id) string(lump-name | '?')
// A sound definition.
int const soundId = parseSoundId(lexer);
AutoStr const *lumpName = parseLumpName(lexer);
int const soundId = lexer.readSoundId();
AutoStr const *lumpName = lexer.readLumpName();

if(soundId)
{
Expand Down
21 changes: 6 additions & 15 deletions doomsday/plugins/hexen/src/p_anim.cpp
Expand Up @@ -64,17 +64,8 @@ void AnimDefsParser(Str const *path)
// string(texture-scheme) string(texture-path)
if(char const *scheme = textureScheme(lexer.token()))
{
if(!lexer.readToken()) // Name.
{
lexer.scriptError("Missing string");
}

Uri *uri = Uri_SetScheme(Uri_New(), scheme);
AutoStr *path = Str_PercentEncode(Str_Copy(AutoStr_NewStd(), lexer.token()));
Uri_SetPath(uri, Str_Text(path));

Uri *uri = lexer.readTextureUri(scheme);
int const texNumBase = Textures_UniqueId2(uri, !isCustom);

Uri_Delete(uri);

bool const ignore = (texNumBase == -1);
Expand All @@ -88,18 +79,18 @@ void AnimDefsParser(Str const *path)
{
if(!Str_CompareIgnoreCase(lexer.token(), "pic"))
{
int picNum = lexer.mustGetNumber();
int picNum = lexer.readNumber();

int min = 0, max = 0;
Str const *label = lexer.mustGetString();
Str const *label = lexer.readString();
if(!Str_CompareIgnoreCase(label, "tics"))
{
min = lexer.mustGetNumber();
min = lexer.readNumber();
}
else if(!Str_CompareIgnoreCase(label, "rand"))
{
min = lexer.mustGetNumber();
max = lexer.mustGetNumber();
min = lexer.readNumber();
max = lexer.readNumber();
}
else
{
Expand Down

0 comments on commit e0d0882

Please sign in to comment.