modules/rdf/parser/nquads.zzm

rdf-0.0.1 source code

=encoding utf8

=head1 NAME

rdf/parser/nquads - N-Quads parser.

=head1 SYNOPSIS

  from rdf/parser/nquads import NQuadsParser;
  
  let parser := new NQuadsParser();
  let quads := parser.parse_string("<s> <p> \"o\" <g> .\n");


=head1 DESCRIPTION

C<NQuadsParser> parses RDF 1.1 N-Quads into RDF quads. Lines without an
explicit graph are loaded into the default graph. It accepts C<base> and
C<into> parser options for consistency with the other RDF parsers, though
N-Quads requires absolute IRIs.

=head1 EXPORTS

=head2 Classes

=over

=item C<NQuadsParser>

=over

=item C<< parse_string(String text, ... options) >>

Parses C<text>. Returns an array of quads, or the supplied C<into> store
after adding the quads. Throws C<RDFSyntaxError> on invalid input.

=item C<< parse_file(path, ... options) >>

Reads UTF-8 from C<path> and parses it.

=item C<< parse_lines(Array lines, ... options) >>

Parses concatenated line chunks.

=item C<< parse_chunks(Array chunks, ... options) >>

Parses concatenated string or nested-array chunks.

=back

=back

=head1 COPYRIGHT AND LICENCE

B<< rdf/parser/nquads >> is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under
the terms of either the Artistic License 1.0 or the GNU General Public
License version 2.

=cut

from rdf/parser/common import RDFReader, _parser_result;

function _rdf_concat_chunks ( Array chunks ) {
	let text := "";
	for ( let chunk in chunks ) {
		if ( chunk instanceof Array ) {
			text _= _rdf_concat_chunks(chunk);
		}
		else {
			text _= chunk;
		}
	}
	return text;
}

class NQuadsParser {
	method parse_string ( String text, ... PairList options ) {
		return _parser_result(
			( new RDFReader(source: text) ).read_nt_all(true),
			options,
		);
	}

	method parse_file ( path, ... PairList options ) {
		return self.parse_string(path.slurp_utf8(), ...options);
	}

	method parse_lines ( Array lines, ... PairList options ) {
		return self.parse_string(_rdf_concat_chunks(lines), ...options);
	}

	method parse_chunks ( Array chunks, ... PairList options ) {
		return self.parse_string(_rdf_concat_chunks(chunks), ...options);
	}
}