]> gitweb.factorcode.org Git - factor.git/blob - core/lexer/lexer.factor
classes: use check-instance in a few places, to remove duplication.
[factor.git] / core / lexer / lexer.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes combinators continuations io
4 kernel kernel.private math math.parser namespaces sequences
5 sequences.private source-files.errors strings vectors ;
6 IN: lexer
7
8 TUPLE: lexer
9     { text array }
10     { line fixnum }
11     { line-text string }
12     { line-length fixnum }
13     { column fixnum }
14     { parsing-words vector } ;
15
16 TUPLE: lexer-parsing-word word line line-text column ;
17
18 : next-line ( lexer -- )
19     lexer check-instance
20     dup [ line>> ] [ text>> ] bi ?nth "" or
21     [ >>line-text ] [ length >>line-length ] bi
22     [ 1 + ] change-line
23     0 >>column
24     drop ;
25
26 : push-parsing-word ( word -- )
27     lexer get lexer check-instance [
28         [ line>> ] [ line-text>> ] [ column>> ] tri
29         lexer-parsing-word boa
30     ] [ parsing-words>> push ] bi ;
31
32 : pop-parsing-word ( -- )
33     lexer get lexer check-instance parsing-words>> pop* ;
34
35 : new-lexer ( text class -- lexer )
36     new
37         0 >>line
38         swap >>text
39         V{ } clone >>parsing-words
40     dup next-line ; inline
41
42 : <lexer> ( text -- lexer )
43     lexer new-lexer ;
44
45 ERROR: unexpected want got ;
46
47 : forbid-tab ( c -- c )
48     [ CHAR: \t eq? [ "[space]" "[tab]" unexpected ] when ] keep ; inline
49
50 : skip ( i seq ? -- n )
51     over length [
52         [ swap forbid-tab CHAR: \s eq? xor ] curry find-from drop
53     ] dip or ; inline
54
55 : change-lexer-column ( ..a lexer quot: ( ..a col line -- ..b newcol ) -- ..b )
56     [ lexer check-instance [ column>> ] [ line-text>> ] bi ] prepose
57     keep column<< ; inline
58
59 GENERIC: skip-blank ( lexer -- )
60
61 <PRIVATE
62
63 : shebang? ( lexer -- lexer ? )
64     dup line>> 1 = [
65         dup column>> zero? [
66             dup line-text>> "#!" head?
67         ] [ f ] if
68     ] [ f ] if ; inline
69
70 PRIVATE>
71
72 M: lexer skip-blank
73     shebang? [
74         [ nip length ] change-lexer-column
75     ] [
76         [ t skip ] change-lexer-column
77     ] if ;
78
79 GENERIC: skip-word ( lexer -- )
80
81 M: lexer skip-word
82     [
83         2dup nth CHAR: \" eq? [ drop 1 + ] [ f skip ] if
84     ] change-lexer-column ;
85
86 : still-parsing? ( lexer -- ? )
87     lexer check-instance [ line>> ] [ text>> length ] bi <= ;
88
89 : still-parsing-line? ( lexer -- ? )
90     lexer check-instance [ column>> ] [ line-length>> ] bi < ;
91
92 : (parse-raw) ( lexer -- str )
93     lexer check-instance {
94         [ column>> ]
95         [ skip-word ]
96         [ column>> ]
97         [ line-text>> ]
98     } cleave subseq ;
99
100 : parse-raw ( lexer -- str/f )
101     dup still-parsing? [
102         dup skip-blank
103         dup still-parsing-line?
104         [ (parse-raw) ] [ dup next-line parse-raw ] if
105     ] [ drop f ] if ;
106
107 DEFER: parse-token
108
109 : skip-comments ( lexer str -- str' )
110     dup "!" = [
111         drop [ next-line ] keep parse-token
112     ] [
113         nip
114     ] if ;
115
116 : parse-token ( lexer -- str/f )
117     dup parse-raw [ skip-comments ] [ drop f ] if* ;
118
119 : ?scan-token ( -- str/f ) lexer get parse-token ;
120
121 PREDICATE: unexpected-eof < unexpected got>> not ;
122
123 : throw-unexpected-eof ( word -- * ) f unexpected ;
124
125 : scan-token ( -- str )
126     ?scan-token [ "token" throw-unexpected-eof ] unless* ;
127
128 : expect ( token -- )
129     scan-token 2dup = [ 2drop ] [ unexpected ] if ;
130
131 : each-token ( ... end quot: ( ... token -- ... ) -- ... )
132     [ scan-token ] 2dip 2over =
133     [ 3drop ] [ [ nip call ] [ each-token ] 2bi ] if ; inline recursive
134
135 : map-tokens ( ... end quot: ( ... token -- ... elt ) -- ... seq )
136     collector [ each-token ] dip { } like ; inline
137
138 : parse-tokens ( end -- seq )
139     [ ] map-tokens ;
140
141 TUPLE: lexer-error line column line-text parsing-words error ;
142
143 M: lexer-error error-file error>> error-file ;
144
145 M: lexer-error error-line [ error>> error-line ] [ line>> ] bi or ;
146
147 : <lexer-error> ( msg -- error )
148     [
149         lexer get {
150             [ line>> ]
151             [ column>> ]
152             [ line-text>> ]
153             [ parsing-words>> clone ]
154         } cleave
155     ] dip lexer-error boa ;
156
157 <PRIVATE
158
159 : simple-lexer-dump ( error -- )
160     [ line>> number>string ": " append ]
161     [ line-text>> ]
162     [ column>> ] tri
163     pick length + CHAR: \s <string>
164     [ write ] [ print ] [ write "^" print ] tri* ;
165
166 : parsing-word-lexer-dump ( error parsing-word -- error )
167     2dup [ line>> ] same? [ drop ] [
168         [
169             line>> number>string
170             over line>> number>string length
171             CHAR: \s pad-head
172             ": " append write
173         ] [ line-text>> print ] bi
174     ] if ;
175
176 PRIVATE>
177
178 : lexer-dump ( error -- )
179     dup parsing-words>> ?last [
180         parsing-word-lexer-dump
181     ] when* simple-lexer-dump ;
182
183 : with-lexer ( lexer quot -- newquot )
184     [ [ <lexer-error> rethrow ] recover ] curry
185     [ lexer ] dip with-variable ; inline