Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Compiler
  • Engine
  • Helpers
  • HtmlNode
  • MacroNode
  • MacroTokens
  • Parser
  • PhpHelpers
  • PhpWriter
  • Token
  • TokenIterator
  • Tokenizer

Interfaces

  • ILoader
  • IMacro

Traits

  • Strict

Exceptions

  • CompileException
  • RegexpException
  • RuntimeException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Latte (https://latte.nette.org)
 5:  * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
 6:  */
 7: 
 8: namespace Latte;
 9: 
10: 
11: /**
12:  * Simple lexical analyser.
13:  * @internal
14:  */
15: class Tokenizer
16: {
17:     use Strict;
18: 
19:     const VALUE = 0,
20:         OFFSET = 1,
21:         TYPE = 2;
22: 
23:     /** @var string */
24:     private $re;
25: 
26:     /** @var array */
27:     private $types;
28: 
29: 
30:     /**
31:      * @param  array of [(int) symbol type => pattern]
32:      * @param  string  regular expression flag
33:      */
34:     public function __construct(array $patterns, $flags = '')
35:     {
36:         $this->re = '~(' . implode(')|(', $patterns) . ')~A' . $flags;
37:         $this->types = array_keys($patterns);
38:     }
39: 
40: 
41:     /**
42:      * Tokenizes string.
43:      * @param  string
44:      * @return array
45:      */
46:     public function tokenize($input)
47:     {
48:         preg_match_all($this->re, $input, $tokens, PREG_SET_ORDER);
49:         if (preg_last_error()) {
50:             throw new RegexpException(null, preg_last_error());
51:         }
52:         $len = 0;
53:         $count = count($this->types);
54:         foreach ($tokens as &$match) {
55:             $type = null;
56:             for ($i = 1; $i <= $count; $i++) {
57:                 if (!isset($match[$i])) {
58:                     break;
59:                 } elseif ($match[$i] != null) {
60:                     $type = $this->types[$i - 1];
61:                     break;
62:                 }
63:             }
64:             $match = [self::VALUE => $match[0], self::OFFSET => $len, self::TYPE => $type];
65:             $len += strlen($match[self::VALUE]);
66:         }
67:         if ($len !== strlen($input)) {
68:             list($line, $col) = $this->getCoordinates($input, $len);
69:             $token = str_replace("\n", '\n', substr($input, $len, 10));
70:             throw new CompileException("Unexpected '$token' on line $line, column $col.");
71:         }
72:         return $tokens;
73:     }
74: 
75: 
76:     /**
77:      * Returns position of token in input string.
78:      * @param  string
79:      * @param  int
80:      * @return array of [line, column]
81:      */
82:     public static function getCoordinates($text, $offset)
83:     {
84:         $text = substr($text, 0, $offset);
85:         return [substr_count($text, "\n") + 1, $offset - strrpos("\n" . $text, "\n") + 1];
86:     }
87: }
88: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0