Skip to content

Commit ab2120e

Browse files
committed
dfmc-reader: Remove code duplication in string literal functions
1 parent bd9fe43 commit ab2120e

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

sources/dfmc/reader/lexer-transitions.dylan

+2-2
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,13 @@ define constant $initial-state :: <state>
548548
#('"' . #"sharp-r-2-double-quotes"),
549549
#(" !#-~" . #"raw-1string"),
550550
pair($ascii-8-bit-extensions, #"raw-1string")),
551-
state(#"sharp-r-2-double-quotes", make-one-line-raw-string-literal,
551+
state(#"sharp-r-2-double-quotes", make-raw-string-literal,
552552
#('"' . #"raw-3string-start")),
553553
state(#"raw-1string", #f, // seen #r" plus one non-" char
554554
#('"' . #"raw-1string-end"),
555555
#(" !#-~" . #"raw-1string"),
556556
pair($ascii-8-bit-extensions, #"raw-1string")),
557-
state(#"raw-1string-end", make-one-line-raw-string-literal),
557+
state(#"raw-1string-end", make-raw-string-literal),
558558
state(#"raw-3string-start", #f, // seen #r"""
559559
#('"' . #"raw-3string-double-quote"),
560560
#(" !#-~\r\n" . #"raw-3string"),

sources/dfmc/reader/lexer.dylan

+18-40
Original file line numberDiff line numberDiff line change
@@ -1173,54 +1173,32 @@ define method make-character-literal
11731173
end));
11741174
end method make-character-literal;
11751175

1176-
define method make-string-literal
1177-
(lexer :: <lexer>, source-location :: <lexer-source-location>)
1176+
define method %make-string-literal
1177+
(lexer :: <lexer>, source-location :: <lexer-source-location>,
1178+
start-offset :: <integer>, end-offset :: <integer>,
1179+
allow-escapes? :: <boolean>)
11781180
=> (res :: <string-fragment>)
1181+
let bpos = source-location.start-posn + start-offset;
1182+
let epos = source-location.end-posn - end-offset;
1183+
let string = decode-string(source-location, bpos, epos, allow-escapes?);
11791184
make(<string-fragment>,
11801185
record: source-location.source-location-record,
11811186
source-position: source-location.source-location-source-position,
11821187
// kind: $string-token,
1183-
value: as-fragment-value(decode-string(source-location,
1184-
source-location.start-posn + 1,
1185-
source-location.end-posn - 1,
1186-
#t)));
1187-
end method make-string-literal;
1188+
value: as-fragment-value(string))
1189+
end method;
11881190

1189-
define method make-multi-line-string-literal
1190-
(lexer :: <lexer>, source-location :: <lexer-source-location>)
1191-
=> (res :: <string-fragment>)
1192-
let bpos = source-location.start-posn + 3; // """
1193-
let epos = source-location.end-posn - 3; // """
1194-
make(<string-fragment>,
1195-
record: source-location.source-location-record,
1196-
source-position: source-location.source-location-source-position,
1197-
// kind: $string-token,
1198-
value: as-fragment-value(decode-string(source-location, bpos, epos, #t)))
1199-
end method make-multi-line-string-literal;
1191+
define constant make-string-literal // "..."
1192+
= rcurry(%make-string-literal, 1, 1, #t);
12001193

1201-
define method make-multi-line-raw-string-literal
1202-
(lexer :: <lexer>, source-location :: <lexer-source-location>)
1203-
=> (res :: <string-fragment>)
1204-
let bpos = source-location.start-posn + 5; // #r"""
1205-
let epos = source-location.end-posn - 3; // """
1206-
make(<string-fragment>,
1207-
record: source-location.source-location-record,
1208-
source-position: source-location.source-location-source-position,
1209-
// kind: $string-token,
1210-
value: as-fragment-value(decode-string(source-location, bpos, epos, #f)))
1211-
end method;
1194+
define constant make-multi-line-string-literal // """..."""
1195+
= rcurry(%make-string-literal, 3, 3, #t);
12121196

1213-
define method make-one-line-raw-string-literal
1214-
(lexer :: <lexer>, source-location :: <lexer-source-location>)
1215-
=> (res :: <string-fragment>)
1216-
let bpos = source-location.start-posn + 3; // #r"
1217-
let epos = source-location.end-posn - 1; // "
1218-
make(<string-fragment>,
1219-
record: source-location.source-location-record,
1220-
source-position: source-location.source-location-source-position,
1221-
// kind: $string-token,
1222-
value: as-fragment-value(decode-string(source-location, bpos, epos, #f)))
1223-
end method;
1197+
define constant make-raw-string-literal // #r"..."
1198+
= rcurry(%make-string-literal, 3, 1, #f);
1199+
1200+
define constant make-multi-line-raw-string-literal // #r"""..."""
1201+
= rcurry(%make-string-literal, 5, 3, #f);
12241202

12251203
define method parse-ratio-literal
12261204
(lexer :: <lexer>, source-location :: <lexer-source-location>)

0 commit comments

Comments
 (0)