Skip to content

Instantly share code, notes, and snippets.

@ruv
Last active November 18, 2020 08:24
Show Gist options
  • Save ruv/60450afc33a7ad0f614bedefe4841ddb to your computer and use it in GitHub Desktop.
Save ruv/60450afc33a7ad0f614bedefe4841ddb to your computer and use it in GitHub Desktop.
A recognizer for multi-line string literal
\ 2020-06-27 -- initial version
\ 2020-08-30 -- update
\ 2020-11-14 -- publish
require ffl/str.fs \ http://irdvo.nl/FFL/docs/str.html
[undefined] slit, [if] : slit, ( c-addr u -- ) postpone sliteral ; [then]
[undefined] tt-slit [if] str-new value _tmp_s
: tt-slit ( c-addr u -- c-addr2 u | )
state @ if slit, exit then
over source over + within if _tmp_s str-set _tmp_s str-get exit then
;
[then]
str-new value _tmp_s_ml
: delim ( -- c-addr u ) s\" \"\"\"" ; \ triple quote sign: """
: (parse-ml) ( c-addr1 u1 "ccc" -- c-addr2 u2 )
2dup + >r _tmp_s_ml str-set
source >in @ /string over 1- r> = if
\ include the delimiter if the left and right parts are adjacent with a single delimiter.
-1 /string
then
begin ( c-addr u ) delim split- >r _tmp_s_ml str-append-string r> 0= while
$A 1 _tmp_s_ml str-append-chars refill while source
repeat else ( c-addr2 u2 ) drop source drop - >in ! then
_tmp_s_ml str-get
;
: recognize-string-literal-multiline ( c-addr u -- k*x xt-tt )
delim match-head 0= if 2drop 0 exit then
delim split if nip 0= if ['] tt-slit exit then 2drop 0 exit then
[: (parse-ml) tt-slit ;] ( c-addr u xt )
;
\ For testing, let's place this new recognizer before any other
' recognize-string-literal-multiline set-perceptor-before
( usage example:
"""multi
line
string""" type
)
@ruv
Copy link
Author

ruv commented Nov 14, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment