Skip to content

Commit f3ba29a

Browse files
committed
Have ." and s" parse single-line, and the string recognizer parse multiline
1 parent 7e5bef9 commit f3ba29a

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

debugs.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,8 @@ s" You've reached a !!FIXME!! marker" exception constant FIXME#
137137
\G word that should never be reached
138138
FIXME# throw ;
139139

140-
141140
\ warn beginners that double numbers clash with floating points
142141

143-
[IFUNDEF] ?warning \ fix compilation problem
144-
Defer ?warning
145-
[THEN]
146-
147142
:is ?warning ( f xt -- )
148143
\ if f, output a warning by EXECUTEing xt
149144
swap warnings @ 0<> and if
@@ -196,6 +191,11 @@ s" You've reached a !!FIXME!! marker" exception constant FIXME#
196191

197192
' ?warn-dp is ?warn#
198193

194+
\ eof warning
195+
196+
:is eof-warning ( -- )
197+
state @ [: ." EOF reached while " get-state id. ;] ?warning ;
198+
199199
\ replacing one word with another
200200

201201
: >colon-body ( xt -- addr )

gforth.el

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ PARSED-TYPE specifies what kind of text is parsed. It should be
328328
("\\([&#]-?[0-9a-f.]+\\|\\(0x-?\\|\\$-?\\)[0-9a-f.]+\\|%-?[01]+\\)"
329329
immediate (font-lock-constant-face . 3))
330330
("\"[^\"]**" immediate (font-lock-string-face . 1)
331-
"[\"\n]" nil string (font-lock-string-face . 1))
332-
("\".*\""
331+
"[\"]" nil string (font-lock-string-face . 1))
332+
("\"[[:ascii:][:nonascii:]]*?\""
333333
immediate (font-lock-string-face . 3))
334334
("[a-z\-0-9]+(" immediate (font-lock-comment-face . 1)
335335
"[)]" nil comment (font-lock-comment-face . 1))

kernel/input.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,15 @@ defer line-end-hook ( -- ) \ gforth
200200
\G called at every end-of-line when text-interpreting from a file
201201
\ alternatively we could use a wrapper for REFILL
202202
' noop is line-end-hook
203+
Defer eof-warning
204+
' noop is eof-warning
203205

204206
: read-loop1 ( i*x -- j*x )
205207
BEGIN refill WHILE interpret line-end-hook REPEAT ;
206208

207209
: read-loop ( i*x -- j*x ) \ gforth-internal
208210
\G refill and interpret a file until EOF
209-
['] read-loop1 bt-rp0-wrapper
210-
state @ warning" EOF reached while compiling" ;
211+
['] read-loop1 bt-rp0-wrapper eof-warning ;
211212

212213
Variable second-ctrl-c 0 second-ctrl-c !
213214

quotes.fs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,35 @@ create \-escape-table
8585
endif
8686
c, char+ ;
8787

88-
Defer string-lineend
8988
$Variable mlstringpos
9089

9190
s" End of string expected" exception >r
9291

93-
: singleline-strings ( -- ) \ gforth-experimental
94-
\G set strings to end within a line (default).
95-
[: ( -- never ) [ r@ ]L throw ;] is string-lineend ;
96-
97-
: multiline-strings ( -- ) \ gforth-experimental
98-
\G set strings to span multiple lines
99-
[: ( -- parse-area parse-end ) #lf c,
100-
source-id 0= IF
101-
success-color ." string" default-color cr
102-
input-color THEN
103-
refill IF source ELSE
104-
mlstringpos get-stack 2 - -rot 2>r restore-input drop
105-
2r> source drop + swap input-lexeme! [ r> ]L throw THEN
106-
over + ;] is string-lineend ;
107-
108-
singleline-strings
109-
110-
: \"-parse ( "string"<"> -- c-addr u ) \ gforth-internal backslash-quote-parse
92+
: singleline-string ( -- never ) \ gforth-experimental
93+
\G throw exception when string reaches the end of a line
94+
[ r@ ]L throw ;
95+
96+
: multiline-string ( -- parse-area parse-end ) \ gforth-experimental
97+
\G parses multiline strings
98+
#lf c,
99+
source-id 0= IF
100+
success-color ." string" default-color cr
101+
input-color THEN
102+
refill IF
103+
source
104+
\ skip auto-indent blanks
105+
mlstringpos stack> dup mlstringpos >stack
106+
0 U+DO over c@ bl = IF 1 safe/string THEN LOOP
107+
ELSE
108+
mlstringpos get-stack 2 - -rot 2>r restore-input drop
109+
2r> source drop + swap input-lexeme! [ r> ]L throw THEN
110+
over + ;
111+
112+
: \"-parse ( "string"<"> xt -- c-addr u ) \ gforth-internal backslash-quote-parse
111113
\G parses string, translating @code{\}-escapes to characters (as in
112114
\G C). The resulting string resides at @code{here}. See @code{S\"}
113115
\G for the supported @code{\-escapes}.
114-
here >r
116+
{ xt: string-lineend } here >r
115117
save-input input-lexeme 2@ swap source drop - rot 2 + mlstringpos set-stack
116118
>in @ chars source chars over + >r + begin ( parse-area R: here parse-end )
117119
dup r@ u>= IF
@@ -130,8 +132,8 @@ singleline-strings
130132
here r> - dup negate allot
131133
here swap char/ ;
132134

133-
:noname \"-parse save-mem ;
134-
:noname \"-parse save-mem 2dup postpone sliteral drop free throw ;
135+
:noname ['] singleline-string \"-parse save-mem ;
136+
:noname ['] singleline-string \"-parse save-mem 2dup postpone sliteral drop free throw ;
135137
interpret/compile: s\" ( Interpretation 'ccc"' -- c-addr u ) \ core-ext,file-ext s-backslash-quote
136138
\G Interpretation: Parse the string @i{ccc} delimited by a @code{"}
137139
\G (but not @code{\"}), and convert escaped characters as described
@@ -146,7 +148,7 @@ interpret/compile: s\" ( Interpretation 'ccc"' -- c-addr u ) \ core-ext,file-ext
146148
\G Run-time @code{( -- c-addr u )}: Push a descriptor for the
147149
\G resulting string.
148150

149-
:noname \"-parse type ;
151+
:noname ['] singleline-string \"-parse type ;
150152
:noname postpone s\" postpone type ;
151153
interpret/compile: .\" ( compilation 'ccc"' -- ; run-time -- ) \ gforth dot-backslash-quote
152154
\G Like @code{."}, but translates C-like \-escape-sequences (see

rec-string.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ s" Scanned string not in input buffer" exception >r
2626

2727
: scan-string ( addr u -- addr' u' )
2828
2dup ?in-inbuf
29-
drop source drop - 1+ >in ! \"-parse save-mem ;
29+
drop source drop - 1+ >in !
30+
['] multiline-string \"-parse save-mem ;
3031

3132
: slit, postpone sliteral ;
3233

0 commit comments

Comments
 (0)