Skip to main content

dive_pluscal/
rewrite.rs

1//! Bounded translation-block editing and pinned line-concatenation CRC32.
2use crate::{Code, Diagnostic, ast::Span};
3use serde::{Deserialize, Serialize};
4
5/// Rewriting always computes the body CRC and uses an optional exact algorithm CRC.
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
7pub enum ChecksumPolicy {
8    /// Keep an exact algorithm CRC when available; otherwise disable only that
9    /// value using the pinned `\\in STRING` spelling and retain the body CRC.
10    #[default]
11    BestEffort,
12}
13/// In-memory replacement limits and explicit overwrite policy.
14#[derive(Clone, Debug, Serialize, Deserialize)]
15pub struct RewriteOptions {
16    /// The best-effort checksum policy.
17    pub checksums: ChecksumPolicy,
18    /// Maximum source and rewritten document bytes, checked before construction.
19    pub max_bytes: usize,
20    /// Permit replacing an unverified or modified existing body.
21    pub replace_modified: bool,
22    /// Normalize all document line endings to LF, matching pinned Unix output.
23    /// Otherwise unchanged source bytes and the source's first newline are kept.
24    pub normalize_newlines: bool,
25}
26impl Default for RewriteOptions {
27    fn default() -> Self {
28        Self {
29            checksums: ChecksumPolicy::BestEffort,
30            max_bytes: 4_194_304,
31            replace_modified: false,
32            normalize_newlines: false,
33        }
34    }
35}
36/// Recorded body/algorithm CRC evidence, not source identity or a proof.
37#[derive(Clone, Debug, Serialize, Deserialize)]
38pub struct PreviousChecksum {
39    /// Recorded hexadecimal CRC, if the recognized header supplies one.
40    pub recorded: Option<String>,
41    /// Recorded algorithm CRC; rewriting does not use it as an edit gate.
42    pub algorithm_recorded: Option<String>,
43    /// Whether a recognized old header deliberately disables algorithm CRC.
44    pub algorithm_disabled: bool,
45    /// CRC of the original body lines, without newline bytes.
46    pub computed: String,
47    /// Exact case-sensitive match, as in pinned Java; absence is unverified.
48    pub matched: bool,
49}
50/// Original source spans selected by bounded document editing.
51#[derive(Clone, Debug, Serialize, Deserialize)]
52pub struct RewriteSelection {
53    /// Complete algorithm block-comment span.
54    pub comment: Span,
55    /// Algorithm parse region, excluding the outer comment terminator.
56    pub algorithm: Span,
57    /// Old translation section including both marker lines, if present.
58    pub previous: Option<Span>,
59    /// Body between old marker lines, if present.
60    pub previous_body: Option<Span>,
61    /// Old body checksum evidence; missing/disabled checksums are not verified.
62    pub checksum: Option<PreviousChecksum>,
63    /// Insertion point immediately after the algorithm comment's complete line.
64    pub insertion: usize,
65}
66/// A source replacement, independently of body generation or semantic admission.
67#[derive(Clone, Debug, Serialize, Deserialize)]
68pub struct RewritePlan {
69    /// Exact original byte interval to replace.
70    pub span: Span,
71    /// Replacement text using the selected newline policy.
72    pub replacement: String,
73    /// Complete resulting document; this does not itself mean TLA acceptance.
74    pub module_source: String,
75    /// CRC of generated body lines, excluding line-ending bytes.
76    pub translation_checksum: String,
77    /// Exact algorithm CRC when its serializer supports the source.
78    pub algorithm_checksum: Option<String>,
79    /// Nonfatal checksum unavailability; generation and body integrity remain independent.
80    pub warnings: Vec<Diagnostic>,
81    /// Selection and previous body verification evidence.
82    pub selection: RewriteSelection,
83    /// Whether unchanged source newline bytes were normalized.
84    pub newlines_normalized: bool,
85}
86fn failure(code: Code, span: Span, message: &str) -> Diagnostic {
87    Diagnostic {
88        code,
89        span,
90        message: message.to_owned(),
91    }
92}
93#[derive(Clone, Copy)]
94struct Line<'a> {
95    start: usize,
96    end: usize,
97    text: &'a str,
98}
99fn lines(source: &str) -> Vec<Line<'_>> {
100    let mut result = Vec::new();
101    let mut start = 0;
102    let bytes = source.as_bytes();
103    let mut cursor = 0;
104    while cursor < bytes.len() {
105        if bytes[cursor] == b'\r' || bytes[cursor] == b'\n' {
106            let end_text = cursor;
107            cursor += 1;
108            if bytes[end_text] == b'\r' && bytes.get(cursor) == Some(&b'\n') {
109                cursor += 1;
110            }
111            result.push(Line {
112                start,
113                end: cursor,
114                text: &source[start..end_text],
115            });
116            start = cursor;
117        } else {
118            cursor += 1;
119        }
120    }
121    if start < source.len() {
122        result.push(Line {
123            start,
124            end: source.len(),
125            text: &source[start..],
126        });
127    }
128    result
129}
130/// CRC32 matching pinned `Validator.checksum(Vector<String>)`: UTF-8 lines are
131/// concatenated without any LF/CR separator; the hexadecimal result is unpadded.
132/// This integrity checksum is not a cryptographic hash or semantic validation.
133#[must_use]
134pub fn translation_checksum(body: &str) -> String {
135    crc32_iter(lines(body).iter().flat_map(|line| line.text.bytes()))
136}
137pub(crate) fn crc32(bytes: &[u8]) -> String {
138    crc32_iter(bytes.iter().copied())
139}
140fn crc32_iter(bytes: impl Iterator<Item = u8>) -> String {
141    let mut crc = u32::MAX;
142    for byte in bytes {
143        crc ^= u32::from(byte);
144        for _ in 0..8 {
145            crc = (crc >> 1) ^ (0xedb8_8320_u32 & 0_u32.wrapping_sub(crc & 1));
146        }
147    }
148    format!("{:x}", !crc)
149}
150fn marker(line: &str, which: &str) -> bool {
151    let mut words = line.trim_start_matches([' ', '\t']).split_whitespace();
152    words.next() == Some("\\*")
153        && words.next() == Some(which)
154        && words.next() == Some("TRANSLATION")
155}
156// The recognized partial contract is deliberately narrower than arbitrary Java
157// regex matches. Algorithm evidence is compared only by a full AST request.
158struct Header<'a> {
159    algorithm: Option<&'a str>,
160    translation: Option<&'a str>,
161    algorithm_value: Span,
162    translation_value: Span,
163    translation_hex: Option<Span>,
164}
165fn whitespace(text: &str) -> &str {
166    text.trim_start_matches(|c: char| c.is_ascii_whitespace())
167}
168fn checksum_value(text: &str) -> Option<(Option<&str>, &str)> {
169    let text = whitespace(text);
170    if let Some(text) = text.strip_prefix("\\in") {
171        return Some((None, whitespace(text).strip_prefix("STRING")?));
172    }
173    let text = whitespace(text.strip_prefix('=')?).strip_prefix('"')?;
174    let end = text.find('"')?;
175    let value = &text[..end];
176    if !value.bytes().all(|b| b.is_ascii_hexdigit()) {
177        return None;
178    }
179    Some((Some(value), &text[end + 1..]))
180}
181fn field<'a>(text: &'a str, names: &[&str]) -> Option<&'a str> {
182    for name in names {
183        if text
184            .get(..name.len())
185            .is_some_and(|prefix| prefix.eq_ignore_ascii_case(name))
186        {
187            return Some(&text[name.len()..]);
188        }
189    }
190    None
191}
192fn checksum_header(header: &str) -> Option<Header<'_>> {
193    let start = header.find("\\* BEGIN TRANSLATION")? + "\\* BEGIN TRANSLATION".len();
194    let text = &header[start..];
195    if text.len() == whitespace(text).len() {
196        return None;
197    }
198    let text = whitespace(whitespace(text).strip_prefix('(')?);
199    let text = field(
200        text,
201        &[
202            "chksum(pcal)",
203            "checksum(pcal)",
204            "chksum(pluscal)",
205            "checksum(pluscal)",
206        ],
207    )?;
208    let algorithm_value_start = header.len() - whitespace(text).len();
209    let (algorithm, text) = checksum_value(text)?;
210    let algorithm_value = Span {
211        start: algorithm_value_start,
212        end: header.len() - text.len(),
213    };
214    let text = whitespace(whitespace(text).strip_prefix("/\\")?);
215    let text = field(
216        text,
217        &[
218            "chksum(tla)",
219            "checksum(tla)",
220            "chksum(tla+)",
221            "checksum(tla+)",
222        ],
223    )?;
224    let translation_value_start = header.len() - whitespace(text).len();
225    let (translation, text) = checksum_value(text)?;
226    let translation_value = Span {
227        start: translation_value_start,
228        end: header.len() - text.len(),
229    };
230    let translation_hex = translation.map(|value| Span {
231        start: translation_value.end - value.len() - 1,
232        end: translation_value.end - 1,
233    });
234    whitespace(text).strip_prefix(')')?;
235    Some(Header {
236        algorithm,
237        translation,
238        algorithm_value,
239        translation_value,
240        translation_hex,
241    })
242}
243/// Select the algorithm region inside an already identified block comment.
244/// Pinned translation ignores trailing ASCII-star comment decoration. The
245/// enclosing comment and original source remain untouched; arbitrary trailing
246/// prose is not removed. This selects bytes, without grammar or artifact admission.
247/// Standalone parsing still requires every algorithm token to be consumed.
248#[must_use]
249pub fn algorithm_comment_region(source: &str, comment: Span) -> Option<Span> {
250    let text = source.get(comment.start..comment.end)?;
251    if !text.starts_with("(*") || !text.ends_with("*)") {
252        return None;
253    }
254    let marker = algorithm_marker(text)?;
255    let start = comment.start + marker;
256    let mut end = comment.end.checked_sub(2)?;
257    let content = source.get(start..end)?;
258    let trimmed = content.trim_end_matches(['*', ' ', '\t', '\r', '\n']);
259    if content.get(trimmed.len()..)?.contains('*') {
260        end = start + trimmed.len();
261    }
262    Some(Span { start, end })
263}
264
265fn algorithm_marker(text: &str) -> Option<usize> {
266    // trans.java scans lines in order and prefers --algorithm over --fair
267    // only when both occur on that same line. Parsing validates the selected
268    // marker; in particular, whitespace may separate --fair and algorithm.
269    let mut offset = 0;
270    text.split_inclusive(['\r', '\n']).find_map(|line| {
271        let marker = line.find("--algorithm").or_else(|| line.find("--fair"));
272        let start = offset;
273        offset += line.len();
274        marker.map(|column| start + column)
275    })
276}
277
278/// Select only a completely parsed algorithm inside an identified comment.
279/// The exact consumed C/P grammar end determines ownership; ordinary trailing
280/// prose stays in the original comment but is not algorithm input. Unterminated
281/// quoted/comment lexical state and resource limits remain deciding errors.
282/// Standalone `parse_at` retains its strict EOF contract.
283///
284/// # Errors
285/// Invalid comment selection or bounded source/lexical/grammar diagnostics.
286pub fn owned_algorithm_comment_region(
287    source: &str,
288    comment: Span,
289    limits: &crate::Options,
290) -> Result<Span, Diagnostic> {
291    let region = algorithm_comment_region(source, comment).ok_or_else(|| {
292        // An invalid caller range must still yield a valid UTF-8 diagnostic.
293        let boundary = |offset: usize| {
294            let mut offset = offset.min(source.len());
295            while !source.is_char_boundary(offset) {
296                offset -= 1;
297            }
298            offset
299        };
300        let start = boundary(comment.start);
301        failure(
302            Code::Syntax,
303            Span {
304                start,
305                end: boundary(comment.end).max(start),
306            },
307            "invalid algorithm comment region",
308        )
309    })?;
310    let parsed = crate::parser::parse_prefix_at(source, region.start..region.end, limits)?;
311    Ok(parsed.algorithm.span)
312}
313
314/// Selects one outer algorithm comment and at most one following marker pair.
315/// Unknown/ambiguous marker layouts yield named diagnostics, not guessed edits.
316///
317/// # Errors
318/// Syntax/unsupported/resource diagnostics with exact source byte spans.
319pub fn select_rewrite(
320    source: &str,
321    options: &RewriteOptions,
322) -> Result<RewriteSelection, Diagnostic> {
323    select_rewrite_with_parser(
324        source,
325        options,
326        &crate::Options {
327            max_bytes: options.max_bytes,
328            ..crate::Options::default()
329        },
330    )
331}
332
333/// Select a source replacement using the resolved input grammar and parser bounds.
334/// Callers resolve options from the original whole file before masking a previous
335/// translation. Syntax failures retain the raw candidate for downstream admission;
336/// a successfully parsed first algorithm owns only its consumed prefix.
337///
338/// # Errors
339/// Syntax/unsupported/resource diagnostics with exact source byte spans.
340#[allow(
341    clippy::too_many_lines,
342    reason = "ordered line/lexical selection retains deciding source spans"
343)]
344pub fn select_rewrite_with_parser(
345    source: &str,
346    options: &RewriteOptions,
347    parser: &crate::Options,
348) -> Result<RewriteSelection, Diagnostic> {
349    if source.len() > options.max_bytes {
350        return Err(failure(
351            Code::Resource,
352            Span::default(),
353            "PlusCal rewrite source byte limit reached",
354        ));
355    }
356    let source_lines = lines(source);
357    for line in &source_lines {
358        for keyword in ["BEGIN", "END"] {
359            if let Some(index) = line.text.find(keyword)
360                && line.text[index + keyword.len()..]
361                    .trim_start_matches([' ', '\t'])
362                    .starts_with("TRANSLATION")
363                && !marker(line.text, keyword)
364            {
365                return Err(failure(
366                    Code::Unsupported,
367                    Span {
368                        start: line.start,
369                        end: line.end,
370                    },
371                    "translation-marker text outside a proved line-comment marker",
372                ));
373            }
374        }
375    }
376
377    let begins: Vec<_> = source_lines
378        .iter()
379        .enumerate()
380        .filter(|(_, l)| marker(l.text, "BEGIN"))
381        .map(|(i, _)| i)
382        .collect();
383    let ends: Vec<_> = source_lines
384        .iter()
385        .enumerate()
386        .filter(|(_, l)| marker(l.text, "END"))
387        .map(|(i, _)| i)
388        .collect();
389    if begins.len() > 1 || ends.len() > 1 {
390        return Err(failure(
391            Code::Unsupported,
392            Span::default(),
393            "ambiguous multiple translation markers",
394        ));
395    }
396    let (previous, previous_body, checksum) = match (begins.first(), ends.first()) {
397        (None, None) => (None, None, None),
398        (Some(&begin), Some(&end)) if begin < end => {
399            let body = Span {
400                start: source_lines[begin].end,
401                end: source_lines[end].start,
402            };
403            let computed = translation_checksum(&source[body.start..body.end]);
404            let old_header = checksum_header(source_lines[begin].text);
405            let recorded = old_header
406                .as_ref()
407                .and_then(|h| h.translation)
408                .map(str::to_owned);
409            let algorithm_recorded = old_header
410                .as_ref()
411                .and_then(|h| h.algorithm)
412                .map(str::to_owned);
413            let algorithm_disabled = old_header.as_ref().is_some_and(|h| h.algorithm.is_none());
414            let matched = recorded.as_ref() == Some(&computed);
415            (
416                Some(Span {
417                    start: source_lines[begin].start,
418                    end: source_lines[end].end,
419                }),
420                Some(body),
421                Some(PreviousChecksum {
422                    recorded,
423                    algorithm_recorded,
424                    algorithm_disabled,
425                    computed,
426                    matched,
427                }),
428            )
429        }
430        _ => {
431            return Err(failure(
432                Code::Syntax,
433                Span::default(),
434                "missing or reversed translation marker pair",
435            ));
436        }
437    };
438    let masked = mask_previous(source, previous);
439    let mut selected = None;
440    let mut module_end = None;
441    for token in dive_syntax::tokens::tokenize_spec(&masked) {
442        let start = usize::from(token.range.start());
443        let end = usize::from(token.range.end());
444        if token.kind == dive_syntax::SyntaxKind::END_MODULE {
445            module_end = Some(start);
446        }
447        if token.kind != dive_syntax::SyntaxKind::BLOCK_COMMENT {
448            continue;
449        }
450        let text = &source[start..end];
451        let offset = algorithm_marker(text);
452        if offset.is_some() {
453            if token.error.is_some() {
454                return Err(failure(
455                    Code::Lexical,
456                    Span { start, end },
457                    "unterminated algorithm comment",
458                ));
459            }
460            let comment = Span { start, end };
461            let candidate = algorithm_comment_region(source, comment).ok_or_else(|| {
462                failure(Code::Syntax, comment, "invalid algorithm comment region")
463            })?;
464            // Low-level source selection is not grammar/artifact admission.
465            // Refine only a proved complete prefix; malformed grammar keeps
466            // its raw region and the strict downstream deciding diagnostic.
467            let region = match owned_algorithm_comment_region(source, comment, parser) {
468                Ok(owned) => owned,
469                Err(error) if matches!(error.code, Code::Resource | Code::Internal) => {
470                    return Err(error);
471                }
472                Err(_) => candidate,
473            };
474            if selected.replace((comment, region)).is_some() {
475                return Err(failure(
476                    Code::Unsupported,
477                    Span { start, end },
478                    "multiple algorithm comments",
479                ));
480            }
481        }
482    }
483    let (comment, algorithm) = selected.ok_or_else(|| {
484        failure(
485            Code::Unsupported,
486            Span::default(),
487            "missing algorithm block comment",
488        )
489    })?;
490    let module_end = module_end.ok_or_else(|| {
491        failure(
492            Code::Syntax,
493            Span::default(),
494            "missing enclosing module terminator",
495        )
496    })?;
497    if previous.is_some_and(|span| span.start < comment.end || span.end > module_end) {
498        return Err(failure(
499            Code::Unsupported,
500            previous.unwrap_or_default(),
501            "translation markers outside the proved post-algorithm module region",
502        ));
503    }
504    let line = source_lines
505        .iter()
506        .find(|line| line.start < comment.end && comment.end <= line.end)
507        .ok_or_else(|| failure(Code::Internal, comment, "missing algorithm closing line"))?;
508    // trans.java uses String.trim(), whose boundary is ASCII space (0x20).
509    let tail = source[comment.end..line.start + line.text.len()]
510        .trim_matches(|character: char| character <= ' ');
511    if !tail.is_empty() && !tail.starts_with("\\*") {
512        return Err(failure(
513            Code::Syntax,
514            comment,
515            "text follows algorithm comment on the same line",
516        ));
517    }
518    Ok(RewriteSelection {
519        comment,
520        algorithm,
521        previous,
522        previous_body,
523        checksum,
524        insertion: line.end,
525    })
526}
527/// Produces a byte-position-preserving masked view for native source generation.
528/// Existing translation lines are blanked, with UTF-8 byte offsets and CR/LF kept.
529#[must_use]
530pub fn mask_previous(source: &str, previous: Option<Span>) -> String {
531    let Some(span) = previous.filter(|s| {
532        s.start <= s.end
533            && source.is_char_boundary(s.start)
534            && source.is_char_boundary(s.end)
535            && s.end <= source.len()
536    }) else {
537        return source.to_owned();
538    };
539    let mut masked = String::with_capacity(source.len());
540    masked.push_str(&source[..span.start]);
541    for byte in source.as_bytes()[span.start..span.end].iter().copied() {
542        masked.push(if byte == b'\r' || byte == b'\n' {
543            char::from(byte)
544        } else {
545            ' '
546        });
547    }
548    masked.push_str(&source[span.end..]);
549    masked
550}
551/// Builds a bounded body-checksummed replacement. Body generation and enclosing
552/// module semantic admission belong to the caller and are never implied here.
553///
554/// # Errors
555/// Changed/unverified body checksum, invalid editing boundaries, or resource limit.
556pub fn rewrite_document(
557    source: &str,
558    body: &str,
559    options: &RewriteOptions,
560) -> Result<RewritePlan, Diagnostic> {
561    rewrite_document_with_translation_options(
562        source,
563        body,
564        options,
565        &crate::TranslationOptions::default(),
566    )
567}
568/// Build a replacement with the exact label/options context used by generation.
569/// Algorithm CRC availability never gates replacement; the body CRC is always written.
570/// A supplied body is never an admitted artifact or model proof by itself.
571///
572/// # Errors
573/// Modified/unverified body integrity, invalid source or byte limits.
574#[allow(
575    clippy::too_many_lines,
576    reason = "source replacement and both integrity contracts remain ordered and bounded"
577)]
578pub fn rewrite_document_with_translation_options(
579    source: &str,
580    body: &str,
581    options: &RewriteOptions,
582    translation: &crate::TranslationOptions,
583) -> Result<RewritePlan, Diagnostic> {
584    if body.len() > options.max_bytes {
585        return Err(failure(
586            Code::Resource,
587            Span::default(),
588            "PlusCal rewrite body byte limit reached",
589        ));
590    }
591    // Validate marker/layout boundaries before resolving original source options.
592    select_rewrite(source, options)?;
593    let context = crate::resolve_translation_options(source, translation)
594        .map_err(|error| error.diagnostic)?;
595    let selection = select_rewrite_with_parser(source, options, &context.effective.parser)?;
596    let masked = mask_previous(source, selection.previous);
597    let mut warnings = Vec::new();
598    let algorithm_checksum = match crate::algorithm_checksum_at(
599        &masked,
600        selection.algorithm.start..selection.algorithm.end,
601        translation,
602    ) {
603        Ok(result) => Some(result.checksum),
604        Err(error) if matches!(error.diagnostic.code, Code::Unsupported | Code::Resource) => {
605            warnings.push(error.diagnostic);
606            None
607        }
608        Err(error) => return Err(error.diagnostic),
609    };
610    if selection.checksum.as_ref().is_some_and(|c| !c.matched) && !options.replace_modified {
611        return Err(failure(
612            Code::Unsupported,
613            selection.previous.unwrap_or_default(),
614            "existing body checksum is modified, missing or disabled; explicit replacement is required",
615        ));
616    }
617    let newline = if options.normalize_newlines {
618        "\n"
619    } else {
620        source.find(['\r', '\n']).map_or("\n", |i| {
621            if source.as_bytes()[i] == b'\r' {
622                if source.as_bytes().get(i + 1) == Some(&b'\n') {
623                    "\r\n"
624                } else {
625                    "\r"
626                }
627            } else {
628                "\n"
629            }
630        })
631    };
632    let checksum = translation_checksum(body);
633    let algorithm_value = algorithm_checksum.as_ref().map_or_else(
634        || "\\in STRING".to_owned(),
635        |checksum| format!("= \"{checksum}\""),
636    );
637    let checksum_header = format!(
638        "\\* BEGIN TRANSLATION (chksum(pcal) {algorithm_value} /\\ chksum(tla) = \"{checksum}\")"
639    );
640    let (header, end_marker) = if let Some(previous) = selection.previous {
641        let old_lines = lines(&source[previous.start..previous.end]);
642        let first = old_lines
643            .first()
644            .ok_or_else(|| failure(Code::Internal, previous, "missing previous header"))?
645            .text;
646        let last = old_lines
647            .last()
648            .ok_or_else(|| failure(Code::Internal, previous, "missing previous end marker"))?
649            .text;
650        let prefix_end = first
651            .find("TRANSLATION")
652            .map(|i| i + "TRANSLATION".len())
653            .ok_or_else(|| failure(Code::Internal, previous, "missing previous marker token"))?;
654        let header = if let Some(old) = self::checksum_header(first) {
655            let mut text = first.to_owned();
656            if let Some(span) = old.translation_hex {
657                text.replace_range(span.start..span.end, &checksum);
658            } else {
659                text.replace_range(
660                    old.translation_value.start..old.translation_value.end,
661                    &format!("= \"{checksum}\""),
662                );
663            }
664            if old.algorithm.is_some() || algorithm_checksum.is_some() {
665                text.replace_range(
666                    old.algorithm_value.start..old.algorithm_value.end,
667                    &algorithm_value,
668                );
669            }
670            text
671        } else if first[prefix_end..].trim().is_empty() {
672            checksum_header.clone()
673        } else {
674            return Err(failure(
675                Code::Unsupported,
676                previous,
677                "unrecognized previous checksum header text",
678            ));
679        };
680        (header, last.to_owned())
681    } else {
682        (checksum_header, "\\* END TRANSLATION ".to_owned())
683    };
684    let span = selection.previous.unwrap_or(Span {
685        start: selection.insertion,
686        end: selection.insertion,
687    });
688    let prefix_newline = span.start > 0 && !source[..span.start].ends_with(['\r', '\n']);
689    let body_lines = lines(body);
690    // PcalTLAGen ends its final Spec/Termination block with an empty line.
691    // Preserve an existing empty line, or add that real separator before END.
692    let final_separator = body_lines.last().is_none_or(|line| !line.text.is_empty());
693    let replacement_bytes = header
694        .len()
695        .checked_add(body_lines.iter().map(|l| l.text.len()).sum::<usize>())
696        .and_then(|s| {
697            s.checked_add(
698                (body_lines.len() + 2 + usize::from(prefix_newline) + usize::from(final_separator))
699                    * newline.len(),
700            )
701        })
702        .and_then(|s| s.checked_add(end_marker.len()))
703        .ok_or_else(|| failure(Code::Resource, span, "PlusCal rewrite output size overflow"))?;
704    let total = source
705        .len()
706        .saturating_sub(span.end - span.start)
707        .checked_add(replacement_bytes)
708        .ok_or_else(|| failure(Code::Resource, span, "PlusCal rewrite output size overflow"))?;
709    if total > options.max_bytes {
710        return Err(failure(
711            Code::Resource,
712            span,
713            "PlusCal rewrite output byte limit reached",
714        ));
715    }
716    let mut replacement = String::with_capacity(replacement_bytes);
717    if prefix_newline {
718        replacement.push_str(newline);
719    }
720    replacement.push_str(&header);
721    replacement.push_str(newline);
722    for line in body_lines {
723        replacement.push_str(line.text);
724        replacement.push_str(newline);
725    }
726    if final_separator {
727        replacement.push_str(newline);
728    }
729    replacement.push_str(&end_marker);
730    replacement.push_str(newline);
731    let mut module_source = String::with_capacity(total);
732    module_source.push_str(&source[..span.start]);
733    module_source.push_str(&replacement);
734    module_source.push_str(&source[span.end..]);
735    if options.normalize_newlines {
736        let normalized = lines(&module_source);
737        let length = normalized.iter().map(|l| l.text.len() + 1).sum::<usize>();
738        if length > options.max_bytes {
739            return Err(failure(
740                Code::Resource,
741                span,
742                "normalized PlusCal rewrite output byte limit reached",
743            ));
744        }
745        let mut text = String::with_capacity(length);
746        for line in normalized {
747            text.push_str(line.text);
748            text.push('\n');
749        }
750        module_source = text;
751    }
752    Ok(RewritePlan {
753        span,
754        replacement,
755        module_source,
756        translation_checksum: checksum,
757        algorithm_checksum,
758        warnings,
759        selection,
760        newlines_normalized: options.normalize_newlines,
761    })
762}