1
0
forked from M-Labs/nac3

Merge pull request 'nac3parser: add comment support' (#68) from with_nac3comment into master

Reviewed-on: M-Labs/nac3#68
This commit is contained in:
pca006132 2021-11-05 20:46:42 +08:00
commit 1fea51d9b3
23 changed files with 1384 additions and 139 deletions

View File

@ -10,43 +10,45 @@ module Python
stmt = FunctionDef(identifier name, arguments args,
stmt* body, expr* decorator_list, expr? returns,
string? type_comment)
string? type_comment, identifier* config_comment)
| AsyncFunctionDef(identifier name, arguments args,
stmt* body, expr* decorator_list, expr? returns,
string? type_comment)
string? type_comment, identifier* config_comment)
| ClassDef(identifier name,
expr* bases,
keyword* keywords,
stmt* body,
expr* decorator_list)
| Return(expr? value)
expr* decorator_list, identifier* config_comment)
| Return(expr? value, identifier* config_comment)
| Delete(expr* targets)
| Assign(expr* targets, expr value, string? type_comment)
| AugAssign(expr target, operator op, expr value)
| Delete(expr* targets, identifier* config_comment)
| Assign(expr* targets, expr value, string? type_comment, identifier* config_comment)
| AugAssign(expr target, operator op, expr value, identifier* config_comment)
-- 'simple' indicates that we annotate simple name without parens
| AnnAssign(expr target, expr annotation, expr? value, bool simple)
| AnnAssign(expr target, expr annotation, expr? value, bool simple, identifier* config_comment)
-- use 'orelse' because else is a keyword in target languages
| For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
| AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
| While(expr test, stmt* body, stmt* orelse)
| If(expr test, stmt* body, stmt* orelse)
| With(withitem* items, stmt* body, string? type_comment)
| AsyncWith(withitem* items, stmt* body, string? type_comment)
| For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment, identifier* config_comment)
| AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment, identifier* config_comment)
| While(expr test, stmt* body, stmt* orelse, identifier* config_comment)
| If(expr test, stmt* body, stmt* orelse, identifier* config_comment)
| With(withitem* items, stmt* body, string? type_comment, identifier* config_comment)
| AsyncWith(withitem* items, stmt* body, string? type_comment, identifier* config_comment)
| Raise(expr? exc, expr? cause)
| Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
| Assert(expr test, expr? msg)
| Raise(expr? exc, expr? cause, identifier* config_comment)
| Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody, identifier* config_comment)
| Assert(expr test, expr? msg, identifier* config_comment)
| Import(alias* names)
| ImportFrom(identifier? module, alias* names, int level)
| Import(alias* names, identifier* config_comment)
| ImportFrom(identifier? module, alias* names, int level, identifier* config_comment)
| Global(identifier* names)
| Nonlocal(identifier* names)
| Expr(expr value)
| Pass | Break | Continue
| Global(identifier* names, identifier* config_comment)
| Nonlocal(identifier* names, identifier* config_comment)
| Expr(expr value, identifier* config_comment)
| Pass(identifier* config_comment)
| Break(identifier* config_comment)
| Continue(identifier* config_comment)
-- col_offset is the byte offset in the utf8 string the parser uses
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)

View File

