]> gitweb.factorcode.org Git - factor.git/blob - core/parser/parser.factor
Refactor the lexer/parser to expose friendlier words for scanning tokens. The preferr...
[factor.git] / core / parser / parser.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions generic assocs kernel math namespaces
4 sequences strings vectors words words.symbol quotations io
5 combinators sorting splitting math.parser effects continuations
6 io.files vocabs io.encodings.utf8 source-files classes
7 hashtables compiler.units accessors sets lexer vocabs.parser
8 slots parser.notes ;
9 IN: parser
10
11 : location ( -- loc )
12     file get lexer get line>> 2dup and
13     [ [ path>> ] dip 2array ] [ 2drop f ] if ;
14
15 : save-location ( definition -- )
16     location remember-definition ;
17
18 M: parsing-word stack-effect drop (( parsed -- parsed )) ;
19
20 : create-in ( str -- word )
21     current-vocab create dup set-word dup save-location ;
22
23 SYMBOL: auto-use?
24
25 : no-word-restarted ( restart-value -- word )
26     dup word? [
27         dup vocabulary>>
28         [ auto-use-vocab ]
29         [ "Added \"" "\" vocabulary to search path" surround note. ] bi
30     ] [ create-in ] if ;
31
32 : ignore-forwards ( seq -- seq' )
33     [ forward-reference? not ] filter ;
34
35 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
36
37 : ignore-privates ( seq -- seq' )
38     dup [ private? ] all? [ [ private? not ] filter ] unless ;
39
40 : no-word ( name -- newword )
41     dup words-named ignore-forwards
42     dup ignore-privates dup length 1 = auto-use? get and
43     [ 2nip first no-word-restarted ]
44     [ drop <no-word-error> throw-restarts no-word-restarted ]
45     if ;
46
47 : parse-word ( string -- word )
48     dup search [ ] [ no-word ] ?if ;
49
50 : parse-word/number ( string -- word/number )
51     dup search [ ] [
52         dup string>number [ ] [ no-word ] ?if
53     ] ?if ;
54
55 : (scan-word) ( -- word/number/f )
56     (scan-token) dup [ parse-word/number ] when ;
57
58 : scan-word ( -- word/number )
59     (scan-word) [ \ word unexpected-eof ] unless* ;
60
61 : scan-word-name ( -- string )
62     scan-token
63     dup string>number [
64         "Word names cannot be numbers" throw
65     ] when ;
66
67 : scan-new ( -- word )
68     scan-word-name create-in ;
69
70 : scan-new-word ( -- word )
71     scan-new dup reset-generic ;
72
73 ERROR: staging-violation word ;
74
75 : (execute-parsing) ( accum word -- accum )
76     dup push-parsing-word
77     execute( accum -- accum )
78     pop-parsing-word ; inline
79
80 : execute-parsing ( accum word -- accum )
81     dup changed-definitions get key? [ staging-violation ] when
82     (execute-parsing) ;
83
84 : scan-object ( -- object )
85     scan-word
86     dup parsing-word? [
87         V{ } clone swap execute-parsing first
88     ] when ;
89
90 : parse-step ( accum end -- accum ? )
91     (scan-word) {
92         { [ 2dup eq? ] [ 2drop f ] }
93         { [ dup not ] [ drop unexpected-eof t ] }
94         { [ dup delimiter? ] [ unexpected t ] }
95         { [ dup parsing-word? ] [ nip execute-parsing t ] }
96         [ pick push drop t ]
97     } cond ;
98
99 : (parse-until) ( accum end -- accum )
100     [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
101
102 : parse-until ( end -- vec )
103     100 <vector> swap (parse-until) ;
104
105 SYMBOL: quotation-parser
106
107 HOOK: parse-quotation quotation-parser ( -- quot )
108
109 M: f parse-quotation \ ] parse-until >quotation ;
110
111 : (parse-lines) ( lexer -- quot )
112     [ f parse-until >quotation ] with-lexer ;
113
114 : parse-lines ( lines -- quot )
115     lexer-factory get call( lines -- lexer ) (parse-lines) ;
116
117 : parse-literal ( accum end quot -- accum )
118     [ parse-until ] dip call suffix! ; inline
119
120 : parse-definition ( -- quot )
121     \ ; parse-until >quotation ;
122
123 ERROR: bad-number ;
124
125 : scan-base ( base -- n )
126     scan-token swap base> [ bad-number ] unless* ;
127
128 : parse-base ( parsed base -- parsed )
129     scan-base suffix! ;
130
131 SYMBOL: bootstrap-syntax
132
133 : with-file-vocabs ( quot -- )
134     [
135         "syntax" use-vocab
136         bootstrap-syntax get [ use-words ] when*
137         call
138     ] with-manifest ; inline
139
140 SYMBOL: print-use-hook
141
142 print-use-hook [ [ ] ] initialize
143
144 : parse-fresh ( lines -- quot )
145     [
146         parse-lines
147         auto-used? [ print-use-hook get call( -- ) ] when
148     ] with-file-vocabs ;
149
150 : parsing-file ( file -- )
151     parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
152
153 : filter-moved ( assoc1 assoc2 -- seq )
154     swap assoc-diff keys [
155         {
156             { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
157             { [ dup reader-method? ] [ f ] }
158             { [ dup writer-method? ] [ f ] }
159             [ t ]
160         } cond nip
161     ] filter ;
162
163 : removed-definitions ( -- assoc1 assoc2 )
164     new-definitions old-definitions
165     [ get first2 assoc-union ] bi@ ;
166
167 : removed-classes ( -- assoc1 assoc2 )
168     new-definitions old-definitions
169     [ get second ] bi@ ;
170
171 : forget-removed-definitions ( -- )
172     removed-definitions filter-moved forget-all ;
173
174 : reset-removed-classes ( -- )
175     removed-classes
176     filter-moved [ class? ] filter [ forget-class ] each ;
177
178 : fix-class-words ( -- )
179     #! If a class word had a compound definition which was
180     #! removed, it must go back to being a symbol.
181     new-definitions get first2
182     filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
183
184 : forget-smudged ( -- )
185     forget-removed-definitions
186     reset-removed-classes
187     fix-class-words ;
188
189 : finish-parsing ( lines quot -- )
190     file get
191     [ record-top-level-form ]
192     [ record-definitions ]
193     [ record-checksum ]
194     tri ;
195
196 : parse-stream ( stream name -- quot )
197     [
198         [
199             stream-lines dup parse-fresh
200             [ nip ] [ finish-parsing ] 2bi
201             forget-smudged
202         ] with-source-file
203     ] with-compilation-unit ;
204
205 : parse-file-restarts ( file -- restarts )
206     "Load " " again" surround t 2array 1array ;
207
208 : parse-file ( file -- quot )
209     [
210         [ parsing-file ] keep
211         [ utf8 <file-reader> ] keep
212         parse-stream
213     ] [
214         over parse-file-restarts rethrow-restarts
215         drop parse-file
216     ] recover ;
217
218 : run-file ( file -- )
219     parse-file call( -- ) ;
220
221 : ?run-file ( path -- )
222     dup exists? [ run-file ] [ drop ] if ;
223
224 ERROR: version-control-merge-conflict ;