Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Fix #345
Browse files Browse the repository at this point in the history
MSON aligned parameter parser trips up when inline description contains round brackets
  • Loading branch information
Jiri Kratochvil committed Sep 1, 2016
1 parent c2609e5 commit 1638ae9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/ParameterParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,32 @@ namespace snowcrash {
}
}

static size_t matchBracket(const mdp::ByteBuffer& text) {

size_t index = 0;

if (text.at(index) != '(') {
return std::string::npos;
}

size_t depth = 1;

while (depth && (index < (text.length() - 1))) { // "- 1" cause nonenclosed bracket and

++index;

if (text.at(index) == '(') {
depth++;
}
else if (text.at(index) == ')') {
depth--;
}

}

return depth ? std::string::npos : index;
}

/**
* \brief Determine the type of parameter using the signature
*/
Expand Down Expand Up @@ -477,8 +503,7 @@ namespace snowcrash {

if (innerSignature.substr(0, 1) == "(") {

// We should use `matchBrackets` if the parameters are supported to be more complex
size_t endOfAttributesPos = innerSignature.find_last_of(")");
size_t endOfAttributesPos = matchBracket(innerSignature);

if (endOfAttributesPos == std::string::npos) {
return NotParameterType; // Expecting close of attributes
Expand Down
15 changes: 15 additions & 0 deletions test/test-MSONParameterParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ TEST_CASE("Warn missing default value in values in new parameter syntax", "[mson
REQUIRE(parameter.node.exampleValue == "Value2");
REQUIRE(parameter.node.defaultValue == "Value1");
}

TEST_CASE("Parentheses in parameter description with new syntax", "[parameter]")
{
mdp::ByteBuffer source = "+ id (string) - lorem (ipsum) dolor\n";

ParseResult<MSONParameter> parameter;
SectionParserHelper<MSONParameter, MSONParameterParser>::parse(source, MSONParameterSectionType, parameter);

REQUIRE(parameter.report.error.code == Error::OK);
REQUIRE(parameter.report.warnings.empty());

REQUIRE(parameter.node.name == "id");
REQUIRE(parameter.node.type == "string");
REQUIRE(parameter.node.description == "lorem (ipsum) dolor");
}

0 comments on commit 1638ae9

Please sign in to comment.