1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Syntax elements of Turtle.
use iref::IriRefBuf;
use locspan::Loc;
use langtag::LanguageTagBuf;
pub use rdf_types::{BlankIdBuf, StringLiteral};

/// An IRI or compact IRI.
#[derive(Clone, Debug)]
pub enum Iri<F> {
	IriRef(IriRefBuf),
	Compact(Option<Loc<String, F>>, Loc<String, F>),
}

/// A Turtle document.
pub struct Document<F> {
	pub statements: Vec<Loc<Statement<F>, F>>,
}

impl<F> Document<F> {
	pub fn base_prefix(&self) -> Option<iref::IriRef> {
		for stm in &self.statements {
			if let Statement::Directive(Directive::Base(iri)) = stm.as_ref() {
				return Some(iri.as_iri_ref());
			}
		}

		None
	}
}

/// A statement (directive of triples declaration).
pub enum Statement<F> {
	/// Directive.
	Directive(Directive<F>),

	/// Triples declaration.
	Triples(Loc<Subject<F>, F>, Vec<Loc<PredicateObjects<F>, F>>),
}

/// A directive.
pub enum Directive<F> {
	/// `@prefix` directive.
	Prefix(Loc<String, F>, Loc<IriRefBuf, F>),

	/// `@base` directive.
	Base(Loc<IriRefBuf, F>),

	/// SPARQL `PREFIX` directive.
	SparqlPrefix(Loc<String, F>, Loc<IriRefBuf, F>),

	/// SPARQL `BASE` directive.
	SparqlBase(Loc<IriRefBuf, F>),
}

/// Verb (either `a` or a predicate).
#[derive(Debug)]
pub enum Verb<F> {
	/// `a` keyword.
	A,

	/// Predicate.
	Predicate(Iri<F>),
}

#[derive(Debug)]
pub enum BlankNode<F> {
	Label(BlankIdBuf),
	Anonymous(Vec<Loc<PredicateObjects<F>, F>>),
}

/// Subject of a triples declaration.
pub enum Subject<F> {
	/// IRI or compact IRI.
	Iri(Iri<F>),

	/// Blank node.
	BlankNode(BlankNode<F>),

	/// Collection of subjects.
	Collection(Vec<Loc<Object<F>, F>>),
}

/// Object of a triples declaration.
#[derive(Debug)]
pub enum Object<F> {
	/// IRI or compact IRI.
	Iri(Iri<F>),

	/// Blank node.
	BlankNode(BlankNode<F>),

	/// Collection of objects.
	Collection(Vec<Loc<Self, F>>),

	/// Literal value.
	Literal(Literal<F>),
}

#[derive(Debug)]
pub struct PredicateObjects<F> {
	pub verb: Loc<Verb<F>, F>,
	pub objects: Loc<Vec<Loc<Object<F>, F>>, F>,
}

/// Literal value.
#[derive(Debug)]
pub enum Literal<F> {
	Rdf(RdfLiteral<F>),

	/// Numerical value.
	Numeric(Numeric),

	/// Boolean value.
	Boolean(bool),
}

/// RDF Literal.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum RdfLiteral<F> {
	/// Untyped string literal.
	String(Loc<StringLiteral, F>),

	/// Typed string literal.
	TypedString(Loc<StringLiteral, F>, Loc<IriRefBuf, F>),

	/// Language string.
	LangString(Loc<StringLiteral, F>, Loc<LanguageTagBuf, F>),
}

pub use crate::lexing::Numeric;