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 ;
12 { line-length fixnum }
14 { parsing-words vector } ;
16 TUPLE: lexer-parsing-word word line line-text column ;
18 : next-line ( lexer -- )
20 dup [ line>> ] [ text>> ] bi ?nth "" or
21 [ >>line-text ] [ length >>line-length ] bi
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 ;
32 : pop-parsing-word ( -- )
33 lexer get lexer check-instance parsing-words>> pop* ;
35 : new-lexer ( text class -- lexer )
39 V{ } clone >>parsing-words
40 dup next-line ; inline
42 : <lexer> ( text -- lexer )
45 ERROR: unexpected want got ;
47 : forbid-tab ( c -- c )
48 [ CHAR: \t eq? [ "[space]" "[tab]" unexpected ] when ] keep ; inline
50 : skip ( i seq ? -- n )
52 [ swap forbid-tab CHAR: \s eq? xor ] curry find-from drop
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
59 GENERIC: skip-blank ( lexer -- )
63 : shebang? ( lexer -- lexer ? )
66 dup line-text>> "#!" head?
74 [ nip length ] change-lexer-column
76 [ t skip ] change-lexer-column
79 GENERIC: skip-word ( lexer -- )
83 2dup nth CHAR: \" eq? [ drop 1 + ] [ f skip ] if
84 ] change-lexer-column ;
86 : still-parsing? ( lexer -- ? )
87 lexer check-instance [ line>> ] [ text>> length ] bi <= ;
89 : still-parsing-line? ( lexer -- ? )
90 lexer check-instance [ column>> ] [ line-length>> ] bi < ;
92 : (parse-raw) ( lexer -- str )
93 lexer check-instance {
100 : parse-raw ( lexer -- str/f )
103 dup still-parsing-line?
104 [ (parse-raw) ] [ dup next-line parse-raw ] if
109 : skip-comments ( lexer str -- str' )
111 drop [ next-line ] keep parse-token
116 : parse-token ( lexer -- str/f )
117 dup parse-raw [ skip-comments ] [ drop f ] if* ;
119 : ?scan-token ( -- str/f ) lexer get parse-token ;
121 PREDICATE: unexpected-eof < unexpected got>> not ;
123 : throw-unexpected-eof ( word -- * ) f unexpected ;
125 : scan-token ( -- str )
126 ?scan-token [ "token" throw-unexpected-eof ] unless* ;
128 : expect ( token -- )
129 scan-token 2dup = [ 2drop ] [ unexpected ] if ;
131 : each-token ( ... end quot: ( ... token -- ... ) -- ... )
132 [ scan-token ] 2dip 2over =
133 [ 3drop ] [ [ nip call ] [ each-token ] 2bi ] if ; inline recursive
135 : map-tokens ( ... end quot: ( ... token -- ... elt ) -- ... seq )
136 collector [ each-token ] dip { } like ; inline
138 : parse-tokens ( end -- seq )
141 TUPLE: lexer-error line column line-text parsing-words error ;
143 M: lexer-error error-file error>> error-file ;
145 M: lexer-error error-line [ error>> error-line ] [ line>> ] bi or ;
147 : <lexer-error> ( msg -- error )
153 [ parsing-words>> clone ]
155 ] dip lexer-error boa ;
159 : simple-lexer-dump ( error -- )
160 [ line>> number>string ": " append ]
163 pick length + CHAR: \s <string>
164 [ write ] [ print ] [ write "^" print ] tri* ;
166 : parsing-word-lexer-dump ( error parsing-word -- error )
167 2dup [ line>> ] same? [ drop ] [
170 over line>> number>string length
173 ] [ line-text>> print ] bi
178 : lexer-dump ( error -- )
179 dup parsing-words>> ?last [
180 parsing-word-lexer-dump
181 ] when* simple-lexer-dump ;
183 : with-lexer ( lexer quot -- newquot )
184 [ [ <lexer-error> rethrow ] recover ] curry
185 [ lexer ] dip with-variable ; inline