Skip to content

Commit

Permalink
N3Lexer: allow typeIRI tokens in line mode (closes pietercolpaert#32) (
Browse files Browse the repository at this point in the history
…pietercolpaert#41)

* N3Lexer: allow typeIRI tokens in line mode (closes pietercolpaert#32)

See https://www.w3.org/TR/n-triples/#grammar-production-literal
and https://www.w3.org/TR/n-quads/#grammar-production-literal

* N3Lexer: allow blank tokens in line mode (closes pietercolpaert#34)

Blank nodes are allowed as subjects and predicates in n-triples and n-quads -
see https://www.w3.org/TR/n-triples/#grammar-production-subject and
https://www.w3.org/TR/n-quads/#grammar-production-subject (in n-quads
also as graphs).
  • Loading branch information
zozlak authored Feb 27, 2024
1 parent 099f49e commit 142f38d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/N3Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct($options = [])
$this->_tokenize = function ($input, $finalize = true) use ($self) {
$tokens = \call_user_func($this->_oldTokenize, $input, $finalize);
foreach ($tokens as $token) {
if (!preg_match('/^(?:IRI|prefixed|literal|langcode|type|\.|eof)$/', $token['type'])) {
if (!preg_match('/^(?:blank|IRI|prefixed|literal|langcode|type|typeIRI|\.|eof)$/', $token['type'])) {
throw $self->syntaxError($token['type'], $token['line']);
}
}
Expand Down
12 changes: 11 additions & 1 deletion test/TriGParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ private function shouldParse($createParser, $input = ''): void
//expect($error).not.to.exist;
if ($triple) {
$results[] = $triple;
} elseif ($error) {
throw $error;
} else {
$this->assertEquals(self::toSortedJSON($items), self::toSortedJSON($results));
}
Expand Down Expand Up @@ -1211,7 +1213,7 @@ public function testTriGFormat(): void

public function testNTriplesFormat(): void
{
$parser = function () { return new TriGParser(['format' => 'N-Triples']); };
$parser = function () { return new TriGParser(['format' => 'N-Triples', 'blankNodePrefix' => '']); };

// should parse a single triple
$this->shouldParse($parser, '<http://ex.org/a> <http://ex.org/b> "c".',
Expand Down Expand Up @@ -1241,6 +1243,14 @@ public function testNTriplesFormat(): void

// should not parse a formula as object
$this->shouldNotParse($parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{" on line 1.');

// https://github.com/pietercolpaert/hardf/issues/32
$this->shouldParse($parser, '<http://a.example/s> <http://a.example/p> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .',
['http://a.example/s', 'http://a.example/p', '"1"^^http://www.w3.org/2001/XMLSchema#integer']);

// https://github.com/pietercolpaert/hardf/issues/34
$this->shouldParse($parser, '_:r1 <https://foo.bar> "baz".',
['_:r1', 'https://foo.bar', '"baz"']);
}

public function testNQuadsFormat(): void
Expand Down

0 comments on commit 142f38d

Please sign in to comment.