]> gitweb.factorcode.org Git - factor.git/blob - basis/multiline/multiline.factor
sequences: rename subsequence? words to subseq? again.
[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 strings.parser
5 strings.parser.private words ;
6 IN: multiline
7
8 <PRIVATE
9
10 : rest-of-line ( lexer -- seq )
11     [ line-text>> ] [ column>> ] bi tail ;
12
13 : next-line-text ( lexer -- str ? )
14     [ next-line ] [ line-text>> ] [ still-parsing? ] tri ;
15
16 : (parse-here) ( lexer -- )
17     dup next-line-text [
18         dup ";" =
19         [ drop next-line ]
20         [ % CHAR: \n , (parse-here) ] if
21     ] [ ";" throw-unexpected-eof ] if ;
22
23 PRIVATE>
24
25 ERROR: text-found-before-eol string ;
26
27 : parse-here ( -- str )
28     [
29         lexer get
30         dup rest-of-line [ text-found-before-eol ] unless-empty
31         (parse-here)
32     ] "" make but-last ;
33
34 SYNTAX: STRING:
35     scan-new-word
36     parse-here 1quotation
37     ( -- string ) define-inline ;
38
39 <PRIVATE
40
41 :: (scan-multiline-string) ( i end lexer -- j )
42     lexer line-text>> :> text
43     lexer still-parsing? [
44         i text end subseq-index-from [| j |
45             i j text subseq % j end length +
46         ] [
47             text i bound tail % CHAR: \n ,
48             lexer next-line
49             0 end lexer (scan-multiline-string)
50         ] if*
51     ] [ end throw-unexpected-eof ] if ;
52
53 :: (parse-multiline-string) ( end-text lexer skip-n-chars -- str )
54     [
55         lexer
56         [ skip-n-chars + end-text lexer (scan-multiline-string) ]
57         change-column check-space
58     ] "" make ;
59
60 : advance-same-line ( lexer text -- )
61     length [ + ] curry change-column drop ;
62
63 PRIVATE>
64
65 : parse-multiline-string ( end-text -- str )
66     lexer get 1 (parse-multiline-string) ;
67
68 SYNTAX: /* "*/" parse-multiline-string drop ;
69
70 SYNTAX: [[ "]]" parse-multiline-string suffix! ;
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
78 SYNTAX: ![[ "]]" parse-multiline-string drop ;
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 ;