@@[target("rust")]

// Dogfooded body closer — C++ language brace matcher.
// Extends C with C++ raw string literals: R"delim(...)delim"
//
// State machine flow:
//   $Init.scan() → $Scanning.$>() ↔ $InString/$InCharLiteral/$InLineComment/$InBlockComment/$InRawString

@@system CppBodyCloserFsm {
    interface:
        scan()

    machine:
        $Init {
            scan() {
                -> $Scanning
            }
        }

        $Scanning {
            $>() {
                let n = self.bytes.len();
                while self.pos < n {
                    let b = self.bytes[self.pos];
                    if b == b'\n' {
                        self.pos += 1;
                    } else if b == b'/' && self.pos + 1 < n && self.bytes[self.pos + 1] == b'/' {
                        self.pos += 2;
                        -> $InLineComment
                    } else if b == b'/' && self.pos + 1 < n && self.bytes[self.pos + 1] == b'*' {
                        self.pos += 2;
                        -> $InBlockComment
                    } else if b == b'\'' {
                        self.pos += 1;
                        -> $InCharLiteral
                    } else if b == b'"' {
                        self.pos += 1;
                        -> $InString
                    } else if b == b'R' && self.pos + 1 < n && self.bytes[self.pos + 1] == b'"' {
                        // C++ raw string R"delim(...)delim"
                        let mut j = self.pos + 2;
                        let mut delim: Vec<u8> = Vec::new();
                        while j < n && self.bytes[j] != b'(' {
                            delim.push(self.bytes[j]);
                            j += 1;
                            if delim.len() > 32 { break; }
                        }
                        if j >= n || self.bytes[j] != b'(' {
                            self.pos += 1;
                            continue;
                        }
                        j += 1;
                        self.raw_delim = delim;
                        self.pos = j;
                        -> $InRawString
                    } else if b == b'{' {
                        self.depth += 1;
                        self.pos += 1;
                    } else if b == b'}' {
                        self.depth -= 1;
                        self.pos += 1;
                        if self.depth == 0 {
                            self.result_pos = self.pos - 1;
                            self.error_kind = 0;
                            return
                        }
                    } else {
                        self.pos += 1;
                    }
                }
                self.error_kind = 3;
                self.error_msg = "body not closed".to_string();
            }
        }

        $InString {
            $>() {
                let n = self.bytes.len();
                while self.pos < n {
                    if self.bytes[self.pos] == b'\\' {
                        self.pos += 2;
                        continue;
                    }
                    if self.bytes[self.pos] == b'"' {
                        self.pos += 1;
                        -> $Scanning
                    }
                    self.pos += 1;
                }
                self.error_kind = 1;
                self.error_msg = "unterminated string".to_string();
            }
        }

        $InCharLiteral {
            $>() {
                let n = self.bytes.len();
                while self.pos < n {
                    if self.bytes[self.pos] == b'\\' {
                        self.pos += 2;
                        continue;
                    }
                    if self.bytes[self.pos] == b'\'' {
                        self.pos += 1;
                        -> $Scanning
                    }
                    self.pos += 1;
                }
                self.error_kind = 1;
                self.error_msg = "unterminated char".to_string();
            }
        }

        $InLineComment {
            $>() {
                let n = self.bytes.len();
                while self.pos < n && self.bytes[self.pos] != b'\n' {
                    self.pos += 1;
                }
                -> $Scanning
            }
        }

        $InBlockComment {
            $>() {
                let n = self.bytes.len();
                while self.pos + 1 < n {
                    if self.bytes[self.pos] == b'*' && self.bytes[self.pos + 1] == b'/' {
                        self.pos += 2;
                        -> $Scanning
                    }
                    self.pos += 1;
                }
                self.error_kind = 2;
                self.error_msg = "unterminated comment".to_string();
            }
        }

        $InRawString {
            $>() {
                // Find closing )delim"
                let n = self.bytes.len();
                loop {
                    if self.pos >= n {
                        self.error_kind = 4;
                        self.error_msg = "unterminated raw".to_string();
                        return
                    }
                    if self.bytes[self.pos] == b')' {
                        let mut k = self.pos + 1;
                        let mut m: usize = 0;
                        while m < self.raw_delim.len() && k < n && self.bytes[k] == self.raw_delim[m] {
                            k += 1;
                            m += 1;
                        }
                        if m == self.raw_delim.len() && k < n && self.bytes[k] == b'"' {
                            self.pos = k + 1;
                            -> $Scanning
                        }
                    }
                    self.pos += 1;
                }
            }
        }

    domain:
        bytes: Vec<u8> = Vec::new()
        pos: usize = 0
        depth: i32 = 1
        result_pos: usize = 0
        error_kind: usize = 0
        error_msg: String = String::new()
        raw_delim: Vec<u8> = Vec::new()
}
