1 (in-package #:topic-maps.ctm)
3 (defstruct (parser (:constructor make-parser (lexer document-iri)))
4 lexer document-iri lookahead (prefixes (make-hash-table :test #'equal)))
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)))))
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))))
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))))
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))))
41 (defun identity-value (parser &optional explicit-kind)
42 (let ((token (consume parser)))
43 (case (token-kind token)
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))))
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)))
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.")))))
66 (defun identity-reference (parser map)
67 (multiple-value-bind (kind iri) (identity-value parser)
68 (topic-maps.tmdm:intern-topic map kind iri)))
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.")))))
79 (defun read-occurrence-literal (parser)
80 (let ((token (look parser)))
81 (case (token-kind token)
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)))
90 ;; Same value-representation policy as bare integers; token spelling is
91 ;; retained by the lexer, qualified strings remain lexical model data.
92 (values (topic-maps.tmdm:canonical-decimal-lexical-form (token-value token))
93 (topic-maps.tmdm:decimal-datatype)))
96 (values (topic-maps.tmdm:canonical-date-lexical-form (token-value token)) (topic-maps.tmdm:date-datatype)))
98 (problem 'ctm-unsupported :occurrence-literal-datatype (token-position token)
99 "DateTime and the separate CTM '*' literal are outside this slice."))
102 (if (punctuation-p parser #\^)
103 (let ((caret (consume parser)))
104 (unless (and (punctuation-p parser #\^)
105 (= (token-position (look parser)) (1+ (token-position caret))))
106 (problem 'ctm-rejection :invalid-datatype-marker (token-position caret) "Expected contiguous '^^'."))
108 (let ((datatype (literal-iri-reference parser)))
109 (when (and (string= datatype (topic-maps.tmdm:integer-datatype))
110 (not (topic-maps.tmdm:integer-lexical-p (token-value token))))
111 (problem 'ctm-rejection :invalid-integer-value (token-position token)
112 "Quoted value is not an xsd:integer lexical form."))
113 (when (and (string= datatype (topic-maps.tmdm:decimal-datatype))
114 (not (topic-maps.tmdm:decimal-lexical-p (token-value token))))
115 (problem 'ctm-rejection :invalid-decimal-value (token-position token)
116 "Quoted value is not an xsd:decimal lexical form."))
117 (when (and (string= datatype (topic-maps.tmdm:date-datatype))
118 (not (topic-maps.tmdm:date-lexical-p (token-value token))))
119 (problem 'ctm-rejection :invalid-date-value (token-position token)
120 "Quoted value is not a valid XSD 1.0 date lexical form."))
121 ;; TMDM 4.4 requires absolute anyURI content; quoted content is
122 ;; lexical data, never an IRI reference to resolve against the document.
123 (when (and (string= datatype (topic-maps.tmdm:iri-datatype))
124 (not (topic-maps.tmdm::absolute-locator-p (token-value token))))
125 (problem 'ctm-rejection :relative-uri-value (token-position token)
126 "A quoted anyURI value must already be absolute (TMDM 4.4)."))
127 (values (token-value token) datatype)))
128 (values (token-value token) (topic-maps.tmdm:string-datatype))))
129 ((:iri :qname) (values (literal-iri-reference parser) (topic-maps.tmdm:iri-datatype)))
130 (otherwise (problem 'ctm-rejection :expected-occurrence-value (token-position token)
131 "Expected an integer, decimal, date, quoted string or IRI-form Occurrence value.")))))
133 (defun read-identity-tail (parser map topic)
134 (let ((start (look parser)))
135 (multiple-value-bind (kind iri) (identity-value parser)
136 (if (punctuation-p parser #\:)
139 (multiple-value-bind (value datatype) (read-occurrence-literal parser)
140 (let ((type (topic-maps.tmdm:intern-topic map kind iri)))
141 (topic-maps.tmdm:add-occurrence topic type value :datatype datatype)
142 (topic-maps.tmdm:canonical-topic topic))))
143 (if (eq :identifier (token-kind start))
144 (if (punctuation-p parser #\()
145 (problem 'ctm-unsupported :topic-tail (token-position start) "Template invocations are outside this subset.")
146 (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
147 "Expected ':' after the Occurrence Type."))
148 (topic-maps.tmdm:add-topic-identifier topic kind iri))))))
150 (defun read-topic (parser map &optional (topic (identity-reference parser map)))
151 (let ((has-tail nil))
154 ((punctuation-p parser #\.) (consume parser) (return topic))
155 ((punctuation-p parser #\-)
157 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
159 (let ((value (consume parser)))
160 (when (member (token-kind value) '(:identifier :iri :qname))
161 ;; Recognize the typed-name syntax, without resolving its type
162 ;; or creating information items for unsupported name semantics.
163 (unless (punctuation-p parser #\:)
164 (problem 'ctm-rejection :expected-name-type-separator
165 (token-position (look parser)) "Expected ':' after the name type."))
167 (let ((literal (consume parser)))
168 (unless (eq :string (token-kind literal))
169 (problem 'ctm-rejection :expected-name-value (token-position literal)
170 "Expected a quoted name value after the name type separator.")))
171 (problem 'ctm-unsupported :typed-name (token-position value)
172 "Typed names are outside the implemented subset (CTM 3.11)."))
173 (unless (eq :string (token-kind value))
174 (problem 'ctm-rejection :expected-name-value (token-position value)
175 "Expected a quoted name value after '-'."))
176 (topic-maps.tmdm:add-default-name topic (token-value value)))
178 ((and has-tail (punctuation-p parser #\;))
181 (when (punctuation-p parser #\.) (consume parser) (return topic)))
182 ((eq :eof (token-kind (look parser)))
183 (problem 'ctm-rejection :missing-period (token-position (look parser)) "Topic declaration requires a final period."))
184 ((or (and (eq :punctuation (token-kind (look parser)))
185 (find (token-value (look parser)) "=^"))
186 (member (token-kind (look parser)) '(:identifier :iri :qname)))
188 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
189 (setf topic (read-identity-tail parser map topic) has-tail t))
190 ((punctuation-p parser #\()
191 (problem 'ctm-unsupported :variant (token-position (look parser)) "Variant names are outside the implemented subset."))
192 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
194 (defun read-association (parser map type)
195 "CTM 3.13, restricted to one or two role declarations; no scope/reifier."
196 (consume parser) ; opening parenthesis already recognized by the caller
197 (let ((roles nil) (third-position nil))
199 (when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
200 (problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
201 (when (= 2 (length roles)) (setf third-position (token-position (look parser))))
202 (let* ((role-type (identity-reference parser map))
203 (type-end (lexer-cursor (parser-lexer parser)))
204 (colon (look parser)))
205 (unless (punctuation-p parser #\:)
206 (problem 'ctm-rejection :expected-role-type-separator (token-position colon) "Expected ':' after the Role type."))
208 (let ((player-token (look parser)))
209 ;; CTM role production requires layout on at least one side of ':'.
210 ;; skip-layout handles whitespace and comments; gaps use source offsets.
211 (unless (or (> (token-position colon) type-end)
212 (> (token-position player-token) (1+ (token-position colon))))
213 (problem 'ctm-rejection :role-separator-whitespace (token-position colon)
214 "A Role separator needs whitespace or a comment on at least one side."))
215 (when (or (eq :eof (token-kind player-token))
216 (punctuation-p parser #\)) (punctuation-p parser #\,))
217 (problem 'ctm-rejection :expected-role-player (token-position player-token) "Expected a Topic as the Role player.")))
218 (push (list role-type (identity-reference parser map)) roles))
219 (cond ((punctuation-p parser #\,) (consume parser))
220 ((punctuation-p parser #\)) (consume parser) (return))
221 ((eq :eof (token-kind (look parser)))
222 (problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
223 (t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
224 (when (> (length roles) 2)
225 (problem 'ctm-unsupported :association-arity third-position "Associations with more than two Role declarations are outside Slice 3."))
226 (topic-maps.tmdm:make-association map type (nreverse roles))))
228 (defun deserialize-string (text &key document-iri source-name)
229 "Read the documented CTM subset, with per-document prefixes and qualified strings.
230 Source positions are zero-based character offsets; lines/columns are one-based."
231 (check-type text string)
232 (let ((*source-name* source-name) (*source-text* text))
233 (require-document-iri document-iri)
234 (let ((parser (make-parser (make-lexer text) document-iri))
235 (map (topic-maps.tmdm:make-topic-map)))
236 (loop until (eq :eof (token-kind (look parser))) do
237 (if (eq :directive (token-kind (look parser)))
238 (read-prefix-directive parser)
239 (let ((topic (identity-reference parser map)))
240 (if (punctuation-p parser #\()
241 (read-association parser map topic)
242 (read-topic parser map topic)))))
245 (defun deserialize-file (pathname &key document-iri source-name)
246 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
247 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
248 (let ((*source-name* source-name) (*source-text* ""))
249 (require-document-iri document-iri)
250 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
251 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
252 (unless (= (length bytes) (read-sequence bytes input))
253 (error "Input file changed or was truncated during reading."))
254 (deserialize-string (decode-utf8 bytes :strip-bom t)
255 :document-iri document-iri :source-name source-name)))))