]> gitweb.factorcode.org Git - factor.git/blob - basis/multiline/multiline.factor
multiline: remove HEREDOC:
[factor.git] / basis / multiline / multiline.factor
1 ! Copyright (C) 2007 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators kernel lexer locals make math
4 namespaces parser quotations sequences words ;
5 IN: multiline
6
7 ERROR: bad-heredoc identifier ;
8
9 <PRIVATE
10
11 : rest-of-line ( lexer -- seq )
12     [ line-text>> ] [ column>> ] bi tail ;
13
14 : next-line-text ( lexer -- str ? )
15     [ next-line ] [ line-text>> ] [ still-parsing? ] tri ;
16
17 : (parse-here) ( lexer -- )
18     dup next-line-text [
19         dup ";" =
20         [ drop next-line ]
21         [ % CHAR: \n , (parse-here) ] if
22     ] [ ";" throw-unexpected-eof ] if ;
23
24 PRIVATE>
25
26 ERROR: text-found-before-eol string ;
27
28 : parse-here ( -- str )
29     [
30         lexer get
31         dup rest-of-line [ text-found-before-eol ] unless-empty
32         (parse-here)
33     ] "" make but-last ;
34
35 SYNTAX: STRING:
36     scan-new-word
37     parse-here 1quotation
38     ( -- string ) define-inline ;
39
40 <PRIVATE
41
42 :: (scan-multiline-string) ( i end lexer -- j )
43     lexer line-text>> :> text
44     lexer still-parsing? [
45         end text i subseq-start-from [| j |
46             i j text subseq % j end length +
47         ] [
48             text i short tail % CHAR: \n ,
49             lexer next-line
50             0 end lexer (scan-multiline-string)
51         ] if*
52     ] [ end throw-unexpected-eof ] if ;
53
54 :: (parse-multiline-string) ( end-text lexer skip-n-chars -- str )
55     [
56         lexer
57         [ skip-n-chars + end-text lexer (scan-multiline-string) ]
58         change-column drop
59     ] "" make ;
60
61 : advance-same-line ( lexer text -- )
62     length [ + ] curry change-column drop ;
63
64 PRIVATE>
65
66 : parse-multiline-string ( end-text -- str )
67     lexer get 1 (parse-multiline-string) ;
68
69 SYNTAX: /* "*/" parse-multiline-string drop ;
70
71 SYNTAX: [[ "]]" parse-multiline-string suffix! ;
72 SYNTAX: [=[ "]=]" parse-multiline-string suffix! ;
73 SYNTAX: [==[ "]==]" parse-multiline-string suffix! ;
74 SYNTAX: [===[ "]===]" parse-multiline-string suffix! ;
75 SYNTAX: [====[ "]====]" parse-multiline-string suffix! ;
76 SYNTAX: [=====[ "]=====]" parse-multiline-string suffix! ;
77 SYNTAX: [======[ "]======]" parse-multiline-string suffix! ;
78
79 SYNTAX: ![[ "]]" parse-multiline-string drop ;
80 SYNTAX: ![=[ "]=]" parse-multiline-string drop ;
81 SYNTAX: ![==[ "]==]" parse-multiline-string drop ;
82 SYNTAX: ![===[ "]===]" parse-multiline-string drop ;
83 SYNTAX: ![====[ "]====]" parse-multiline-string drop ;
84 SYNTAX: ![=====[ "]=====]" parse-multiline-string drop ;
85 SYNTAX: ![======[ "]======]" parse-multiline-string drop ;