From: <cw...@us...> - 2007-08-13 06:44:39
|
Revision: 501 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=501&view=rev Author: cweiske Date: 2007-08-12 23:44:36 -0700 (Sun, 12 Aug 2007) Log Message: ----------- Add single quote support to N3 parser, along with unit tests Modified Paths: -------------- trunk/rdfapi-php/api/syntax/N3Parser.php trunk/rdfapi-php/test/unit/Syntax/n3Parser_test.php Modified: trunk/rdfapi-php/api/syntax/N3Parser.php =================================================================== --- trunk/rdfapi-php/api/syntax/N3Parser.php 2007-08-13 06:14:37 UTC (rev 500) +++ trunk/rdfapi-php/api/syntax/N3Parser.php 2007-08-13 06:44:36 UTC (rev 501) @@ -76,7 +76,12 @@ $bNode = '_:'.$Name; $Univar = '\?'.$Name; $QName = '(?:[A-Za-z][A-Za-z0-9_@\.]*)?:'.$Name; - $Literal = '"(\\\"|[^"])*"'; # '"(?:\\"|[^"])*"' + $Literal = '(?:' + . '"(\\\"|[^"])*"' + . '|' + . "'(\\\'|[^'])*'" + . ')'; + # '"(?:\\"|[^"])*"' $Number = '[-+]?[0-9]+(\\.[0-9]+)?([eE][-+]?[0-9]+)?'; $Boolean = '@(?:true|false)'; // $Literal = '"[^"\\\\]*(?:\\.\\[^"\\]*)*"'; # '"(?:\\"|[^"])*"' @@ -84,7 +89,11 @@ $Datatype = '(\^\^)[^ ,\.;)]+'; $Datatype_URI = '(\^\^)'.$URI; // $LLiteral = '"""[^"\\\\]*(?:(?:.|"(?!""))[^"\\\\]*)*"""'; - $LLiteral = '"""[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""'; + $LLiteral = '(?:' + . '"""[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""' + . '|' + . "'''[^'\\\\]*(?:(?:\\\\.|'(?!''))[^\"\\\\]*)*'''" + . ')'; // '"""[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""' $Comment = '#.*$'; $Prefix = '(?:[A-Za-z][A-Za-z0-9_]*)?:'; @@ -576,7 +585,7 @@ //float or decimal // After conversion we cannot distinguish between both $list[$i] = floatval($list[$i]); - } else if ((!strstr('<_"?.;,{}[]()@', $list[$i]{0})) + } else if ((!strstr('<_"\'?.;,{}[]()@', $list[$i]{0})) && (substr($list[$i],0,3) != '^^<') ) { //prefix or unknown @@ -596,16 +605,28 @@ } } else { - if ($list[$i]{0} == '"') { // Congratulations - it's a literal! - if (substr($list[$i],0,3) == '"""') { - if (substr($list[$i],-3,3) == '"""') { // A big literal... + if ($list[$i]{0} == '"') { + $bLiteral = true; + $chBase = '"'; + } else if ($list[$i]{0} == '\'') { + $bLiteral = true; + $chBase = '\''; + } else { + $bLiteral = false; + } + if ($bLiteral) { + $tripleBase = $chBase . $chBase . $chBase; + // Congratulations - it's a literal! + if (substr($list[$i], 0, 3) == $tripleBase) { + if (substr($list[$i],-3,3) == $tripleBase) { + // A big literal... $lit = substr($list[$i],3,-3); // print "++$lit++"; $lit=str_replace('\n', '\\n',$lit); - $lit=ereg_replace("[^\\]\"", "\\\"", $lit); + $lit=ereg_replace("[^\\]" . $chBase, "\\" . $chBase, $lit); - $list[$i] = '"'.$lit.'"'; + $list[$i] = $chBase . $lit . $chBase; } else { die ('Incorrect string formatting: '.substr($list[$i],-3,3)); } @@ -1044,7 +1065,7 @@ function toRDFNode($s, $state) { $ins = substr($s, 1, -1); - if ($s{0} == '"') { + if ($s{0} == '"' || $s{0} == '\'') { $lang = NULL; if (count($state)>3) { Modified: trunk/rdfapi-php/test/unit/Syntax/n3Parser_test.php =================================================================== --- trunk/rdfapi-php/test/unit/Syntax/n3Parser_test.php 2007-08-13 06:14:37 UTC (rev 500) +++ trunk/rdfapi-php/test/unit/Syntax/n3Parser_test.php 2007-08-13 06:44:36 UTC (rev 501) @@ -37,16 +37,21 @@ } - function testIsMemmodel() { + + + function testIsMemmodel() + { // Import Package $n3pars= new N3Parser(); $model=$n3pars->parse2model($_SESSION['n3TestInput'],false); $this->assertIsA($model, 'memmodel'); } - function testParsing() { + + function testParsing() + { $n3pars= new N3Parser(); $model=$n3pars->parse2model($_SESSION['n3TestInput'],false); @@ -77,7 +82,62 @@ $this->assertTrue($model->containsAll($model2)); } - function testPrefixNotDeclared() { + + + /** + * Test different string quotation methods + */ + function testQuotes() + { + $n3 = <<<EOT +@prefix : <http://example.org/#> . + +# This file uses UNIX line end conventions. + +:x1 :p1 'x' . +:x2 :p2 '''x +y''' . + +:x3 :p3 """x +y"""^^:someType . + + +EOT; + $parser = &new N3Parser(); + $model = &$parser->parse2model($n3, false); + + $model2 = new MemModel(); + $model2->add( + new Statement( + new Resource("http://example.org/#x1"), + new Resource("http://example.org/#p1"), + new Literal('x') + ) + ); + $model2->add( + new Statement( + new Resource("http://example.org/#x2"), + new Resource("http://example.org/#p2"), + new Literal("x\ny") + ) + ); + $model2->add( + new Statement( + new Resource("http://example.org/#x3"), + new Resource("http://example.org/#p3"), + new Literal("x\ny", null, 'http://example.org/#someType') + ) + ); + + //var_dump($model->triples, $model2->triples); + $this->assertEqual(3, $model->size()); + $this->assertTrue($model->containsAll($model2)); + }//function testQuotes() + + + + function testPrefixNotDeclared() + { $rdfInput=' @prefix m: <http://www.example.org/meeting_organization#>. @@ -93,7 +153,10 @@ $this->assertErrorPattern('[Prefix not declared: p:]'); } - function testLoneSemicolon() { + + + function testLoneSemicolon() + { $n3 = '<a> <b> <c> ; .'; $parser = &new N3Parser(); $model = &$parser->parse2model($n3, false); @@ -101,7 +164,10 @@ $this->assertNoErrors(); } - function testTightClosingList() { + + + function testTightClosingList() + { $n3 = '@prefix : <http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql4/manifest#> . @prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . <> mf:entries ( mf:syn-09) .'; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |