]> gitweb.factorcode.org Git - factor.git/blob - library/platform/native/parse-syntax.factor
working on the test suite
[factor.git] / library / platform / native / parse-syntax.factor
1 ! :folding=indent:collapseFolds=1:
2
3 ! $Id$
4 !
5 ! Copyright (C) 2004 Slava Pestov.
6
7 ! Redistribution and use in source and binary forms, with or without
8 ! modification, are permitted provided that the following conditions are met:
9
10 ! 1. Redistributions of source code must retain the above copyright notice,
11 !    this list of conditions and the following disclaimer.
12
13 ! 2. Redistributions in binary form must reproduce the above copyright notice,
14 !    this list of conditions and the following disclaimer in the documentation
15 !    and/or other materials provided with the distribution.
16
17 ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 ! Parsing words. 'builtins' is a stupid vocabulary name now
29 ! that it does not contain Java words anymore!
30 IN: builtins
31
32 USE: arithmetic
33 USE: combinators
34 USE: cross-compiler
35 USE: errors
36 USE: kernel
37 USE: lists
38 USE: logic
39 USE: namespaces
40 USE: parser
41 USE: stack
42 USE: strings
43 USE: words
44 USE: vectors
45 USE: vocabularies
46 USE: unparser
47
48 ! Constants
49 : t t parsed ; parsing
50 : f f parsed ; parsing
51
52 ! Lists
53 : [ [ ] ; parsing
54 : ] nreverse parsed ; parsing
55
56 : | ( syntax: | cdr ] )
57     #! See the word 'parsed'. We push a special sentinel, and
58     #! 'parsed' acts accordingly.
59     "|" ; parsing
60
61 ! Vectors
62 : { f ; parsing
63 : } nreverse list>vector parsed ; parsing
64
65 ! Do not execute parsing word
66 : POSTPONE: ( -- ) scan parse-word parsed ; parsing
67
68 ! Colon defs
69 : CREATE scan "in" get create ;
70
71 : :
72     #! Begin a word definition. Word name follows.
73     CREATE [ ]  ; parsing
74
75 : ;
76     #! End a word definition.
77     nreverse
78     "cross-compiling" get
79     [ compound, ] [ define ] ifte ; parsing
80
81 ! Vocabularies
82 : DEFER: CREATE drop ; parsing
83 : USE: scan "use" cons@ ; parsing
84 : IN: scan dup "use" cons@ "in" set ; parsing
85
86 ! \x
87 : unicode-escape ( -- esc )
88     #! Read \u....
89     next-ch digit> 16 *
90     next-ch digit> + 16 *
91     next-ch digit> + 16 *
92     next-ch digit> + ;
93
94 : ascii-escape ( ch -- esc )
95     [
96         [ CHAR: e | CHAR: \e ]
97         [ CHAR: n | CHAR: \n ]
98         [ CHAR: r | CHAR: \r ]
99         [ CHAR: t | CHAR: \t ]
100         [ CHAR: s | CHAR: \s ]
101         [ CHAR: \s | CHAR: \s ]
102         [ CHAR: 0 | CHAR: \0 ]
103         [ CHAR: \\ | CHAR: \\ ]
104         [ CHAR: \" | CHAR: \" ]
105     ] assoc ;
106
107 : escape ( ch -- esc )
108     dup CHAR: u = [
109         drop unicode-escape
110     ] [
111         ascii-escape
112     ] ifte ;
113
114 ! String literal
115
116 : parse-escape ( -- )
117     next-ch escape dup [ drop "Bad escape" throw ] unless ;
118
119 : parse-ch ( ch -- ch )
120     dup CHAR: \\ = [ drop parse-escape ] when ;
121
122 : parse-string ( -- )
123     next-ch dup CHAR: " = [
124         drop
125     ] [
126         parse-ch % parse-string
127     ] ifte ;
128
129 : "
130     #! Note the ugly hack to carry the new value of 'pos' from
131     #! the <% %> scope up to the original scope.
132     <% parse-string "pos" get %> swap "pos" set parsed ; parsing
133
134 ! Char literal
135 : CHAR: ( -- ) skip-blank next-ch parse-ch parsed ; parsing
136
137 ! Comments
138 : ( ")" until drop ; parsing
139 : ! until-eol drop ; parsing
140 : #! until-eol drop ; parsing
141
142 ! Reading numbers in other bases
143
144 : BASE: ( base -- )
145     #! Read a number in a specific base.
146     "base" get >r "base" set scan number, r> "base" set ;
147
148 : HEX: 16 BASE: ; parsing
149 : DEC: 10 BASE: ; parsing
150 : OCT: 8 BASE: ; parsing
151 : BIN: 2 BASE: ; parsing