factor/core/lexer/lexer.factor

186 lines
4.6 KiB
Factor
Raw Permalink Normal View History

! Copyright (C) 2008, 2010 Slava Pestov, Joe Groff.
2008-06-25 04:25:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays classes combinators continuations io
kernel kernel.private math math.parser namespaces sequences
2014-05-20 11:53:41 -04:00
sequences.private source-files.errors strings vectors ;
2008-06-25 04:25:08 -04:00
IN: lexer
2012-07-27 19:05:28 -04:00
TUPLE: lexer
{ text array }
{ line fixnum }
{ line-text string }
{ line-length fixnum }
{ column fixnum }
{ parsing-words vector } ;
TUPLE: lexer-parsing-word word line line-text column ;
2008-06-25 04:25:08 -04:00
: next-line ( lexer -- )
lexer check-instance
dup [ line>> ] [ text>> ] bi ?nth "" or
2011-09-17 00:54:17 -04:00
[ >>line-text ] [ length >>line-length ] bi
[ 1 + ] change-line
2008-06-25 04:25:08 -04:00
0 >>column
drop ;
: push-parsing-word ( word -- )
lexer get lexer check-instance [
2014-02-21 04:10:21 -05:00
[ line>> ] [ line-text>> ] [ column>> ] tri
lexer-parsing-word boa
] [ parsing-words>> push ] bi ;
: pop-parsing-word ( -- )
lexer get lexer check-instance parsing-words>> pop* ;
2008-06-25 04:25:08 -04:00
: new-lexer ( text class -- lexer )
new
0 >>line
swap >>text
V{ } clone >>parsing-words
2008-06-25 04:25:08 -04:00
dup next-line ; inline
: <lexer> ( text -- lexer )
lexer new-lexer ;
2009-06-16 14:47:56 -04:00
ERROR: unexpected want got ;
: forbid-tab ( c -- c )
[ CHAR: \t eq? [ "[space]" "[tab]" unexpected ] when ] keep ; inline
2009-06-16 14:47:56 -04:00
2008-06-25 04:25:08 -04:00
: skip ( i seq ? -- n )
2014-02-21 04:10:21 -05:00
over length [
[ swap forbid-tab CHAR: \s eq? xor ] curry find-from drop
] dip or ; inline
2008-06-25 04:25:08 -04:00
2016-03-31 00:08:41 -04:00
: change-lexer-column ( ..a lexer quot: ( ..a col line -- ..b newcol ) -- ..b )
[ lexer check-instance [ column>> ] [ line-text>> ] bi ] prepose
keep column<< ; inline
2008-06-25 04:25:08 -04:00
GENERIC: skip-blank ( lexer -- )
<PRIVATE
: shebang? ( lexer -- lexer ? )
dup line>> 1 = [
dup column>> zero? [
dup line-text>> "#!" head?
] [ f ] if
] [ f ] if ; inline
PRIVATE>
M: lexer skip-blank
shebang? [
[ nip length ] change-lexer-column
] [
[ t skip ] change-lexer-column
] if ;
GENERIC: skip-word ( lexer -- )
M: lexer skip-word
2008-06-25 04:25:08 -04:00
[
2dup nth CHAR: \" eq? [ drop 1 + ] [ f skip ] if
2008-06-25 04:25:08 -04:00
] change-lexer-column ;
: still-parsing? ( lexer -- ? )
lexer check-instance [ line>> ] [ text>> length ] bi <= ;
2008-06-25 04:25:08 -04:00
: still-parsing-line? ( lexer -- ? )
lexer check-instance [ column>> ] [ line-length>> ] bi < ;
2008-06-25 04:25:08 -04:00
2016-03-31 00:21:40 -04:00
: (parse-raw) ( lexer -- str )
lexer check-instance {
2016-03-31 00:21:40 -04:00
[ column>> ]
[ skip-word ]
[ column>> ]
[ line-text>> ]
} cleave subseq ;
2016-03-31 00:08:41 -04:00
2016-03-31 00:21:40 -04:00
: parse-raw ( lexer -- str/f )
dup still-parsing? [
dup skip-blank
dup still-parsing-line?
[ (parse-raw) ] [ dup next-line parse-raw ] if
] [ drop f ] if ;
DEFER: parse-token
2016-03-31 00:08:41 -04:00
: skip-comments ( lexer str -- str' )
dup "!" = [
drop [ next-line ] keep parse-token
] [
nip
] if ;
: parse-token ( lexer -- str/f )
2016-03-31 00:21:40 -04:00
dup parse-raw [ skip-comments ] [ drop f ] if* ;
2008-06-25 04:25:08 -04:00
: ?scan-token ( -- str/f ) lexer get parse-token ;
2008-06-25 04:25:08 -04:00
PREDICATE: unexpected-eof < unexpected got>> not ;
2008-06-25 04:25:08 -04:00
2013-03-24 00:35:50 -04:00
: throw-unexpected-eof ( word -- * ) f unexpected ;
2008-06-25 04:25:08 -04:00
2013-03-24 00:35:50 -04:00
: scan-token ( -- str )
?scan-token [ "token" throw-unexpected-eof ] unless* ;
: expect ( token -- )
scan-token 2dup = [ 2drop ] [ unexpected ] if ;
: each-token ( ... end quot: ( ... token -- ... ) -- ... )
[ scan-token ] 2dip 2over =
[ 3drop ] [ [ nip call ] [ each-token ] 2bi ] if ; inline recursive
: map-tokens ( ... end quot: ( ... token -- ... elt ) -- ... seq )
2010-03-26 16:31:48 -04:00
collector [ each-token ] dip { } like ; inline
2008-06-25 04:25:08 -04:00
: parse-tokens ( end -- seq )
[ ] map-tokens ;
2008-06-25 04:25:08 -04:00
TUPLE: lexer-error line column line-text parsing-words error ;
2008-06-25 04:25:08 -04:00
M: lexer-error error-file error>> error-file ;
M: lexer-error error-line [ error>> error-line ] [ line>> ] bi or ;
2008-06-25 04:25:08 -04:00
: <lexer-error> ( msg -- error )
2014-02-21 04:10:21 -05:00
[
lexer get {
[ line>> ]
[ column>> ]
[ line-text>> ]
[ parsing-words>> clone ]
} cleave
] dip lexer-error boa ;
2008-06-25 04:25:08 -04:00
<PRIVATE
: simple-lexer-dump ( error -- )
2008-06-25 04:25:08 -04:00
[ line>> number>string ": " append ]
[ line-text>> ]
[ column>> ] tri
2008-06-25 04:25:08 -04:00
pick length + CHAR: \s <string>
[ write ] [ print ] [ write "^" print ] tri* ;
: parsing-word-lexer-dump ( error parsing-word -- error )
2dup [ line>> ] same? [ drop ] [
[
line>> number>string
over line>> number>string length
CHAR: \s pad-head
": " append write
] [ line-text>> print ] bi
] if ;
PRIVATE>
: lexer-dump ( error -- )
dup parsing-words>> ?last [
parsing-word-lexer-dump
] when* simple-lexer-dump ;
2008-06-25 04:25:08 -04:00
: with-lexer ( lexer quot -- newquot )
[ [ <lexer-error> rethrow ] recover ] curry
[ lexer ] dip with-variable ; inline