1 (in-package #:topic-maps.ctm)
3 (defstruct (parser (:constructor make-parser (lexer document-iri))) lexer document-iri lookahead)
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)))))
13 (defun identity-value (parser &optional explicit-kind)
14 (let ((token (consume parser)))
15 (case (token-kind token)
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 Slice 1."))
21 (values :item-identifier
22 (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
24 (values (or explicit-kind :subject-identifier)
25 (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
27 ;; No prefix declarations are supported in Slice 1. 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))))))
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.")))))
43 (defun identity-reference (parser map)
44 (multiple-value-bind (kind iri) (identity-value parser)
45 (topic-maps.tmdm:intern-topic map kind iri)))
47 (defun read-topic (parser map)
48 (let ((topic (identity-reference parser map)) (has-tail nil))
51 ((punctuation-p parser #\.) (consume parser) (return topic))
52 ((punctuation-p parser #\-)
54 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
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."))
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 Slice 1 (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)))
75 ((and has-tail (punctuation-p parser #\;))
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)))
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)))
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 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
95 (defun deserialize-string (text &key document-iri source-name)
96 "Read the documented identity/name CTM subset into a new TMDM Topic Map.
97 Source positions are zero-based character offsets; lines/columns are one-based."
98 (check-type text string)
99 (let ((*source-name* source-name) (*source-text* text))
100 (require-document-iri document-iri)
101 (let ((parser (make-parser (make-lexer text) document-iri))
102 (map (topic-maps.tmdm:make-topic-map)))
103 (loop until (eq :eof (token-kind (look parser))) do (read-topic parser map))
106 (defun deserialize-file (pathname &key document-iri source-name)
107 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
108 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
109 (let ((*source-name* source-name) (*source-text* ""))
110 (require-document-iri document-iri)
111 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
112 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
113 (unless (= (length bytes) (read-sequence bytes input))
114 (error "Input file changed or was truncated during reading."))
115 (deserialize-string (decode-utf8 bytes :strip-bom t)
116 :document-iri document-iri :source-name source-name)))))