Commit | Line | Data |
---|---|---|
dea4d055 MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Basic scanner interface | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
10 | ;;; This file is part of the Sensble Object Design, an object system for C. | |
11 | ;;; | |
12 | ;;; SOD is free software; you can redistribute it and/or modify | |
13 | ;;; it under the terms of the GNU General Public License as published by | |
14 | ;;; the Free Software Foundation; either version 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
16 | ;;; | |
17 | ;;; SOD is distributed in the hope that it will be useful, | |
18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;;; GNU General Public License for more details. | |
21 | ;;; | |
22 | ;;; You should have received a copy of the GNU General Public License | |
23 | ;;; along with SOD; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | (cl:in-package #:sod-parser) | |
27 | ||
28 | ;;;-------------------------------------------------------------------------- | |
29 | ;;; Common scanner implementation.. | |
30 | ||
31 | (defmethod file-location ((scanner character-scanner)) | |
32 | (scanner-file-location scanner)) | |
33 | ||
34 | ;;;-------------------------------------------------------------------------- | |
35 | ;;; Streams on character scanners. | |
36 | ||
37 | (defmethod stream-read-char ((stream character-scanner-stream)) | |
38 | (with-slots (scanner) stream | |
39 | (if (scanner-at-eof-p scanner) | |
40 | :eof | |
41 | (prog1 (scanner-current-char scanner) | |
42 | (scanner-step scanner))))) | |
43 | ||
44 | (defmethod stream-unread-char ((stream character-scanner-stream) char) | |
45 | (with-slots (scanner) stream | |
46 | (scanner-unread scanner char))) | |
47 | ||
48 | (defmethod stream-peek-char ((stream character-scanner-stream)) | |
49 | (with-slots (scanner) stream | |
50 | (scanner-current-char scanner))) | |
51 | ||
52 | ;;;-------------------------------------------------------------------------- | |
53 | ;;; String scanner. | |
54 | ||
55 | ;; This is much more convenient for testing lexers than the full character | |
56 | ;; buffer scanner. | |
57 | ||
58 | (export '(string-scanner make-string-scanner string-scanner-p)) | |
59 | (defstruct (string-scanner | |
60 | (:constructor make-string-scanner | |
61 | (string &key (start 0) end | |
62 | &aux (index start) | |
63 | (limit (or end (length string)))))) | |
64 | "Scanner structure for a simple string scanner." | |
65 | (string "" :type string :read-only t) | |
66 | (index 0 :type (and fixnum unsigned-byte)) | |
67 | (limit nil :type (and fixnum unsigned-byte) :read-only t)) | |
68 | ||
69 | (defmethod scanner-at-eof-p ((scanner string-scanner)) | |
70 | (>= (string-scanner-index scanner) (string-scanner-limit scanner))) | |
71 | ||
72 | (defmethod scanner-current-char ((scanner string-scanner)) | |
73 | (char (string-scanner-string scanner) (string-scanner-index scanner))) | |
74 | ||
75 | (defmethod scanner-step ((scanner string-scanner)) | |
76 | (incf (string-scanner-index scanner))) | |
77 | ||
78 | (defmethod scanner-capture-place ((scanner string-scanner)) | |
79 | (string-scanner-index scanner)) | |
80 | ||
81 | (defmethod scanner-restore-place ((scanner string-scanner) place) | |
82 | (setf (string-scanner-index scanner) place)) | |
83 | ||
84 | (defmethod scanner-interval | |
85 | ((scanner string-scanner) place-a &optional place-b) | |
86 | (with-slots (string index) scanner | |
87 | (subseq string place-a (or place-b index)))) | |
88 | ||
89 | ;;;-------------------------------------------------------------------------- | |
90 | ;;; List scanner. | |
91 | ||
92 | (export 'list-scanner) | |
93 | (defstruct (list-scanner | |
94 | (:constructor make-list-scanner (list))) | |
95 | "Simple token scanner for lists. | |
96 | ||
97 | The list elements are the token semantic values; the token types are the | |
98 | names of the elements' classes. This is just about adequate for testing | |
99 | purposes, but is far from ideal for real use." | |
100 | (list nil :type list)) | |
101 | ||
102 | (defmethod scanner-step ((scanner list-scanner)) | |
103 | (pop (list-scanner-list scanner))) | |
104 | ||
105 | (defmethod scanner-at-eof-p ((scanner list-scanner)) | |
106 | (null (list-scanner-list scanner))) | |
107 | ||
108 | (defmethod token-type ((scanner list-scanner)) | |
109 | (class-name (class-of (car (list-scanner-list scanner))))) | |
110 | ||
111 | (defmethod token-value ((scanner list-scanner)) | |
112 | (car (list-scanner-list scanner))) | |
113 | ||
114 | (defmethod scanner-capture-place ((scanner list-scanner)) | |
115 | (list-scanner-list scanner)) | |
116 | ||
117 | (defmethod scanner-restore-place ((scanner list-scanner) place) | |
118 | (setf (list-scanner-list scanner) place)) | |
119 | ||
120 | ;;;----- That's all, folks -------------------------------------------------- |