Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Dotenv\Parser;
6
 
7
final class Lexer
8
{
9
    /**
10
     * The regex for each type of token.
11
     */
12
    private const PATTERNS = [
13
        '[\r\n]{1,1000}', '[^\S\r\n]{1,1000}', '\\\\', '\'', '"', '\\#', '\\$', '([^(\s\\\\\'"\\#\\$)]|\\(|\\)){1,1000}',
14
    ];
15
 
16
    /**
17
     * This class is a singleton.
18
     *
19
     * @codeCoverageIgnore
20
     *
21
     * @return void
22
     */
23
    private function __construct()
24
    {
25
        //
26
    }
27
 
28
    /**
29
     * Convert content into a token stream.
30
     *
31
     * Multibyte string processing is not needed here, and nether is error
32
     * handling, for performance reasons.
33
     *
34
     * @param string $content
35
     *
36
     * @return \Generator<string>
37
     */
38
    public static function lex(string $content)
39
    {
40
        static $regex;
41
 
42
        if ($regex === null) {
43
            $regex = '(('.\implode(')|(', self::PATTERNS).'))A';
44
        }
45
 
46
        $offset = 0;
47
 
48
        while (isset($content[$offset])) {
49
            if (!\preg_match($regex, $content, $matches, 0, $offset)) {
50
                throw new \Error(\sprintf('Lexer encountered unexpected character [%s].', $content[$offset]));
51
            }
52
 
53
            $offset += \strlen($matches[0]);
54
 
55
            yield $matches[0];
56
        }
57
    }
58
}