forked from gitonomy/gitlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserBase.php
More file actions
153 lines (120 loc) · 3.79 KB
/
ParserBase.php
File metadata and controls
153 lines (120 loc) · 3.79 KB
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
<?php
/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <alexandre.salome@gmail.com>
* (c) Julien DIDIER <genzo.wm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Gitonomy\Git\Parser;
use Gitonomy\Git\Exception\RuntimeException;
abstract class ParserBase
{
protected $cursor;
protected $content;
protected $length;
abstract protected function doParse();
public function parse($content)
{
$this->cursor = 0;
$this->content = $content ?? '';
$this->length = strlen($this->content);
$this->doParse();
}
protected function isFinished()
{
return $this->cursor === $this->length;
}
protected function consumeAll()
{
$rest = substr($this->content, $this->cursor);
$this->cursor += strlen($rest);
return $rest;
}
protected function expects($expected)
{
$length = strlen($expected);
$actual = substr($this->content, $this->cursor, $length);
if ($actual !== $expected) {
return false;
}
$this->cursor += $length;
return true;
}
protected function consumeShortHash()
{
if (!preg_match('/([A-Za-z0-9]{7,40})/A', $this->content, $vars, 0, $this->cursor)) {
throw new RuntimeException('No short hash found: '.substr($this->content, $this->cursor, 7));
}
$this->cursor += strlen($vars[1]);
return $vars[1];
}
protected function consumeHash()
{
if (!preg_match('/([A-Za-z0-9]{40})/A', $this->content, $vars, 0, $this->cursor)) {
throw new RuntimeException('No hash found: '.substr($this->content, $this->cursor, 40));
}
$this->cursor += 40;
return $vars[1];
}
protected function consumeRegexp($regexp)
{
if (!preg_match($regexp.'A', $this->content, $vars, 0, $this->cursor)) {
throw new RuntimeException('No match for regexp '.$regexp.' Upcoming: '.substr($this->content, $this->cursor, 500));
}
$this->cursor += strlen($vars[0]);
return $vars;
}
protected function consumeTo($text)
{
$pos = strpos($this->content, $text, $this->cursor);
if (false === $pos) {
throw new RuntimeException(sprintf('Unable to find "%s"', $text));
}
$result = substr($this->content, $this->cursor, $pos - $this->cursor);
$this->cursor = $pos;
return $result;
}
protected function consume($expected)
{
$length = strlen($expected);
$actual = substr($this->content, $this->cursor, $length);
if ($actual !== $expected) {
throw new RuntimeException(sprintf('Expected "%s", but got "%s" (%s)', $expected, $actual, substr($this->content, $this->cursor, 10)));
}
$this->cursor += $length;
return $expected;
}
protected function consumeNewLine()
{
return $this->consume("\n");
}
/**
* @return string
*/
protected function consumeGPGSignature()
{
$expected = "\ngpgsig ";
$length = strlen($expected);
$actual = substr($this->content, $this->cursor, $length);
if ($actual != $expected) {
return '';
}
$this->cursor += $length;
return $this->consumeTo("\n\n");
}
protected function consumeMergeTag()
{
$expected = "\nmergetag ";
$length = strlen($expected);
$actual = substr($this->content, $this->cursor, $length);
if ($actual != $expected) {
return '';
}
$this->cursor += $length;
$this->consumeTo('-----END PGP SIGNATURE-----');
$this->consume('-----END PGP SIGNATURE-----');
}
}