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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Syntax elements of Turtle.

use source_span::Loc;
use iref::{
	Iri,
	IriBuf
};

/// A blank node name.
pub type BlankNode = String;

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

impl IriRef {
	pub fn curie_from_str(s: &str) -> IriRef {
		if let Some(i) = s.find(':') {
			let (prefix, id) = s.split_at(i);
			let (_, id) = id.split_at(1);
			IriRef::Curie(Some(prefix.to_string()), id.to_string())
		} else {
			IriRef::Curie(None, s.to_string())
		}
	}
}

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

impl Document {
	pub fn base_prefix(&self) -> Option<Iri> {
		for stm in &self.statements {
			match stm.as_ref() {
				Statement::Directive(Directive::Base(iri)) => return Some(iri.as_iri()),
				_ => ()
			}
		}

		None
	}
}

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

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

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

	/// `@base` directive.
	Base(Loc<IriBuf>)
}

/// verb-objects part of a triples declaration.
pub struct VerbObjects {
	pub verb: Loc<Verb>,
	pub objects: Loc<Vec<Loc<Object>>>
}

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

	/// Predicate.
	Predicate(IriRef)
}

impl<'a> From<&'a str> for Verb {
	fn from(s: &'a str) -> Verb {
		if s == "a" {
			Verb::A
		} else {
			if let Some(i) = s.find(':') {
				let (prefix, id) = s.split_at(i);
				let (_, id) = id.split_at(1);
				Verb::Predicate(IriRef::Curie(Some(prefix.to_string()), id.to_string()))
			} else {
				Verb::Predicate(IriRef::Curie(None, s.to_string()))
			}
		}
	}
}

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

	/// Blank node.
	BlankNode(BlankNode),

	/// Collection of subjects.
	Collection(Vec<Loc<Subject>>),

	/// No subject.
	Blank(Vec<Loc<VerbObjects>>)
}

impl<'a> From<&'a str> for Subject {
	fn from(s: &'a str) -> Subject {
		if let Some(i) = s.find(':') {
			let (prefix, id) = s.split_at(i);
			if prefix == "_" {
				// println!("bl: {}", s);
				Subject::BlankNode(id.to_string())
			} else {
				let (_, id) = id.split_at(1);
				Subject::Iri(IriRef::Curie(Some(prefix.to_string()), id.to_string()))
			}
		} else {
			Subject::Iri(IriRef::Curie(None, s.to_string()))
		}
	}
}

/// Object of a triples declaration.
pub enum Object {
	/// IRI or compact IRI.
	Iri(IriRef),

	/// Blank node.
	BlankNode(BlankNode),

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

	/// No object.
	Blank(Vec<Loc<VerbObjects>>),

	/// Literal value.
	Literal(Literal)
}

impl<'a> From<&'a str> for Object {
	fn from(s: &'a str) -> Object {
		if let Some(i) = s.find(':') {
			let (prefix, id) = s.split_at(i);
			if prefix == "_" {
				Object::BlankNode(id.to_string())
			} else {
				let (_, id) = id.split_at(1);
				Object::Iri(IriRef::Curie(Some(prefix.to_string()), id.to_string()))
			}
		} else {
			Object::Iri(IriRef::Curie(None, s.to_string()))
		}
	}
}

/// Literal value.
pub enum Literal {
	/// Maybe tagged string.
	String(String, Option<Loc<Tag>>),

	/// NUmerical value.
	Numeric(Numeric),

	/// Boolean value.
	Boolean(bool)
}

/// String tag.
pub enum Tag {
	/// Lang tag for lang strings.
	Lang(String),

	/// IRI tag.
	Iri(IriRef)
}

/// Numerical value.
#[derive(Clone, Copy, Debug)]
pub enum Numeric {
	/// Integer.
	Int(i64),

	/// Decimal value.
	Decimal(bool, u32, u32), // sign, integer part, decimal part

	/// Double precision decimal value.
	Double(bool, u32, u32, i32) // sign, integer part, decimal part, exponent
}