1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays byte-arrays definitions generic
4 hashtables kernel math namespaces parser lexer sequences strings
5 strings.parser sbufs vectors words words.symbol words.constant
6 words.alias quotations io assocs splitting classes.tuple
7 generic.standard generic.hook generic.math generic.parser classes
8 io.pathnames vocabs vocabs.parser classes.parser classes.union
9 classes.intersection classes.mixin classes.predicate
10 classes.singleton classes.tuple.parser compiler.units
11 combinators effects.parser slots ;
14 ! These words are defined as a top-level form, instead of with
15 ! defining parsing words, because during stage1 bootstrap, the
16 ! "syntax" vocabulary is copied from the host. When stage1
17 ! bootstrap completes, the host's syntax vocabulary is deleted
18 ! from the target, then this top-level form creates the
19 ! target's "syntax" vocabulary as one of the first things done
22 : define-delimiter ( name -- )
23 "syntax" lookup t "delimiter" set-word-prop ;
25 : define-core-syntax ( name quot -- )
26 [ dup "syntax" lookup [ ] [ no-word-error ] ?if ] dip
30 { "]" "}" ";" ">>" } [ define-delimiter ] each
33 "Primitive definition is not supported" throw
37 "Call stack literals are not supported" throw
40 "!" [ lexer get next-line ] define-core-syntax
42 "#!" [ POSTPONE: ! ] define-core-syntax
44 "IN:" [ scan set-in ] define-core-syntax
46 "PRIVATE>" [ in get ".private" ?tail drop set-in ] define-core-syntax
49 POSTPONE: PRIVATE> in get ".private" append set-in
52 "USE:" [ scan use+ ] define-core-syntax
54 "USING:" [ ";" parse-tokens add-use ] define-core-syntax
56 "QUALIFIED:" [ scan dup add-qualified ] define-core-syntax
58 "QUALIFIED-WITH:" [ scan scan add-qualified ] define-core-syntax
61 scan "=>" expect ";" parse-tokens swap add-words-from
65 scan "=>" expect ";" parse-tokens swap add-words-excluding
69 scan scan "=>" expect scan add-renamed-word
72 "HEX:" [ 16 parse-base ] define-core-syntax
73 "OCT:" [ 8 parse-base ] define-core-syntax
74 "BIN:" [ 2 parse-base ] define-core-syntax
76 "f" [ f parsed ] define-core-syntax
77 "t" "syntax" lookup define-singleton-class
81 { [ dup length 1 = ] [ first ] }
82 { [ "\\" ?head ] [ next-escape >string "" assert= ] }
83 [ name>char-hook get call( name -- char ) ]
87 "\"" [ parse-string parsed ] define-core-syntax
90 lexer get skip-blank parse-string >sbuf parsed
94 lexer get skip-blank parse-string <pathname> parsed
97 "[" [ parse-quotation parsed ] define-core-syntax
98 "{" [ \ } [ >array ] parse-literal ] define-core-syntax
99 "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
100 "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
101 "H{" [ \ } [ >hashtable ] parse-literal ] define-core-syntax
102 "T{" [ parse-tuple-literal parsed ] define-core-syntax
103 "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
105 "POSTPONE:" [ scan-word parsed ] define-core-syntax
106 "\\" [ scan-word <wrapper> parsed ] define-core-syntax
107 "M\\" [ scan-word scan-word method <wrapper> parsed ] define-core-syntax
108 "inline" [ word make-inline ] define-core-syntax
109 "recursive" [ word make-recursive ] define-core-syntax
110 "foldable" [ word make-foldable ] define-core-syntax
111 "flushable" [ word make-flushable ] define-core-syntax
112 "delimiter" [ word t "delimiter" set-word-prop ] define-core-syntax
115 CREATE-WORD parse-definition define-syntax
119 CREATE-WORD define-symbol
124 [ create-in dup reset-generic define-symbol ] each
129 [ create-class-in define-singleton-class ] each
133 scan current-vocab create
134 [ fake-definition ] [ set-word ] [ [ undefined ] define ] tri
138 CREATE-WORD scan-word define-alias
142 CREATE-WORD scan-object define-constant
150 [ simple-combination ] (GENERIC:)
154 [ scan-word <standard-combination> ] (GENERIC:)
158 [ math-combination ] (GENERIC:)
162 [ scan-word <hook-combination> ] (GENERIC:)
170 CREATE-CLASS parse-definition define-union-class
174 CREATE-CLASS parse-definition define-intersection-class
178 CREATE-CLASS define-mixin-class
183 scan-word scan-word 2dup add-mixin-instance
185 ] dip remember-definition
192 parse-definition define-predicate-class
196 CREATE-CLASS define-singleton-class
200 parse-tuple-definition define-tuple-class
204 scan define-protocol-slot
208 CREATE-WORD scan-word define-boa-word
212 parse-tuple-definition
222 ")" parse-effect drop
226 "))" parse-effect parsed
229 "MAIN:" [ scan-word in get vocab (>>main) ] define-core-syntax
233 \ >> parse-until >quotation
234 ] with-nested-compilation-unit call( -- )
240 \ (call-next-method) parsed
242 not-in-a-method-error
246 "initial:" "syntax" lookup define-symbol
248 "read-only" "syntax" lookup define-symbol
250 "call(" [ \ call-effect parse-call( ] define-core-syntax
252 "execute(" [ \ execute-effect parse-call( ] define-core-syntax
253 ] with-compilation-unit