1
1
REBOL [
2
- Title: "Prebol - Official REBOL Preprocessor"
3
- Version: 1.1.3
2
+ Name: prebol
3
+ Type: module
4
+ Exports: [process-source]
5
+ Title: "Prebol - Official REBOL Preprocessor"
6
+ Version: 1.1.4
4
7
Author: ["Carl Sassenrath" "Holger Kruse" "Oldes" ]
5
8
Purpose: {
6
9
The official REBOL preprocessor. Combines multiple
@@ -49,63 +52,35 @@ REBOL [
49
52
"Added #include-block command"
50
53
"Includes are described in the result code using comment"
51
54
]
52
- ]
53
- Example: [
54
- "With requester" [
55
- set [in-file out-file] system/script/args
56
-
57
- if view? [
58
- if none? in-file [
59
- in-file: request-file/title "Input file?" "Open"
60
- if none? in-file [quit]
61
- in-file: in-file/1
62
- ]
63
- if none? out-file [
64
- out-file: request-file/title/save "Output file?" "Save"
65
- if none? out-file [quit]
66
- out-file: out-file/1
67
- ]
68
- ]
69
-
70
- if not all [
71
- msg: "Missing input file name argument"
72
- in-file
73
- msg: "Input file not found"
74
- exists? in-file
75
- msg: "Missing output file name argument"
76
- out-file
77
- msg: "Input file is not a REBOL file"
78
- script? in-file
79
- msg: ["Cannot load REBOL input file" mold clean-path in-file]
80
- not error? try [data: load/all in-file]
81
- ][
82
- error msg
83
- ]
84
-
85
- size: process-source data size? in-file
86
- save out-file head data
87
- print ["Processed:" size "bytes" ]
55
+ 5-Mar-2021 "Oldes" [
56
+ "Updated for use with current Rebol version"
88
57
]
89
58
]
90
59
]
91
60
92
61
system/options/binary-base: 64 ; output data in base-64
93
62
94
- ;vn
95
- ;error: func [msg] [print msg halt]
96
- error : func [ [catch] msg] [throw make error! reform msg]
63
+ error : func [ msg] [
64
+ if block? msg [msg: reform msg]
65
+ sys/log/error 'prebol msg
66
+ halt
67
+ ]
97
68
98
- process -source-comment?: true ;adds comments before included content
69
+ include -source-comment?: true ;adds comments before included content
99
70
process-source : func [
100
71
; Process REBOL source code block. Modifies the block. Returns size.
101
72
blk [any-block! ] "Block of source to process"
102
73
size [integer! ] "Starting size"
74
+ /only "Don't use recursive processing"
103
75
/local file data expr cmd else tmp path include-cmds header do-expr
104
76
] [
105
77
do-expr : func [ expr /local result] [
106
78
; Evaluate expression and make sure it returns a result.
107
- if unset? set /any 'result do expr [
108
- print ["***" cmd "must return a value or none:" mold expr]
79
+ set /any 'result try [do :expr ]
80
+ if function? :result [set /any 'result try [do :result ]]
81
+ case [
82
+ unset? :result [ error [mold cmd "must return a value or none:" mold expr] ]
83
+ error? :result [ error result ]
109
84
]
110
85
:result
111
86
]
@@ -120,7 +95,7 @@ process-source: func [
120
95
]
121
96
header: make header data/2
122
97
remove/part data 2
123
- if process -source-comment? [
98
+ if include -source-comment? [
124
99
insert data compose [
125
100
comment (rejoin [{
126
101
#### Include: } mold file {
@@ -150,11 +125,9 @@ comment (rejoin [{---- end of include } mold file { ----}])
150
125
set /any 'data do file ;file is not a file but a block to evaluate!!!
151
126
head insert /only copy [] data ; return it
152
127
]
153
-
154
128
]
155
129
156
130
while [not tail? blk][
157
-
158
131
; Source pragmas begin with # (they are of the ISSUE datatype)
159
132
; and are followed by a filename, a block, or a paren.
160
133
either issue? blk/1 [
@@ -168,22 +141,19 @@ comment (rejoin [{---- end of include } mold file { ----}])
168
141
169
142
; Expression such as #include (join %file num: num + 1)
170
143
if paren? :file [
171
- file: do-expr reduce [file] ; for older REBOL compatibility
144
+ file: do-expr to block! :file
172
145
]
173
146
174
147
; Include requires a file argument:
175
- if not file? file [error ["*** Invalid" cmd "file expression:" mold file]]
148
+ if not file? file [error ["Invalid" mold cmd "file expression:" mold file]]
176
149
177
150
; File must exist:
178
- if not exists? file [error ["***" cmd "file not found:" mold file]]
151
+ if not exists? file [error [mold cmd "file not found:" mold file]]
179
152
180
153
size: size + size? file ; for stats only
181
154
182
155
; Execute the include:
183
- if error? data: try select include-cmds cmd [
184
- data: disarm :data
185
- error ["***" cmd file "error:" data/id "at" mold data/near ]
186
- ]
156
+ if error? data: try select include-cmds cmd [ error data ]
187
157
; Replace include command with contents of file:
188
158
remove/part blk 2
189
159
insert blk data
@@ -222,7 +192,7 @@ comment (rejoin [{---- end of include } mold file { ----}])
222
192
insert /only blk tmp
223
193
]
224
194
#comments [ ; #comments true/false
225
- process -source-comment?: either find [false off] blk/2 [false][true]
195
+ include -source-comment?: either find [false off] blk/2 [false][true]
226
196
blk: skip blk 2
227
197
]
228
198
][
@@ -234,12 +204,10 @@ comment (rejoin [{---- end of include } mold file { ----}])
234
204
blk: next blk
235
205
]
236
206
]
237
-
238
- foreach item head blk [
239
- if block? :item [size: process-source item size]
207
+ unless only [
208
+ foreach item head blk [
209
+ if block? :item [size: process-source item size]
210
+ ]
240
211
]
241
-
242
212
size
243
213
]
244
-
245
-
0 commit comments