Skip to content

Commit

Permalink
Info|libcore: Implicit block type
Browse files Browse the repository at this point in the history
The Info parser now supports an optional implicit block type. This
simplifies writing ScriptedInfo because named groups don't need to
use "group" as their type.
  • Loading branch information
skyjake committed Aug 9, 2015
1 parent 367ea23 commit 7e8b59b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
24 changes: 22 additions & 2 deletions doomsday/sdk/libcore/include/de/data/info.h
@@ -1,7 +1,7 @@
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* Copyright © 2012-2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
Expand All @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_INFO_H
Expand Down Expand Up @@ -314,6 +314,26 @@ class DENG2_PUBLIC Info

void setAllowDuplicateBlocksOfType(QStringList const &duplicatesAllowed);

/**
* Sets the block type used for single-token blocks. By default, this is not set,
* meaning that single-token blocks are treated as anonymous.
*
* <pre>group {
* key: value
* }</pre>
*
* This could be interpreted as an anonymous block with type "group", or a untyped
* block named "group". Setting the implicit block type will treat this as a
* block of type @a implicitBlock named "group".
*
* However, this behavior only applies when the block type is not the same as @a
* implicitBlock. In this example, if @a implicitBlock is set to "group", the
* resulting block would still be anonymous and have type "group".
*
* @param implicitBlock Block type for anonymous/untyped blocks.
*/
void setImplicitBlockType(String const &implicitBlock);

/**
* Parses the Info contents from a text string.
*
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/scriptsys/scriptedinfo.h
@@ -1,6 +1,6 @@
/** @file scriptedinfo.h Info document tree with script context.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2013-2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
Expand Down
23 changes: 19 additions & 4 deletions doomsday/sdk/libcore/src/data/info.cpp
@@ -1,7 +1,7 @@
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* Copyright © 2012-2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
Expand All @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/Info"
Expand Down Expand Up @@ -50,6 +50,8 @@ DENG2_PIMPL(Info)

QStringList scriptBlockTypes;
QStringList allowDuplicateBlocksOfType;
String implicitBlockType;

String sourcePath; ///< May be unknown (empty).
String content;
int currentLine;
Expand Down Expand Up @@ -342,7 +344,7 @@ DENG2_PIMPL(Info)
// No more strings to append.
return value;
}
}
}

// Then it must be a single token.
value = peekToken();
Expand Down Expand Up @@ -492,7 +494,7 @@ success:;
* Parse a block element, identified by the given name.
* @param blockType Identifier of the block.
*/
BlockElement *parseBlockElement(String const &blockType)
BlockElement *parseBlockElement(String blockType)
{
DENG2_ASSERT(blockType != "}");
DENG2_ASSERT(blockType != ")");
Expand All @@ -503,6 +505,14 @@ success:;
blockName = parseValue();
}

if(!implicitBlockType.isEmpty() && blockName.isEmpty() &&
blockType != implicitBlockType &&
!scriptBlockTypes.contains(blockType))
{
blockName = blockType;
blockType = implicitBlockType;
}

QScopedPointer<BlockElement> block(new BlockElement(blockType, blockName, self));
int startLine = currentLine;

Expand Down Expand Up @@ -814,6 +824,11 @@ void Info::setAllowDuplicateBlocksOfType(QStringList const &duplicatesAllowed)
d->allowDuplicateBlocksOfType = duplicatesAllowed;
}

void Info::setImplicitBlockType(String const &implicitBlock)
{
d->implicitBlockType = implicitBlock;
}

void Info::parse(String const &infoSource)
{
d->parse(infoSource);
Expand Down
5 changes: 4 additions & 1 deletion doomsday/sdk/libcore/src/scriptsys/scriptedinfo.cpp
@@ -1,6 +1,6 @@
/** @file scriptedinfo.cpp Info document tree with script context.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2013-2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
Expand Down Expand Up @@ -49,6 +49,9 @@ DENG2_PIMPL(ScriptedInfo)
// No limitation on duplicates for the special block types.
info.setAllowDuplicateBlocksOfType(
QStringList() << BLOCK_GROUP << BLOCK_NAMESPACE);

// Single-token blocks are implicitly treated as "group" blocks.
info.setImplicitBlockType(BLOCK_GROUP);
}

void clear()
Expand Down

0 comments on commit 7e8b59b

Please sign in to comment.