ironwork-syntax 0.7.0

ironwork for COBOL: fixed-format source reader, lexer and parser
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! COPY and COPY REPLACING, applied to the logical text before it is lexed. Matching is by
//! text-words, as the standard defines them, and replacement edits the text itself, so a
//! pseudo-text such as `==:TAG:==` can replace part of a word like `:TAG:-RECORD`.

use crate::bms;
use crate::source::{self, Source};
use crate::system;
use crate::{Error, Pos};
use std::path::{Path, PathBuf};

/// Directories searched for COPY members, in order. A `COPY X OF LIB` looks in `<dir>/LIB` first.
/// The file being compiled, when named, is never one of its own members. The compliance level is
/// how the program and its members are read.
#[derive(Clone, Debug, Default)]
pub struct Libraries {
    dirs: Vec<PathBuf>,
    program: Option<PathBuf>,
    compliance: numeric::Compliance,
}

const COPYBOOKS: &[&str] = &[".cpy", ".CPY", ".copy", ".COPY"];
const PROGRAM_SOURCES: &[&str] = &[".cbl", ".CBL", ".cob", ".COB"];
const BARE: &[&str] = &[""];
const MAX_DEPTH: usize = 32;

impl Libraries {
    pub fn new(dirs: Vec<PathBuf>) -> Self {
        Self { dirs, program: None, compliance: numeric::Compliance::Strict }
    }

    /// These libraries, for compiling the program in `program`.
    pub fn with_program(&self, program: &Path) -> Self {
        Self { program: Some(program.to_path_buf()), ..self.clone() }
    }

    /// These libraries, read under `compliance`.
    pub fn with_compliance(&self, compliance: numeric::Compliance) -> Self {
        Self { compliance, ..self.clone() }
    }

    pub fn compliance(&self) -> numeric::Compliance {
        self.compliance
    }

    /// A round of extensions searches every library before the next round starts, so a copybook in
    /// any library is found before a program source (assumptions C85 to C87).
    fn find(&self, name: &str, library: Option<&str>, literal: bool) -> Option<PathBuf> {
        let rounds = if literal { [BARE, COPYBOOKS, PROGRAM_SOURCES] } else { [COPYBOOKS, PROGRAM_SOURCES, BARE] };
        self.find_in_rounds(name, library, &rounds)
    }

    pub(crate) fn find_bms(&self, name: &str, library: Option<&str>) -> Option<PathBuf> {
        self.find_in_rounds(name, library, &[&[".bms", ".BMS"]])
    }

    fn find_in_rounds(&self, name: &str, library: Option<&str>, rounds: &[&[&str]]) -> Option<PathBuf> {
        let mut places: Vec<PathBuf> = Vec::new();
        for d in &self.dirs {
            if let Some(lib) = library {
                places.extend([lib.to_owned(), lib.to_ascii_lowercase()].iter().map(|l| d.join(l)));
            }
            places.push(d.clone());
        }
        let mut names = vec![name.to_owned()];
        for variant in [name.to_ascii_uppercase(), name.to_ascii_lowercase()] {
            if !names.contains(&variant) {
                names.push(variant);
            }
        }
        let (places, names) = (&places, &names);
        rounds
            .iter()
            .flat_map(|extensions| places.iter().flat_map(move |p| extensions.iter().flat_map(move |e| names.iter().map(move |n| p.join(format!("{n}{e}"))))))
            .find(|p| p.is_file() && !self.program.as_deref().is_some_and(|program| same_file(program, p)))
    }
}

/// Whether two paths name one file, however each is spelled.
fn same_file(a: &Path, b: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        if let (Ok(x), Ok(y)) = (std::fs::metadata(a), std::fs::metadata(b)) {
            return x.dev() == y.dev() && x.ino() == y.ino();
        }
    }
    matches!((std::fs::canonicalize(a), std::fs::canonicalize(b)), (Ok(x), Ok(y)) if x == y)
}

