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 : change-lexer-column ( ..a lexer quot: ( ..a col line -- ..b newcol ) -- ..b )
48 [ lexer check-instance [ column>> ] [ line-text>> ] bi ] prepose
49 keep column<< ; inline
53 : shebang? ( lexer -- lexer ? )
56 dup line-text>> "#!" head?
60 : forbid-tab ( c -- c )
61 [ CHAR: \t eq? [ "[space]" "[tab]" unexpected ] when ] keep ; inline
68 GENERIC: skip-blank ( lexer -- )
72 [ nip length ] change-lexer-column
75 [ [ forbid-tab CHAR: \s eq? not ] find-from drop ]
80 GENERIC: skip-word ( lexer -- )
84 [ [ forbid-tab " \"" member-eq? ] find-from CHAR: \" eq? [ 1 + ] when ]
86 ] change-lexer-column ;
88 : still-parsing? ( lexer -- ? )
89 lexer check-instance [ line>> ] [ text>> length ] bi <= ;
91 : still-parsing-line? ( lexer -- ? )
92 lexer check-instance [ column>> ] [ line-length>> ] bi < ;
94 : (parse-raw) ( lexer -- str )
95 lexer check-instance {
102 : parse-raw ( lexer -- str/f )
105 dup still-parsing-line?
106 [ (parse-raw) ] [ dup next-line parse-raw ] if
111 : skip-comments ( lexer str -- str' )
113 drop [ next-line ] keep parse-token
118 : parse-token ( lexer -- str/f )
119 dup parse-raw [ skip-comments ] [ drop f ] if* ;
121 : ?scan-token ( -- str/f ) lexer get parse-token ;
123 PREDICATE: unexpected-eof < unexpected got>> not ;
125 : throw-unexpected-eof ( word -- * ) f unexpected ;
127 : scan-token ( -- str )
128 ?scan-token [ "token" throw-unexpected-eof ] unless* ;
130 : expect ( token -- )
131 scan-token 2dup = [ 2drop ] [ unexpected ] if ;
133 : each-token ( ... end quot: ( ... token -- ... ) -- ... )
134 [ scan-token ] 2dip 2over =
135 [ 3drop ] [ [ nip call ] [ each-token ] 2bi ] if ; inline recursive
137 : map-tokens ( ... end quot: ( ... token -- ... elt ) -- ... seq )
138 collector [ each-token ] dip { } like ; inline
140 : parse-tokens ( end -- seq )
143 TUPLE: lexer-error line column line-text parsing-words error ;
145 M: lexer-error error-file error>> error-file ;
147 M: lexer-error error-line [ error>> error-line ] [ line>> ] bi or ;
149 : <lexer-error> ( msg -- error )
155 [ parsing-words>> clone ]
157 ] dip lexer-error boa ;
161 : simple-lexer-dump ( error -- )
162 [ line>> number>string ": " append ]
165 pick length + CHAR: \s <string>
166 [ write ] [ print ] [ write "^" print ] tri* ;
168 : parsing-word-lexer-dump ( error parsing-word -- error )
169 2dup [ line>> ] same? [ drop ] [
172 over line>> number>string length
175 ] [ line-text>> print ] bi
180 : lexer-dump ( error -- )
181 dup parsing-words>> ?last [
182 parsing-word-lexer-dump
183 ] when* simple-lexer-dump ;
185 : with-lexer ( lexer quot -- newquot )
186 [ [ <lexer-error> rethrow ] recover ] curry
187 [ lexer ] dip with-variable ; inline