pasta_lua 0.2.1

Pasta Lua - Lua integration for Pasta DSL
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Basic integration tests for pasta_lua transpiler.
//!
//! Tests that verify the transpiler generates correct Lua code for basic constructs:
//! header, actors, scenes, actions, scope separation, config, and reference patterns.

use crate::common;

use common::normalize_line_endings;
use pasta_dsl::parse_str;
use pasta_dsl::parser::{ActorScope, FileItem, GlobalSceneScope};
use pasta_lua::{LuaTranspiler, TranspilerConfig};

/// Helper to get global scene scopes from PastaFile
fn get_global_scene_scopes(file: &pasta_dsl::parser::PastaFile) -> Vec<&GlobalSceneScope> {
    file.items
        .iter()
        .filter_map(|item| {
            if let FileItem::GlobalSceneScope(scene) = item {
                Some(scene)
            } else {
                None
            }
        })
        .collect()
}

/// Helper to get actor scopes from PastaFile
fn get_actor_scopes(file: &pasta_dsl::parser::PastaFile) -> Vec<&ActorScope> {
    file.items
        .iter()
        .filter_map(|item| {
            if let FileItem::ActorScope(actor) = item {
                Some(actor)
            } else {
                None
            }
        })
        .collect()
}

/// Sample Pasta source for testing
/// アクター定義では @ (word_marker) を使用して表情を定義
const SAMPLE_PASTA: &str = r#"
%さくら
 @通常:\s[0]
 @照れ:\s[1]

%うにゅう
 @通常:\s[10]

*メイン
  さくら:こんにちは。
  うにゅう:やふぅ。

 ・自己紹介
   さくら:私はさくらです。
   うにゅう:ワイはうにゅうや。
"#;

#[test]
fn test_transpile_sample_pasta_header() {
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    // Parse the sample - returns PastaFile directly
    let file = parse_str(SAMPLE_PASTA, "test.pasta").unwrap();

    let result = transpiler.transpile(&file, &mut output);
    assert!(result.is_ok());

    let lua_code = String::from_utf8(output).unwrap();

    // Verify header
    assert!(
        lua_code.contains("local PASTA = require \"pasta\""),
        "Missing PASTA require statement"
    );
}

#[test]
fn test_transpile_sample_pasta_actors() {
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    let file = parse_str(SAMPLE_PASTA, "test.pasta").unwrap();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Verify actor definitions (Requirement 3a)
    assert!(
        lua_code.contains("PASTA.create_actor(\"さくら\")"),
        "Missing actor さくら"
    );
    assert!(
        lua_code.contains("PASTA.create_actor(\"うにゅう\")"),
        "Missing actor うにゅう"
    );

    // Verify actor attributes with symmetric API: ACTOR:create_word(key):entry(...) (actor-word-dictionary)
    assert!(
        lua_code.contains("ACTOR:create_word(\"通常\"):entry([=[\\s[0]]=])"),
        "Missing さくら.通常 attribute with create_word API"
    );
    assert!(
        lua_code.contains("ACTOR:create_word(\"照れ\"):entry([=[\\s[1]]=])"),
        "Missing さくら.照れ attribute"
    );
    assert!(
        lua_code.contains("ACTOR:create_word(\"通常\"):entry([=[\\s[10]]=])"),
        "Missing うにゅう.通常 attribute"
    );
}

#[test]
fn test_transpile_sample_pasta_scenes() {
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    let file = parse_str(SAMPLE_PASTA, "test.pasta").unwrap();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Verify scene definition (Requirement 3b)
    // Counter is now assigned by Lua runtime, not transpiler (Requirement 8.5)
    assert!(
        lua_code.contains("PASTA.create_scene(\"メイン\")"),
        "Missing scene メイン"
    );

    // Verify entry point function (Requirement 3c)
    assert!(
        lua_code.contains("function SCENE.__start__(act, ...)"),
        "Missing __start__ function"
    );

    // Verify session initialization
    assert!(
        lua_code.contains("local args = { ... }"),
        "Missing args initialization"
    );
    assert!(
        lua_code.contains("local save, var = act:init_scene(SCENE)"),
        "Missing session initialization"
    );

    // Verify local scene function (Requirement 3c)
    assert!(
        lua_code.contains("function SCENE.自己紹介_"),
        "Missing 自己紹介 local scene function"
    );
}

