From 7e8b59b8b296a6e43f4f9f8709f47101e50cbc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sun, 9 Aug 2015 19:41:55 +0300 Subject: [PATCH] Info|libcore: Implicit block type 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. --- doomsday/sdk/libcore/include/de/data/info.h | 24 +++++++++++++++++-- .../include/de/scriptsys/scriptedinfo.h | 2 +- doomsday/sdk/libcore/src/data/info.cpp | 23 ++++++++++++++---- .../libcore/src/scriptsys/scriptedinfo.cpp | 5 +++- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/doomsday/sdk/libcore/include/de/data/info.h b/doomsday/sdk/libcore/include/de/data/info.h index 599c6e0d7e..21471cb25a 100644 --- a/doomsday/sdk/libcore/include/de/data/info.h +++ b/doomsday/sdk/libcore/include/de/data/info.h @@ -1,7 +1,7 @@ /* * The Doomsday Engine Project -- libcore * - * Copyright © 2012-2013 Jaakko Keränen + * Copyright © 2012-2015 Jaakko Keränen * * @par License * LGPL: http://www.gnu.org/licenses/lgpl.html @@ -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 + * http://www.gnu.org/licenses */ #ifndef LIBDENG2_INFO_H @@ -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. + * + *
group {
+     *   key: value
+     * }
+ * + * 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. * diff --git a/doomsday/sdk/libcore/include/de/scriptsys/scriptedinfo.h b/doomsday/sdk/libcore/include/de/scriptsys/scriptedinfo.h index db572c8e16..9b2e6bf4c3 100644 --- a/doomsday/sdk/libcore/include/de/scriptsys/scriptedinfo.h +++ b/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 + * @authors Copyright © 2013-2015 Jaakko Keränen * * @par License * LGPL: http://www.gnu.org/licenses/lgpl.html diff --git a/doomsday/sdk/libcore/src/data/info.cpp b/doomsday/sdk/libcore/src/data/info.cpp index 1a95ed06c1..dc2959c555 100644 --- a/doomsday/sdk/libcore/src/data/info.cpp +++ b/doomsday/sdk/libcore/src/data/info.cpp @@ -1,7 +1,7 @@ /* * The Doomsday Engine Project -- libcore * - * Copyright © 2012-2013 Jaakko Keränen + * Copyright © 2012-2015 Jaakko Keränen * * @par License * LGPL: http://www.gnu.org/licenses/lgpl.html @@ -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 + * http://www.gnu.org/licenses */ #include "de/Info" @@ -50,6 +50,8 @@ DENG2_PIMPL(Info) QStringList scriptBlockTypes; QStringList allowDuplicateBlocksOfType; + String implicitBlockType; + String sourcePath; ///< May be unknown (empty). String content; int currentLine; @@ -342,7 +344,7 @@ DENG2_PIMPL(Info) // No more strings to append. return value; } - } + } // Then it must be a single token. value = peekToken(); @@ -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 != ")"); @@ -503,6 +505,14 @@ success:; blockName = parseValue(); } + if(!implicitBlockType.isEmpty() && blockName.isEmpty() && + blockType != implicitBlockType && + !scriptBlockTypes.contains(blockType)) + { + blockName = blockType; + blockType = implicitBlockType; + } + QScopedPointer block(new BlockElement(blockType, blockName, self)); int startLine = currentLine; @@ -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); diff --git a/doomsday/sdk/libcore/src/scriptsys/scriptedinfo.cpp b/doomsday/sdk/libcore/src/scriptsys/scriptedinfo.cpp index f3da1dd824..e6b800449d 100644 --- a/doomsday/sdk/libcore/src/scriptsys/scriptedinfo.cpp +++ b/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 + * @authors Copyright © 2013-2015 Jaakko Keränen * * @par License * LGPL: http://www.gnu.org/licenses/lgpl.html @@ -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()