/** * Scans a literal (any character inside single or double quotes. * * @return Token */ private function scanLiteral() { $pos = $this->createPosition(); $start = $this->currentCharacter(); $str = $start; while ($this->hasNextCharacter()) { $this->nextCharacter(); $str .= $this->currentCharacter(); // Ensure that a lieral opened with " is not temrinated by ' and vice versa. if (ScannerHelper::isQuote($this->currentCharacter()) && $this->currentCharacter() === $start) { break; } } return new Token(Token::LITERAL, $str, $pos); }
public function testIsQuote() { $this->assertTrue(ScannerHelper::isQuote('"')); $this->assertTrue(ScannerHelper::isQuote("'")); foreach ($this->ws as $c) { $this->assertFalse(ScannerHelper::isQuote($c), $c); } foreach ($this->ops as $c) { $this->assertFalse(ScannerHelper::isQuote($c), $c); } foreach ($this->nums as $c) { $this->assertFalse(ScannerHelper::isQuote($c), $c); } foreach ($this->lowAlpha as $c) { $this->assertFalse(ScannerHelper::isQuote($c), $c); } foreach ($this->upAlpha as $c) { $this->assertFalse(ScannerHelper::isQuote($c), $c); } }
/** * Checks if a passes string is encapsulated in quotes and removes them. * Also unescape inside quotes. * * @param string $str The string to unquote. * * @return string */ public static function unquoteString($str) { $start = 0; $length = strlen($str) - 1; if (ScannerHelper::isQuote($str[$start])) { $start++; } if (ScannerHelper::isQuote($str[$length])) { $length--; } $str = substr($str, $start, $length); return stripcslashes($str); }