Skip to content

Instantly share code, notes, and snippets.

@Fustrate
Created August 17, 2011 02:27
Show Gist options
  • Save Fustrate/1150680 to your computer and use it in GitHub Desktop.
Save Fustrate/1150680 to your computer and use it in GitHub Desktop.
readLangRef() to accept complex language strings
protected function readLangRef()
{
// It looks like this: #xyz.abc[$mno][nilla].$rpg
// Which means:
// x.y.z = x [ y ] [ z ]
// x[y.z] = x [ y [ z ] ]
// x[y][z] = x [ y ] [ z ]
// x[y[z]] = x [ y [ z ] ]
//
// When we hit a ., the next item is surrounded by brackets.
// When we hit a [, the next item has a [ before it.
// When we hit a ], there is no item, but just a ].
if ($this->data_pos >= $this->data_len - 1 || $this->data[$this->data_pos + 1] === ':' || $this->data[$this->data_pos + 1] === '}')
$this->toss('expression_lang_name_empty');
$brackets = 0;
$key = true;
while ($this->data_pos < $this->data_len)
{
$next = $this->firstPosOf(array('[', '.', ']', '}', ':'), 1);
if ($next === false)
$next = $this->data_len;
$c = $this->data[$this->data_pos];
$this->data_pos++;
switch ($c)
{
case '#':
$name = $this->eatUntil($next);
if ($name === '')
$this->toss('expression_lang_name_empty');
$this->built[] = self::$lang_function . '(array(\'' . $name . '\'';
break;
case '.':
if ($key)
{
$this->built[] = ',';
$this->readVarPart($next, true);
}
else
{
$this->built[] = '[';
$this->readVarPart($next, true);
$this->built[] = ']';
}
break;
case '[':
if ($key)
{
$this->built[] = ',';
$this->eatWhite();
$this->readVarPart($next, false);
$this->eatWhite();
}
else
{
$this->built[] = '[';
$this->eatWhite();
$this->readVarPart($next, false);
$this->eatWhite();
}
$brackets++;
break;
case ']':
// Ah, hit the end, jump out. Must be a nested one.
if ($brackets <= 0)
{
$this->data_pos--;
break 2;
}
if (!$key)
$this->built[] = ']';
$brackets--;
break;
// All done - but don't skip it, our caller doesn't expect that.
case '}':
$this->data_pos--;
$this->built[] = '))';
break 2;
// Maybe we're done with this?
case ':':
if ($key)
{
$this->built[] = '), array(';
}
else
{
$this->built[] = ',';
}
$key = false;
$this->readVarPart($next);
break;
}
}
if ($brackets != 0)
$this->toss('expression_brackets_unmatched');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment