@@[target("rust")]

// Dogfooded body closer — Go language brace matcher.
// Go has the same syntax as Java for braces, double-quoted strings,
// and comments, PLUS backtick raw strings (`...`).
//
// State machine flow:
//   $Init.scan() → $Scanning.$>() ↔ $InString/$InCharLiteral/$InLineComment/$InBlockComment/$InRawString

@@system GoBodyCloserFsm {
    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'`' {
                        self.pos += 1;
                        -> $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();
            }
        }

        $InRawString {
            $>() {
                // Go raw strings: `...` — no escape sequences, just scan to closing backtick
                let n = self.bytes.len();
                while self.pos < n {
                    if self.bytes[self.pos] == b'`' {
                        self.pos += 1;
                        -> $Scanning
                    }
                    self.pos += 1;
                }
                self.error_kind = 1;
                self.error_msg = "unterminated raw 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();
            }
        }

    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()
}
