summaryrefslogtreecommitdiff
path: root/src/HTML5/Parser/Scanner.php
blob: 2500dabe6df9129436f6ee6dedd7100425197cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace HTML5\Parser;

/**
 * The scanner.
 *
 * This scans over an input stream.
 */
class Scanner {
  const CHARS_HEX = 'abcdefABCDEF01234567890';
  const CHARS_ALNUM = 'abcdefAghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
  const CHARS_ALPHA = 'abcdefAghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ';


  protected $char;
  protected $is;

  /**
   * Create a new Scanner.
   *
   * @param \HTML5\InputStream $input
   *   An InputStream to be scanned.
   */
  public function __construct($input) {
    $this->is = $input;
  }

  /**
   * Get the current position.
   *
   * @return int
   *   The current intiger byte position.
   */
  public function position() {
    return $this->is->position();
  }

  /**
   * Take a peek at the character after the next character in the data.
   *
   * For example, you start scanning the string abc. The pointer is before a.
   * When you start peek() will return b while next() will return a.
   *
   * @return string
   *   The next character.
   */
  public function peek() {
    return $this->is->peek();
  }

  /**
   * Get the next character.
   * 
   * Note: This advances the pointer.
   *
   * @return string
   *   The next character.
   */
  public function next() {
    $this->char = $this->is->char();
    return $this->char;
  }

  /**
   * Get the current character.
   *
   * Note, this does not advance the pointer.
   * 
   * @return string
   *   The current character.
   */
  public function current() {
    return $this->char;
  }

  /**
   * Unconsume some of the data. This moves the data pointer backwards.
   *
   * @param  int $howMany
   *   The number of characters to move the pointer back.
   */
  public function unconsume($howMany = 1) {
    for ($i = 0; $i < $howMany; ++$i) {
      $this->is->unconsume();
    }
  }

  /**
   * Get the next group of that is a hex value.
   *
   * Note, along with getting the characters the pointer in the data will be
   * moved as well.
   *
   * @todo There is a potential for many false positives with this method. Make it more accurate.
   * 
   * @return string
   *   The next group that is a hex value.
   */
  public function getHex() {
    return $this->is->charsWhile(self::CHARS_HEX);
  }

  /**
   * Get the next group of characters that are ASCII Alpha characters.
   *
   * Note, along with getting the characters the pointer in the data will be
   * moved as well.
   * 
   * @return string
   *   The next group of ASCII alpha characters.
   */
  public function getAsciiAlpha() {
    return $this->is->charsWhile(self::CHARS_ALPHA);
  }

  /**
   * Get the next group of characters that are ASCII Alpha characters and numbers.
   *
   * Note, along with getting the characters the pointer in the data will be
   * moved as well.
   * 
   * @return string
   *   The next group of ASCII alpha characters and numbers.
   */
  public function getAsciiAlphaNum() {
    return $this->is->charsWhile(self::CHARS_ALNUM);
  }

  /**
   * Get the next group of numbers.
   *
   * Note, along with getting the characters the pointer in the data will be
   * moved as well.
   * 
   * @return string
   *   The next group of numbers.
   */
  public function getNumeric() {
    return $this->is->charsWhile('0123456789');
  }
}