swc-project/swc · error · swc_xml_parser::error::Error
UnexpectedTokenInMainPhase
UnexpectedTokenInMainPhase
Error message
Unexpected token in main phase
What it means
XML parser error in Phase::Main: a token type that is not valid in the main content phase (e.g. doctype) appeared after the root element started. Recorded as ErrorKind::UnexpectedEofInMainPhase-family unexpected-token error and the token is skipped/handled per phase rules.
Source
Thrown at crates/swc_xml_parser/src/parser/mod.rs:373
let processing_instruction = self.create_processing_instruction(token_and_info);
self.append_node(self.get_current_element(), processing_instruction);
}
Token::Cdata { .. } => {
let cdata = self.create_cdata_section(token_and_info);
self.append_node(self.get_current_element(), cdata);
}
Token::Eof => {
self.errors.push(Error::new(
token_and_info.span,
ErrorKind::UnexpectedEofInMainPhase,
));
self.process_token(token_and_info, Some(Phase::EndPhase))?;
}
_ => {
self.errors.push(Error::new(
token_and_info.span,
ErrorKind::UnexpectedTokenInMainPhase,
));
}
},
Phase::EndPhase => match &token_and_info.token {
Token::Comment { .. } => {
self.append_comment_to_doc(token_and_info)?;
}
Token::ProcessingInstruction { .. } => {
self.append_processing_instruction_to_doc(token_and_info)?;
}
Token::Cdata { .. } => {
self.errors.push(Error::new(
token_and_info.span,
ErrorKind::UnexpectedTokenInEndPhase,
));
View on GitHub (pinned to 5176682b65)
Solutions
- Move phase-invalid tokens (such as DOCTYPE) before the root element
- Remove the invalid token from the document body
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/swc_xml_parser/src/parser/mod.rs:373 when the library encounters an invalid state.
Common situations: See trigger scenarios.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/32c7806c79635885.
Report an issue: GitHub.