@ -72,7 +72,7 @@ pub fn get_str_from_ref<'a>(lock: &'a MutexGuard<Interner>, id: StrRef) -> &'a s
lock.resolve(id.0).unwrap()
}
type Ident = StrRef;
pub type Ident = StrRef;
#[derive(Clone, Debug, PartialEq)]
pub struct Located<T, U = ()> {
@ -114,6 +114,7 @@ pub enum StmtKind<U = ()> {
decorator_list: Vec<Expr<U>>,
returns: Option<Box<Expr<U>>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
AsyncFunctionDef {
name: Ident,
@ -122,6 +123,7 @@ pub enum StmtKind<U = ()> {
decorator_list: Vec<Expr<U>>,
returns: Option<Box<Expr<U>>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
ClassDef {
name: Ident,
@ -129,28 +131,34 @@ pub enum StmtKind<U = ()> {
keywords: Vec<Keyword<U>>,
body: Vec<Stmt<U>>,
decorator_list: Vec<Expr<U>>,
config_comment: Vec<Ident>,
},
Return {
value: Option<Box<Expr<U>>>,
config_comment: Vec<Ident>,
},
Delete {
targets: Vec<Expr<U>>,
config_comment: Vec<Ident>,
},
Assign {
targets: Vec<Expr<U>>,
value: Box<Expr<U>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
AugAssign {
target: Box<Expr<U>>,
op: Operator,
value: Box<Expr<U>>,
config_comment: Vec<Ident>,
},
AnnAssign {
target: Box<Expr<U>>,
annotation: Box<Expr<U>>,
value: Option<Box<Expr<U>>>,
simple: bool,
config_comment: Vec<Ident>,
},
For {
target: Box<Expr<U>>,
@ -158,6 +166,7 @@ pub enum StmtKind<U = ()> {
body: Vec<Stmt<U>>,
orelse: Vec<Stmt<U>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
AsyncFor {
target: Box<Expr<U>>,
@ -165,61 +174,80 @@ pub enum StmtKind<U = ()> {
body: Vec<Stmt<U>>,
orelse: Vec<Stmt<U>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
While {
test: Box<Expr<U>>,
body: Vec<Stmt<U>>,
orelse: Vec<Stmt<U>>,
config_comment: Vec<Ident>,
},
If {
test: Box<Expr<U>>,
body: Vec<Stmt<U>>,
orelse: Vec<Stmt<U>>,
config_comment: Vec<Ident>,
},
With {
items: Vec<Withitem<U>>,
body: Vec<Stmt<U>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
AsyncWith {
items: Vec<Withitem<U>>,
body: Vec<Stmt<U>>,
type_comment: Option<String>,
config_comment: Vec<Ident>,
},
Raise {
exc: Option<Box<Expr<U>>>,
cause: Option<Box<Expr<U>>>,
config_comment: Vec<Ident>,
},
Try {
body: Vec<Stmt<U>>,
handlers: Vec<Excepthandler<U>>,
orelse: Vec<Stmt<U>>,
finalbody: Vec<Stmt<U>>,
config_comment: Vec<Ident>,
},
Assert {
test: Box<Expr<U>>,
msg: Option<Box<Expr<U>>>,
config_comment: Vec<Ident>,
},
Import {
names: Vec<Alias>,
config_comment: Vec<Ident>,
},
ImportFrom {
module: Option<Ident>,
names: Vec<Alias>,
level: usize,
config_comment: Vec<Ident>,
},
Global {
names: Vec<Ident>,
config_comment: Vec<Ident>,
},
Nonlocal {
names: Vec<Ident>,
config_comment: Vec<Ident>,
},
Expr {
value: Box<Expr<U>>,
config_comment: Vec<Ident>,
},
Pass {
config_comment: Vec<Ident>,
},
Break {
config_comment: Vec<Ident>,
},
Continue {
config_comment: Vec<Ident>,
},
Pass,
Break,
Continue,
}
pub type Stmt<U = ()> = Located<StmtKind<U>, U>;
@ -558,7 +586,7 @@ pub mod fold {
pub fn fold_stmt<U, F: Fold<U> + ?Sized>(#[allow(unused)] folder: &mut F, node: Stmt<U>) -> Result<Stmt<F::TargetU>, F::Error> {
fold_located(folder, node, |folder, node| {
match node {
StmtKind::FunctionDef { name,args,body,decorator_list,returns,type_comment } => {
StmtKind::FunctionDef { name,args,body,decorator_list,returns,type_comment,config_comment } => {
Ok(StmtKind::FunctionDef {
name: Foldable::fold(name, folder)?,
args: Foldable::fold(args, folder)?,
@ -566,9 +594,10 @@ pub mod fold {
decorator_list: Foldable::fold(decorator_list, folder)?,
returns: Foldable::fold(returns, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::AsyncFunctionDef { name,args,body,decorator_list,returns,type_comment } => {
StmtKind::AsyncFunctionDef { name,args,body,decorator_list,returns,type_comment,config_comment } => {
Ok(StmtKind::AsyncFunctionDef {
name: Foldable::fold(name, folder)?,
args: Foldable::fold(args, folder)?,
@ -576,152 +605,176 @@ pub mod fold {
decorator_list: Foldable::fold(decorator_list, folder)?,
returns: Foldable::fold(returns, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::ClassDef { name,bases,keywords,body,decorator_list } => {
StmtKind::ClassDef { name,bases,keywords,body,decorator_list,config_comment } => {
Ok(StmtKind::ClassDef {
name: Foldable::fold(name, folder)?,
bases: Foldable::fold(bases, folder)?,
keywords: Foldable::fold(keywords, folder)?,
body: Foldable::fold(body, folder)?,
decorator_list: Foldable::fold(decorator_list, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Return { value } => {
StmtKind::Return { value,config_comment } => {
Ok(StmtKind::Return {
value: Foldable::fold(value, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Delete { targets } => {
StmtKind::Delete { targets,config_comment } => {
Ok(StmtKind::Delete {
targets: Foldable::fold(targets, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Assign { targets,value,type_comment } => {
StmtKind::Assign { targets,value,type_comment,config_comment } => {
Ok(StmtKind::Assign {
targets: Foldable::fold(targets, folder)?,
value: Foldable::fold(value, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::AugAssign { target,op,value } => {
StmtKind::AugAssign { target,op,value,config_comment } => {
Ok(StmtKind::AugAssign {
target: Foldable::fold(target, folder)?,
op: Foldable::fold(op, folder)?,
value: Foldable::fold(value, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::AnnAssign { target,annotation,value,simple } => {
StmtKind::AnnAssign { target,annotation,value,simple,config_comment } => {
Ok(StmtKind::AnnAssign {
target: Foldable::fold(target, folder)?,
annotation: Foldable::fold(annotation, folder)?,
value: Foldable::fold(value, folder)?,
simple: Foldable::fold(simple, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::For { target,iter,body,orelse,type_comment } => {
StmtKind::For { target,iter,body,orelse,type_comment,config_comment } => {
Ok(StmtKind::For {
target: Foldable::fold(target, folder)?,
iter: Foldable::fold(iter, folder)?,
body: Foldable::fold(body, folder)?,
orelse: Foldable::fold(orelse, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::AsyncFor { target,iter,body,orelse,type_comment } => {
StmtKind::AsyncFor { target,iter,body,orelse,type_comment,config_comment } => {
Ok(StmtKind::AsyncFor {
target: Foldable::fold(target, folder)?,
iter: Foldable::fold(iter, folder)?,
body: Foldable::fold(body, folder)?,
orelse: Foldable::fold(orelse, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::While { test,body,orelse } => {
StmtKind::While { test,body,orelse,config_comment } => {
Ok(StmtKind::While {
test: Foldable::fold(test, folder)?,
body: Foldable::fold(body, folder)?,
orelse: Foldable::fold(orelse, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::If { test,body,orelse } => {
StmtKind::If { test,body,orelse,config_comment } => {
Ok(StmtKind::If {
test: Foldable::fold(test, folder)?,
body: Foldable::fold(body, folder)?,
orelse: Foldable::fold(orelse, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::With { items,body,type_comment } => {
StmtKind::With { items,body,type_comment,config_comment } => {
Ok(StmtKind::With {
items: Foldable::fold(items, folder)?,
body: Foldable::fold(body, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::AsyncWith { items,body,type_comment } => {
StmtKind::AsyncWith { items,body,type_comment,config_comment } => {
Ok(StmtKind::AsyncWith {
items: Foldable::fold(items, folder)?,
body: Foldable::fold(body, folder)?,
type_comment: Foldable::fold(type_comment, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Raise { exc,cause } => {
StmtKind::Raise { exc,cause,config_comment } => {
Ok(StmtKind::Raise {
exc: Foldable::fold(exc, folder)?,
cause: Foldable::fold(cause, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Try { body,handlers,orelse,finalbody } => {
StmtKind::Try { body,handlers,orelse,finalbody,config_comment } => {
Ok(StmtKind::Try {
body: Foldable::fold(body, folder)?,
handlers: Foldable::fold(handlers, folder)?,
orelse: Foldable::fold(orelse, folder)?,
finalbody: Foldable::fold(finalbody, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Assert { test,msg } => {
StmtKind::Assert { test,msg,config_comment } => {
Ok(StmtKind::Assert {
test: Foldable::fold(test, folder)?,
msg: Foldable::fold(msg, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Import { names } => {
StmtKind::Import { names,config_comment } => {
Ok(StmtKind::Import {
names: Foldable::fold(names, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::ImportFrom { module,names,level } => {
StmtKind::ImportFrom { module,names,level,config_comment } => {
Ok(StmtKind::ImportFrom {
module: Foldable::fold(module, folder)?,
names: Foldable::fold(names, folder)?,
level: Foldable::fold(level, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Global { names } => {
StmtKind::Global { names,config_comment } => {
Ok(StmtKind::Global {
names: Foldable::fold(names, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Nonlocal { names } => {
StmtKind::Nonlocal { names,config_comment } => {
Ok(StmtKind::Nonlocal {
names: Foldable::fold(names, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Expr { value } => {
StmtKind::Expr { value,config_comment } => {
Ok(StmtKind::Expr {
value: Foldable::fold(value, folder)?,
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Pass { } => {
StmtKind::Pass { config_comment } => {
Ok(StmtKind::Pass {
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Break { } => {
StmtKind::Break { config_comment } => {
Ok(StmtKind::Break {
config_comment: Foldable::fold(config_comment, folder)?,
})
}
StmtKind::Continue { } => {
StmtKind::Continue { config_comment } => {
Ok(StmtKind::Continue {
config_comment: Foldable::fold(config_comment, folder)?,
})
}
}

View File

@ -186,7 +186,7 @@ pub fn gen_while<'ctx, 'a, G: CodeGenerator + ?Sized>(
ctx: &mut CodeGenContext<'ctx, 'a>,
stmt: &Stmt<Option<Type>>,
) {
if let StmtKind::While { test, body, orelse } = &stmt.node {
if let StmtKind::While { test, body, orelse, .. } = &stmt.node {
let current = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
let test_bb = ctx.ctx.append_basic_block(current, "test");
let body_bb = ctx.ctx.append_basic_block(current, "body");
@ -228,7 +228,7 @@ pub fn gen_if<'ctx, 'a, G: CodeGenerator + ?Sized>(
ctx: &mut CodeGenContext<'ctx, 'a>,
stmt: &Stmt<Option<Type>>,
) -> bool {
if let StmtKind::If { test, body, orelse } = &stmt.node {
if let StmtKind::If { test, body, orelse, .. } = &stmt.node {
let current = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
let test_bb = ctx.ctx.append_basic_block(current, "test");
let body_bb = ctx.ctx.append_basic_block(current, "body");
@ -306,11 +306,11 @@ pub fn gen_stmt<'ctx, 'a, G: CodeGenerator + ?Sized>(
stmt: &Stmt<Option<Type>>,
) -> bool {
match &stmt.node {
StmtKind::Pass => {}
StmtKind::Expr { value } => {
StmtKind::Pass { .. } => {}
StmtKind::Expr { value, .. } => {
generator.gen_expr(ctx, value);
}
StmtKind::Return { value } => {
StmtKind::Return { value, .. } => {
let value = value.as_ref().map(|v| generator.gen_expr(ctx, v).unwrap());
let value = value.as_ref().map(|v| v as &dyn BasicValue);
ctx.builder.build_return(value);
@ -328,11 +328,11 @@ pub fn gen_stmt<'ctx, 'a, G: CodeGenerator + ?Sized>(
generator.gen_assign(ctx, target, value);
}
}
StmtKind::Continue => {
StmtKind::Continue { .. } => {
ctx.builder.build_unconditional_branch(ctx.loop_bb.unwrap().0);
return true;
}
StmtKind::Break => {
StmtKind::Break { .. }=> {
ctx.builder.build_unconditional_branch(ctx.loop_bb.unwrap().1);
return true;
}

View File

@ -1439,8 +1439,8 @@ impl TopLevelComposer {
return Err("unsupported statement type in class definition body".into());
}
}
ast::StmtKind::Pass => {}
ast::StmtKind::Expr { value: _ } => {} // typically a docstring; ignoring all expressions matches CPython behavior
ast::StmtKind::Pass { .. } => {}
ast::StmtKind::Expr { value: _, .. } => {} // typically a docstring; ignoring all expressions matches CPython behavior
_ => return Err("unsupported statement type in class definition body".into()),
}
}

View File

@ -330,7 +330,7 @@ impl TopLevelComposer {
ast::StmtKind::With { body, .. } => {
result.extend(Self::get_all_assigned_field(body.as_slice())?);
}
ast::StmtKind::Pass => {}
ast::StmtKind::Pass { .. } => {}
ast::StmtKind::Assert { .. } => {}
ast::StmtKind::Expr { .. } => {}

View File

@ -196,7 +196,7 @@ impl<'a> Inferencer<'a> {
}
Ok(false)
}
StmtKind::If { test, body, orelse } => {
StmtKind::If { test, body, orelse, .. } => {
self.check_expr(test, defined_identifiers)?;
self.should_have_value(test)?;
let mut body_identifiers = defined_identifiers.clone();
@ -211,7 +211,7 @@ impl<'a> Inferencer<'a> {
}
Ok(body_returned && orelse_returned)
}
StmtKind::While { test, body, orelse } => {
StmtKind::While { test, body, orelse, .. } => {
self.check_expr(test, defined_identifiers)?;
self.should_have_value(test)?;
let mut defined_identifiers = defined_identifiers.clone();
@ -230,7 +230,7 @@ impl<'a> Inferencer<'a> {
self.check_block(body, &mut new_defined_identifiers)?;
Ok(false)
}
StmtKind::Expr { value } => {
StmtKind::Expr { value, .. } => {
self.check_expr(value, defined_identifiers)?;
Ok(false)
}
@ -250,7 +250,7 @@ impl<'a> Inferencer<'a> {
}
Ok(false)
}
StmtKind::Return { value } => {
StmtKind::Return { value, .. } => {
if let Some(value) = value {
self.check_expr(value, defined_identifiers)?;
self.should_have_value(value)?;

View File

@ -80,7 +80,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
fn fold_stmt(&mut self, node: ast::Stmt<()>) -> Result<ast::Stmt<Self::TargetU>, Self::Error> {
let stmt = match node.node {
// we don't want fold over type annotation
ast::StmtKind::AnnAssign { target, annotation, value, simple } => {
ast::StmtKind::AnnAssign { target, annotation, value, simple, config_comment } => {
self.infer_pattern(&target)?;
let target = Box::new(self.fold_expr(*target)?);
let value = if let Some(v) = value {
@ -105,14 +105,14 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
Located {
location: node.location,
custom: None,
node: ast::StmtKind::AnnAssign { target, annotation, value, simple },
node: ast::StmtKind::AnnAssign { target, annotation, value, simple, config_comment },
}
}
ast::StmtKind::For { ref target, .. } => {
self.infer_pattern(target)?;
fold::fold_stmt(self, node)?
}
ast::StmtKind::Assign { ref targets, .. } => {
ast::StmtKind::Assign { ref targets, ref config_comment, .. } => {
if targets.iter().all(|t| matches!(t.node, ast::ExprKind::Name { .. })) {
if let ast::StmtKind::Assign { targets, value, .. } = node.node {
let value = self.fold_expr(*value)?;
@ -158,6 +158,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
targets,
value: Box::new(value),
type_comment: None,
config_comment: config_comment.clone()
},
custom: None,
});
@ -198,7 +199,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
}
}
ast::StmtKind::AnnAssign { .. } | ast::StmtKind::Expr { .. } => {}
ast::StmtKind::Break | ast::StmtKind::Continue | ast::StmtKind::Pass => {}
ast::StmtKind::Break { .. } | ast::StmtKind::Continue { .. } | ast::StmtKind::Pass { .. } => {}
ast::StmtKind::With { items, .. } => {
for item in items.iter() {
let ty = item.context_expr.custom.unwrap();
@ -272,7 +273,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
}
}
}
ast::StmtKind::Return { value } => match (value, self.function_data.return_type) {
ast::StmtKind::Return { value, .. } => match (value, self.function_data.return_type) {
(Some(v), Some(v1)) => {
self.unify(v.custom.unwrap(), v1, &v.location)?;
}

View File

@ -0,0 +1,85 @@
use lalrpop_util::ParseError;
use nac3ast::*;
use crate::ast::Ident;
use crate::ast::Location;
use crate::token::Tok;
use crate::error::*;
pub fn make_config_comment(
com_loc: Location,
stmt_loc: Location,
nac3com_above: Vec<(Ident, Tok)>,
nac3com_end: Option<Ident>
) -> Result<Vec<Ident>, ParseError<Location, Tok, LexicalError>> {
if com_loc.column() != stmt_loc.column() {
return Err(ParseError::User {
error: LexicalError {
location: com_loc,
error: LexicalErrorType::OtherError(
format!(
"config comment at top must have the same indentation with what it applies, comment at {}, statement at {}",
com_loc,
stmt_loc,
)
)
}
})
};
Ok(
nac3com_above
.into_iter()
.map(|(com, _)| com)
.chain(nac3com_end.map_or_else(|| vec![].into_iter(), |com| vec![com].into_iter()))
.collect()
)
}
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() {
return Err(ParseError::User {
error: LexicalError {
location: com_above_loc,
error: LexicalErrorType::OtherError(
format!(
"config comment at top must have the same indentation with what it applies, comment at {}, statement at {}",
com_above_loc,
stmts[0].location,
)
)
}
})
}
apply_config_comments(
&mut stmts[0],
nac3com_above
.into_iter()
.map(|(com, _)| com).collect()
);
apply_config_comments(
stmts.last_mut().unwrap(),
nac3com_end.map_or_else(Vec::new, |com| vec![com])
);
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, .. }
| StmtKind::Return { config_comment, .. }
| 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),
_ => { unreachable!("only small statements should call this function") }
}
}

View File

@ -65,6 +65,7 @@ pub struct Lexer<T: Iterator<Item = char>> {
chr1: Option<char>,
chr2: Option<char>,
location: Location,
config_comment_prefix: Option<&'static str>
}
pub static KEYWORDS: phf::Map<&'static str, Tok> = phf::phf_map! {
@ -196,6 +197,7 @@ where
location: start,
chr1: None,
chr2: None,
config_comment_prefix: Some(" nac3:")
};
lxr.next_char();
lxr.next_char();
@ -415,17 +417,45 @@ where
}
}
/// Skip everything until end of line
fn lex_comment(&mut self) {
/// Skip everything until end of line, may produce nac3 pseudocomment
fn lex_comment(&mut self) -> Option<Spanned> {
self.next_char();
// if possibly nac3 pseudocomment, special handling for `# nac3:`
let (mut prefix, mut is_comment) = self
.config_comment_prefix
.map_or_else(|| ("".chars(), false), |v| (v.chars(), true));
// for the correct location of config comment
let mut start_loc = self.location;
start_loc.go_left();
loop {
match self.chr0 {
Some('\n') => return,
Some(_) => {}
None => return,
Some('\n') => return None,
None => return None,
Some(c) => {
if let (true, Some(p)) = (is_comment, prefix.next()) {
is_comment = is_comment && c == p
} else {
// done checking prefix, if is comment then return the spanned
if is_comment {
let mut content = String::new();
loop {
match self.chr0 {
Some('\n') | None => break,
Some(c) => content.push(c),
}
self.next_char();
}
return Some((
start_loc,
Tok::ConfigComment { content: content.trim().into() },
self.location
));
}
}
}
}
self.next_char();
};
}
fn unicode_literal(&mut self, literal_number: usize) -> Result<char, LexicalError> {
@ -658,10 +688,11 @@ where
}
/// Given we are at the start of a line, count the number of spaces and/or tabs until the first character.
fn eat_indentation(&mut self) -> Result<IndentationLevel, LexicalError> {
fn eat_indentation(&mut self) -> Result<(IndentationLevel, Option<Spanned>), LexicalError> {
// Determine indentation:
let mut spaces: usize = 0;
let mut tabs: usize = 0;
let mut nac3comment: Option<Spanned> = None;
loop {
match self.chr0 {
Some(' ') => {
@ -693,7 +724,14 @@ where
tabs += 1;
}
Some('#') => {
self.lex_comment();
nac3comment = self.lex_comment();
// if is nac3comment, we need to add newline, so it is not begin of line
// and we should break from the loop, else in the next loop it will be
// regarded as a empty line
if nac3comment.is_some() {
self.at_begin_of_line = false;
break;
}
spaces = 0;
tabs = 0;
}
@ -722,11 +760,12 @@ where
}
}
Ok(IndentationLevel { tabs, spaces })
Ok((IndentationLevel { tabs, spaces }, nac3comment))
}
fn handle_indentations(&mut self) -> Result<(), LexicalError> {
let indentation_level = self.eat_indentation()?;
let eat_result = self.eat_indentation()?;
let indentation_level = eat_result.0;
if self.nesting == 0 {
// Determine indent or dedent:
@ -770,6 +809,10 @@ where
}
}
}
};
if let Some(comment) = eat_result.1 {
self.emit(comment);
}
Ok(())
@ -833,7 +876,9 @@ where
self.emit(number);
}
'#' => {
self.lex_comment();
if let Some(c) = self.lex_comment() {
self.emit(c);
};
}
'"' | '\'' => {
let string = self.lex_string(false, false, false, false)?;
@ -1287,6 +1332,85 @@ mod tests {
lexer.map(|x| x.unwrap().1).collect()
}
#[test]
fn test_nac3comment() {
let src = "\
a: int32
# nac3:
b: int64";
let tokens = lex_source(src);
assert_eq!(
tokens,
vec![
Tok::Name { name: "a".into() },
Tok::Colon,
Tok::Name { name: "int32".into() },
Tok::Newline,
Tok::ConfigComment { content: "".into() },
Tok::Newline,
Tok::Name { name: "b".into() },
Tok::Colon,
Tok::Name { name: "int64".into() },
Tok::Newline,
]
);
}
#[test]
fn test_class_lex_with_nac3comment() {
use Tok::*;
let source = "\
class Foo(A, B):
# normal comment
# nac3: no indent
# nac3: correct indent
b: int32
a: int32 # nac3: no need indent
def __init__(self):
pass";
let tokens = lex_source(source);
assert_eq!(
tokens,
vec![
Class,
Name { name: "Foo".into() },
Lpar,
Name { name: "A".into() },
Comma,
Name { name: "B".into() },
Rpar,
Colon,
Newline,
ConfigComment { content: "no indent".into() },
Newline,
Indent,
ConfigComment { content: "correct indent".into() },
Newline,
Name { name: "b".into() },
Colon,
Name { name: "int32".into() },
Newline,
Name { name: "a".into() },
Colon,
Name { name: "int32".into() },
ConfigComment { content: "no need indent".into() },
Newline,
Def,
Name { name: "__init__".into() },
Lpar,
Name { name: "self".into() },
Rpar,
Colon,
Newline,
Indent,
Pass,
Newline,
Dedent,
Dedent
]
)
}
#[test]
fn test_newline_processor() {
// Escape \ followed by \n (by removal):

View File

@ -32,3 +32,4 @@ lalrpop_mod!(
python
);
pub mod token;
pub mod config_comment_helper;

View File

@ -164,4 +164,67 @@ class Foo(A, B):
let parse_ast = parse_expression(&source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_more_comment() {
let source = "\
a: int # nac3: sf1
# nac3: sdf4
for i in (1, '12'): # nac3: sf2
a: int
# nac3: 3
# nac3: 5
while i < 2: # nac3: 4
# nac3: real pass
pass
# nac3: expr1
# nac3: expr3
1 + 2 # nac3: expr2
# nac3: if3
# nac3: if1
if 1: # nac3: if2
3";
insta::assert_debug_snapshot!(parse_program(&source).unwrap());
}
#[test]
fn test_sample_comment() {
let source = "\
# nac3: while1
# nac3: while2
# normal comment
while test: # nac3: while3
# nac3: simple assign0
a = 3 # nac3: simple assign1
";
insta::assert_debug_snapshot!(parse_program(&source).unwrap());
}
#[test]
fn test_comment_ambiguity() {
let source = "\
if a: d; # nac3: for d
if b: c # nac3: for c
if d: # nac3: for if d
b; b + 3; # nac3: for b + 3
a = 3; a + 3; b = a; # nac3: notif
# nac3: smallsingle1
# nac3: smallsingle3
aa = 3 # nac3: smallsingle2
if a: # nac3: small2
a
for i in a: # nac3: for1
pass
";
insta::assert_debug_snapshot!(parse_program(&source).unwrap());
}
#[test]
fn test_comment_should_fail() {
let source = "\
if a: # nac3: something
a = 3
";
assert!(parse_program(&source).is_err());
}
}

View File

@ -9,8 +9,11 @@ use crate::ast;
use crate::fstring::parse_located_fstring;
use crate::function::{ArgumentList, parse_args, parse_params};
use crate::error::LexicalError;
use crate::error::LexicalErrorType;
use crate::lexer;
use crate::config_comment_helper::*;
use lalrpop_util::ParseError;
use num_bigint::BigInt;
grammar;
@ -47,10 +50,11 @@ Statement: ast::Suite = {
};
SimpleStatement: ast::Suite = {
<s1:SmallStatement> <s2:(";" SmallStatement)*> ";"? "\n" => {
<com_loc:@L> <nac3com_above:(config_comment "\n")*> <s1:SmallStatement> <s2:(";" SmallStatement)*> ";"? <nac3com_end:config_comment?> "\n" =>? {
let mut statements = vec![s1];
statements.extend(s2.into_iter().map(|e| e.1));
statements
handle_small_stmt(&mut statements, nac3com_above, nac3com_end, com_loc)?;
Ok(statements)
}
};
@ -70,7 +74,7 @@ PassStatement: ast::Stmt = {
ast::Stmt {
location,
custom: (),
node: ast::StmtKind::Pass,
node: ast::StmtKind::Pass { config_comment: vec![] },
}
},
};
@ -80,7 +84,7 @@ DelStatement: ast::Stmt = {
ast::Stmt {
location,
custom: (),
node: ast::StmtKind::Delete { targets },
node: ast::StmtKind::Delete { targets, config_comment: vec![] },
}
},
};
@ -92,7 +96,7 @@ ExpressionStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Expr { value: Box::new(expression) }
node: ast::StmtKind::Expr { value: Box::new(expression), config_comment: vec![] }
}
} else {
let mut targets = vec![expression];
@ -107,7 +111,7 @@ ExpressionStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Assign { targets, value, type_comment: None },
node: ast::StmtKind::Assign { targets, value, type_comment: None, config_comment: vec![] },
}
}
},
@ -118,7 +122,8 @@ ExpressionStatement: ast::Stmt = {
node: ast::StmtKind::AugAssign {
target: Box::new(target),
op,
value: Box::new(rhs)
value: Box::new(rhs),
config_comment: vec![],
},
}
},
@ -132,6 +137,7 @@ ExpressionStatement: ast::Stmt = {
annotation: Box::new(annotation),
value: rhs.map(Box::new),
simple,
config_comment: vec![],
},
}
},
@ -187,28 +193,28 @@ FlowStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Break,
node: ast::StmtKind::Break { config_comment: vec![] },
}
},
<location:@L> "continue" => {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Continue,
node: ast::StmtKind::Continue { config_comment: vec![] },
}
},
<location:@L> "return" <value:TestList?> => {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Return { value: value.map(Box::new) },
node: ast::StmtKind::Return { value: value.map(Box::new), config_comment: vec![] },
}
},
<location:@L> <expression:YieldExpr> => {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Expr { value: Box::new(expression) },
node: ast::StmtKind::Expr { value: Box::new(expression), config_comment: vec![] },
}
},
RaiseStatement,
@ -219,14 +225,14 @@ RaiseStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Raise { exc: None, cause: None },
node: ast::StmtKind::Raise { exc: None, cause: None, config_comment: vec![] },
}
},
<location:@L> "raise" <t:Test> <c:("from" Test)?> => {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Raise { exc: Some(Box::new(t)), cause: c.map(|x| Box::new(x.1)) },
node: ast::StmtKind::Raise { exc: Some(Box::new(t)), cause: c.map(|x| Box::new(x.1)), config_comment: vec![] },
}
},
};
@ -236,7 +242,7 @@ ImportStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Import { names },
node: ast::StmtKind::Import { names, config_comment: vec![] },
}
},
<location:@L> "from" <source:ImportFromLocation> "import" <names: ImportAsNames> => {
@ -247,7 +253,8 @@ ImportStatement: ast::Stmt = {
node: ast::StmtKind::ImportFrom {
level,
module: module.map(|s| s.into()),
names
names,
config_comment: vec![]
},
}
},
@ -301,7 +308,7 @@ GlobalStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Global { names }
node: ast::StmtKind::Global { names, config_comment: vec![] }
}
},
};
@ -311,7 +318,7 @@ NonlocalStatement: ast::Stmt = {
ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Nonlocal { names }
node: ast::StmtKind::Nonlocal { names, config_comment: vec![] }
}
},
};
@ -323,7 +330,8 @@ AssertStatement: ast::Stmt = {
location,
node: ast::StmtKind::Assert {
test: Box::new(test),
msg: msg.map(|e| Box::new(e.1))
msg: msg.map(|e| Box::new(e.1)),
config_comment: vec![],
}
}
},
@ -340,7 +348,7 @@ CompoundStatement: ast::Stmt = {
};
IfStatement: ast::Stmt = {
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> => {
<location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> "if" <test:NamedExpressionTest> ":" <nac3com_end:config_comment?> <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> =>? {
// Determine last else:
let mut last = s3.map(|s| s.2).unwrap_or_default();
@ -349,54 +357,74 @@ IfStatement: ast::Stmt = {
let x = ast::Stmt {
custom: (),
location: i.0,
node: ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last },
node: ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last, config_comment: vec![] },
};
last = vec![x];
}
ast::Stmt {
Ok(ast::Stmt {
custom: (),
location,
node: ast::StmtKind::If { test: Box::new(test), body, orelse: last }
node: ast::StmtKind::If {
test: Box::new(test),
body,
orelse: last,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
})
},
};
WhileStatement: ast::Stmt = {
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
<location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> "while" <test:NamedExpressionTest> ":" <nac3com_end:config_comment?> <body:Suite> <s2:("else" ":" Suite)?> =>? {
let orelse = s2.map(|s| s.2).unwrap_or_default();
ast::Stmt {
Ok(ast::Stmt {
custom: (),
location,
node: ast::StmtKind::While {
test: Box::new(test),
body,
orelse
orelse,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
},
}
})
},
};
ForStatement: ast::Stmt = {
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
<location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <nac3com_end:config_comment?> <body:Suite> <s2:("else" ":" Suite)?> =>? {
let orelse = s2.map(|s| s.2).unwrap_or_default();
let target = Box::new(target);
let iter = Box::new(iter);
let type_comment = None;
let node = if is_async.is_some() {
ast::StmtKind::AsyncFor { target, iter, body, orelse, type_comment }
ast::StmtKind::AsyncFor {
target,
iter,
body,
orelse,
type_comment,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
} else {
ast::StmtKind::For { target, iter, body, orelse, type_comment }
ast::StmtKind::For {
target,
iter,
body,
orelse,
type_comment,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
};
ast::Stmt::new(location, node)
Ok(ast::Stmt::new(location, node))
},
};
TryStatement: ast::Stmt = {
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> => {
<location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> "try" ":" <nac3com_end:config_comment?> <body:Suite> <handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> =>? {
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
let finalbody = finally.map(|s| s.2).unwrap_or_default();
ast::Stmt {
Ok(ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Try {
@ -404,14 +432,15 @@ TryStatement: ast::Stmt = {
handlers,
orelse,
finalbody,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
},
}
})
},
<location:@L> "try" ":" <body:Suite> <finally:("finally" ":" Suite)> => {
<location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> "try" ":" <nac3com_end:config_comment?> <body:Suite> <finally:("finally" ":" Suite)> =>? {
let handlers = vec![];
let orelse = vec![];
let finalbody = finally.2;
ast::Stmt {
Ok(ast::Stmt {
custom: (),
location,
node: ast::StmtKind::Try {
@ -419,8 +448,9 @@ TryStatement: ast::Stmt = {
handlers,
orelse,
finalbody,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
},
}
})
},
};
@ -448,14 +478,24 @@ ExceptClause: ast::Excepthandler = {
};
WithStatement: ast::Stmt = {
<location:@L> <is_async:"async"?> "with" <items:OneOrMore<WithItem>> ":" <body:Suite> => {
<location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> <is_async:"async"?> "with" <items:OneOrMore<WithItem>> ":" <nac3com_end:config_comment?> <body:Suite> =>? {
let type_comment = None;
let node = if is_async.is_some() {
ast::StmtKind::AsyncWith { items, body, type_comment }
ast::StmtKind::AsyncWith {
items,
body,
type_comment,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
} else {
ast::StmtKind::With { items, body, type_comment }
ast::StmtKind::With {
items,
body,
type_comment,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
};
ast::Stmt::new(location, node)
Ok(ast::Stmt::new(location, node))
},
};
@ -468,16 +508,32 @@ WithItem: ast::Withitem = {
};
FuncDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" Test)?> ":" <body:Suite> => {
<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> =>? {
let args = Box::new(args);
let returns = r.map(|x| Box::new(x.1));
let type_comment = None;
let node = if is_async.is_some() {
ast::StmtKind::AsyncFunctionDef { name, args, body, decorator_list, returns, type_comment }
ast::StmtKind::AsyncFunctionDef {
name,
args,
body,
decorator_list,
returns,
type_comment,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
} else {
ast::StmtKind::FunctionDef { name, args, body, decorator_list, returns, type_comment }
ast::StmtKind::FunctionDef {
name,
args,
body,
decorator_list,
returns,
type_comment,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
}
};
ast::Stmt::new(location, node)
Ok(ast::Stmt::new(location, node))
},
};
@ -615,12 +671,12 @@ KwargParameter<ArgType>: Option<Box<ast::Arg>> = {
};
ClassDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <body:Suite> => {
<decorator_list:Decorator*> <location:@L> <nac3com_above:(config_comment "\n")*> <stmt_loc:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <nac3com_end:config_comment?> <body:Suite> =>? {
let (bases, keywords) = match a {
Some((_, arg, _)) => (arg.args, arg.keywords),
None => (vec![], vec![]),
};
ast::Stmt {
Ok(ast::Stmt {
custom: (),
location,
node: ast::StmtKind::ClassDef {
@ -629,8 +685,9 @@ ClassDef: ast::Stmt = {
keywords,
body,
decorator_list,
config_comment: make_config_comment(location, stmt_loc, nac3com_above, nac3com_end)?
},
}
})
},
};
@ -1301,6 +1358,7 @@ extern {
string => lexer::Tok::String { value: <String>, is_fstring: <bool> },
bytes => lexer::Tok::Bytes { value: <Vec<u8>> },
name => lexer::Tok::Name { name: <ast::StrRef> },
config_comment => lexer::Tok::ConfigComment { content: <ast::StrRef> },
"\n" => lexer::Tok::Newline,
";" => lexer::Tok::Semi,
}

View File

@ -0,0 +1,448 @@
---
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 1,
column: 4,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Name {
id: "d",
ctx: Load,
},
},
config_comment: [
"for d",
],
},
},
],
orelse: [],
config_comment: [],
},
},
Located {
location: Location {
row: 2,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 2,
column: 4,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 2,
column: 7,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 2,
column: 7,
},
custom: (),
node: Name {
id: "c",
ctx: Load,
},
},
config_comment: [
"for c",
],
},
},
],
orelse: [],
config_comment: [],
},
},
Located {
location: Location {
row: 3,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 3,
column: 4,
},
custom: (),
node: Name {
id: "d",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
config_comment: [],
},
},
Located {
location: Location {
row: 4,
column: 8,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 4,
column: 10,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 4,
column: 8,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
op: Add,
right: Located {
location: Location {
row: 4,
column: 12,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
},
},
config_comment: [
"for b + 3",
],
},
},
],
orelse: [],
config_comment: [
"for if d",
],
},
},
Located {
location: Location {
row: 5,
column: 1,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 5,
column: 1,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 5,
column: 5,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
type_comment: None,
config_comment: [],
},
},
Located {
location: Location {
row: 5,
column: 8,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 5,
column: 10,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 5,
column: 8,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
op: Add,
right: Located {
location: Location {
row: 5,
column: 12,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
},
},
config_comment: [],
},
},
Located {
location: Location {
row: 5,
column: 15,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 5,
column: 15,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 5,
column: 19,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
type_comment: None,
config_comment: [
"notif",
],
},
},
Located {
location: Location {
row: 8,
column: 1,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 8,
column: 1,
},
custom: (),
node: Name {
id: "aa",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 8,
column: 6,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
type_comment: None,
config_comment: [
"smallsingle1",
"smallsingle3",
"smallsingle2",
],
},
},
Located {
location: Location {
row: 9,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 9,
column: 4,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 10,
column: 5,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 10,
column: 5,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
config_comment: [],
},
},
],
orelse: [],
config_comment: [
"small2",
],
},
},
Located {
location: Location {
row: 11,
column: 1,
},
custom: (),
node: For {
target: Located {
location: Location {
row: 11,
column: 5,
},
custom: (),
node: Name {
id: "i",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 11,
column: 10,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 12,
column: 5,
},
custom: (),
node: Pass {
config_comment: [],
},
},
],
orelse: [],
type_comment: None,
config_comment: [
"for1",
],
},
},
]

View File

@ -0,0 +1,313 @@
---
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: AnnAssign {
target: Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
annotation: Located {
location: Location {
row: 1,
column: 4,
},
custom: (),
node: Name {
id: "int",
ctx: Load,
},
},
value: None,
simple: true,
config_comment: [
"sf1",
],
},
},
Located {
location: Location {
row: 2,
column: 1,
},
custom: (),
node: For {
target: Located {
location: Location {
row: 3,
column: 5,
},
custom: (),
node: Name {
id: "i",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 3,
column: 11,
},
custom: (),
node: Tuple {
elts: [
Located {
location: Location {
row: 3,
column: 11,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
Located {
location: Location {
row: 3,
column: 15,
},
custom: (),
node: Constant {
value: Str(
"12",
),
kind: None,
},
},
],
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: AnnAssign {
target: Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
annotation: Located {
location: Location {
row: 4,
column: 8,
},
custom: (),
node: Name {
id: "int",
ctx: Load,
},
},
value: None,
simple: true,
config_comment: [],
},
},
],
orelse: [],
type_comment: None,
config_comment: [
"sdf4",
"sf2",
],
},
},
Located {
location: Location {
row: 5,
column: 1,
},
custom: (),
node: While {
test: Located {
location: Location {
row: 7,
column: 9,
},
custom: (),
node: Compare {
left: Located {
location: Location {
row: 7,
column: 7,
},
custom: (),
node: Name {
id: "i",
ctx: Load,
},
},
ops: [
Lt,
],
comparators: [
Located {
location: Location {
row: 7,
column: 11,
},
custom: (),
node: Constant {
value: Int(
2,
),
kind: None,
},
},
],
},
},
body: [
Located {
location: Location {
row: 9,
column: 5,
},
custom: (),
node: Pass {
config_comment: [
"real pass",
],
},
},
Located {
location: Location {
row: 12,
column: 5,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 12,
column: 7,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 12,
column: 5,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
op: Add,
right: Located {
location: Location {
row: 12,
column: 9,
},
custom: (),
node: Constant {
value: Int(
2,
),
kind: None,
},
},
},
},
config_comment: [
"expr1",
"expr3",
"expr2",
],
},
},
Located {
location: Location {
row: 13,
column: 5,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 15,
column: 8,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
body: [
Located {
location: Location {
row: 16,
column: 9,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 16,
column: 9,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
config_comment: [],
},
},
],
orelse: [],
config_comment: [
"if3",
"if1",
"if2",
],
},
},
],
orelse: [],
config_comment: [
"3",
"5",
"4",
],
},
},
]

View File

@ -1,6 +1,7 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
@ -74,12 +75,15 @@ expression: parse_program(&source).unwrap()
column: 3,
},
custom: (),
node: Pass,
node: Pass {
config_comment: [],
},
},
],
decorator_list: [],
returns: None,
type_comment: None,
config_comment: [],
},
},
Located {
@ -145,16 +149,20 @@ expression: parse_program(&source).unwrap()
column: 3,
},
custom: (),
node: Pass,
node: Pass {
config_comment: [],
},
},
],
decorator_list: [],
returns: None,
type_comment: None,
config_comment: [],
},
},
],
decorator_list: [],
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
@ -45,6 +45,7 @@ expression: parse_ast
kind: None,
},
},
config_comment: [],
},
},
],
@ -90,6 +91,7 @@ expression: parse_ast
kind: None,
},
},
config_comment: [],
},
},
],
@ -114,12 +116,15 @@ expression: parse_ast
kind: None,
},
},
config_comment: [],
},
},
],
config_comment: [],
},
},
],
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
@ -73,6 +73,7 @@ expression: parse_ast
],
},
},
config_comment: [],
},
},
]

View File

@ -1,6 +1,7 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
[
Located {
@ -85,6 +86,7 @@ expression: parse_ast
},
},
},
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
@ -60,6 +60,7 @@ expression: parse_ast
keywords: [],
},
},
config_comment: [],
},
},
]

View File

@ -1,6 +1,7 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
[
Located {
@ -46,6 +47,7 @@ expression: parse_ast
keywords: [],
},
},
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
@ -86,6 +86,7 @@ expression: parse_program(&source).unwrap()
},
},
type_comment: None,
config_comment: [],
},
},
]

View File

@ -0,0 +1,75 @@
---
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: While {
test: Located {
location: Location {
row: 4,
column: 7,
},
custom: (),
node: Name {
id: "test",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 6,
column: 5,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 6,
column: 5,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 6,
column: 9,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
type_comment: None,
config_comment: [
"simple assign0",
"simple assign1",
],
},
},
],
orelse: [],
config_comment: [
"while1",
"while2",
"while3",
],
},
},
]

View File

@ -13,6 +13,7 @@ pub enum Tok {
Complex { real: f64, imag: f64 },
String { value: String, is_fstring: bool },
Bytes { value: Vec<u8> },
ConfigComment { content: ast::StrRef },
Newline,
Indent,
Dedent,
@ -134,6 +135,7 @@ impl fmt::Display for Tok {
}
f.write_str("\"")
}
ConfigComment { content } => write!(f, "ConfigComment: '{}'", ast::get_str_from_ref(&ast::get_str_ref_lock(), *content)),
Newline => f.write_str("Newline"),
Indent => f.write_str("Indent"),
Dedent => f.write_str("Dedent"),