summaryrefslogtreecommitdiff
path: root/src/HTML5/Parser/Scanner.php
blob: 4716fc4dc582af192b1aaf770d2dd6daac7153f5 (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
<?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() {
    return $this->is->current();
  }

  /**
   * 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');
  }
}