1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! StringLiteralizer - Lua string literal conversion.
//!
//! This module provides functionality to convert strings to optimal Lua literal format.
//! Implements Requirement 2: Lua文字列リテラル形式の標準化.
use super::error::TranspileError;
use pasta_dsl::parser::Span;
/// Maximum number of `=` signs to try before giving up.
const MAX_EQUALS: usize = 10;
/// String literalizer for Lua output.
///
/// Converts strings to the optimal Lua literal format based on content.
pub struct StringLiteralizer;
impl StringLiteralizer {
/// Convert a string to Lua literal format.
///
/// # Rules
/// - Rule 1: If no escape-needing characters (`\` or `"`), use `"text"` format.
/// - Rule 2: If escape-needing characters exist, use long string format `[=[text]=]`.
///
/// # Long String `=` Count Algorithm
/// - Start with n=0 (danger pattern = `]`)
/// - If text contains danger pattern `]` + n `=`, increment n
/// - Continue until no danger pattern found or n > MAX_EQUALS
///
/// # Examples
/// - `hello world` → `"hello world"`
/// - `hello\nworld` (with backslash) → `[[hello\nworld]]`
/// - `hello]world` → `[=[hello]world]=]`
pub fn literalize(text: &str) -> Result<String, TranspileError> {
Self::literalize_with_span(text, &Span::default())
}
/// Convert a string to Lua literal format with span information for errors.
pub fn literalize_with_span(text: &str, span: &Span) -> Result<String, TranspileError> {
if !Self::needs_long_string(text) {
// Rule 1: Simple double-quoted string
Ok(format!("\"{}\"", text))
} else {
// Rule 2: Long string format
for n in 0..=MAX_EQUALS {
if !Self::contains_danger_pattern(text, n) {
let equals = "=".repeat(n);
return Ok(format!("[{}[{}]{}]", equals, text, equals));
}
}
// Could not find safe format
Err(TranspileError::string_literal_error(span, text))
}
}
/// Check if the text needs long string format.
///
/// Returns true if text contains `\` or `"`.
fn needs_long_string(text: &str) -> bool {
text.contains('\\') || text.contains('"')
}
/// Check if text contains the danger pattern for given n.
///
/// Danger pattern for n equals: `]` followed by n `=` signs.
/// This is the prefix of the closing delimiter `]=...=]` (without final `]`).
fn contains_danger_pattern(text: &str, n: usize) -> bool {
if n == 0 {
// For n=0, danger pattern is just `]`
text.contains(']')
} else {
// For n>0, danger pattern is `]` + n `=`
let pattern: String = format!("]{}", "=".repeat(n));
text.contains(&pattern)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple_string_no_special_chars() {
// Rule 1: No special characters
let result = StringLiteralizer::literalize("hello world").unwrap();
assert_eq!(result, "\"hello world\"");
}
#[test]
fn test_simple_string_japanese() {
// Rule 1: Japanese text without special chars
let result = StringLiteralizer::literalize("こんにちは").unwrap();
assert_eq!(result, "\"こんにちは\"");
}
#[test]
fn test_long_string_with_backslash() {
// Rule 2: Contains backslash, use long string
let result = StringLiteralizer::literalize("\\s[0]").unwrap();
// \s[0] contains ] so needs n=1
assert_eq!(result, "[=[\\s[0]]=]");
}
#[test]
fn test_long_string_with_quote() {
// Rule 2: Contains quote, use long string
let result = StringLiteralizer::literalize("hello \"world\"").unwrap();
assert_eq!(result, "[[hello \"world\"]]");
}
#[test]
fn test_long_string_with_bracket() {
// Text contains ] but no backslash/quote - still simple
let result = StringLiteralizer::literalize("hello]world").unwrap();
assert_eq!(result, "\"hello]world\"");
}
#[test]
fn test_long_string_with_backslash_and_bracket() {
// Contains both backslash and bracket
let result = StringLiteralizer::literalize("\\test]value").unwrap();
// Contains ] so n=0 is not safe, try n=1
assert_eq!(result, "[=[\\test]value]=]");
}
#[test]
fn test_long_string_n1_pattern() {
// Contains ]= pattern, needs n=2
let result = StringLiteralizer::literalize("\\test]=value").unwrap();
assert_eq!(result, "[==[\\test]=value]==]");
}
#[test]
fn test_long_string_n2_pattern() {
// Contains ]== pattern, needs n=3
let result = StringLiteralizer::literalize("\\test]==value").unwrap();
assert_eq!(result, "[===[\\test]==value]===]");
}
#[test]
fn test_sakura_script_s0() {
// Typical SakuraScript: \s[0]
let result = StringLiteralizer::literalize("\\s[0]").unwrap();
assert_eq!(result, "[=[\\s[0]]=]");
}
#[test]
fn test_sakura_script_s10() {
// Typical SakuraScript: \s[10]
let result = StringLiteralizer::literalize("\\s[10]").unwrap();
assert_eq!(result, "[=[\\s[10]]=]");
}
#[test]
fn test_needs_long_string_with_backslash() {
assert!(StringLiteralizer::needs_long_string("\\test"));
}
#[test]
fn test_needs_long_string_with_quote() {
assert!(StringLiteralizer::needs_long_string("hello\"world"));
}
#[test]
fn test_needs_long_string_plain_text() {
assert!(!StringLiteralizer::needs_long_string("hello world"));
}
#[test]
fn test_danger_pattern_n0() {
assert!(StringLiteralizer::contains_danger_pattern("hello]world", 0));
assert!(!StringLiteralizer::contains_danger_pattern(
"hello world",
0
));
}
#[test]
fn test_danger_pattern_n1() {
assert!(StringLiteralizer::contains_danger_pattern(
"hello]=world",
1
));
assert!(!StringLiteralizer::contains_danger_pattern(
"hello]world",
1
));
}
#[test]
fn test_danger_pattern_n2() {
assert!(StringLiteralizer::contains_danger_pattern(
"hello]==world",
2
));
assert!(!StringLiteralizer::contains_danger_pattern(
"hello]=world",
2
));
}
}