1<?php
2/**
3* Default parsing status, expecting a group of selector sequences
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\Ast;
13  use PhpCss\Scanner;
14
15  /**
16  * Default parsing status, expecting a group of selector sequences
17  *
18  * @package PhpCss
19  * @subpackage Parser
20  */
21  class Standard extends PhpCss\Parser {
22
23    public const ALLOW_RELATIVE_SELECTORS = 1;
24
25    private $_options;
26
27    public function __construct(array &$tokens, $options = 0) {
28      parent::__construct($tokens);
29      $this->_options = $options;
30    }
31
32    /**
33    * Tokens that start a sequence, if anything except whitespaces
34    * is found it delegates to the sequence parser
35    *
36    * @var array
37    */
38    private $_expectedTokens = array(
39      Scanner\Token::WHITESPACE,
40      Scanner\Token::IDENTIFIER,
41      Scanner\Token::ID_SELECTOR,
42      Scanner\Token::CLASS_SELECTOR,
43      Scanner\Token::PSEUDO_CLASS,
44      Scanner\Token::PSEUDO_ELEMENT,
45      Scanner\Token::ATTRIBUTE_SELECTOR_START
46    );
47
48    /**
49     * Start parsing looking for anything valid except whitespaces, add
50     * returned sequences to the group
51     *
52     * @return Ast\Selector\Group
53     * @throws PhpCss\Exception\ParserException
54     * @see PhpCss\Parser::parse()
55     */
56    public function parse(): Ast\Node {
57      $expectedTokens = $this->_expectedTokens;
58      if (($this->_options & self::ALLOW_RELATIVE_SELECTORS) === self::ALLOW_RELATIVE_SELECTORS) {
59        $expectedTokens[] = Scanner\Token::COMBINATOR;
60      }
61      $group = new Ast\Selector\Group();
62      $this->ignore(Scanner\Token::WHITESPACE);
63      while (!$this->endOfTokens()) {
64        if ($currentToken = $this->lookahead($expectedTokens)) {
65          if ($currentToken->type === Scanner\Token::WHITESPACE) {
66            $this->read(Scanner\Token::WHITESPACE);
67            continue;
68          }
69          $group[] = $this->delegate(Sequence::CLASS);
70        }
71      }
72      return $group;
73    }
74  }
75}
76