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