#[test]
fn test_transpile_sample_pasta_actions() {
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    let file = parse_str(SAMPLE_PASTA, "test.pasta").unwrap();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Verify talk actions (Requirement 3d)
    assert!(
        lua_code.contains("act.さくら:talk(\"こんにちは。\")"),
        "Missing さくら talk action"
    );
    assert!(
        lua_code.contains("act.うにゅう:talk(\"やふぅ。\")"),
        "Missing うにゅう talk action"
    );
}

#[test]
fn test_transpile_do_end_scope_separation() {
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    let file = parse_str(SAMPLE_PASTA, "test.pasta").unwrap();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Normalize line endings for reliable pattern matching
    let lua_code = normalize_line_endings(&lua_code);

    // Verify do...end scope separation (Requirement 1)
    // Count do/end pairs
    let do_count = lua_code.matches("\ndo\n").count() + lua_code.matches("do\n").count();
    let end_count = lua_code.matches("\nend\n").count() + lua_code.matches("end\n").count();

    // Should have at least 3 do...end blocks (2 actors + 1 scene)
    assert!(
        do_count >= 3,
        "Expected at least 3 do blocks, found {}",
        do_count
    );
    assert!(
        end_count >= do_count,
        "Unbalanced do/end blocks: {} do, {} end",
        do_count,
        end_count
    );
}

#[test]
fn test_string_literalizer_in_transpile() {
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    // Test with SakuraScript that needs long string format
    let pasta = r#"
%さくら
 @通常:\s[0]
 @照れ:\s[1]

*メイン
  さくら:テスト
"#;

    let file = parse_str(pasta, "test.pasta").unwrap();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Verify long string format for SakuraScript with brackets
    assert!(
        lua_code.contains("[=[\\s[0]]=]"),
        "SakuraScript should use [=[...]=] format due to brackets"
    );
}

#[test]
fn test_transpile_config_no_comments() {
    let config = TranspilerConfig::without_comments();
    let transpiler = LuaTranspiler::new(config);
    let mut output = Vec::new();

    let file = parse_str(SAMPLE_PASTA, "test.pasta").unwrap();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Config without comments should still produce valid code
    assert!(lua_code.contains("PASTA.create_actor"));
    assert!(lua_code.contains("PASTA.create_scene"));
}

/// Test case for the reference implementation sample.pasta
#[test]
fn test_transpile_reference_sample_structure() {
    let sample_pasta = include_str!("../fixtures/sample.pasta");

    let file = parse_str(sample_pasta, "sample.pasta").unwrap();
    let actors = get_actor_scopes(&file);
    let scenes = get_global_scene_scopes(&file);

    // Verify the sample parses correctly
    assert_eq!(actors.len(), 2, "Expected 2 actors");
    assert_eq!(scenes.len(), 2, "Expected 2 global scenes");

    // Transpile
    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();

    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Verify key structural elements
    assert!(
        lua_code.contains("PASTA.create_actor(\"さくら\")"),
        "Missing actor さくら"
    );
    assert!(
        lua_code.contains("PASTA.create_actor(\"うにゅう\")"),
        "Missing actor うにゅう"
    );
    // Counter is now assigned by Lua runtime, not transpiler (Requirement 8.5)
    assert!(
        lua_code.contains("PASTA.create_scene(\"メイン\")"),
        "Missing scene メイン"
    );
    // Each unique scene name gets counter at runtime
    assert!(
        lua_code.contains("PASTA.create_scene(\"会話分岐\")"),
        "Missing scene 会話分岐"
    );
}

