]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
factor: Rename GENERIC# to GENERIC#:.
[factor.git] / core / syntax / syntax.factor
1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays byte-vectors
4 classes.algebra.private classes.builtin classes.error
5 classes.intersection classes.maybe classes.mixin classes.parser
6 classes.predicate classes.singleton classes.tuple classes.tuple.parser
7 classes.union combinators compiler.units definitions effects
8 effects.parser generic generic.hook generic.math generic.parser
9 generic.standard hash-sets hashtables io.pathnames kernel lexer
10 math namespaces parser quotations sbufs sequences slots
11 source-files splitting strings strings.parser
12 strings.parser.private vectors vocabs vocabs.parser words
13 words.alias words.constant words.symbol ;
14 IN: bootstrap.syntax
15
16 ! These words are defined as a top-level form, instead of with
17 ! defining parsing words, because during stage1 bootstrap, the
18 ! "syntax" vocabulary is copied from the host. When stage1
19 ! bootstrap completes, the host's syntax vocabulary is deleted
20 ! from the target, then this top-level form creates the
21 ! target's "syntax" vocabulary as one of the first things done
22 ! in stage2.
23
24 : define-delimiter ( name -- )
25     "syntax" lookup-word t "delimiter" set-word-prop ;
26
27 ! Keep track of words defined by SYNTAX: as opposed to words
28 ! merely generated by define-syntax.
29 : mark-top-level-syntax ( word -- word )
30     dup t "syntax" set-word-prop ;
31
32 : define-core-syntax ( name quot -- )
33     [
34         dup "syntax" lookup-word [ ] [ no-word-error ] ?if
35         mark-top-level-syntax
36     ] dip
37     define-syntax ;
38
39 [
40     { "]" "}" ";" ">>" } [ define-delimiter ] each
41
42     "PRIMITIVE:" [
43         current-vocab name>>
44         scan-word scan-effect ensure-primitive
45     ] define-core-syntax
46
47     "CS{" [
48         "Call stack literals are not supported" throw
49     ] define-core-syntax
50
51     "IN:" [ scan-token set-current-vocab ] define-core-syntax
52
53     "<PRIVATE" [ begin-private ] define-core-syntax
54
55     "PRIVATE>" [ end-private ] define-core-syntax
56
57     "USE:" [ scan-token use-vocab ] define-core-syntax
58
59     "UNUSE:" [ scan-token unuse-vocab ] define-core-syntax
60
61     "USING:" [ ";" [ use-vocab ] each-token ] define-core-syntax
62
63     "QUALIFIED:" [ scan-token dup add-qualified ] define-core-syntax
64
65     "QUALIFIED-WITH:" [ scan-token scan-token add-qualified ] define-core-syntax
66
67     "FROM:" [
68         scan-token "=>" expect ";" parse-tokens add-words-from
69     ] define-core-syntax
70
71     "EXCLUDE:" [
72         scan-token "=>" expect ";" parse-tokens add-words-excluding
73     ] define-core-syntax
74
75     "RENAME:" [
76         scan-token scan-token "=>" expect scan-token add-renamed-word
77     ] define-core-syntax
78
79     "NAN:" [ 16 scan-base <fp-nan> suffix! ] define-core-syntax
80
81     "f" [ f suffix! ] define-core-syntax
82
83     "CHAR:" [
84         lexer get parse-raw [ "token" throw-unexpected-eof ] unless* {
85             { [ dup length 1 = ] [ first ] }
86             { [ "\\" ?head ] [ next-escape >string "" assert= ] }
87             [ name>char-hook get call( name -- char ) ]
88         } cond suffix!
89     ] define-core-syntax
90
91     "\"" [ parse-string suffix! ] define-core-syntax
92
93     "SBUF\"" [
94         lexer get skip-blank parse-string >sbuf suffix!
95     ] define-core-syntax
96
97     "P\"" [
98         lexer get skip-blank parse-string <pathname> suffix!
99     ] define-core-syntax
100
101     "[" [ parse-quotation suffix! ] define-core-syntax
102     "{" [ \ } [ >array ] parse-literal ] define-core-syntax
103     "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
104     "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
105     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-core-syntax
106     "H{" [ \ } [ parse-hashtable ] parse-literal ] define-core-syntax
107     "T{" [ parse-tuple-literal suffix! ] define-core-syntax
108     "TH{" [ parse-tuple-hash-literal suffix! ] define-core-syntax
109     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
110     "HS{" [ \ } [ >hash-set ] parse-literal ] define-core-syntax
111
112     "POSTPONE:" [ scan-word suffix! ] define-core-syntax
113     "\\" [ scan-word <wrapper> suffix! ] define-core-syntax
114     "M\\" [ scan-word scan-word lookup-method <wrapper> suffix! ] define-core-syntax
115     "inline" [ last-word make-inline ] define-core-syntax
116     "recursive" [ last-word make-recursive ] define-core-syntax
117     "foldable" [ last-word make-foldable ] define-core-syntax
118     "flushable" [ last-word make-flushable ] define-core-syntax
119     "delimiter" [ last-word t "delimiter" set-word-prop ] define-core-syntax
120     "deprecated" [ last-word make-deprecated ] define-core-syntax
121
122     "SYNTAX:" [
123         scan-new-word
124         mark-top-level-syntax
125         parse-definition define-syntax
126     ] define-core-syntax
127
128     "BUILTIN:" [
129         scan-word-name
130         current-vocab lookup-word
131         (parse-tuple-definition) 2drop check-builtin
132     ] define-core-syntax
133
134     "SYMBOL:" [
135         scan-new-word define-symbol
136     ] define-core-syntax
137
138     "SYMBOLS:" [
139         ";" [ create-word-in [ reset-generic ] [ define-symbol ] bi ] each-token
140     ] define-core-syntax
141
142     "SINGLETONS:" [
143         ";" [ create-class-in define-singleton-class ] each-token
144     ] define-core-syntax
145
146     "DEFER:" [
147         scan-token current-vocab create-word
148         [ fake-definition ] [ set-last-word ] [ undefined-def define ] tri
149     ] define-core-syntax
150
151     "ALIAS:" [
152         scan-new-word scan-word define-alias
153     ] define-core-syntax
154
155     "CONSTANT:" [
156         scan-new-word scan-object define-constant
157     ] define-core-syntax
158
159     ":" [
160         (:) define-declared
161     ] define-core-syntax
162
163     "GENERIC:" [
164         [ simple-combination ] (GENERIC:)
165     ] define-core-syntax
166
167     "GENERIC#:" [
168         [ scan-number <standard-combination> ] (GENERIC:)
169     ] define-core-syntax
170
171     "MATH:" [
172         [ math-combination ] (GENERIC:)
173     ] define-core-syntax
174
175     "HOOK:" [
176         [ scan-word <hook-combination> ] (GENERIC:)
177     ] define-core-syntax
178
179     "M:" [
180         (M:) define
181     ] define-core-syntax
182
183     "UNION:" [
184         scan-new-class parse-array-def define-union-class
185     ] define-core-syntax
186
187     "INTERSECTION:" [
188         scan-new-class parse-array-def define-intersection-class
189     ] define-core-syntax
190
191     "MIXIN:" [
192         scan-new-class define-mixin-class
193     ] define-core-syntax
194
195     "INSTANCE:" [
196         location [
197             scan-word scan-word 2dup add-mixin-instance
198             <mixin-instance>
199         ] dip remember-definition
200     ] define-core-syntax
201
202     "PREDICATE:" [
203         scan-new-class
204         "<" expect
205         scan-class
206         parse-definition define-predicate-class
207     ] define-core-syntax
208
209     "SINGLETON:" [
210         scan-new-class define-singleton-class
211     ] define-core-syntax
212
213     "TUPLE:" [
214         parse-tuple-definition define-tuple-class
215     ] define-core-syntax
216
217     "final" [
218         last-word make-final
219     ] define-core-syntax
220
221     "SLOT:" [
222         scan-token define-protocol-slot
223     ] define-core-syntax
224
225     "C:" [
226         scan-new-word scan-word define-boa-word
227     ] define-core-syntax
228
229     "ERROR:" [
230         parse-tuple-definition
231         pick save-location
232         define-error-class
233     ] define-core-syntax
234
235     "FORGET:" [
236         scan-object forget
237     ] define-core-syntax
238
239     "(" [
240         ")" parse-effect suffix!
241     ] define-core-syntax
242
243     "MAIN:" [
244         scan-word
245         dup ( -- ) check-stack-effect
246         [ current-vocab main<< ]
247         [ current-source-file get [ main<< ] [ drop ] if* ] bi
248     ] define-core-syntax
249
250     "<<" [
251         [
252             \ >> parse-until >quotation
253         ] with-nested-compilation-unit call( -- )
254     ] define-core-syntax
255
256     "call-next-method" [
257         current-method get [
258             literalize suffix!
259             \ (call-next-method) suffix!
260         ] [
261             not-in-a-method-error
262         ] if*
263     ] define-core-syntax
264
265     "maybe{" [
266         \ } [ <anonymous-union> <maybe> ] parse-literal
267     ] define-core-syntax
268
269     "not{" [
270         \ } [ <anonymous-union> <anonymous-complement> ] parse-literal
271     ] define-core-syntax
272
273     "intersection{" [
274          \ } [ <anonymous-intersection> ] parse-literal
275     ] define-core-syntax
276
277     "union{" [
278         \ } [ <anonymous-union> ] parse-literal
279     ] define-core-syntax
280
281     "initial:" "syntax" lookup-word define-symbol
282
283     "read-only" "syntax" lookup-word define-symbol
284
285     "call(" [ \ call-effect parse-call-paren ] define-core-syntax
286
287     "execute(" [ \ execute-effect parse-call-paren ] define-core-syntax
288
289     "<<<<<<<" [ version-control-merge-conflict ] define-core-syntax
290     "=======" [ version-control-merge-conflict ] define-core-syntax
291     ">>>>>>>" [ version-control-merge-conflict ] define-core-syntax
292
293     "<<<<<<" [ version-control-merge-conflict ] define-core-syntax
294     "======" [ version-control-merge-conflict ] define-core-syntax
295     ">>>>>>" [ version-control-merge-conflict ] define-core-syntax
296 ] with-compilation-unit