• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / neon / 25702243303

11 May 2026 10:57PM UTC coverage: 97.925% (-0.6%) from 98.485%
25702243303

push

github

dg
used attribute Deprecated

472 of 482 relevant lines covered (97.93%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/Neon/TokenStream.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Neon;
9

10
use function in_array, str_replace, substr;
11

12

13
/** @internal */
14
final class TokenStream
15
{
16
        private int $index = 0;
17

18

19
        public function __construct(
1✔
20
                /** @var list<Token> */
21
                public readonly array $tokens,
22
        ) {
23
        }
1✔
24

25

26
        public function getIndex(): int
27
        {
28
                return $this->index;
1✔
29
        }
30

31

32
        public function seek(int $index): void
1✔
33
        {
34
                $this->index = $index;
1✔
35
        }
1✔
36

37

38
        /**
39
         * Tells whether the token at current position is of given kind.
40
         */
41
        public function is(int|string ...$kind): bool
1✔
42
        {
43
                while ($this->tokens[$this->index]->is(Token::Comment, Token::Whitespace)) {
1✔
44
                        $this->index++;
1✔
45
                }
46

47
                return $kind
1✔
48
                        ? $this->tokens[$this->index]->is(...$kind)
1✔
49
                        : $this->tokens[$this->index]->type !== Token::End;
1✔
50
        }
51

52

53
        /**
54
         * Consumes the current token of given kind or returns null.
55
         */
56
        public function tryConsume(int|string ...$kind): ?Token
1✔
57
        {
58
                return $this->is(...$kind)
1✔
59
                        ? $this->tokens[$this->index++]
1✔
60
                        : null;
1✔
61
        }
62

63

64
        /**
65
         * Returns the whitespace indentation of the token at the current position,
66
         * i.e. the whitespace token immediately following a newline (or at the start).
67
         */
68
        public function getIndentation(): string
69
        {
70
                return in_array($this->tokens[$this->index - 2]->type ?? null, [Token::Newline, null], strict: true)
1✔
71
                        && ($this->tokens[$this->index - 1]->type ?? null) === Token::Whitespace
1✔
72
                        ? $this->tokens[$this->index - 1]->text
1✔
73
                        : '';
1✔
74
        }
75

76

77
        /** Throws a parsing exception with position information from the current or given token. */
78
        public function error(?string $message = null, ?int $pos = null): never
1✔
79
        {
80
                $pos ??= $this->index;
1✔
81
                $token = $this->tokens[$pos];
1✔
82
                $message ??= 'Unexpected ' . ($token->type === Token::End
1✔
83
                        ? 'end'
1✔
84
                        : "'" . str_replace("\n", '<new line>', substr($token->text, 0, 40)) . "'");
1✔
85
                throw new Exception($message, $token->position);
1✔
86
        }
×
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc