Skip to content

Commit

Permalink
Fixed bug #10 "Error: cannot parse quote in table name"
Browse files Browse the repository at this point in the history
  • Loading branch information
yosymfony committed Aug 20, 2015
1 parent ecc417e commit 33cf47d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
65 changes: 31 additions & 34 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,40 +180,35 @@ private function processTable()
$key = '';
$quotesKey = false;

if (Lexer::TOKEN_QUOTES === $this->lexer->getNextToken()->getType()) {
$quotesKey = true;
$this->lexer->getToken();
while ($this->isTokenValidForTablename($this->lexer->getToken()) == true) {
if (Lexer::TOKEN_QUOTES === $this->lexer->getCurrentToken()->getType()) {
if (Lexer::TOKEN_STRING !== $this->lexer->getToken()->getType()) {
throw new ParseException(
sprintf('Syntax error: expected string. Key: %s', $key),
$this->currentLine,
$this->lexer->getCurrentToken()->getValue());
}

if (Lexer::TOKEN_STRING !== $this->lexer->getToken()->getType()) {
throw new ParseException(
sprintf('Syntax error: expected string. Key: %s', $key),
$this->currentLine,
$this->lexer->getCurrentToken()->getValue());
} else {
$key = $this->lexer->getCurrentToken()->getValue();
}
} else {
while ($this->isTokenValidForTablename($this->lexer->getToken())) {
$key .= $this->lexer->getCurrentToken()->getValue();
}
}
$key .= str_replace('.', '/./', $this->lexer->getCurrentToken()->getValue());

if ($quotesKey) {
if (Lexer::TOKEN_QUOTES === $this->lexer->getToken()->getType()) {
$this->lexer->getToken();
if (Lexer::TOKEN_QUOTES !== $this->lexer->getToken()->getType()) {
throw new ParseException(
sprintf('Syntax error: expected quotes for closing key: %s', $key),
$this->currentLine,
$this->lexer->getCurrentToken()->getValue());
}
} else {
throw new ParseException(
sprintf('Syntax error: expected quotes for closing key: %s', $key),
$this->currentLine,
$this->lexer->getCurrentToken()->getValue());
}
}
$subkey = $this->lexer->getCurrentToken()->getValue();

if (false === $this->isValidKey($key, $quotesKey)) {
throw new ParseException(
sprintf('Syntax error: the key %s is invalid. A key without embraces can not contains white spaces', $key),
$this->currentLine,
$this->lexer->getCurrentToken()->getValue());
if (false === $this->isValidKey($subkey, false)) {
throw new ParseException(
sprintf('Syntax error: the key %s is invalid. A key without embraces can not contains white spaces', $key),
$this->currentLine,
$subkey);
}

$key .= $subkey;
}
}

$this->setTable($key);
Expand All @@ -228,12 +223,12 @@ private function processTable()

private function isTokenValidForTablename(Token $token)
{
return Lexer::TOKEN_LITERAL === $token->getType();
return Lexer::TOKEN_LITERAL === $token->getType() || Lexer::TOKEN_QUOTES === $token->getType();
}

private function setTable($key)
{
$nameParts = explode('.', $key);
$nameParts = preg_split('/(?<=[^\/])[.](?<![\/])/', $key);
$this->data = &$this->result;

if (in_array($key, $this->tableNames) || in_array($key, $this->arrayTableNames)) {
Expand All @@ -245,6 +240,8 @@ private function setTable($key)
$this->tableNames[] = $key;

foreach ($nameParts as $namePart) {
$namePart = str_replace('/./', '.', $namePart);

if (0 == strlen($namePart)) {
throw new ParseException('The name of the table must not be empty', $this->currentLine, $key);
}
Expand Down Expand Up @@ -404,7 +401,7 @@ private function isValidKey($key, $quotesActived)

private function getStringValue($openToken)
{
$result = "";
$result = '';

if (Lexer::TOKEN_STRING !== $this->lexer->getToken()->getType()) {
throw new ParseException(
Expand Down Expand Up @@ -455,7 +452,7 @@ private function getArrayValue()
break;
case Lexer::TOKEN_LITERAL:
$value = $this->getLiteralValue();
$lastType = gettype($value);
$lastType = gettype($value);
$dataType = $dataType == null ? $lastType : $dataType;
$result[] = $value;
break;
Expand Down
14 changes: 14 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,20 @@ public function testTableWhiteSpace()
$this->assertTrue(is_array($array['valid key']));
}

public function testTableQuotedName()
{
$filename = __DIR__.'/fixtures/valid/tableQuotedName.toml';

$parser = new Parser();

$array = $parser->parse(file_get_contents($filename));

$this->assertNotNull($array);

$this->assertTrue(is_array($array['dog']));
$this->assertTrue(is_array($array['dog']['tater.man']));
}

public function testTableWithPound()
{
$filename = __DIR__.'/fixtures/valid/tableWithPound.toml';
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/valid/tableQuotedName.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dog."tater.man"]
type = "pug"

0 comments on commit 33cf47d

Please sign in to comment.