summaryrefslogtreecommitdiff
path: root/src/HTML5/Parser/Scanner.php
blob: f9527040fb627895b9acf7a59366e1cad72da47e (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?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 $is;

  /**
   * Create a new Scanner.
   *
   * @param \HTML5\Parser\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->key();
  }

  /**
   * Take a peek at the next character in the data.
   *
   * @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->is->next();
    if ($this->is->valid()) {
      return $this->is->current();
    }
    return FALSE;
  }

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

  /**
   * 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) {
    $this->is->unconsume($howMany);
  }

  /**
   * Get the next group of that contains hex characters.
   *
   * Note, along with getting the characters the pointer in the data will be
   * moved as well.
   * 
   * @return string
   *   The next group that is hex characters.
   */
  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');
  }

  /**
   * Returns the current line that is being consumed.
   *
   * @return int
   *   The current line number.
   */
  public function currentLine() {
    return $this->is->currentLine();
  }

  /**
   * Returns the current column of the current line that the tokenizer is at.
   *
   * Newlines are column 0. The first char after a newline is column 1.
   *
   * @return int
   *   The column number.
   */
  public function columnOffset() {
    return $this->is->columnOffset();
  }

  /**
   * Get all characters until EOF.
   *
   * This consumes characters until the EOF.
   *
   * @return int
   *   The number of characters remaining.
   */
  public function remainingChars() {
    return $this->is->remainingChars();
  }
}