/// Reads a source file's bytes: UTF-8 when valid, otherwise one character per byte (Latin-1).
pub fn decode(bytes: &[u8]) -> String {
    match std::str::from_utf8(bytes) {
        Ok(s) => s.to_owned(),
        Err(_) => bytes.iter().map(|&b| b as char).collect(),
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Word {
    start: usize,
    end: usize,
    text: String,
}

fn text_words(chars: &[char]) -> Vec<Word> {
    let mut words = Vec::new();
    let mut i = 0;
    let separator_at = |i: usize| chars.get(i + 1).is_none_or(|c| c.is_whitespace());
    while i < chars.len() {
        let c = chars[i];
        if c.is_whitespace() || ((c == ',' || c == ';') && separator_at(i)) {
            i += 1;
            continue;
        }
        let start = i;
        if c == '\'' || c == '"' {
            i += 1;
            while i < chars.len() {
                if chars[i] == c && chars.get(i + 1) == Some(&c) {
                    i += 2;
                } else if chars[i] == c {
                    i += 1;
                    break;
                } else {
                    i += 1;
                }
            }
        } else if c == '=' && chars.get(i + 1) == Some(&'=') {
            i += 2;
        } else if matches!(c, '(' | ')' | ':') || (c == '.' && separator_at(i)) {
            i += 1;
        } else {
            while i < chars.len() {
                let d = chars[i];
                let ends = d.is_whitespace()
                    || matches!(d, '(' | ')' | ':' | '\'' | '"')
                    || ((d == '.' || d == ',' || d == ';') && separator_at(i))
                    || (d == '=' && chars.get(i + 1) == Some(&'='));
                if ends {
                    break;
                }
                i += 1;
            }
        }
        words.push(Word { start, end: i, text: chars[start..i].iter().collect() });
    }
    words
}

fn same(a: &str, b: &str) -> bool {
    if a.starts_with(['\'', '"']) { a == b } else { a.eq_ignore_ascii_case(b) }
}

#[derive(Clone, Debug)]
enum Mode {
    Whole,
    Leading,
    Trailing,
}

#[derive(Clone, Debug)]
struct Replacing {
    mode: Mode,
    pattern: Vec<String>,
    replacement: String,
}

struct Statement {
    name: String,
    /// Whether the name is a literal, which IBM takes as a file name as written.
    literal: bool,
    library: Option<String>,
    replacing: Vec<Replacing>,
    /// Index of the word after the terminating period.
    next: usize,
}

fn copy_statement(words: &[Word], at: usize, pos: Pos) -> Result<Statement, Error> {
    let err = |m: &str| Error::at(pos, format!("COPY: {m}"));
    let text = |i: usize| words.get(i).map(|w| w.text.as_str());
    let mut i = at + 1;
    let quoted = |s: &str| s.starts_with(['\'', '"']);
    let unquote = |s: &str| s.trim_matches(|c| c == '\'' || c == '"').to_owned();
    // In `COPY X..` the separator period is the second, so the name is `X.` (assumption C88).
    let word = |s: &str| {
        if s.ends_with('.') && !quoted(s) {
            return Err(Error::at(pos, format!("COPY {s}: the name ends in a period; the period that ends a COPY statement is the one followed by a space")));
        }
        Ok(unquote(s))
    };
    let first = text(i).ok_or_else(|| err("a member name"))?;
    let (name, literal) = (word(first)?, quoted(first));
    i += 1;
    let mut library = None;
    if text(i).is_some_and(|w| w.eq_ignore_ascii_case("OF") || w.eq_ignore_ascii_case("IN")) {
        library = Some(word(text(i + 1).ok_or_else(|| err("a library name"))?)?);
        i += 2;
    }
    if text(i).is_some_and(|w| w.eq_ignore_ascii_case("SUPPRESS")) {
        i += 1;
    }
    let mut replacing = Vec::new();
    if text(i).is_some_and(|w| w.eq_ignore_ascii_case("REPLACING")) {
        (replacing, i) = operands(words, i + 1, "COPY", pos)?;
    }
    if text(i) != Some(".") {
        return Err(err("a period to end the statement"));
    }
    Ok(Statement { name, literal, library, replacing, next: i + 1 })
}

/// The operand pairs of COPY REPLACING or of REPLACE, from word `at` to the period that ends the
/// statement, and that period's index. REPLACE takes pseudo-text alone (Language Reference
/// SC27-8713-03, p. 708).
fn operands(words: &[Word], at: usize, verb: &str, pos: Pos) -> Result<(Vec<Replacing>, usize), Error> {
    let err = |m: &str| Error::at(pos, format!("{verb}: {m}"));
    let text = |i: usize| words.get(i).map(|w| w.text.as_str());
    let replace = verb == "REPLACE";
    let pseudo_text = |i: usize| !replace || text(i) == Some("==");
    let (first, second) = if replace { ("pseudo-text between == delimiters to replace", "pseudo-text between == delimiters after BY") } else { ("an operand to replace", "an operand after BY") };
    let (mut replacing, mut i) = (Vec::new(), at);
    while text(i).is_some_and(|w| w != ".") {
        let mode = match text(i) {
            Some(w) if w.eq_ignore_ascii_case("LEADING") => Mode::Leading,
            Some(w) if w.eq_ignore_ascii_case("TRAILING") => Mode::Trailing,
            _ => Mode::Whole,
        };
        if !matches!(mode, Mode::Whole) {
            i += 1;
        }
        let (pattern, after) = operand(words, i).filter(|_| pseudo_text(i)).ok_or_else(|| err(first))?;
        if !text(after).is_some_and(|w| w.eq_ignore_ascii_case("BY")) {
            return Err(err("BY"));
        }
        let (replacement, after) = operand(words, after + 1).filter(|_| pseudo_text(after + 1)).ok_or_else(|| err(second))?;
        if pattern.is_empty() || (!matches!(mode, Mode::Whole) && pattern.len() != 1) {
            return Err(err("LEADING and TRAILING take one word; an empty pattern matches nothing"));
        }
        replacing.push(Replacing { mode, pattern, replacement: replacement.join(" ") });
        i = after;
    }
    Ok((replacing, i))
}

/// A REPLACING operand: pseudo-text `==...==` as its text-words, or one text-word.
fn operand(words: &[Word], at: usize) -> Option<(Vec<String>, usize)> {
    let first = words.get(at)?;
    if first.text != "==" {
        return Some((vec![first.text.clone()], at + 1));
    }
    let close = words[at + 1..].iter().position(|w| w.text == "==")? + at + 1;
    Some((words[at + 1..close].iter().map(|w| w.text.clone()).collect(), close + 1))
}

/// Appends `chars[range]` to `out` with the positions they came from.
fn copy_span(out: &mut Source, chars: &[char], positions: &[Pos], range: std::ops::Range<usize>) {
    out.text.extend(&chars[range.clone()]);
    out.positions.extend_from_slice(&positions[range]);
}

fn apply(src: &Source, replacing: &[Replacing]) -> Source {
    if replacing.is_empty() {
        return Source { text: src.text.clone(), positions: src.positions.clone(), options: Vec::new(), debugging: None, free: Vec::new() };
    }
    let chars: Vec<char> = src.text.chars().collect();
    let words = text_words(&chars);
    let mut out = Source { text: String::new(), positions: Vec::new(), options: Vec::new(), debugging: None, free: Vec::new() };
    let emit = |out: &mut Source, text: &str, pos: Pos| {
        for c in text.chars() {
            out.text.push(c);
            out.positions.push(pos);
        }
    };
    let (mut cursor, mut i) = (0usize, 0usize);
    while i < words.len() {
        copy_span(&mut out, &chars, &src.positions, cursor..words[i].start);
        let pos = src.positions[words[i].start];
        let hit = replacing.iter().find_map(|r| match r.mode {
            Mode::Whole => (words.len() - i >= r.pattern.len() && r.pattern.iter().zip(&words[i..]).all(|(p, w)| same(p, &w.text)))
                .then(|| (r.replacement.clone(), r.pattern.len())),
            Mode::Leading => {
                let w = &words[i].text;
                (w.len() > r.pattern[0].len() || w.eq_ignore_ascii_case(&r.pattern[0]))
                    .then_some(())
                    .filter(|_| w.to_ascii_uppercase().starts_with(&r.pattern[0].to_ascii_uppercase()))
                    .map(|_| (format!("{}{}", r.replacement, &w[r.pattern[0].len()..]), 1))
            }
            Mode::Trailing => {
                let w = &words[i].text;
                w.to_ascii_uppercase()
                    .ends_with(&r.pattern[0].to_ascii_uppercase())
                    .then(|| (format!("{}{}", &w[..w.len() - r.pattern[0].len()], r.replacement), 1))
            }
        });
        match hit {
            Some((text, consumed)) => {
                emit(&mut out, &text, pos);
                cursor = words[i + consumed - 1].end;
                i += consumed;
            }
            None => {
                copy_span(&mut out, &chars, &src.positions, words[i].start..words[i].end);
                cursor = words[i].end;
                i += 1;
            }
        }
    }
    copy_span(&mut out, &chars, &src.positions, cursor..chars.len());
    out
}

/// Replaces every COPY statement in `source` with its member's text, recursively. `files` names
/// each source file; a position's `file` indexes it.
pub fn expand(source: Source, libraries: &Libraries, files: &mut Vec<String>) -> Result<Source, Error> {
    let mut stack = Vec::new();
    expand_nested(source, libraries, files, &mut stack)
}

/// `EXEC SQL INCLUDE name END-EXEC`, which the Db2 precompiler treats as a COPY of its member (and
/// of its own SQLCA or SQLDA): the member's name, whether it is quoted, and the word after END-EXEC
/// and any period that ends it.
fn sql_include(words: &[Word], at: usize) -> Option<(String, bool, usize)> {
    let is = |k: usize, w: &str| words.get(at + k).is_some_and(|x| x.text.eq_ignore_ascii_case(w));
    if !(is(0, "EXEC") && is(1, "SQL") && is(2, "INCLUDE") && is(4, "END-EXEC")) {
        return None;
    }
    let word = &words[at + 3].text;
    let name = word.trim_matches(|c| c == '\'' || c == '"').to_owned();
    Some((name, word.starts_with(['\'', '"']), at + 5 + usize::from(is(5, "."))))
}

fn expand_nested(source: Source, libraries: &Libraries, files: &mut Vec<String>, stack: &mut Vec<String>) -> Result<Source, Error> {
    let chars: Vec<char> = source.text.chars().collect();
    let words = text_words(&chars);
    if !words.iter().any(|w| w.text.eq_ignore_ascii_case("COPY") || w.text.eq_ignore_ascii_case("INCLUDE")) {
        return Ok(source);
    }
    let mut out = Source { text: String::new(), positions: Vec::new(), options: source.options.clone(), debugging: source.debugging.clone(), free: source.free.clone() };
    let read = |text: &str, file: u16| source::read_under(text, file, source.debugging.is_some(), libraries.compliance());
    let (mut cursor, mut i) = (0usize, 0usize);
    while i < words.len() {
        let pos = source.positions[words[i].start];
        let (name, literal, library, replacing, next, sql) = if words[i].text.eq_ignore_ascii_case("COPY") {
            let st = copy_statement(&words, i, pos)?;
            (st.name, st.literal, st.library, st.replacing, st.next, false)
        } else if let Some((name, literal, next)) = sql_include(&words, i) {
            (name, literal, None, Vec::new(), next, true)
        } else {
            i += 1;
            continue;
        };
        copy_span(&mut out, &chars, &source.positions, cursor..words[i].start);
        let own = sql && matches!(name.to_ascii_uppercase().as_str(), "SQLCA" | "SQLDA");
        let path = if own { None } else { libraries.find(&name, library.as_deref(), literal) };
        let verb = if sql { "EXEC SQL INCLUDE" } else { "COPY" };
        let mapset = if own || path.is_some() { None } else { bms::load(libraries, &name, library.as_deref()) };
        let (key, member) = match (path, mapset) {
            (Some(path), _) => {
                let copied_free = source.free_at(pos).is_some();
                let copied = |text: &str, file: u16| source::read_copied(text, file, source.debugging.is_some(), libraries.compliance(), copied_free);
                (path.display().to_string(), read_member(&path, pos, files, &copied)?)
            }
            (None, Some((path, mapset))) => {
                let mapset = mapset.map_err(|e| Error::at(pos, format!("{verb} {name}: {}", e.place(&path.display().to_string()))))?;
                let file = u16::try_from(files.len()).map_err(|_| Error::at(pos, "more than 65535 copy members"))?;
                files.push(path.display().to_string());
                (path.display().to_string(), read(&bms::symbolic_map(&mapset), file)?)
            }
            (None, None) => {
                let text = system::member(&name).ok_or_else(|| Error::at(pos, format!("{verb} {name}: no such member in the copy libraries")))?;
                let key = format!("(system member {})", name.to_ascii_uppercase());
                let file = u16::try_from(files.len()).map_err(|_| Error::at(pos, "more than 65535 copy members"))?;
                files.push(key.clone());
                (key, read(&text, file)?)
            }
        };
        if stack.contains(&key) || stack.len() >= MAX_DEPTH {
            return Err(Error::at(pos, format!("{verb} {name}: copies itself, or nests deeper than {MAX_DEPTH}")));
        }
        stack.push(key);
        let mut member = expand_nested(member, libraries, files, stack)?;
        stack.pop();
        if let (Some(lines), Some(copied)) = (&mut out.debugging, member.debugging.take()) {
            // A COPY on a debugging line makes all of its member's text debugging lines.
            if lines.contains(&(pos.file, pos.line)) {
                lines.extend(member.positions.iter().map(|p| (p.file, p.line)));
            }
            lines.extend(copied);
        }
        out.free.extend(member.free.iter().cloned());
        let replaced = apply(&member, &replacing);
        out.text.push_str(&replaced.text);
        out.positions.extend(replaced.positions);
        out.text.push('\n');
        out.positions.push(pos);
        cursor = words.get(next - 1).map_or(chars.len(), |w| w.end);
        i = next;
    }
    copy_span(&mut out, &chars, &source.positions, cursor..chars.len());
    Ok(out)
}

/// Applies the REPLACE statements in `source`, which COPY has expanded (Language Reference
/// SC27-8713-03, pp. 708-712): each one's operands act on the text from its period to the next
/// REPLACE statement or the end of the source, with COPY REPLACING's matching, and the statements
/// themselves are left out (assumption C160).
pub fn replace(source: Source) -> Result<Source, Error> {
    let chars: Vec<char> = source.text.chars().collect();
    let words = text_words(&chars);
    let starts = |i: usize| words[i].text.eq_ignore_ascii_case("REPLACE");
    if !(0..words.len()).any(starts) {
        return Ok(source);
    }
    let mut out = Source { text: String::new(), positions: Vec::new(), options: source.options.clone(), debugging: source.debugging.clone(), free: source.free.clone() };
    let segment = |out: &mut Source, range: std::ops::Range<usize>, active: &[Replacing]| {
        let text = Source { text: chars[range.clone()].iter().collect(), positions: source.positions[range].to_vec(), options: Vec::new(), debugging: None, free: Vec::new() };
        let replaced = apply(&text, active);
        out.text.push_str(&replaced.text);
        out.positions.extend(replaced.positions);
    };
    let (mut active, mut cursor, mut i) = (Vec::new(), 0usize, 0usize);
    while i < words.len() {
        if !starts(i) {
            i += 1;
            continue;
        }
        let pos = source.positions[words[i].start];
        let next = match words.get(i + 1).map(|w| w.text.to_ascii_uppercase()).as_deref() {
            Some("OFF") if words.get(i + 2).is_some_and(|w| w.text == ".") => (Vec::new(), i + 3),
            Some("OFF") => return Err(Error::at(pos, "REPLACE OFF: a period to end the statement")),
            Some("ALSO" | "LAST") => return Err(Error::at(pos, "REPLACE ALSO and REPLACE LAST OFF are the 2014 COBOL standard's; Enterprise COBOL has REPLACE pseudo-text BY pseudo-text and REPLACE OFF")),
            Some("==" | "LEADING" | "TRAILING") => {
                let (replacing, period) = operands(&words, i + 1, "REPLACE", pos)?;
                if words.get(period).is_none_or(|w| w.text != ".") {
                    return Err(Error::at(pos, "REPLACE: a period to end the statement"));
                }
                (replacing, period + 1)
            }
            _ => {
                i += 1;
                continue;
            }
        };
        segment(&mut out, cursor..words[i].start, &active);
        (active, i) = next;
        cursor = words[i - 1].end;
    }
    segment(&mut out, cursor..chars.len(), &active);
    Ok(out)
}

fn read_member(path: &Path, pos: Pos, files: &mut Vec<String>, read: &dyn Fn(&str, u16) -> Result<Source, Error>) -> Result<Source, Error> {
    let bytes = std::fs::read(path).map_err(|e| Error::at(pos, format!("COPY {}: {e}", path.display())))?;
    let file = u16::try_from(files.len()).map_err(|_| Error::at(pos, "more than 65535 copy members"))?;
    files.push(path.display().to_string());
    read(&decode(&bytes), file)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn words(s: &str) -> Vec<String> {
        text_words(&s.chars().collect::<Vec<_>>()).into_iter().map(|w| w.text).collect()
    }

    #[test]
    fn text_words_follow_the_separators() {
        assert_eq!(words("01 :TAG:-REC PIC X(3)."), ["01", ":", "TAG", ":", "-REC", "PIC", "X", "(", "3", ")", "."]);
        assert_eq!(words("MOVE 'A B' TO X, Y."), ["MOVE", "'A B'", "TO", "X", "Y", "."]);
        assert_eq!(words("==A== BY ==B==."), ["==", "A", "==", "BY", "==", "B", "==", "."]);
        assert_eq!(words("GENAUW.CLAIM 0.1"), ["GENAUW.CLAIM", "0.1"]);
    }

    fn dir_with(files: &[(&str, &str)]) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("ironwork-copy-{}-{}", std::process::id(), files[0].0));
        std::fs::create_dir_all(&dir).unwrap();
        for (name, text) in files {
            std::fs::write(dir.join(name), text).unwrap();
        }
        dir
    }

    fn expanded(main: &str, dir: &Path) -> Result<String, Error> {
        let src = source::read(main)?;
        let mut files = vec![String::new()];
        expand(src, &Libraries::new(vec![dir.to_path_buf()]), &mut files).map(|s| s.text)
    }

    #[test]
    fn copy_replacing_whole_words_and_tags_inside_words() {
        let dir = dir_with(&[("RECS.cpy", "       01  :TAG:-REC.\n           05 :TAG:-ID PIC 9(4) VALUE OLD.\n")]);
        let text = expanded("           COPY RECS REPLACING ==:TAG:== BY ==CUST== OLD BY 42.\n", &dir).unwrap();
        assert!(text.contains("01  CUST-REC."), "{text}");
        assert!(text.contains("05 CUST-ID PIC 9(4) VALUE 42."), "{text}");
        assert!(!text.contains("COPY"));
    }

    #[test]
    fn leading_and_trailing_replace_part_of_a_word() {
        let dir = dir_with(&[("PART.cpy", "       01  WS-A PIC X.\n       01  B-WS PIC X.\n")]);
        let text = expanded("           COPY PART REPLACING LEADING ==WS== BY ==LK==\n               TRAILING ==WS== BY ==XX==.\n", &dir).unwrap();
        assert!(text.contains("LK-A") && text.contains("B-XX"), "{text}");
    }

    #[test]
    fn nested_copies_and_a_missing_member() {
        let dir = dir_with(&[("OUTER.cpy", "       COPY INNER.\n"), ("inner.cpy", "       01  X PIC X.\n")]);
        assert!(expanded("       COPY OUTER.\n", &dir).unwrap().contains("01  X PIC X."));
        let err = expanded("       COPY NOPE.\n", &dir).unwrap_err();
        assert!(err.message.contains("NOPE"));
    }

    #[test]
    fn system_members_answer_when_no_library_does() {
        let dir = dir_with(&[("OTHER.cpy", "       01  O PIC X.\n")]);
        let text = expanded("       COPY DFHAID.\n           EXEC SQL INCLUDE SQLCA END-EXEC.\n", &dir).unwrap();
        assert!(text.contains("DFHENTER") && text.contains("SQLCODE"), "{text}");
        assert!(!text.contains("END-EXEC"));
        let dir = dir_with(&[("DFHAID.cpy", "       01  VENDORED PIC X.\n")]);
        assert!(expanded("       COPY DFHAID.\n", &dir).unwrap().contains("VENDORED"));
    }

    #[test]
    fn a_member_that_copies_itself_is_refused() {
        let dir = dir_with(&[("LOOP.cpy", "       COPY LOOP.\n")]);
        assert!(expanded("       COPY LOOP.\n", &dir).unwrap_err().message.contains("copies itself"));
    }

    /// A fresh directory holding `files`, each at its path under it.
    fn tree(tag: &str, files: &[(&str, &str)]) -> PathBuf {
        let root = std::env::temp_dir().join(format!("ironwork-copy-{}-{tag}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        for (name, text) in files {
            let path = root.join(name);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(path, text).unwrap();
        }
        root
    }

    fn expanded_with(main: &str, libraries: &Libraries) -> Result<String, Error> {
        let mut files = vec![String::new()];
        expand(source::read(main)?, libraries, &mut files).map(|s| s.text)
    }

    #[test]
    fn a_copybook_in_any_library_is_found_before_a_program_source() {
        let root = tree(
            "rounds",
            &[
                ("src/INQACC.cbl", "       01  PROGRAM-SOURCE PIC X.\n"),
                ("src/ONLY.cbl", "       01  ONLY-SOURCE PIC X.\n"),
                ("src/BARE", "       01  BARE-FILE PIC X.\n"),
                ("cpy/INQACC.cpy", "       01  COPYBOOK PIC X.\n"),
                ("cpy/BARE.copy", "       01  COPY-FILE PIC X.\n"),
            ],
        );
        let libraries = Libraries::new(vec![root.join("src"), root.join("cpy")]);
        assert!(expanded_with("       COPY INQACC.\n", &libraries).unwrap().contains("COPYBOOK"));
        assert!(expanded_with("       COPY inqacc.\n", &libraries).unwrap().contains("COPYBOOK"));
        assert!(expanded_with("       COPY BARE.\n", &libraries).unwrap().contains("COPY-FILE"));
        assert!(expanded_with("       COPY ONLY.\n", &libraries).unwrap().contains("ONLY-SOURCE"));
    }

    #[test]
    fn a_name_alone_is_tried_after_its_extensions_and_a_literal_first() {
        let root = tree("bare", &[("lib/MEMBER", "       01  BARE-FILE PIC X.\n"), ("lib/MEMBER.cpy", "       01  COPYBOOK PIC X.\n"), ("lib/ALONE", "       01  ALONE PIC X.\n")]);
        let libraries = Libraries::new(vec![root.join("lib")]);
        assert!(expanded_with("       COPY MEMBER.\n", &libraries).unwrap().contains("COPYBOOK"));
        assert!(expanded_with("       COPY \"MEMBER\".\n", &libraries).unwrap().contains("BARE-FILE"));
        assert!(expanded_with("       COPY ALONE.\n", &libraries).unwrap().contains("01  ALONE"));
    }

    #[test]
    fn the_program_being_compiled_is_never_its_own_member() {
        let program = "       IDENTIFICATION DIVISION.\n       PROGRAM-ID. PGMC.\n       DATA DIVISION.\n       WORKING-STORAGE SECTION.\n       COPY PGMC.\n";
        let root = tree("self", &[("src/PGMC.cbl", program), ("src/OUTER.cpy", "       COPY PGMC.\n"), ("lib/PGMC.cbl", "       01  PGMC-X PIC X.\n")]);
        let main = root.join("src/../src/PGMC.cbl");
        let libraries = Libraries::new(vec![root.join("src"), root.join("lib")]).with_program(&main);
        assert!(expanded_with(program, &libraries).unwrap().contains("PGMC-X"));
        assert!(expanded_with("       COPY OUTER.\n", &libraries).unwrap().contains("PGMC-X"));
        let alone = Libraries::new(vec![root.join("src")]).with_program(&main);
        assert!(expanded_with(program, &alone).unwrap_err().message.contains("no such member"));
        assert!(expanded_with(program, &Libraries::new(vec![root.join("src")])).unwrap_err().message.contains("copies itself"));
    }

    #[test]
    fn a_doubled_period_is_refused_by_name() {
        let root = tree("period", &[("lib/COBCPARMS.cpy", "       01  PARMS PIC X.\n")]);
        let libraries = Libraries::new(vec![root.join("lib")]);
        let err = expanded_with("       COPY COBCPARMS..\n", &libraries).unwrap_err();
        assert!(err.message.contains("COPY COBCPARMS.: the name ends in a period"), "{}", err.message);
        let err = expanded_with("       COPY COBCPARMS OF LIB..\n", &libraries).unwrap_err();
        assert!(err.message.contains("COPY LIB.: "), "{}", err.message);
        assert!(expanded_with("       COPY COBCPARMS.\n", &libraries).unwrap().contains("PARMS"));
    }

    fn replaced(text: &str) -> Result<String, Error> {
        replace(source::read(text)?).map(|s| s.text)
    }

    #[test]
    fn replace_acts_from_its_period_to_the_next_replace() {
        let text = replaced(concat!(
            "       01  A PICTURE X.\n",
            "       REPLACE ==PICTURE== BY ==PIC==.\n",
            "       01  B PICTURE X.\n",
            "       01  C PICTURE X VALUE 'PICTURE'.\n",
            "       REPLACE OFF.\n",
            "       01  D PICTURE X.\n",
        ))
        .unwrap();
        let words: Vec<&str> = text.split_whitespace().collect();
        assert_eq!(words, ["01", "A", "PICTURE", "X.", "01", "B", "PIC", "X.", "01", "C", "PIC", "X", "VALUE", "'PICTURE'.", "01", "D", "PICTURE", "X."]);
    }

    #[test]
    fn replace_takes_the_language_references_example() {
        let text = replaced(concat!(
            "           REPLACE ==\"(Hello, World!)\"== BY ==\"(Hello, Mom!)\"==.\n",
            "       01 WS-STRING1 PIC X(30) VALUE \"(Hello, World!)\".\n",
            "           DISPLAY \"Modified: \" XX-WS-:TAG:1\n",
            "           REPLACE LEADING ==XX-==  BY ====\n",
            "                           ==:TAG:==  BY ==STRING==\n",
            "                   TRAILING ==1== BY ==2==.\n",
            "           DISPLAY \"Modified: \" XX-WS-:TAG:1\n",
        ))
        .unwrap();
        assert!(text.contains("VALUE \"(Hello, Mom!)\"."), "{text}");
        assert!(text.contains("DISPLAY \"Modified: \" XX-WS-:TAG:1\n") && text.ends_with("DISPLAY \"Modified: \" WS-STRING2"), "{text}");
        assert!(!text.contains("REPLACE"), "{text}");
    }

    #[test]
    fn a_later_replace_supersedes_and_several_words_match_as_one() {
        let text = replaced(concat!(
            "           REPLACE ==AO== BY ==TO== == = == BY ==EQUAL==.\n",
            "           MOVE \"*\" AO X.\n",
            "           REPLACE ==MOVE \"*\" TO X.\n",
            "                      IF X = \"*\"== BY ==DISPLAY X==.\n",
            "           MOVE \"*\" TO X.\n",
            "           IF X = \"*\" DISPLAY Y.\n",
        ))
        .unwrap();
        assert!(text.contains("MOVE \"*\" TO X."), "{text}");
        assert!(text.contains("DISPLAY X DISPLAY Y."), "{text}");
    }

    #[test]
    fn replace_takes_only_pseudo_text() {
        let text = replaced("           MOVE REPLACE TO X.\n           DISPLAY Y.\n").unwrap();
        assert!(text.contains("MOVE REPLACE TO X."));
        assert!(replaced("       REPLACE A BY B.\n").unwrap().contains("REPLACE A BY B."));
        let err = replaced("       REPLACE ==A== BY B.\n").unwrap_err();
        assert_eq!(err.message, "REPLACE: pseudo-text between == delimiters after BY");
        let err = replaced("       REPLACE ALSO ==A== BY ==B==.\n").unwrap_err();
        assert!(err.message.starts_with("REPLACE ALSO and REPLACE LAST OFF are the 2014"), "{}", err.message);
        assert_eq!(replaced("       REPLACE ==A== BY ==B==\n").unwrap_err().message, "REPLACE: a period to end the statement");
    }

    #[test]
    fn replace_acts_on_copied_text() {
        let dir = dir_with(&[("REPMEM.cpy", "       01  :TAG:-ID PIC 9.\n")]);
        let src = source::read("       REPLACE ==:TAG:== BY ==CUST==.\n       COPY REPMEM.\n").unwrap();
        let mut files = vec![String::new()];
        let text = expand(src, &Libraries::new(vec![dir]), &mut files).and_then(replace).unwrap().text;
        assert!(text.contains("01  CUST-ID PIC 9."), "{text}");
    }

    #[test]
    fn positions_in_a_member_name_its_file() {
        let dir = dir_with(&[("POS.cpy", "       01  Y PIC X.\n")]);
        let src = source::read("       COPY POS.\n").unwrap();
        let mut files = vec![String::new()];
        let out = expand(src, &Libraries::new(vec![dir]), &mut files).unwrap();
        let at = out.text.find('Y').unwrap();
        assert_eq!(out.positions[at].file, 1);
        assert!(files[1].ends_with("POS.cpy"));
    }
}