|
3 | 3 | include 'lib/sparql.lex.php';
|
4 | 4 | include 'lib/sparql.php';
|
5 | 5 |
|
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; |
10 | 11 |
|
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 | + } |
28 | 17 |
|
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 | + } |
35 | 29 |
|
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 | + } |
44 | 52 | }
|
0 commit comments