]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/reader.lisp
Implement CTM integer occurrences and canonical integer CXTM
[topic-maps.git] / src / ctm / reader.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defstruct (parser (:constructor make-parser (lexer document-iri)))
4 lexer document-iri lookahead (prefixes (make-hash-table :test #'equal)))
5 (defun look (parser)
6 (or (parser-lookahead parser)
7 (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
8 (defun consume (parser)
9 (prog1 (look parser) (setf (parser-lookahead parser) nil)))
10 (defun punctuation-p (parser character)
11 (and (eq :punctuation (token-kind (look parser)))
12 (eql character (token-value (look parser)))))
13
14 (defun resolve-qname (parser token)
15 "CTM 3.3.4: concatenate the resolved binding and local part exactly once."
16 (let* ((text (token-value token)) (colon (position #\: text))
17 (prefix (subseq text 0 colon)) (bound (gethash prefix (parser-prefixes parser))))
18 (unless bound
19 (problem 'undefined-prefix :undefined-prefix (token-position token)
20 (format nil "Prefix ~S has no binding." prefix)))
21 ;; Do not re-resolve or percent-decode an already resolved binding.
22 (validate-iri-characters (concatenate 'string bound (subseq text (1+ colon)))
23 (token-position token))))
24
25 (defun read-prefix-directive (parser)
26 "CTM 3.16.1: document-ordered, per-deserialization bindings; reference is an IRI."
27 (consume parser) ; %prefix
28 (let ((name (consume parser)) (reference (consume parser)))
29 (unless (eq :identifier (token-kind name))
30 (problem 'ctm-rejection :expected-prefix-name (token-position name) "Expected a prefix identifier."))
31 (unless (eq :iri (token-kind reference))
32 (problem 'ctm-rejection :expected-prefix-iri (token-position reference)
33 "A prefix binding requires an IRI, not a QName or Topic identifier."))
34 (let* ((iri (resolve-iri (token-value reference) (parser-document-iri parser) (token-position reference)))
35 (old (gethash (token-value name) (parser-prefixes parser))))
36 (when (and old (not (string= old iri)))
37 (problem 'prefix-collision :prefix-collision (token-position name)
38 (format nil "Prefix ~S is already bound to ~S, not ~S." (token-value name) old iri)))
39 (setf (gethash (token-value name) (parser-prefixes parser)) iri))))
40
41 (defun identity-value (parser &optional explicit-kind)
42 (let ((token (consume parser)))
43 (case (token-kind token)
44 (:identifier
45 (when explicit-kind
46 (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
47 (when (member (token-value token) '("def" "isa" "ako" "end") :test #'string=)
48 (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
49 (values :item-identifier
50 (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
51 (:iri
52 (values (or explicit-kind :subject-identifier)
53 (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
54 (:qname (values (or explicit-kind :subject-identifier) (resolve-qname parser token)))
55 (:punctuation
56 (case (token-value token)
57 (#\= (if explicit-kind
58 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
59 (identity-value parser :subject-locator)))
60 (#\^ (if explicit-kind
61 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
62 (identity-value parser :item-identifier)))
63 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity."))))
64 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
65
66 (defun identity-reference (parser map)
67 (multiple-value-bind (kind iri) (identity-value parser)
68 (topic-maps.tmdm:intern-topic map kind iri)))
69
70 (defun literal-iri-reference (parser)
71 "An IRI locator, not a Topic identity. Used for values/datatype syntax."
72 (let ((token (consume parser)))
73 (case (token-kind token)
74 (:iri (resolve-iri (token-value token) (parser-document-iri parser) (token-position token)))
75 (:qname (resolve-qname parser token))
76 (otherwise (problem 'ctm-rejection :expected-datatype-iri (token-position token)
77 "A datatype qualifier requires an IRI reference, not a Topic identifier.")))))
78
79 (defun read-occurrence-literal (parser)
80 (let ((token (look parser)))
81 (case (token-kind token)
82 (:integer
83 (consume parser)
84 ;; Numeric-literal value policy: represent the unquoted integer value
85 ;; canonically; qualified quoted strings retain their lexical value.
86 (values (topic-maps.tmdm:canonical-integer-lexical-form (token-value token))
87 (topic-maps.tmdm:integer-datatype)))
88 (:excluded-literal
89 (problem 'ctm-unsupported :occurrence-literal-datatype (token-position token)
90 "Decimal/date/dateTime and the separate CTM '*' literal are outside this slice."))
91 (:string
92 (consume parser)
93 (if (punctuation-p parser #\^)
94 (let ((caret (consume parser)))
95 (unless (and (punctuation-p parser #\^)
96 (= (token-position (look parser)) (1+ (token-position caret))))
97 (problem 'ctm-rejection :invalid-datatype-marker (token-position caret) "Expected contiguous '^^'."))
98 (consume parser)
99 (let ((datatype (literal-iri-reference parser)))
100 (when (and (string= datatype (topic-maps.tmdm:integer-datatype))
101 (not (topic-maps.tmdm:integer-lexical-p (token-value token))))
102 (problem 'ctm-rejection :invalid-integer-value (token-position token)
103 "Quoted value is not an xsd:integer lexical form."))
104 ;; TMDM 4.4 requires absolute anyURI content; quoted content is
105 ;; lexical data, never an IRI reference to resolve against the document.
106 (when (and (string= datatype (topic-maps.tmdm:iri-datatype))
107 (not (topic-maps.tmdm::absolute-locator-p (token-value token))))
108 (problem 'ctm-rejection :relative-uri-value (token-position token)
109 "A quoted anyURI value must already be absolute (TMDM 4.4)."))
110 (values (token-value token) datatype)))
111 (values (token-value token) (topic-maps.tmdm:string-datatype))))
112 ((:iri :qname) (values (literal-iri-reference parser) (topic-maps.tmdm:iri-datatype)))
113 (otherwise (problem 'ctm-rejection :expected-occurrence-value (token-position token)
114 "Expected an integer, quoted string or IRI-form Occurrence value.")))))
115
116 (defun read-identity-tail (parser map topic)
117 (let ((start (look parser)))
118 (multiple-value-bind (kind iri) (identity-value parser)
119 (if (punctuation-p parser #\:)
120 (progn
121 (consume parser)
122 (multiple-value-bind (value datatype) (read-occurrence-literal parser)
123 (let ((type (topic-maps.tmdm:intern-topic map kind iri)))
124 (topic-maps.tmdm:add-occurrence topic type value :datatype datatype)
125 (topic-maps.tmdm:canonical-topic topic))))
126 (if (eq :identifier (token-kind start))
127 (if (punctuation-p parser #\()
128 (problem 'ctm-unsupported :topic-tail (token-position start) "Template invocations are outside this subset.")
129 (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
130 "Expected ':' after the Occurrence Type."))
131 (topic-maps.tmdm:add-topic-identifier topic kind iri))))))
132
133 (defun read-topic (parser map &optional (topic (identity-reference parser map)))
134 (let ((has-tail nil))
135 (loop
136 (cond
137 ((punctuation-p parser #\.) (consume parser) (return topic))
138 ((punctuation-p parser #\-)
139 (when has-tail
140 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
141 (consume parser)
142 (let ((value (consume parser)))
143 (when (member (token-kind value) '(:identifier :iri :qname))
144 ;; Recognize the typed-name syntax, without resolving its type
145 ;; or creating information items for unsupported name semantics.
146 (unless (punctuation-p parser #\:)
147 (problem 'ctm-rejection :expected-name-type-separator
148 (token-position (look parser)) "Expected ':' after the name type."))
149 (consume parser)
150 (let ((literal (consume parser)))
151 (unless (eq :string (token-kind literal))
152 (problem 'ctm-rejection :expected-name-value (token-position literal)
153 "Expected a quoted name value after the name type separator.")))
154 (problem 'ctm-unsupported :typed-name (token-position value)
155 "Typed names are outside the implemented subset (CTM 3.11)."))
156 (unless (eq :string (token-kind value))
157 (problem 'ctm-rejection :expected-name-value (token-position value)
158 "Expected a quoted name value after '-'."))
159 (topic-maps.tmdm:add-default-name topic (token-value value)))
160 (setf has-tail t))
161 ((and has-tail (punctuation-p parser #\;))
162 (consume parser)
163 (setf has-tail nil)
164 (when (punctuation-p parser #\.) (consume parser) (return topic)))
165 ((eq :eof (token-kind (look parser)))
166 (problem 'ctm-rejection :missing-period (token-position (look parser)) "Topic declaration requires a final period."))
167 ((or (and (eq :punctuation (token-kind (look parser)))
168 (find (token-value (look parser)) "=^"))
169 (member (token-kind (look parser)) '(:identifier :iri :qname)))
170 (when has-tail
171 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
172 (setf topic (read-identity-tail parser map topic) has-tail t))
173 ((punctuation-p parser #\()
174 (problem 'ctm-unsupported :variant (token-position (look parser)) "Variant names are outside the implemented subset."))
175 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
176
177 (defun read-association (parser map type)
178 "CTM 3.13, restricted to one or two role declarations; no scope/reifier."
179 (consume parser) ; opening parenthesis already recognized by the caller
180 (let ((roles nil) (third-position nil))
181 (loop
182 (when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
183 (problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
184 (when (= 2 (length roles)) (setf third-position (token-position (look parser))))
185 (let* ((role-type (identity-reference parser map))
186 (type-end (lexer-cursor (parser-lexer parser)))
187 (colon (look parser)))
188 (unless (punctuation-p parser #\:)
189 (problem 'ctm-rejection :expected-role-type-separator (token-position colon) "Expected ':' after the Role type."))
190 (consume parser)
191 (let ((player-token (look parser)))
192 ;; CTM role production requires layout on at least one side of ':'.
193 ;; skip-layout handles whitespace and comments; gaps use source offsets.
194 (unless (or (> (token-position colon) type-end)
195 (> (token-position player-token) (1+ (token-position colon))))
196 (problem 'ctm-rejection :role-separator-whitespace (token-position colon)
197 "A Role separator needs whitespace or a comment on at least one side."))
198 (when (or (eq :eof (token-kind player-token))
199 (punctuation-p parser #\)) (punctuation-p parser #\,))
200 (problem 'ctm-rejection :expected-role-player (token-position player-token) "Expected a Topic as the Role player.")))
201 (push (list role-type (identity-reference parser map)) roles))
202 (cond ((punctuation-p parser #\,) (consume parser))
203 ((punctuation-p parser #\)) (consume parser) (return))
204 ((eq :eof (token-kind (look parser)))
205 (problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
206 (t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
207 (when (> (length roles) 2)
208 (problem 'ctm-unsupported :association-arity third-position "Associations with more than two Role declarations are outside Slice 3."))
209 (topic-maps.tmdm:make-association map type (nreverse roles))))
210
211 (defun deserialize-string (text &key document-iri source-name)
212 "Read the documented CTM subset, with per-document prefixes and qualified strings.
213 Source positions are zero-based character offsets; lines/columns are one-based."
214 (check-type text string)
215 (let ((*source-name* source-name) (*source-text* text))
216 (require-document-iri document-iri)
217 (let ((parser (make-parser (make-lexer text) document-iri))
218 (map (topic-maps.tmdm:make-topic-map)))
219 (loop until (eq :eof (token-kind (look parser))) do
220 (if (eq :directive (token-kind (look parser)))
221 (read-prefix-directive parser)
222 (let ((topic (identity-reference parser map)))
223 (if (punctuation-p parser #\()
224 (read-association parser map topic)
225 (read-topic parser map topic)))))
226 map)))
227
228 (defun deserialize-file (pathname &key document-iri source-name)
229 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
230 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
231 (let ((*source-name* source-name) (*source-text* ""))
232 (require-document-iri document-iri)
233 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
234 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
235 (unless (= (length bytes) (read-sequence bytes input))
236 (error "Input file changed or was truncated during reading."))
237 (deserialize-string (decode-utf8 bytes :strip-bom t)
238 :document-iri document-iri :source-name source-name)))))