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