Skip to content

Commit a46f241

Browse files
authored
Merge pull request shinobu#1 from white-gecko/feature/reorganizeCode
Reorganize Code of SparqlPHPParserMain.php
2 parents c5e7538 + 2f4f6f0 commit a46f241

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

SparqlPHPParserMain.php

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,50 @@
33
include 'lib/sparql.lex.php';
44
include 'lib/sparql.php';
55

6-
class SparqlPHPParserMain {
7-
//root gets set in the parser
8-
public $root = null;
9-
public $parser = null;
6+
class SparqlPHPParserMain
7+
{
8+
//root gets set in the parser
9+
public $root = null;
10+
public $parser = null;
1011

11-
private function parse($fp) {
12-
if (!isset($parser)) {
13-
$this->parser = new SparqlPHPParser($this);
14-
}
15-
$scanner = new SparqlLexer($fp);
16-
while ($x = $scanner->nextToken())
17-
{
18-
if($x->type != -1) {
19-
$this->parser->doParse($x->type, $x);
20-
} else {
21-
$err = 'Invalid Input in line ' . $x->line . '. Problem with: ' . $x->value . fgets($fp, 15);
22-
throw new Exception($err);
23-
}
24-
}
25-
$this->parser->doParse(0);
26-
27-
}
12+
private function parse($filePointer)
13+
{
14+
if (!isset($this->parser)) {
15+
$this->parser = new SparqlPHPParser($this);
16+
}
2817

29-
function parseString($string) {
30-
$fp = fopen("php://memory", 'r+');
31-
fwrite($fp, $string);
32-
rewind($fp);
33-
return $this->parse($fp);
34-
}
18+
$scanner = new SparqlLexer($filePointer);
19+
while ($token = $scanner->nextToken()) {
20+
if ($token->type != -1) {
21+
$this->parser->doParse($token->type, $token);
22+
} else {
23+
$err = 'Invalid Input in line ' . $token->line . '. Problem with: ' . $token->value . fgets($filePointer, 15);
24+
throw new Exception($err);
25+
}
26+
}
27+
$this->parser->doParse(0);
28+
}
3529

36-
function parseFile($file) {
37-
$fp = fopen($file, 'r+') or die('Unable to open file with path: ' . $file);
38-
return $this->parse($fp);
39-
}
40-
41-
function resetParser() {
42-
//TODO
43-
}
30+
public function parseString($string)
31+
{
32+
$filePointer = fopen("php://memory", 'r+');
33+
fwrite($filePointer, $string);
34+
rewind($filePointer);
35+
return $this->parse($filePointer);
36+
}
37+
38+
public function parseFile($file)
39+
{
40+
try {
41+
$filePointer = fopen($file, 'r+');
42+
} catch (Exception $e) {
43+
throw new Exception('Unable to open file with path: ' . $file, 0, $e);
44+
}
45+
return $this->parse($filePointer);
46+
}
47+
48+
public function resetParser()
49+
{
50+
//TODO
51+
}
4452
}

0 commit comments

Comments
 (0)