This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrdf-nt-parser.scm
193 lines (154 loc) · 5.99 KB
/
rdf-nt-parser.scm
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
;;; -*- Mode: Scheme; scheme48-package: rdf-nt-parser -*-
;;;; Schemantic Web
;;;; RDF N-Triples Parser
;;; This code is written by Taylor R. Campbell and placed in the Public
;;; Domain. All warranties are disclaimed.
;;; This parser was derived from the grammar at
;;; <http://www.w3.org/TR/rdf-testcases/#ntriples>.
(define-parser nt-parser:document
(parser:sequence
(parser:noise:repeated nt-parser:line)
(parser:map nt-context/user-state (parser:context))))
(define-parser nt-parser:line
(*parser
(nt-parser:ws*)
((parser:choice nt-parser:comment nt-parser:triple (parser:return #f)))
nt-parser:eoln))
(define-parser nt-parser:comment
(parser:sequence
(parser:char= #\#)
(parser:noise:repeated (parser:char-not-in-set nt-char-set:line-break))))
(define-parser nt-parser:triple
(*parser
(subject nt-parser:subject)
(nt-parser:ws+)
(predicate nt-parser:predicate)
(nt-parser:ws+)
(object nt-parser:object)
(nt-parser:ws*)
((parser:char= #\.))
(nt-parser:ws*)
(nt:add-triple (make-rdf-triple subject predicate object))))
(define-parser nt-parser:subject
(parser:choice nt-parser:resource nt-parser:blank))
(define-parser nt-parser:predicate
nt-parser:resource)
(define-parser nt-parser:object
(parser:choice nt-parser:resource nt-parser:blank nt-parser:literal))
(define-parser nt-parser:resource
(*parser (uri-ref nt-parser:uri-ref)
(if (match-string? uri-matcher:uri-reference uri-ref)
(parser:return (string->rdf-uri-ref uri-ref))
(parser:error
(string-append "Malformed RDF URI reference `" uri-ref "'")))))
(define-parser nt-parser:blank
(*parser (node-id nt-parser:node-id)
(parser:return (make-rdf-bnode node-id))))
(define-parser nt-parser:uri-ref
(parser:bracketed-string (parser:char= #\<) (parser:char= #\>)
(parser:char-in-set nt-char-set:uri-ref)))
(define-parser nt-parser:node-id
(*parser ((parser:string= "_:"))
(parser:match->string
(matcher:sequence
(matcher:char-in-set nt-char-set:name-initial)
(matcher:repeated (matcher:char-in-set nt-char-set:name-trailing))))))
(define-parser nt-parser:literal
(*parser (lexical-form nt-parser:string)
(parser:choice
(*parser (datatype-uri nt-parser:literal-datatype-uri)
(parser:return (make-rdf-typed-literal lexical-form datatype-uri)))
(*parser (language-tag
(parser:choice nt-parser:literal-language-tag
(parser:return #f)))
(parser:return (make-rdf-plain-literal lexical-form language-tag))))))
(define-parser nt-parser:literal-datatype-uri
(parser:sequence (parser:string= "^^")
nt-parser:resource))
(define-parser nt-parser:literal-language-tag
(parser:sequence
(parser:char= #\@)
(parser:match->string
(matcher:sequence
(matcher:at-least 1
(matcher:char-in-set nt-char-set:language-initial))
(matcher:repeated
(matcher:sequence
(matcher:char= #\-)
(matcher:at-least 1
(matcher:char-in-set nt-char-set:language-trailing))))))))
(define-parser nt-parser:string
(parser:bracketed-string (parser:char= #\") (parser:char= #\")
nt-parser:string-char))
(define-parser nt-parser:string-char
(*parser (char (parser:char))
(if (char=? char #\\)
nt-parser:string-escape
(parser:return char))))
(define-parser nt-parser:string-escape
(*parser (escape-char (parser:char))
(case escape-char
((#\t) (parser:return (ascii->char #x09)))
((#\n) (parser:return (ascii->char #x0A)))
((#\r) (parser:return (ascii->char #x0D)))
((#\u) (nt-parser:unicode-escape 4))
((#\U) (nt-parser:unicode-escape 8))
(else (parser:return escape-char)))))
(define-parser (nt-parser:unicode-escape length)
(*parser (hex-string (parser:hex-string length))
(parser:return
(let ((number (string->number hex-string #x10)))
(if (< number ascii-limit)
(ascii->char number)
#\?))))) ;++ fix
(define-parser (parser:hex-string length)
(parser:string:exactly length (parser:char-in-set char-set:hex-digit)))
(define-parser nt-parser:eoln
(let ((CR (parser:char= (ascii->char #x0D)))
(LF (parser:char= (ascii->char #x0A))))
(parser:choice (parser:sequence CR (parser:choice LF (parser:return '())))
LF)))
(define-parser nt-parser:ws
(parser:char-in-set nt-char-set:ws))
(define-parser nt-parser:ws*
(parser:noise:repeated nt-parser:ws))
(define-parser nt-parser:ws+
(parser:noise:at-least 1 nt-parser:ws))
;;;; N-Triples Character Sets
(define nt-char-set:ws
(char-set (ascii->char #x09) ;Horizontal tab
(ascii->char #x20))) ;Space
(define nt-char-set:line-break
(char-set (ascii->char #x0A) ;Line feed
(ascii->char #x0D))) ;Carriage return
(define nt-char-set:language-initial
char-set:lower-case)
(define nt-char-set:language-trailing
(char-set-union char-set:lower-case
char-set:digit))
(define nt-char-set:name-initial
char-set:letter)
(define nt-char-set:name-trailing
(char-set-union char-set:letter
char-set:digit))
(define nt-char-set:uri-ref
(char-set-union char-set:letter
char-set:digit
(string->char-set "!#$&'()*+,-./:;=?@_~")))
;;;; N-Triples Context
(define-record-type <nt-context>
(make-nt-context triple-handler user-state)
nt-context?
(triple-handler nt-context/triple-handler)
(user-state nt-context/user-state))
(define (make-nt-parser-context triple-handler initial-user-state)
(make-nt-context triple-handler initial-user-state))
(define (nt-context/add-triple context triple)
(make-nt-context (nt-context/triple-handler context)
((nt-context/triple-handler context)
triple
(nt-context/user-state context))))
(define-parser (nt:add-triple triple)
(parser:modify-context
(lambda (context)
(nt-context/add-triple context triple))))