]> gitweb.factorcode.org Git - factor.git/blob - basis/multiline/multiline.factor
sequences: swap stack arguments for start/start*/subseq?.
[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         text end i start* [| 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 :: (parse-til-line-begins) ( begin-text lexer -- )
65     lexer still-parsing? [
66         lexer line-text>> begin-text sequence= [
67             lexer begin-text advance-same-line
68         ] [
69             lexer line-text>> % CHAR: \n ,
70             lexer next-line
71             begin-text lexer (parse-til-line-begins)
72         ] if
73     ] [
74         begin-text bad-heredoc
75     ] if ;
76
77 : parse-til-line-begins ( begin-text lexer -- seq )
78     [ (parse-til-line-begins) ] "" make ;
79
80 PRIVATE>
81
82 : parse-multiline-string ( end-text -- str )
83     lexer get 1 (parse-multiline-string) ;
84
85 SYNTAX: /* "*/" parse-multiline-string drop ;
86
87 SYNTAX: HEREDOC:
88     lexer get {
89         [ skip-blank ]
90         [ rest-of-line ]
91         [ next-line ]
92         [ parse-til-line-begins ]
93     } cleave suffix! ;