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