@@ -10,6 +10,19 @@ import {always, never} from "../lib/combinators.mjs";
10
10
11
11
export function list_into ( Type ) {
12
12
switch ( Type ) {
13
+ case FTL . BaseComment :
14
+ return ( [ sigil , content = "" ] ) => {
15
+ switch ( sigil ) {
16
+ case "#" :
17
+ return always ( new FTL . Comment ( content ) ) ;
18
+ case "##" :
19
+ return always ( new FTL . GroupComment ( content ) ) ;
20
+ case "###" :
21
+ return always ( new FTL . ResourceComment ( content ) ) ;
22
+ default :
23
+ return never ( `Unknown comment sigil: ${ sigil } ` ) ;
24
+ }
25
+ } ;
13
26
case FTL . CallExpression :
14
27
return ( [ callee , args ] ) => {
15
28
let positional_args = [ ] ;
@@ -32,9 +45,6 @@ export function list_into(Type) {
32
45
let named_args = Array . from ( named_map . values ( ) ) ;
33
46
return always ( new Type ( callee , positional_args , named_args ) ) ;
34
47
} ;
35
- case FTL . Message :
36
- return ( [ comment , ...args ] ) =>
37
- always ( new Type ( ...args , comment ) ) ;
38
48
case FTL . Pattern :
39
49
return elements =>
40
50
always ( new FTL . Pattern (
@@ -46,7 +56,12 @@ export function list_into(Type) {
46
56
return entries =>
47
57
always ( new FTL . Resource (
48
58
entries
49
- . reduce ( join_adjacent ( FTL . Junk ) , [ ] )
59
+ . reduce ( join_adjacent (
60
+ FTL . Junk ,
61
+ FTL . Comment ,
62
+ FTL . GroupComment ,
63
+ FTL . ResourceComment ) , [ ] )
64
+ . reduce ( attach_comments , [ ] )
50
65
. filter ( remove_blank_lines ) ) ) ;
51
66
case FTL . SelectExpression :
52
67
return ( [ selector , variants ] ) => {
@@ -68,9 +83,6 @@ export function list_into(Type) {
68
83
}
69
84
return always ( new Type ( selector , variants ) ) ;
70
85
} ;
71
- case FTL . Term :
72
- return ( [ comment , ...args ] ) =>
73
- always ( new Type ( ...args , comment ) ) ;
74
86
case FTL . VariantList :
75
87
return ( [ variants ] ) =>
76
88
always ( new Type ( variants ) ) ;
@@ -82,21 +94,6 @@ export function list_into(Type) {
82
94
83
95
export function into ( Type ) {
84
96
switch ( Type ) {
85
- case FTL . Comment :
86
- case FTL . GroupComment :
87
- case FTL . ResourceComment :
88
- return content => {
89
- if ( ! content . endsWith ( "\n" ) ) {
90
- // The comment ended with the EOF; don't trim it.
91
- return always ( new Type ( content ) ) ;
92
- }
93
- if ( content . endsWith ( "\r\n" ) ) {
94
- // Trim the CRLF from the end of the comment.
95
- return always ( new Type ( content . slice ( 0 , - 2 ) ) ) ;
96
- }
97
- // Trim the LF from the end of the comment.
98
- return always ( new Type ( content . slice ( 0 , - 1 ) ) ) ;
99
- } ;
100
97
case FTL . Placeable :
101
98
return expression => {
102
99
let invalid_expression_found =
@@ -121,15 +118,16 @@ export function into(Type) {
121
118
}
122
119
}
123
120
124
- function join_adjacent ( Type ) {
121
+ function join_adjacent ( ... types ) {
125
122
return function ( acc , cur ) {
126
123
let prev = acc [ acc . length - 1 ] ;
127
- if ( prev instanceof Type && cur instanceof Type ) {
128
- join_of_type ( Type , prev , cur ) ;
129
- return acc ;
130
- } else {
131
- return acc . concat ( cur ) ;
124
+ for ( let Type of types ) {
125
+ if ( prev instanceof Type && cur instanceof Type ) {
126
+ join_of_type ( Type , prev , cur ) ;
127
+ return acc ;
128
+ }
132
129
}
130
+ return acc . concat ( cur ) ;
133
131
} ;
134
132
}
135
133
@@ -139,12 +137,30 @@ function join_of_type(Type, ...elements) {
139
137
case FTL . TextElement :
140
138
return elements . reduce ( ( a , b ) =>
141
139
( a . value += b . value , a ) ) ;
140
+ case FTL . Comment :
141
+ case FTL . GroupComment :
142
+ case FTL . ResourceComment :
143
+ return elements . reduce ( ( a , b ) =>
144
+ ( a . content += `\n${ b . content } ` , a ) ) ;
142
145
case FTL . Junk :
143
146
return elements . reduce ( ( a , b ) =>
144
147
( a . content += b . content , a ) ) ;
145
148
}
146
149
}
147
150
151
+ function attach_comments ( acc , cur ) {
152
+ let prev = acc [ acc . length - 1 ] ;
153
+ if ( prev instanceof FTL . Comment
154
+ && ( cur instanceof FTL . Message
155
+ || cur instanceof FTL . Term ) ) {
156
+ cur . comment = prev ;
157
+ acc [ acc . length - 1 ] = cur ;
158
+ return acc ;
159
+ } else {
160
+ return acc . concat ( cur ) ;
161
+ }
162
+ }
163
+
148
164
function trim_text_at_extremes ( element , index , array ) {
149
165
if ( element instanceof FTL . TextElement ) {
150
166
if ( index === 0 ) {
0 commit comments