]> gitweb.factorcode.org Git - factor.git/blob - core/lexer/lexer.factor
Merge branch 'lexer-parsing-word-errors' of git://factorcode.org/git/factor into...
[factor.git] / core / lexer / lexer.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences accessors namespaces math words strings
4 io vectors arrays math.parser combinators continuations
5 source-files.errors ;
6 IN: lexer
7
8 TUPLE: lexer text line line-text line-length column parsing-words ;
9
10 TUPLE: lexer-parsing-word word line line-text column ;
11
12 : next-line ( lexer -- )
13     dup [ line>> ] [ text>> ] bi ?nth >>line-text
14     dup line-text>> length >>line-length
15     [ 1 + ] change-line
16     0 >>column
17     drop ;
18
19 : push-parsing-word ( word -- )
20     lexer-parsing-word new
21         swap >>word
22         lexer get [
23             [ line>>      >>line      ]
24             [ line-text>> >>line-text ]
25             [ column>>    >>column    ] tri
26         ] [ parsing-words>> push ] bi ;
27
28 : pop-parsing-word ( -- )
29     lexer get parsing-words>> pop drop ;
30
31 : new-lexer ( text class -- lexer )
32     new
33         0 >>line
34         swap >>text
35         V{ } clone >>parsing-words
36     dup next-line ; inline
37
38 : <lexer> ( text -- lexer )
39     lexer new-lexer ;
40
41 ERROR: unexpected want got ;
42
43 : forbid-tab ( c -- c )
44     [ CHAR: \t eq? [ "[space]" "[tab]" unexpected ] when ] keep ; inline
45
46 : skip ( i seq ? -- n )
47     over length
48     [ [ swap forbid-tab CHAR: \s eq? xor ] curry find-from drop ] dip or ;
49
50 : change-lexer-column ( lexer quot -- )
51     [ [ column>> ] [ line-text>> ] bi ] prepose keep
52     (>>column) ; inline
53
54 GENERIC: skip-blank ( lexer -- )
55
56 M: lexer skip-blank ( lexer -- )
57     [ t skip ] change-lexer-column ;
58
59 GENERIC: skip-word ( lexer -- )
60
61 M: lexer skip-word ( lexer -- )
62     [
63         2dup nth CHAR: " eq? [ drop 1 + ] [ f skip ] if
64     ] change-lexer-column ;
65
66 : still-parsing? ( lexer -- ? )
67     [ line>> ] [ text>> length ] bi <= ;
68
69 : still-parsing-line? ( lexer -- ? )
70     [ column>> ] [ line-length>> ] bi < ;
71
72 : (parse-token) ( lexer -- str )
73     {
74         [ column>> ]
75         [ skip-word ]
76         [ column>> ]
77         [ line-text>> ]
78     } cleave subseq ;
79
80 :  parse-token ( lexer -- str/f )
81     dup still-parsing? [
82         dup skip-blank
83         dup still-parsing-line?
84         [ (parse-token) ] [ dup next-line parse-token ] if
85     ] [ drop f ] if ;
86
87 : scan ( -- str/f ) lexer get parse-token ;
88
89 PREDICATE: unexpected-eof < unexpected
90     got>> not ;
91
92 : unexpected-eof ( word -- * ) f unexpected ;
93
94 : expect ( token -- )
95     scan
96     [ 2dup = [ 2drop ] [ unexpected ] if ]
97     [ unexpected-eof ]
98     if* ;
99
100 : (each-token) ( end quot -- pred quot )
101     [ [ [ scan dup ] ] dip [ = not ] curry [ [ f ] if* ] curry compose ] dip ; inline
102
103 : each-token ( end quot -- )
104     (each-token) while drop ; inline
105
106 : map-tokens ( end quot -- seq )
107     (each-token) produce nip ; inline
108
109 : parse-tokens ( end -- seq )
110     [ ] map-tokens ;
111
112 TUPLE: lexer-error line column line-text parsing-words error ;
113
114 M: lexer-error error-file error>> error-file ;
115 M: lexer-error error-line [ error>> error-line ] [ line>> ] bi or ;
116
117 : <lexer-error> ( msg -- error )
118     \ lexer-error new
119         lexer get [
120             [ line>> >>line ]
121             [ column>> >>column ] bi
122         ] [ 
123             [ line-text>> >>line-text ]
124             [ parsing-words>> clone >>parsing-words ] bi
125         ] bi
126         swap >>error ;
127
128 : simple-lexer-dump ( error -- )
129     [ line>> number>string ": " append ]
130     [ line-text>> dup string? [ drop "" ] unless ]
131     [ column>> 0 or ] tri
132     pick length + CHAR: \s <string>
133     [ write ] [ print ] [ write "^" print ] tri* ;
134
135 : (parsing-word-lexer-dump) ( error parsing-word -- )
136     [
137         line>> number>string
138         over line>> number>string length
139         CHAR: \s pad-head
140         ": " append write
141     ] [ line-text>> dup string? [ drop "" ] unless print ] bi
142     simple-lexer-dump ;
143
144 : parsing-word-lexer-dump ( error parsing-word -- )
145     2dup [ line>> ] bi@ =
146     [ drop simple-lexer-dump ]
147     [ (parsing-word-lexer-dump) ] if ;
148
149 : lexer-dump ( error -- )
150     dup parsing-words>> [ simple-lexer-dump ] [ last parsing-word-lexer-dump ] if-empty ;
151
152 : with-lexer ( lexer quot -- newquot )
153     [ lexer set ] dip [ <lexer-error> rethrow ] recover ; inline
154
155 SYMBOL: lexer-factory
156
157 [ <lexer> ] lexer-factory set-global