]> gitweb.factorcode.org Git - factor.git/blob - basis/multiline/multiline.factor
multiline: pass the lexer around instead of using namespaces all the time.
[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>> ] bi ;
16
17 : (parse-here) ( lexer -- )
18     dup next-line-text [
19         dup ";" =
20         [ drop next-line ]
21         [ % CHAR: \n , (parse-here) ] if
22     ] [ drop ";" 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 : lexer-eof? ( lexer -- ? )
43     [ line>> ] [ text>> length ] bi <= ;
44
45 :: (scan-multiline-string) ( i end lexer -- j )
46     lexer line-text>> :> text
47     lexer lexer-eof? [
48         end text i start* [| j |
49             i j text subseq % j end length +
50         ] [
51             text i short tail % CHAR: \n ,
52             lexer next-line
53             0 end lexer (scan-multiline-string)
54         ] if*
55     ] [ end throw-unexpected-eof ] if ;
56
57 :: (parse-multiline-string) ( end-text lexer skip-n-chars -- str )
58     [
59         lexer
60         [ skip-n-chars + end-text lexer (scan-multiline-string) ]
61         change-column drop
62     ] "" make ;
63
64 : advance-same-line ( lexer text -- )
65     length [ + ] curry change-column drop ;
66
67 :: (parse-til-line-begins) ( begin-text lexer -- )
68     lexer still-parsing? [
69         lexer line-text>> begin-text sequence= [
70             lexer begin-text advance-same-line
71         ] [
72             lexer line-text>> % CHAR: \n ,
73             lexer next-line
74             begin-text lexer (parse-til-line-begins)
75         ] if
76     ] [
77         begin-text bad-heredoc
78     ] if ;
79
80 : parse-til-line-begins ( begin-text lexer -- seq )
81     [ (parse-til-line-begins) ] "" make ;
82
83 PRIVATE>
84
85 : parse-multiline-string ( end-text -- str )
86     lexer get 1 (parse-multiline-string) ;
87
88 SYNTAX: /* "*/" parse-multiline-string drop ;
89
90 SYNTAX: HEREDOC:
91     lexer get {
92         [ skip-blank ]
93         [ rest-of-line ]
94         [ next-line ]
95         [ parse-til-line-begins ]
96     } cleave suffix! ;
97
98 SYNTAX: DELIMITED:
99     lexer get {
100         [ skip-blank ]
101         [ rest-of-line ]
102         [ next-line ]
103         [ 0 (parse-multiline-string) ]
104     } cleave suffix! ;