/// Debug test to dump generated Lua code for analysis
#[test]
fn test_debug_dump_generated_lua() {
    let sample_pasta = include_str!("../fixtures/sample.pasta");

    let file = parse_str(sample_pasta, "sample.pasta").unwrap();
    let scenes = get_global_scene_scopes(&file);

    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();
    transpiler.transpile(&file, &mut output).unwrap();
    let lua_code = String::from_utf8(output).unwrap();

    // Debug: Check code block locations in AST
    eprintln!("\n=== CODE BLOCK LOCATIONS ===");
    for (i, scene) in scenes.iter().enumerate() {
        eprintln!(
            "GlobalScene[{}] '{}': {} code_blocks",
            i,
            scene.name,
            scene.code_blocks.len()
        );
        for (j, local) in scene.local_scenes.iter().enumerate() {
            eprintln!(
                "  LocalScene[{}] '{:?}': {} code_blocks",
                j,
                local.name,
                local.code_blocks.len()
            );
        }
    }
    eprintln!("=== END ===\n");

    // Print specific sections for debugging: code block area
    eprintln!("\n=== GENERATED LUA (引数付き呼び出し and code block) ===");
    let mut in_section = false;
    for line in lua_code.lines() {
        if line.contains("引数付き呼び出し") || line.contains("関数") {
            in_section = true;
        }
        if in_section {
            eprintln!("{}", line);
            if line.trim() == "end" && in_section {
                // Count ends to find the right scope boundary
            }
        }
        // Counter now assigned by Lua runtime, so check for base name
        if line.contains("会話分岐") && line.contains("PASTA.create_scene") {
            break;
        }
    }
    eprintln!("=== END ===\n");
}

/// Requirement 6: Verify specific code patterns match reference
#[test]
fn test_transpile_reference_code_patterns() {
    let sample_pasta = include_str!("../fixtures/sample.pasta");
    let sample_lua = include_str!("../fixtures/sample.lua");

    // Parse and transpile
    let file = parse_str(sample_pasta, "sample.pasta").unwrap();

    let transpiler = LuaTranspiler::default();
    let mut output = Vec::new();
    transpiler.transpile(&file, &mut output).unwrap();
    let generated_lua = String::from_utf8(output).unwrap();

    // Extract code lines (non-comment) from reference
    let reference_patterns: Vec<&str> = sample_lua
        .lines()
        .filter(|line| {
            let trimmed = line.trim();
            !trimmed.is_empty() && !trimmed.starts_with("--") && !trimmed.starts_with("local PASTA") // Skip require line (may differ)
        })
        .map(|line| line.trim())
        .collect();

    // Check that key patterns from reference exist in generated output
    let generated_normalized = generated_lua.replace("    ", "\t");

    let mut missing_patterns = Vec::new();
    let mut found_count = 0;

    for pattern in &reference_patterns {
        // Normalize pattern for comparison
        let pattern_normalized = pattern.replace("    ", "\t");
        if generated_normalized.contains(&pattern_normalized) || generated_lua.contains(*pattern) {
            found_count += 1;
        } else {
            missing_patterns.push(*pattern);
        }
    }

    // Report missing patterns
    if !missing_patterns.is_empty() {
        eprintln!("\n【参照実装に存在するが生成出力に欠落しているパターン】");
        for (idx, pattern) in missing_patterns.iter().take(10).enumerate() {
            eprintln!("  [{}] {}", idx + 1, pattern);
        }
        if missing_patterns.len() > 10 {
            eprintln!("  ... 他 {}", missing_patterns.len() - 10);
        }
    }

    let coverage = (found_count as f64 / reference_patterns.len() as f64) * 100.0;
    println!(
        "\n【パターンカバレッジ】: {:.1}% ({}/{})",
        coverage,
        found_count,
        reference_patterns.len()
    );

    // Assert reasonable coverage
    assert!(
        coverage >= 50.0,
        "Pattern coverage too low: {:.1}%. Expected at least 50%.",
        coverage
    );
}