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