Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
;;;; ;
;;;; (c) 2001 by Jochen Schmidt.
;;;;
;;;; File: meta.lisp
;;;; Revision: 1.0.0
;;;; Description: A simple parsing technique
;;;; Date: 01.07.2001
;;;; Authors: Jochen Schmidt
;;;; Tel: (+49 9 11) 47 20 603
;;;; Email: jsc@dataheaven.de
;;;;
;;;; Redistribution and use in source and binary forms, with or without
;;;; modification, are permitted provided that the following conditions
;;;; are met:
;;;; 1. Redistributions of source code must retain the above copyright
;;;; notice, this list of conditions and the following disclaimer.
;;;; 2. Redistributions in binary form must reproduce the above copyright
;;;; notice, this list of conditions and the following disclaimer in the
;;;; documentation and/or other materials provided with the distribution.
;;;;
;;;; THIS SOFTWARE IS PROVIDED "AS IS" AND THERE ARE NEITHER
;;;; EXPRESSED NOR IMPLIED WARRANTIES - THIS INCLUDES, BUT
;;;; IS NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
;;;; AND FITNESS FOR A PARTICULAR PURPOSE.IN NO WAY ARE THE
;;;; AUTHORS LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;;; SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
;;;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
;;;; LOSS OF USE, DATA, OR PROFITS ; OR BUSINESS INTERRUPTION)
;;;;
;;;; For further details contact the authors of this software.
;;;;
;;;; Jochen Schmidt
;;;; Zuckmantelstr. 11
;;;; 91616 Neusitz
;;;; GERMANY
;;;;
;;;;
;;;; NOTE:
;;;; This code is based on the well known paper "Pragmatic Parsing in Common Lisp"
;;;; of Henry G. Baker. You can find it at:
;;;;
;;;; http://linux.rice.edu/~rahul/hbaker/Prag-Parse.html
;;;;
;;;; The parsing technique Baker describes in his paper goes back to:
;;;;
;;;; Schorre, D.V. "META II: A Syntax-Oriented Compiler Writing Language".
;;;; Proc. 19'th Nat'l. Conf. of the ACM (Aug. 1964),D1.3-1-D1.3-11.
;;;;
;;;;
;;;; Nrnberg, 01.Jul.2001 Jochen Schmidt
(in-package :meta)
;;; String matching
(defmacro string-match (x &key source-symbol)
(etypecase x
(character
`(when (and (< index end) (eql (char ,source-symbol index) ,x))
(incf index)))
(string
(let ((old-index-symbol (gensym "OLD-INDEX-")))
`(let ((,old-index-symbol index))
(or (and ,@(map 'list #'(lambda (c) `(string-match ,c
:source-symbol ',source-symbol)) x))
(progn (setq index ,old-index-symbol) nil)))))))
(defmacro string-match-type (x v &key source-symbol)
(let ((char-sym (gensym)))
`(when (< index end)
(let ((,char-sym (char ,source-symbol index)))
(declare (base-char ,char-sym))
(when (typep ,char-sym ',x)
(setq ,v ,char-sym) (incf index))))))
;;; List matching
(defmacro list-match (x &key source-symbol); sublist uses new lexical index
`(when (and (consp ,source-symbol)
,(if (atom x) `(eql (car ,source-symbol) ',x)
`(let ((,source-symbol (car ,source-symbol))) ,(compile-list x :source-symbol source-symbol))))
(pop ,source-symbol) t))
(defmacro list-match-type (x v &key source-symbol)
`(when (and (consp ,source-symbol) (typep (car ,source-symbol) ',x))
(setq ,v (car ,source-symbol)) (pop ,source-symbol) t))
(defun compile-list (l &key source-symbol)
(if (atom l) `(eql ,source-symbol ',l)
`(and ,(compileit (car l) :meta-parser-type :list :source-symbol source-symbol)
,(compile-list (cdr l) :source-symbol source-symbol))))
;;; Stream matching
(defmacro stream-match (x &key source-symbol)
`(when (eql (peek-char nil ,source-symbol) ',x) (read-char ,source-symbol)))
(defmacro stream-match-type (x v &key source-symbol)
`(when (typep (peek-char nil ,source-symbol) ',x) (setq ,v (read-char ,source-symbol))))
(defstruct (meta
(:print-function
(lambda (m s d &aux (char (meta-char m)) (form (meta-form m)))
(declare (ignore d))
(ecase char
((#\@ #\! #\$) (format s "~A~A" char form))
(#\[ (format s "[~{~A~^ ~}]" form))
(#\{ (format s "{~{~A~^ ~}}" form))))))
char
form)
(defun compileit (x &key meta-parser-type source-symbol)
(typecase x
(meta
(ecase (meta-char x)
(#\! (meta-form x))
(#\[ `(and ,@(mapcar #'(lambda (f) (compileit f
:meta-parser-type meta-parser-type
:source-symbol source-symbol))
(meta-form x))))
(#\{ `(or ,@(mapcar #'(lambda (f) (compileit f
:meta-parser-type meta-parser-type
:source-symbol source-symbol))
(meta-form x))))
(#\$ `(not (do () ((not ,(compileit (meta-form x)
:meta-parser-type meta-parser-type
:source-symbol source-symbol))))))
(#\@ (let ((f (meta-form x))) (list (ecase meta-parser-type
(:list 'list-match-type)
(:string 'string-match-type)
(:stream 'stream-match-type))
(car f) (cadr f)
:source-symbol source-symbol
)))))
(t (list (ecase meta-parser-type
(:list 'list-match)
(:string 'string-match)
(:stream 'stream-match))
x
:source-symbol source-symbol
))))
(eval-when (compile load eval)
(defparameter *saved-readtable* (copy-readtable))
(defparameter *meta-readtable* (copy-readtable))
(defun meta-reader (s c) (make-meta :char c :form (read s)))
(mapc #'(lambda (c) (set-macro-character c #'meta-reader nil *meta-readtable*)) '(#\@ #\$ #\!))
(set-macro-character #\{
#'(lambda (s c) (make-meta :char c :form (read-delimited-list #\} s t))) nil *meta-readtable*)
(set-macro-character #\[
#'(lambda (s c) (make-meta :char c :form (read-delimited-list #\] s t))) nil *meta-readtable*)
(mapc #'(lambda (c) (set-macro-character c (get-macro-character #\)) nil *meta-readtable*))
'(#\] #\}))
)
(defmacro with-stream-meta ((source-symbol stream) &body body)
`(let ((,source-symbol ,stream))
(macrolet ((match (x)
(compileit x
:meta-parser-type :stream
:source-symbol ',source-symbol)))
,@body)))
(defmacro with-string-meta ((source-symbol string-buffer &key (start 0) end) &body body)
`(let* ((,source-symbol ,string-buffer)
(index ,start)
(end ,(or end `(length ,source-symbol))))
(declare (fixnum index end)
(type simple-base-string ,source-symbol))
(macrolet ((match (x)
(compileit x
:meta-parser-type :string
:source-symbol ',source-symbol)))
,@body)))
(defmacro with-list-meta ((source-symbol list) &body body)
`(let ((,source-symbol ,list))
(macrolet ((match (x)
(compileit x
:meta-parser-type :list
:source-symbol ',source-symbol)))
,@body)))
(defun enable-meta-syntax ()
(copy-readtable *meta-readtable* *readtable*))
(defun disable-meta-syntax()
(copy-readtable *saved-readtable* *readtable*))
(provide 'meta)
#|
(eval-when (compile load eval)
(deftype digit () '(member #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
(deftype non-digit () '(not (member #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)))
(defun ctoi (d) (- (char-code d) #.(char-code #\0)))
)
(eval-when (compile load eval)
(enable-meta-syntax)
)
(defun parse-int (string &aux (s +1) d (n 0))
(with-string-meta (buffer string)
(and
(match
[{#\+ [#\- !(setq s -1)] []}
@(digit d) !(setq n (ctoi d))
$[@(digit d) !(setq n (+ (* n 10) (ctoi d)))]])
(* s n))))
(eval-when (compile load eval)
(disable-meta-syntax)
)
|#