]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/reader.lisp
feat(ctm): read associations and roles with merge-safe topic references
[topic-maps.git] / src / ctm / reader.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defstruct (parser (:constructor make-parser (lexer document-iri))) lexer document-iri lookahead)
4 (defun look (parser)
5 (or (parser-lookahead parser)
6 (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
7 (defun consume (parser)
8 (prog1 (look parser) (setf (parser-lookahead parser) nil)))
9 (defun punctuation-p (parser character)
10 (and (eq :punctuation (token-kind (look parser)))
11 (eql character (token-value (look parser)))))
12
13 (defun identity-value (parser &optional explicit-kind)
14 (let ((token (consume parser)))
15 (case (token-kind token)
16 (:identifier
17 (when explicit-kind
18 (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
19 (when (member (token-value token) '("def" "isa" "ako" "end") :test #'string=)
20 (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
21 (values :item-identifier
22 (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
23 (:iri
24 (values (or explicit-kind :subject-identifier)
25 (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
26 (:qname
27 ;; No prefix declarations are supported. This is a specific
28 ;; semantic rejection, not an I/O failure or an unsupported-parser stub.
29 (problem 'undefined-prefix :undefined-prefix (token-position token)
30 (format nil "Prefix ~S has no binding." (subseq (token-value token) 0
31 (position #\: (token-value token))))))
32 (:punctuation
33 (case (token-value token)
34 (#\= (if explicit-kind
35 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
36 (identity-value parser :subject-locator)))
37 (#\^ (if explicit-kind
38 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
39 (identity-value parser :item-identifier)))
40 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity."))))
41 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
42
43 (defun identity-reference (parser map)
44 (multiple-value-bind (kind iri) (identity-value parser)
45 (topic-maps.tmdm:intern-topic map kind iri)))
46
47 (defun read-topic (parser map &optional (topic (identity-reference parser map)))
48 (let ((has-tail nil))
49 (loop
50 (cond
51 ((punctuation-p parser #\.) (consume parser) (return topic))
52 ((punctuation-p parser #\-)
53 (when has-tail
54 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
55 (consume parser)
56 (let ((value (consume parser)))
57 (when (member (token-kind value) '(:identifier :iri :qname))
58 ;; Recognize the typed-name syntax, without resolving its type
59 ;; or creating information items for unsupported name semantics.
60 (unless (punctuation-p parser #\:)
61 (problem 'ctm-rejection :expected-name-type-separator
62 (token-position (look parser)) "Expected ':' after the name type."))
63 (consume parser)
64 (let ((literal (consume parser)))
65 (unless (eq :string (token-kind literal))
66 (problem 'ctm-rejection :expected-name-value (token-position literal)
67 "Expected a quoted name value after the name type separator.")))
68 (problem 'ctm-unsupported :typed-name (token-position value)
69 "Typed names are outside the implemented subset (CTM 3.11)."))
70 (unless (eq :string (token-kind value))
71 (problem 'ctm-rejection :expected-name-value (token-position value)
72 "Expected a quoted name value after '-'."))
73 (topic-maps.tmdm:add-default-name topic (token-value value)))
74 (setf has-tail t))
75 ((and has-tail (punctuation-p parser #\;))
76 (consume parser)
77 (setf has-tail nil)
78 (when (punctuation-p parser #\.) (consume parser) (return topic)))
79 ((eq :eof (token-kind (look parser)))
80 (problem 'ctm-rejection :missing-period (token-position (look parser)) "Topic declaration requires a final period."))
81 ((or (and (eq :punctuation (token-kind (look parser)))
82 (find (token-value (look parser)) "=^"))
83 (member (token-kind (look parser)) '(:iri :qname)))
84 (when has-tail
85 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
86 ;; CTM 3.9 adds the locator to this Topic, without creating another
87 ;; Topic just for the assignment. The model performs required merging.
88 (multiple-value-bind (kind iri) (identity-value parser)
89 (setf topic (topic-maps.tmdm:add-topic-identifier topic kind iri)))
90 (setf has-tail t))
91 ((eq :identifier (token-kind (look parser)))
92 (problem 'ctm-unsupported :topic-tail (token-position (look parser)) "Type/template topic tails are outside the implemented subset."))
93 ((punctuation-p parser #\()
94 (problem 'ctm-unsupported :variant (token-position (look parser)) "Variant names are outside the implemented subset."))
95 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
96
97 (defun read-association (parser map type)
98 "CTM 3.13, restricted to one or two role declarations; no scope/reifier."
99 (consume parser) ; opening parenthesis already recognized by the caller
100 (let ((roles nil) (third-position nil))
101 (loop
102 (when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
103 (problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
104 (when (= 2 (length roles)) (setf third-position (token-position (look parser))))
105 (let* ((role-type (identity-reference parser map))
106 (type-end (lexer-cursor (parser-lexer parser)))
107 (colon (look parser)))
108 (unless (punctuation-p parser #\:)
109 (problem 'ctm-rejection :expected-role-type-separator (token-position colon) "Expected ':' after the Role type."))
110 (consume parser)
111 (let ((player-token (look parser)))
112 ;; CTM role production requires layout on at least one side of ':'.
113 ;; skip-layout handles whitespace and comments; gaps use source offsets.
114 (unless (or (> (token-position colon) type-end)
115 (> (token-position player-token) (1+ (token-position colon))))
116 (problem 'ctm-rejection :role-separator-whitespace (token-position colon)
117 "A Role separator needs whitespace or a comment on at least one side."))
118 (when (or (eq :eof (token-kind player-token))
119 (punctuation-p parser #\)) (punctuation-p parser #\,))
120 (problem 'ctm-rejection :expected-role-player (token-position player-token) "Expected a Topic as the Role player.")))
121 (push (list role-type (identity-reference parser map)) roles))
122 (cond ((punctuation-p parser #\,) (consume parser))
123 ((punctuation-p parser #\)) (consume parser) (return))
124 ((eq :eof (token-kind (look parser)))
125 (problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
126 (t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
127 (when (> (length roles) 2)
128 (problem 'ctm-unsupported :association-arity third-position "Associations with more than two Role declarations are outside Slice 3."))
129 (topic-maps.tmdm:make-association map type (nreverse roles))))
130
131 (defun deserialize-string (text &key document-iri source-name)
132 "Read the documented identity/name/unary-binary Association CTM subset.
133 Source positions are zero-based character offsets; lines/columns are one-based."
134 (check-type text string)
135 (let ((*source-name* source-name) (*source-text* text))
136 (require-document-iri document-iri)
137 (let ((parser (make-parser (make-lexer text) document-iri))
138 (map (topic-maps.tmdm:make-topic-map)))
139 (loop until (eq :eof (token-kind (look parser))) do
140 (let ((topic (identity-reference parser map)))
141 (if (punctuation-p parser #\()
142 (read-association parser map topic)
143 (read-topic parser map topic))))
144 map)))
145
146 (defun deserialize-file (pathname &key document-iri source-name)
147 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
148 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
149 (let ((*source-name* source-name) (*source-text* ""))
150 (require-document-iri document-iri)
151 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
152 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
153 (unless (= (length bytes) (read-sequence bytes input))
154 (error "Input file changed or was truncated during reading."))
155 (deserialize-string (decode-utf8 bytes :strip-bom t)
156 :document-iri document-iri :source-name source-name)))))