nac3parser: add comment support #68

Merged
pca006132 merged 6 commits from with_nac3comment into master 2021-11-05 20:46:42 +08:00
5 changed files with 20 additions and 19 deletions
Showing only changes of commit b239806558 - Show all commits

View File

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

View File

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

View File

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

View File

@ -196,7 +196,7 @@ impl<'a> Inferencer<'a> {
} }
Ok(false) Ok(false)
} }
StmtKind::If { test, body, orelse } => { StmtKind::If { test, body, orelse, .. } => {
self.check_expr(test, defined_identifiers)?; self.check_expr(test, defined_identifiers)?;
self.should_have_value(test)?; self.should_have_value(test)?;
let mut body_identifiers = defined_identifiers.clone(); let mut body_identifiers = defined_identifiers.clone();
@ -211,7 +211,7 @@ impl<'a> Inferencer<'a> {
} }
Ok(body_returned && orelse_returned) Ok(body_returned && orelse_returned)
} }
StmtKind::While { test, body, orelse } => { StmtKind::While { test, body, orelse, .. } => {
self.check_expr(test, defined_identifiers)?; self.check_expr(test, defined_identifiers)?;
self.should_have_value(test)?; self.should_have_value(test)?;
let mut defined_identifiers = defined_identifiers.clone(); let mut defined_identifiers = defined_identifiers.clone();
@ -230,7 +230,7 @@ impl<'a> Inferencer<'a> {
self.check_block(body, &mut new_defined_identifiers)?; self.check_block(body, &mut new_defined_identifiers)?;
Ok(false) Ok(false)
} }
StmtKind::Expr { value } => { StmtKind::Expr { value, .. } => {
self.check_expr(value, defined_identifiers)?; self.check_expr(value, defined_identifiers)?;
Ok(false) Ok(false)
} }
@ -250,7 +250,7 @@ impl<'a> Inferencer<'a> {
} }
Ok(false) Ok(false)
} }
StmtKind::Return { value } => { StmtKind::Return { value, .. } => {
if let Some(value) = value { if let Some(value) = value {
self.check_expr(value, defined_identifiers)?; self.check_expr(value, defined_identifiers)?;
self.should_have_value(value)?; 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> { fn fold_stmt(&mut self, node: ast::Stmt<()>) -> Result<ast::Stmt<Self::TargetU>, Self::Error> {
let stmt = match node.node { let stmt = match node.node {
// we don't want fold over type annotation // 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)?; self.infer_pattern(&target)?;
let target = Box::new(self.fold_expr(*target)?); let target = Box::new(self.fold_expr(*target)?);
let value = if let Some(v) = value { let value = if let Some(v) = value {
@ -105,14 +105,14 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
Located { Located {
location: node.location, location: node.location,
custom: None, 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, .. } => { ast::StmtKind::For { ref target, .. } => {
self.infer_pattern(target)?; self.infer_pattern(target)?;
fold::fold_stmt(self, node)? 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 targets.iter().all(|t| matches!(t.node, ast::ExprKind::Name { .. })) {
if let ast::StmtKind::Assign { targets, value, .. } = node.node { if let ast::StmtKind::Assign { targets, value, .. } = node.node {
let value = self.fold_expr(*value)?; let value = self.fold_expr(*value)?;
@ -158,6 +158,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
targets, targets,
value: Box::new(value), value: Box::new(value),
type_comment: None, type_comment: None,
config_comment: config_comment.clone()
}, },
custom: None, custom: None,
}); });
@ -198,7 +199,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
} }
} }
ast::StmtKind::AnnAssign { .. } | ast::StmtKind::Expr { .. } => {} 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, .. } => { ast::StmtKind::With { items, .. } => {
for item in items.iter() { for item in items.iter() {
let ty = item.context_expr.custom.unwrap(); 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)) => { (Some(v), Some(v1)) => {
self.unify(v.custom.unwrap(), v1, &v.location)?; self.unify(v.custom.unwrap(), v1, &v.location)?;
} }