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