2024-10-03 12:37:56 +08:00
|
|
|
use crate::{
|
|
|
|
ast::{Ident, Location},
|
|
|
|
error::*,
|
|
|
|
token::Tok,
|
|
|
|
};
|
2024-06-12 14:45:03 +08:00
|
|
|
use lalrpop_util::ParseError;
|
2024-10-03 12:37:56 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
use nac3ast::*;
|
2021-11-04 15:00:27 +08:00
|
|
|
|
|
|
|
pub fn make_config_comment(
|
|
|
|
com_loc: Location,
|
|
|
|
stmt_loc: Location,
|
|
|
|
nac3com_above: Vec<(Ident, Tok)>,
|
2024-06-12 14:45:03 +08:00
|
|
|
nac3com_end: Option<Ident>,
|
2021-11-04 15:00:27 +08:00
|
|
|
) -> Result<Vec<Ident>, ParseError<Location, Tok, LexicalError>> {
|
2021-11-06 14:13:17 +08:00
|
|
|
if com_loc.column() != stmt_loc.column() && !nac3com_above.is_empty() {
|
2021-11-04 15:00:27 +08:00
|
|
|
return Err(ParseError::User {
|
|
|
|
error: LexicalError {
|
|
|
|
location: com_loc,
|
|
|
|
error: LexicalErrorType::OtherError(
|
|
|
|
format!(
|
2024-06-12 15:13:09 +08:00
|
|
|
"config comment at top must have the same indentation with what it applies (comment at {com_loc}, statement at {stmt_loc})",
|
2021-11-04 15:00:27 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
});
|
2021-11-04 15:00:27 +08:00
|
|
|
};
|
2024-06-12 14:45:03 +08:00
|
|
|
Ok(nac3com_above
|
|
|
|
.into_iter()
|
|
|
|
.map(|(com, _)| com)
|
|
|
|
.chain(nac3com_end.map_or_else(|| vec![].into_iter(), |com| vec![com].into_iter()))
|
|
|
|
.collect())
|
2021-11-04 15:00:27 +08:00
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn handle_small_stmt<U>(
|
|
|
|
stmts: &mut [Stmt<U>],
|
|
|
|
nac3com_above: Vec<(Ident, Tok)>,
|
|
|
|
nac3com_end: Option<Ident>,
|
|
|
|
com_above_loc: Location,
|
|
|
|
) -> Result<(), ParseError<Location, Tok, LexicalError>> {
|
2021-11-06 14:13:17 +08:00
|
|
|
if com_above_loc.column() != stmts[0].location.column() && !nac3com_above.is_empty() {
|
2021-11-04 15:00:27 +08:00
|
|
|
return Err(ParseError::User {
|
|
|
|
error: LexicalError {
|
|
|
|
location: com_above_loc,
|
|
|
|
error: LexicalErrorType::OtherError(
|
|
|
|
format!(
|
2021-11-06 14:14:40 +08:00
|
|
|
"config comment at top must have the same indentation with what it applies (comment at {}, statement at {})",
|
2021-11-04 15:00:27 +08:00
|
|
|
com_above_loc,
|
|
|
|
stmts[0].location,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
});
|
2021-11-04 15:00:27 +08:00
|
|
|
}
|
2024-06-12 14:45:03 +08:00
|
|
|
apply_config_comments(&mut stmts[0], nac3com_above.into_iter().map(|(com, _)| com).collect());
|
2021-11-04 15:00:27 +08:00
|
|
|
apply_config_comments(
|
|
|
|
stmts.last_mut().unwrap(),
|
2024-06-12 14:45:03 +08:00
|
|
|
nac3com_end.map_or_else(Vec::new, |com| vec![com]),
|
2021-11-04 15:00:27 +08:00
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_config_comments<U>(stmt: &mut Stmt<U>, comments: Vec<Ident>) {
|
|
|
|
match &mut stmt.node {
|
|
|
|
StmtKind::Pass { config_comment, .. }
|
|
|
|
| StmtKind::Delete { config_comment, .. }
|
|
|
|
| StmtKind::Expr { config_comment, .. }
|
|
|
|
| StmtKind::Assign { config_comment, .. }
|
|
|
|
| StmtKind::AugAssign { config_comment, .. }
|
|
|
|
| StmtKind::AnnAssign { config_comment, .. }
|
|
|
|
| StmtKind::Break { config_comment, .. }
|
|
|
|
| StmtKind::Continue { config_comment, .. }
|
2024-06-12 14:45:03 +08:00
|
|
|
| StmtKind::Return { config_comment, .. }
|
2021-11-04 15:00:27 +08:00
|
|
|
| StmtKind::Raise { config_comment, .. }
|
|
|
|
| StmtKind::Import { config_comment, .. }
|
|
|
|
| StmtKind::ImportFrom { config_comment, .. }
|
|
|
|
| StmtKind::Global { config_comment, .. }
|
|
|
|
| StmtKind::Nonlocal { config_comment, .. }
|
|
|
|
| StmtKind::Assert { config_comment, .. } => config_comment.extend(comments),
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
_ => {
|
|
|
|
unreachable!("only small statements should call this function")
|
|
|
|
}
|
2021-11-04 15:00:27 +08:00
|
|
|
}
|
|
|
|
}
|