1 ! Copyright (C) 2017 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators.short-circuit
4 constructors continuations io io.encodings.utf8 io.files
5 io.streams.string kernel modern modern.paths modern.slices
6 multiline prettyprint sequences splitting strings vocabs.loader ;
10 { [ slice? ] [ seq>> string? ] } 1&& ;
12 TUPLE: renamed slice string ;
13 CONSTRUCTOR: <renamed> renamed ( slice string -- obj ) ;
15 : trim-before-newline ( seq -- seq' )
16 dup [ CHAR: \s = not ] find
17 { CHAR: \r CHAR: \n } member?
18 [ tail-slice ] [ drop ] if ;
20 : write-whitespace ( last obj -- )
22 [ swap slice-between ] [ slice-before ] if*
23 trim-before-newline io:write ;
25 GENERIC: write-literal* ( last obj -- last' )
26 M: slice write-literal* [ write-whitespace ] [ write ] [ ] tri ;
27 M: array write-literal* [ write-literal* ] each ;
28 M: renamed write-literal* [ slice>> write-whitespace ] [ string>> write ] [ slice>> ] tri ; ! for refactoring
33 : (map-literals) ( obj quot: ( obj -- obj' ) -- seq )
34 over [ array? ] any? [
35 [ call drop ] [ map-literals ] 2bi
37 over array? [ map-literals ] [ call ] if
38 ] if ; inline recursive
40 : map-literals ( obj quot: ( obj -- obj' ) -- seq )
41 '[ _ (map-literals) ] map ; inline recursive
45 ! Start with no slice as ``last``
46 : write-literal ( obj -- ) f swap write-literal* drop ;
48 : write-modern-string ( seq -- string )
49 [ write-literal ] with-string-writer ; inline
51 : write-modern-path ( seq path -- )
52 utf8 [ write-literal nl ] with-file-writer ; inline
54 : write-modern-vocab ( seq vocab -- )
55 vocab-source-path write-modern-path ; inline
57 : rewrite-path ( path quot: ( obj -- obj' ) -- )
59 '[ [ path>literals _ map-literals ] [ ] bi write-modern-path ]
60 [ drop . ] recover ; inline recursive
62 : rewrite-string ( string quot: ( obj -- obj' ) -- )
64 [ string>literals ] dip map-literals write-modern-string ; inline recursive
66 : rewrite-paths ( seq quot: ( obj -- obj' ) -- ) '[ _ rewrite-path ] each ; inline recursive
68 : rewrite-vocab ( vocab quot: ( obj -- obj' ) -- )
69 [ [ vocab>literals ] dip map-literals ] keepd write-modern-vocab ; inline recursive
71 : rewrite-string-exact ( string -- string' )
72 string>literals write-modern-string ;
75 : rewrite-path-exact ( path -- )
76 [ path>literals ] [ ] bi write-modern-path ;
78 : rewrite-vocab-exact ( name -- )
79 vocab-source-path rewrite-path-exact ;
81 : rewrite-paths ( paths -- )
82 [ rewrite-path-exact ] each ;
85 : strings-core-to-file ( -- )
87 [ ".private" ?tail drop vocab-source-path utf8 file-contents ] zip-with
88 [ "[========[" dup matching-delimiter-string surround ] assoc-map
90 first2 [ "VOCAB: " prepend ] dip " " glue
92 [ " " prepend ] map "\n\n" join
93 "<VOCAB-ROOT: factorcode-core \"https://factorcode.org/git/factor.git\" \"core/\"\n"
94 "\n;VOCAB-ROOT>" surround "resource:core-strings.factor" utf8 set-file-contents ;
96 : parsed-core-to-file ( -- )
98 [ vocab>literals ] zip-with
100 first2 [ "<VOCAB: " prepend ] dip
102 ! [ 3 head ] [ 3 tail* ] bi [ >strings ] bi@ { "..." } glue
106 { "<VOCAB-ROOT:" "factorcode-core" "https://factorcode.org/git/factor.git" "core/" }
107 { ";VOCAB-ROOT>" } surround "resource:core-parsed.factor" utf8 [ ... ] with-file-writer ;