(in-package #:topic-maps.ctm)
+(defstruct template-definition name parameters body position)
+;; Validated concrete syntax, never TMDM information items or Lisp forms.
+(defstruct ctm-statement kind tokens position)
+(defstruct template-argument kind token topic value datatype plain-string-p)
(defstruct (parser (:constructor make-parser (lexer document-iri wildcard-iri wildcard-counter)))
lexer document-iri wildcard-iri wildcard-counter lookahead
+ replay-p remaining recording recorded bindings
+ (templates (make-hash-table :test #'equal))
(wildcards (make-hash-table :test #'equal)) (prefixes (make-hash-table :test #'equal)))
(defun look (parser)
(or (parser-lookahead parser)
- (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
+ (setf (parser-lookahead parser)
+ (if (parser-replay-p parser)
+ (let ((token (or (pop (parser-remaining parser))
+ (make-token :kind :eof :position (lexer-cursor (parser-lexer parser))))))
+ (setf (lexer-cursor (parser-lexer parser)) (or (token-end token) (token-position token))) token)
+ (let ((token (next-token (parser-lexer parser))))
+ (setf (token-end token) (lexer-cursor (parser-lexer parser))) token)))))
(defun consume (parser)
- (prog1 (look parser) (setf (parser-lookahead parser) nil)))
+ (let ((token (look parser)))
+ (when (parser-recording parser) (push token (parser-recorded parser)))
+ (setf (parser-lookahead parser) nil) token))
(defun punctuation-p (parser character)
(and (eq :punctuation (token-kind (look parser)))
(eql character (token-value (look parser)))))
(when name (setf (gethash name (parser-wildcards parser)) iri)) iri)))
(defun topic-reference-start-p (token)
- (or (member (token-kind token) '(:identifier :iri :qname :wildcard))
+ (or (member (token-kind token) '(:identifier :iri :qname :wildcard :variable))
(and (eq :punctuation (token-kind token)) (find (token-value token) "=^["))))
(defun resolve-qname (parser token)
(defun identity-value (parser &optional explicit-kind)
(let ((token (consume parser)))
(case (token-kind token)
+ (:variable (bound-identity parser token explicit-kind))
(:identifier
(when explicit-kind
(problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
- (when (member (token-value token) '("def" "isa" "ako" "end") :test #'string=)
- (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
+ (when (member (token-value token) '("isa" "ako") :test #'string=)
+ (problem 'ctm-rejection :reserved-topic-identifier (token-position token) "A relationship keyword is not a Topic identity."))
+ (when (member (token-value token) '("def" "end") :test #'string=)
+ (problem 'ctm-rejection :unexpected-template-keyword (token-position token) "Unexpected template delimiter."))
(values :item-identifier
(concatenate 'string (parser-document-iri parser) "#" (token-value token))))
(:iri
(otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
(defun identity-reference (parser map)
- (multiple-value-bind (kind iri) (identity-value parser)
- (topic-maps.tmdm:intern-topic map kind iri)))
+ (let ((position (token-position (look parser))))
+ (multiple-value-bind (kind iri) (identity-value parser)
+ (handler-case (if (eq kind :bound-topic) (topic-maps.tmdm:canonical-topic iri)
+ (topic-maps.tmdm:intern-topic map kind iri))
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))))))
(defun topic-reference (parser map)
"CTM 3.3.7: one recursive reference path for Types, Players and Scope themes."
"Shared CTM 3.4.1 literal reader for Occurrences and Variants."
(let ((token (look parser)))
(case (token-kind token)
+ (:variable (consume parser) (bound-literal parser token))
(:integer
(consume parser)
;; Numeric-literal value policy: represent the unquoted integer value
(topic-maps.tmdm:reification-error (c)
(problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c) position (princ-to-string c)))
(topic-maps.tmdm:variant-error (c)
- (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c)))))
+ (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c)))
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))))
item)
(defun read-identity-tail (parser map topic)
(progn
(consume parser)
(multiple-value-bind (value datatype) (read-literal parser)
- (let* ((type (or type (topic-maps.tmdm:intern-topic map kind iri)))
+ (let* ((type (or type (if (eq kind :bound-topic) (topic-maps.tmdm:canonical-topic iri)
+ (topic-maps.tmdm:intern-topic map kind iri))))
(scope (read-scope parser map)))
(multiple-value-bind (r position) (read-reifier parser map)
(attach-reifier (topic-maps.tmdm:add-occurrence topic type value :datatype datatype :scope scope) r position))
(topic-maps.tmdm:canonical-topic topic))))
- (if (or embedded (member (token-kind start) '(:identifier :wildcard)))
- (if (and (eq :identifier (token-kind start)) (punctuation-p parser #\())
- (problem 'ctm-unsupported :topic-tail (token-position start) "Template invocations are outside this subset.")
- (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
- "Expected ':' after the Occurrence Type."))
- (topic-maps.tmdm:add-topic-identifier topic kind iri))))))
+ (if (or embedded (member (token-kind start) '(:identifier :wildcard :variable)))
+ (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
+ "Expected ':' after the Occurrence Type.")
+ (handler-case (topic-maps.tmdm:add-topic-identifier topic kind iri)
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
+ (token-position start) (princ-to-string c)))))))))
(defun read-variant (parser map name)
(consume parser)
"CTM 3.11: optional Topic reference plus ':' followed by a string.
Use the shared Topic reference resolver."
(let* ((token (look parser))
- (typed (topic-reference-start-p token))
+ (typed (and (topic-reference-start-p token)
+ (or (not (eq :variable (token-kind token))) (next-punctuation-p parser #\:))))
(type (when typed (topic-reference parser map))))
(when typed
(unless (punctuation-p parser #\:)
(problem 'ctm-rejection :expected-name-type-separator
(token-position (look parser)) "Expected ':' after the name type."))
(consume parser))
- (let ((value (consume parser)))
- (unless (eq :string (token-kind value))
+ (let* ((value (consume parser))
+ (name-value (if (eq :variable (token-kind value)) (bound-name parser value) (token-value value))))
+ (unless (member (token-kind value) '(:string :variable))
(problem 'ctm-rejection :expected-name-value (token-position value)
"Expected a quoted name value after '-' or the name type separator."))
(let ((scope (read-scope parser map)))
(multiple-value-bind (r position) (read-reifier parser map)
(let ((name (attach-reifier
- (if typed (topic-maps.tmdm:add-name topic (token-value value) :type type :scope scope)
- (topic-maps.tmdm:add-name topic (token-value value) :scope scope)) r position)))
+ (if typed (topic-maps.tmdm:add-name topic name-value :type type :scope scope)
+ (topic-maps.tmdm:add-name topic name-value :scope scope)) r position)))
(loop while (punctuation-p parser #\() do (read-variant parser map name))))))
(topic-maps.tmdm:canonical-topic topic)))
+(defun relationship-keyword-p (token)
+ (and (eq :identifier (token-kind token)) (member (token-value token) '("isa" "ako") :test #'string=)))
+
+(defun read-type-relationship (parser map topic)
+ "CTM 3.9: expand one isa/ako statement using ordinary same-map Associations."
+ (let* ((keyword (consume parser)) (isa (string= (token-value keyword) "isa")) (next (look parser)))
+ (unless (and (topic-reference-start-p next)
+ (not (and (eq :identifier (token-kind next))
+ (member (token-value next) '("isa" "ako" "def" "end") :test #'string=))))
+ (problem 'ctm-rejection :expected-relationship-topic (token-position next)
+ "Expected one Topic reference after isa or ako."))
+ (let ((target (topic-reference parser map)))
+ (when (punctuation-p parser #\,)
+ (problem 'ctm-rejection :unexpected-relationship-list (token-position (look parser))
+ "isa/ako takes one Topic reference; repeat the keyword in a separate tail element."))
+ (handler-case
+ (flet ((core (local)
+ ;; Resolve locators every time; do not cache replaceable Topic objects.
+ (topic-maps.tmdm:intern-topic map :subject-identifier
+ (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))))
+ (let* ((type (core (if isa "type-instance" "supertype-subtype")))
+ (left (core (if isa "instance" "subtype"))) (right (core (if isa "type" "supertype"))))
+ (topic-maps.tmdm:make-association map type (list (list left topic) (list right target)))
+ (topic-maps.tmdm:canonical-topic topic)))
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
+ (token-position keyword) (princ-to-string c)))))))
+
(defun read-topic-tail (parser map topic terminator &key required)
"CTM 3.9: the same Tail reader ends at '.' or the caller's Embedded ']'."
(let ((has-tail nil) (seen-tail nil))
((eq :eof (token-kind (look parser)))
(problem 'ctm-rejection (if (char= terminator #\]) :missing-embedded-close :missing-period)
(token-position (look parser)) "Expected the closing Topic/Embedded Topic delimiter."))
+ ((relationship-keyword-p (look parser))
+ (when has-tail
+ (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
+ (setf topic (read-type-relationship parser map topic) has-tail t seen-tail t))
+ ((tail-invocation-p parser)
+ (when has-tail
+ (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
+ (read-template-invocation parser map topic)
+ (setf topic (topic-maps.tmdm:canonical-topic topic) has-tail t seen-tail t))
((topic-reference-start-p (look parser))
(when has-tail
(problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
(t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
(defun read-association (parser map type)
- "CTM 3.13, restricted to one or two role declarations; optional Scope and Reifiers."
+ "CTM 3.13: one or more Role declarations; optional Scope and Reifiers."
(consume parser) ; opening parenthesis already recognized by the caller
- (let ((roles nil) (role-reifiers nil) (third-position nil))
+ (let ((roles nil) (role-reifiers nil))
(loop
(when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
(problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
- (when (= 2 (length roles)) (setf third-position (token-position (look parser))))
(let* ((role-type (topic-reference parser map))
(type-end (lexer-cursor (parser-lexer parser)))
(colon (look parser)))
((eq :eof (token-kind (look parser)))
(problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
(t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
- (when (> (length roles) 2)
- (problem 'ctm-unsupported :association-arity third-position "Associations with more than two Role declarations are outside Slice 3."))
(let ((scope (read-scope parser map)))
(multiple-value-bind (r position) (read-reifier parser map)
(let ((association (topic-maps.tmdm:make-association map type (nreverse roles) :scope scope)))
(lexer-cursor (parser-lexer parser)) (princ-to-string c))))
(topic-maps.tmdm:reification-error
(lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c)
+ (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
+ (topic-maps.tmdm:core-subject-error
+ (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
(lexer-cursor (parser-lexer parser)) (princ-to-string c)))))
(loop while (eq :directive (token-kind (look parser))) do (read-prefix-directive parser))
(multiple-value-bind (r position) (read-reifier parser map) (attach-reifier map r position))
(when (punctuation-p parser #\~)
(problem 'ctm-rejection :topic-map-reifier-position (token-position (look parser))
"Topic Map Reifier must precede all body declarations."))
- (if (eq :directive (token-kind (look parser)))
- (read-prefix-directive parser)
- (let* ((embedded (punctuation-p parser #\[)) (topic (topic-reference parser map)))
- (if (punctuation-p parser #\()
- (read-association parser map topic)
- (if embedded
- (problem 'ctm-rejection :embedded-topic-at-top-level (token-position (look parser))
- "An Embedded Topic at top level must be an Association Type, not a standalone declaration.")
- (read-topic-tail parser map topic #\.)))))))
+ (cond ((eq :directive (token-kind (look parser))) (read-prefix-directive parser))
+ ((identifier-p (look parser) "def") (read-template-definition parser))
+ (t (read-statement parser map)))))
(values map (parser-wildcard-counter parser)))))
(defun deserialize-file (pathname &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0))