]> gitweb.factorcode.org Git - factor.git/blob - core/lexer/lexer.factor
lexer, parser: show initial parsing word line as part of lexer-errors
[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 : (parse-tokens) ( accum end -- accum )
101     scan 2dup = [
102         2drop
103     ] [
104         [ pick push (parse-tokens) ] [ unexpected-eof ] if*
105     ] if ;
106
107 : parse-tokens ( end -- seq )
108     100 <vector> swap (parse-tokens) >array ;
109
110 TUPLE: lexer-error line column line-text parsing-words error ;
111
112 M: lexer-error error-file error>> error-file ;
113 M: lexer-error error-line [ error>> error-line ] [ line>> ] bi or ;
114
115 : <lexer-error> ( msg -- error )
116     \ lexer-error new
117         lexer get [
118             [ line>> >>line ]
119             [ column>> >>column ] bi
120         ] [ 
121             [ line-text>> >>line-text ]
122             [ parsing-words>> clone >>parsing-words ] bi
123         ] bi
124         swap >>error ;
125
126 : simple-lexer-dump ( error -- )
127     [ line>> number>string ": " append ]
128     [ line-text>> dup string? [ drop "" ] unless ]
129     [ column>> 0 or ] tri
130     pick length + CHAR: \s <string>
131     [ write ] [ print ] [ write "^" print ] tri* ;
132
133 : (parsing-word-lexer-dump) ( error parsing-word -- )
134     [
135         line>> number>string
136         over line>> number>string length
137         CHAR: \s pad-head
138         ": " append write
139     ] [ line-text>> dup string? [ drop "" ] unless print ] bi
140     simple-lexer-dump ;
141
142 : parsing-word-lexer-dump ( error parsing-word -- )
143     2dup [ line>> ] bi@ =
144     [ drop simple-lexer-dump ]
145     [ (parsing-word-lexer-dump) ] if ;
146
147 : lexer-dump ( error -- )
148     dup parsing-words>> [ simple-lexer-dump ] [ last parsing-word-lexer-dump ] if-empty ;
149
150 : with-lexer ( lexer quot -- newquot )
151     [ lexer set ] dip [ <lexer-error> rethrow ] recover ; inline
152
153 SYMBOL: lexer-factory
154
155 [ <lexer> ] lexer-factory set-global