-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.lisp
executable file
·167 lines (157 loc) · 4.9 KB
/
tokenizer.lisp
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
;#!/usr/local/bin/sbcl --noinform
;#!/sw/bin/clisp
;#!/usr/local/bin/clisp
;;;; Hey, Emacs, this is a -*- Mode: Lisp; Syntax: Common-Lisp -*- file!
;;;;
;;;; Programming should be fun. Programs should be beautiful.
;;;; -- Paul Graham
;;;;
;;;; Name: tokenizer.lisp
;;;;
;;;; Started: Mon Dec 26 15:36:19 2005
;;;; Modifications:
;;;;
;;;; Purpose:
;;;;
;;;;
;;;;
;;;; Calling Sequence:
;;;;
;;;;
;;;; Inputs:
;;;;
;;;; Outputs:
;;;;
;;;; Example:
;;;;
;;;; Notes:
;;;;
;;;;
(defpackage tokenizer
(:use common-lisp)
(:export "TOKENIZE" "GET-TOKENS" "NEXT-TOKEN"))
(in-package tokenizer)
(defconstant no-match 'no-match)
;(defconstant no-match (list 'no-match))
(let (index length input-string state start lexeme-start
(eoe (code-char 0)))
; (eoe (code-char 255))
; (eoe (list 'eoe))
; (no-match (list 'no-match)))
(defun tokenize (s)
(setf index 0
length (length s)
input-string s
state 0
start 0
lexeme-start 0))
(defun get-tokens ()
(loop for token = (next-token)
until (eq token eoe)
collect token))
(defun fail ()
(ecase start
(0 (setf start 1))
(1 (setf start 9))
(9 (setf start 12))
(12 (setf start 20))
(20 (setf start 25))
(25 (setf start no-match)))
(setf index lexeme-start)
start)
(defun next-token ()
(setf lexeme-start index
start 0
state 0)
(let ((ch eoe))
(loop
; (ccase state
(case state
(0 (setf ch (get-next-char))
(if (space-char-p ch)
(incf lexeme-start)
(setf state (fail))))
(1 (setf ch (get-next-char))
(if (delimiterp ch)
(return (intern (subseq input-string lexeme-start index)))
(setf state (fail))))
(9 (setf ch (get-next-char))
(if (alpha-char-p ch)
(setf state 10)
(setf state (fail))))
(10 (setf ch (get-next-char))
(if (alphanumericp ch)
(setf state 10)
(setf state 11)))
;; (11 (unless (eq ch eoe)
;; (retract 1))
;; (return (subseq input-string lexeme-start index)))
(12 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 13)
(setf state (fail))))
(13 (setf ch (get-next-char))
(cond ((digit-char-p ch) (setf state 13))
((char= ch #\.) (setf state 14))
((char-equal ch #\e) (setf state 16))
(t (setf state (fail)))) )
(14 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 15)
(setf state (fail))))
(15 (setf ch (get-next-char))
(cond ((digit-char-p ch) (setf state 15))
((char-equal ch #\e) (setf state 16))
(t (setf state (fail)))) )
(16 (setf ch (get-next-char))
(cond ((digit-char-p ch) (setf state 18))
((or (char= ch #\+) (char= ch #\-)) (setf state 17))
(t (setf state (fail)))) )
(17 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 18)
(setf state (fail))))
(18 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 18)
(setf state 19)))
(20 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 21)
(setf state (fail))))
(21 (setf ch (get-next-char))
(cond ((digit-char-p ch) (setf state 21))
((char= ch #\.) (setf state 22))
(t (setf state (fail)))) )
(22 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 23)
(setf state (fail))))
(23 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 23)
(setf state 24)))
(25 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 26)
(setf state (fail))))
(26 (setf ch (get-next-char))
(if (digit-char-p ch)
(setf state 26)
(setf state 27)))
((11 19 24 27) (unless (eq ch eoe)
(retract 1))
(return (read-from-string (subseq input-string lexeme-start index))))
(otherwise (return eoe))))))
; (#.no-match eoe)))) )
(defun get-next-char ()
(if (< index length)
(prog1 (char input-string index) (incf index))
eoe))
(defun retract (i)
(when (<= index length)
(decf index i))))
(defun delimiterp (ch)
(find ch "+-*/%^=()"))
(defun space-char-p (ch)
(find ch '(#\Space #\Page #\Newline #\Return #\Tab #\Vt)))