]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
change ERROR: words from throw-foo back to foo.
[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     "!" [ lexer get next-line ] define-core-syntax
52
53     "#!" [ POSTPONE: ! ] define-core-syntax
54
55     "IN:" [ scan-token set-current-vocab ] define-core-syntax
56
57     "<PRIVATE" [ begin-private ] define-core-syntax
58
59     "PRIVATE>" [ end-private ] define-core-syntax
60
61     "USE:" [ scan-token use-vocab ] define-core-syntax
62
63     "UNUSE:" [ scan-token unuse-vocab ] define-core-syntax
64
65     "USING:" [ ";" [ use-vocab ] each-token ] define-core-syntax
66
67     "QUALIFIED:" [ scan-token dup add-qualified ] define-core-syntax
68
69     "QUALIFIED-WITH:" [ scan-token scan-token add-qualified ] define-core-syntax
70
71     "FROM:" [
72         scan-token "=>" expect ";" parse-tokens add-words-from
73     ] define-core-syntax
74
75     "EXCLUDE:" [
76         scan-token "=>" expect ";" parse-tokens add-words-excluding
77     ] define-core-syntax
78
79     "RENAME:" [
80         scan-token scan-token "=>" expect scan-token add-renamed-word
81     ] define-core-syntax
82
83     "NAN:" [ 16 scan-base <fp-nan> suffix! ] define-core-syntax
84
85     "f" [ f suffix! ] define-core-syntax
86
87     "CHAR:" [
88         scan-token {
89             { [ dup length 1 = ] [ first ] }
90             { [ "\\" ?head ] [ next-escape >string "" assert= ] }
91             [ name>char-hook get call( name -- char ) ]
92         } cond suffix!
93     ] define-core-syntax
94
95     "\"" [ "\"" parse-multiline-string-until suffix! ] define-core-syntax
96
97     "SBUF\"" [
98         lexer get skip-blank parse-string >sbuf suffix!
99     ] define-core-syntax
100
101     "P\"" [
102         lexer get skip-blank parse-string <pathname> suffix!
103     ] define-core-syntax
104
105     "[" [ parse-quotation suffix! ] define-core-syntax
106     "{" [ \ } [ >array ] parse-literal ] define-core-syntax
107     "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
108     "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
109     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-core-syntax
110     "H{" [ \ } [ parse-hashtable ] parse-literal ] define-core-syntax
111     "T{" [ parse-tuple-literal suffix! ] define-core-syntax
112     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
113     "HS{" [ \ } [ >hash-set ] parse-literal ] define-core-syntax
114
115     "POSTPONE:" [ scan-word suffix! ] define-core-syntax
116     "\\" [ scan-word <wrapper> suffix! ] define-core-syntax
117     "M\\" [ scan-word scan-word lookup-method <wrapper> suffix! ] define-core-syntax
118     "inline" [ last-word make-inline ] define-core-syntax
119     "recursive" [ last-word make-recursive ] define-core-syntax
120     "foldable" [ last-word make-foldable ] define-core-syntax
121     "flushable" [ last-word make-flushable ] define-core-syntax
122     "delimiter" [ last-word t "delimiter" set-word-prop ] define-core-syntax
123     "deprecated" [ last-word make-deprecated ] define-core-syntax
124
125     "SYNTAX:" [
126         scan-new-word
127         mark-top-level-syntax
128         parse-definition define-syntax
129     ] define-core-syntax
130
131     "BUILTIN:" [
132         scan-word-name
133         current-vocab lookup-word
134         (parse-tuple-definition) 2drop check-builtin
135     ] define-core-syntax
136
137     "SYMBOL:" [
138         scan-new-word define-symbol
139     ] define-core-syntax
140
141     "SYMBOLS:" [
142         ";" [ create-word-in [ reset-generic ] [ define-symbol ] bi ] each-token
143     ] define-core-syntax
144
145     "SINGLETONS:" [
146         ";" [ create-class-in define-singleton-class ] each-token
147     ] define-core-syntax
148
149     "DEFER:" [
150         scan-token current-vocab create-word
151         [ fake-definition ] [ set-last-word ] [ undefined-def define ] tri
152     ] define-core-syntax
153
154     "ALIAS:" [
155         scan-new-word scan-word define-alias
156     ] define-core-syntax
157
158     "CONSTANT:" [
159         scan-new-word scan-object define-constant
160     ] define-core-syntax
161
162     ":" [
163         (:) define-declared
164     ] define-core-syntax
165
166     "GENERIC:" [
167         [ simple-combination ] (GENERIC:)
168     ] define-core-syntax
169
170     "GENERIC#" [
171         [ scan-number <standard-combination> ] (GENERIC:)
172     ] define-core-syntax
173
174     "MATH:" [
175         [ math-combination ] (GENERIC:)
176     ] define-core-syntax
177
178     "HOOK:" [
179         [ scan-word <hook-combination> ] (GENERIC:)
180     ] define-core-syntax
181
182     "M:" [
183         (M:) define
184     ] define-core-syntax
185
186     "UNION:" [
187         scan-new-class parse-definition define-union-class
188     ] define-core-syntax
189
190     "INTERSECTION:" [
191         scan-new-class parse-definition define-intersection-class
192     ] define-core-syntax
193
194     "MIXIN:" [
195         scan-new-class define-mixin-class
196     ] define-core-syntax
197
198     "INSTANCE:" [
199         location [
200             scan-word scan-word 2dup add-mixin-instance
201             <mixin-instance>
202         ] dip remember-definition
203     ] define-core-syntax
204
205     "PREDICATE:" [
206         scan-new-class
207         "<" expect
208         scan-class
209         parse-definition define-predicate-class
210     ] define-core-syntax
211
212     "SINGLETON:" [
213         scan-new-class define-singleton-class
214     ] define-core-syntax
215
216     "TUPLE:" [
217         parse-tuple-definition define-tuple-class
218     ] define-core-syntax
219
220     "final" [
221         last-word make-final
222     ] define-core-syntax
223
224     "SLOT:" [
225         scan-token define-protocol-slot
226     ] define-core-syntax
227
228     "C:" [
229         scan-new-word scan-word define-boa-word
230     ] define-core-syntax
231
232     "ERROR:" [
233         parse-tuple-definition
234         pick save-location
235         define-error-class
236     ] define-core-syntax
237
238     "FORGET:" [
239         scan-object forget
240     ] define-core-syntax
241
242     "(" [
243         ")" parse-effect suffix!
244     ] define-core-syntax
245
246     "MAIN:" [
247         scan-word
248         dup ( -- ) check-stack-effect
249         [ current-vocab main<< ]
250         [ current-source-file get [ main<< ] [ drop ] if* ] bi
251     ] define-core-syntax
252
253     "<<" [
254         [
255             \ >> parse-until >quotation
256         ] with-nested-compilation-unit call( -- )
257     ] define-core-syntax
258
259     "call-next-method" [
260         current-method get [
261             literalize suffix!
262             \ (call-next-method) suffix!
263         ] [
264             not-in-a-method-error
265         ] if*
266     ] define-core-syntax
267
268     "maybe{" [
269         \ } [ <anonymous-union> <maybe> ] parse-literal
270     ] define-core-syntax
271
272     "not{" [
273         \ } [ <anonymous-union> <anonymous-complement> ] parse-literal
274     ] define-core-syntax
275
276     "intersection{" [
277          \ } [ <anonymous-intersection> ] parse-literal
278     ] define-core-syntax
279
280     "union{" [
281         \ } [ <anonymous-union> ] parse-literal
282     ] define-core-syntax
283
284     "initial:" "syntax" lookup-word define-symbol
285
286     "read-only" "syntax" lookup-word define-symbol
287
288     "call(" [ \ call-effect parse-call-paren ] define-core-syntax
289
290     "execute(" [ \ execute-effect parse-call-paren ] define-core-syntax
291
292     "<<<<<<<" [ version-control-merge-conflict ] define-core-syntax
293     "=======" [ version-control-merge-conflict ] define-core-syntax
294     ">>>>>>>" [ version-control-merge-conflict ] define-core-syntax
295
296     "<<<<<<" [ version-control-merge-conflict ] define-core-syntax
297     "======" [ version-control-merge-conflict ] define-core-syntax
298     ">>>>>>" [ version-control-merge-conflict ] define-core-syntax
299 ] with-compilation-unit