forked from M-Labs/nac3
1
0
Fork 0

nac3parser: fix decorator and above comments

This commit is contained in:
ychenfo 2021-11-06 14:13:17 +08:00
parent 7d66195eae
commit efc9edbc14
2 changed files with 6 additions and 6 deletions

View File

@ -11,13 +11,13 @@ pub fn make_config_comment(
nac3com_above: Vec<(Ident, Tok)>, nac3com_above: Vec<(Ident, Tok)>,
nac3com_end: Option<Ident> nac3com_end: Option<Ident>
) -> Result<Vec<Ident>, ParseError<Location, Tok, LexicalError>> { ) -> Result<Vec<Ident>, ParseError<Location, Tok, LexicalError>> {
if com_loc.column() != stmt_loc.column() { if com_loc.column() != stmt_loc.column() && !nac3com_above.is_empty() {
return Err(ParseError::User { return Err(ParseError::User {
error: LexicalError { error: LexicalError {
location: com_loc, location: com_loc,
error: LexicalErrorType::OtherError( error: LexicalErrorType::OtherError(
format!( format!(
"config comment at top must have the same indentation with what it applies, comment at {}, statement at {}", "config comment at top must have the same indentation with what it applies(comment at {}, statement at {})",
com_loc, com_loc,
stmt_loc, stmt_loc,
) )
@ -35,13 +35,13 @@ pub fn make_config_comment(
} }
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>> { 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>> {
if com_above_loc.column() != stmts[0].location.column() { if com_above_loc.column() != stmts[0].location.column() && !nac3com_above.is_empty() {
return Err(ParseError::User { return Err(ParseError::User {
error: LexicalError { error: LexicalError {
location: com_above_loc, location: com_above_loc,
error: LexicalErrorType::OtherError( error: LexicalErrorType::OtherError(
format!( format!(
"config comment at top must have the same indentation with what it applies, comment at {}, statement at {}", "config comment at top must have the same indentation with what it applies(comment at {}, statement at {})",
com_above_loc, com_above_loc,
stmts[0].location, stmts[0].location,
) )

View File

@ -508,7 +508,7 @@ WithItem: ast::Withitem = {
}; };
FuncDef: ast::Stmt = { FuncDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" Test)?> ":" <nac3com_end:config_comment?> <body:Suite> =>? { <location:@L> <nac3com_above:(config_comment "\n")*> <decorator_list:Decorator*> <stmt_loc:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" Test)?> ":" <nac3com_end:config_comment?> <body:Suite> =>? {
let args = Box::new(args); let args = Box::new(args);
let returns = r.map(|x| Box::new(x.1)); let returns = r.map(|x| Box::new(x.1));
let type_comment = None; let type_comment = None;
@ -671,7 +671,7 @@ KwargParameter<ArgType>: Option<Box<ast::Arg>> = {
}; };
ClassDef: ast::Stmt = { ClassDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <nac3com_end:config_comment?> <body:Suite> =>? { <location:@L> <nac3com_above:(config_comment "\n")*> <decorator_list:Decorator*> <stmt_loc:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <nac3com_end:config_comment?> <body:Suite> =>? {
let (bases, keywords) = match a { let (bases, keywords) = match a {
Some((_, arg, _)) => (arg.args, arg.keywords), Some((_, arg, _)) => (arg.args, arg.keywords),
None => (vec![], vec![]), None => (vec![], vec![]),