1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: parse-macros.lisp
6 ;;;; Purpose: Macros for UMLS file parsing
7 ;;;; Author: Kevin M. Rosenberg
12 ;;;; This file, part of UMLisp, is
13 ;;;; Copyright (c) 2000-2006 by Kevin M. Rosenberg, M.D.
15 ;;;; UMLisp users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the GNU General Public License.
17 ;;;; *************************************************************************
21 (defun read-umls-line (strm &optional (eof 'eof))
22 "Read a line from a UMLS stream, split into fields"
23 (let ((line (read-line strm nil eof)))
26 (delimited-string-to-list line #\| t))))
28 (defun source-files (path)
32 (directory (make-pathname :defaults path
34 :name (concatenate 'string (pathname-name path)
35 (aif (pathname-type path)
36 (concatenate 'string "." it)
39 (string-lessp (pathname-type a) (pathname-type b))))))
41 (defmacro with-buffered-reading-umls-file ((line path) &body body)
42 "Opens a UMLS and processes each parsed line with (body) argument"
43 (let ((ustream (gensym "STRM-"))
44 (buffer (gensym "BUF-"))
46 (files (gensym "FILES-")))
47 `(let ((,eof (gensym "EOFSYM-"))
48 (,buffer (make-fields-buffer))
49 (,files (source-files ,path)))
50 (with-open-file (,ustream (first ,files) :direction :input
51 #+(and clisp unicode) :external-format
52 #+(and clisp unicode) charset:utf-8)
53 (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof)
54 (read-buffered-fields ,buffer ,ustream #\| ,eof)))
56 (setq ,line (coerce ,line 'list))
60 (defmacro with-reading-umls-file ((line path) &body body)
61 "Opens a UMLS and processes each parsed line with (body) argument"
62 (let ((ustream (gensym "STRM-"))
64 (files (gensym "FILES-")))
65 `(let ((,eof (gensym "EOFSYM-"))
66 (,files (source-files ,path)))
68 (error "Can't find files for ~A~%" (namestring ,path)))
69 (with-open-file (,ustream (first ,files) :direction :input
70 #+(and clisp unicode) :external-format
71 #+(and clisp unicode) charset:utf-8)
72 (do ((,line (read-umls-line ,ustream ,eof)
73 (read-umls-line ,ustream ,eof)))
75 (locally (declare (type list ,line))
78 (defmacro with-umls-ufile ((line ufile) &body body)
79 "Opens a UMLS and processes each parsed line with (body) argument"
80 `(with-reading-umls-file (,line (ufile-pathname ,ufile))
83 (defmacro with-umls-file ((line ufile) &body body)
84 "Opens a UMLS and processes each parsed line with (body) argument"
85 `(with-reading-umls-file (,line (umls-pathname ,ufile))
88 (defmacro with-buffered-umls-file ((line filename) &body body)
89 "Opens a UMLS and processes each parsed line with (body) argument"
90 (let ((ustream (gensym "STRM-"))
91 (buffer (gensym "BUF-"))
92 (eof (gensym "EOF-")))
93 `(let ((,buffer (make-fields-buffer))
94 (,eof (gensym "EOFSYM-")))
96 (,ustream (umls-pathname ,filename) :direction :input)
97 (do ((,line (read-buffered-fields ,buffer ,ustream #\| ,eof)
98 (read-buffered-fields ,buffer ,ustream #\| ,eof)))