]> gitweb.factorcode.org Git - factor.git/blob - core/parser/parser.factor
873ca9db4ca0d371ffaed086caf46c360888d6f5
[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 classes.algebra ;
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 : auto-use ( -- ) auto-use? on ;
26
27 : no-word-restarted ( restart-value -- word )
28     dup word? [
29         dup vocabulary>>
30         [ auto-use-vocab ]
31         [ "Added \"" "\" vocabulary to search path" surround note. ] bi
32     ] [ create-in ] if ;
33
34 : ignore-forwards ( seq -- seq' )
35     [ forward-reference? not ] filter ;
36
37 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
38
39 : ignore-privates ( seq -- seq' )
40     dup [ private? ] all? [ [ private? not ] filter ] unless ;
41
42 : no-word ( name -- newword )
43     dup words-named ignore-forwards
44     dup ignore-privates dup length 1 = auto-use? get and
45     [ 2nip first no-word-restarted ]
46     [ drop <no-word-error> throw-restarts no-word-restarted ]
47     if ;
48
49 : parse-word ( string -- word )
50     dup search [ ] [ no-word ] ?if ;
51
52 ERROR: number-expected ;
53
54 : parse-number ( string -- number )
55     string>number [ number-expected ] unless* ;
56
57 : parse-datum ( string -- word/number )
58     dup search [ ] [
59         dup string>number [ ] [ no-word ] ?if
60     ] ?if ;
61
62 : (scan-datum) ( -- word/number/f )
63     (scan-token) dup [ parse-datum ] when ;
64
65 : scan-datum ( -- word/number )
66     (scan-datum) [ \ word unexpected-eof ] unless* ;
67
68 : scan-word ( -- word )
69     (scan-token) parse-word ;
70
71 : scan-number ( -- number )
72     (scan-token) parse-number ;
73
74 : scan-word-name ( -- string )
75     scan-token
76     dup string>number [
77         "Word names cannot be numbers" throw
78     ] when ;
79
80 : scan-new ( -- word )
81     scan-word-name create-in ;
82
83 : scan-new-word ( -- word )
84     scan-new dup reset-generic ;
85
86 ERROR: staging-violation word ;
87
88 : (execute-parsing) ( accum word -- accum )
89     dup push-parsing-word
90     execute( accum -- accum )
91     pop-parsing-word ; inline
92
93 : execute-parsing ( accum word -- accum )
94     dup changed-definitions get key? [ staging-violation ] when
95     (execute-parsing) ;
96
97 : scan-object ( -- object )
98     scan-datum
99     dup parsing-word? [
100         V{ } clone swap execute-parsing first
101     ] when ;
102
103 : scan-class ( -- class )
104     scan-object \ f or ;
105
106 : parse-until-step ( accum end -- accum ? )
107     (scan-datum) {
108         { [ 2dup eq? ] [ 2drop f ] }
109         { [ dup not ] [ drop unexpected-eof t ] }
110         { [ dup delimiter? ] [ unexpected t ] }
111         { [ dup parsing-word? ] [ nip execute-parsing t ] }
112         [ pick push drop t ]
113     } cond ;
114
115 : (parse-until) ( accum end -- accum )
116     [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
117
118 : parse-until ( end -- vec )
119     100 <vector> swap (parse-until) ;
120
121 SYMBOL: quotation-parser
122
123 HOOK: parse-quotation quotation-parser ( -- quot )
124
125 M: f parse-quotation \ ] parse-until >quotation ;
126
127 : (parse-lines) ( lexer -- quot )
128     [ f parse-until >quotation ] with-lexer ;
129
130 : parse-lines ( lines -- quot )
131     >array <lexer> (parse-lines) ;
132
133 : parse-literal ( accum end quot -- accum )
134     [ parse-until ] dip call suffix! ; inline
135
136 : parse-definition ( -- quot )
137     \ ; parse-until >quotation ;
138
139 ERROR: bad-number ;
140
141 : scan-base ( base -- n )
142     scan-token swap base> [ bad-number ] unless* ;
143
144 SYMBOL: bootstrap-syntax
145
146 : with-file-vocabs ( quot -- )
147     [
148         "syntax" use-vocab
149         bootstrap-syntax get [ use-words ] when*
150         call
151     ] with-manifest ; inline
152
153 SYMBOL: print-use-hook
154
155 print-use-hook [ [ ] ] initialize
156
157 : parse-fresh ( lines -- quot )
158     [
159         parse-lines
160         auto-used? [ print-use-hook get call( -- ) ] when
161     ] with-file-vocabs ;
162
163 : parsing-file ( file -- )
164     parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
165
166 : filter-moved ( assoc1 assoc2 -- seq )
167     swap assoc-diff keys [
168         {
169             { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
170             { [ dup reader-method? ] [ f ] }
171             { [ dup writer-method? ] [ f ] }
172             [ t ]
173         } cond nip
174     ] filter ;
175
176 : removed-definitions ( -- assoc1 assoc2 )
177     new-definitions old-definitions
178     [ get first2 assoc-union ] bi@ ;
179
180 : removed-classes ( -- assoc1 assoc2 )
181     new-definitions old-definitions
182     [ get second ] bi@ ;
183
184 : forget-removed-definitions ( -- )
185     removed-definitions filter-moved forget-all ;
186
187 : reset-removed-classes ( -- )
188     removed-classes
189     filter-moved [ class? ] filter [ forget-class ] each ;
190
191 : fix-class-words ( -- )
192     #! If a class word had a compound definition which was
193     #! removed, it must go back to being a symbol.
194     new-definitions get first2
195     filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
196
197 : forget-smudged ( -- )
198     forget-removed-definitions
199     reset-removed-classes
200     fix-class-words ;
201
202 : finish-parsing ( lines quot -- )
203     file get
204     [ record-top-level-form ]
205     [ record-definitions ]
206     [ record-checksum ]
207     tri ;
208
209 : parse-stream ( stream name -- quot )
210     [
211         [
212             stream-lines dup parse-fresh
213             [ nip ] [ finish-parsing ] 2bi
214             forget-smudged
215         ] with-source-file
216     ] with-compilation-unit ;
217
218 : parse-file-restarts ( file -- restarts )
219     "Load " " again" surround t 2array 1array ;
220
221 : parse-file ( file -- quot )
222     [
223         [ parsing-file ] keep
224         [ utf8 <file-reader> ] keep
225         parse-stream
226     ] [
227         over parse-file-restarts rethrow-restarts
228         drop parse-file
229     ] recover ;
230
231 : run-file ( file -- )
232     parse-file call( -- ) ;
233
234 : ?run-file ( path -- )
235     dup exists? [ run-file ] [ drop ] if ;
236
237 ERROR: version-control-merge-conflict ;