1<?php
2/**
3 * The string parser collects all string character tokens until a string end token is found.
4 *
5 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
6 * @copyright Copyright 2010-2014 PhpCss Team
7 */
8
9namespace PhpCss\Parser {
10
11  use PhpCss;
12  use PhpCss\Scanner;
13
14  /**
15   * The string parser collects all string character tokens until a string end token is found.
16   */
17  class Text extends PhpCss\Parser {
18
19    /**
20     * @throws PhpCss\Exception\ParserException
21     */
22    public function parse(): PhpCss\Ast\Node {
23      $string = '';
24      while (TRUE) {
25        $token = $this->read(
26          [
27            Scanner\Token::STRING_CHARACTERS,
28            Scanner\Token::STRING_ESCAPED_CHARACTER,
29            Scanner\Token::SINGLEQUOTE_STRING_END,
30            Scanner\Token::DOUBLEQUOTE_STRING_END,
31          ]
32        );
33        switch ($token->type) {
34        case Scanner\Token::STRING_CHARACTERS :
35          $string .= $token->content;
36          break;
37        case Scanner\Token::STRING_ESCAPED_CHARACTER :
38          $string .= substr($token->content, 1);
39          break;
40        case Scanner\Token::SINGLEQUOTE_STRING_END :
41        case Scanner\Token::DOUBLEQUOTE_STRING_END :
42          return new PhpCss\Ast\Value\Literal($string);
43        }
44      }
45    }
46  }
47}
48