From 1f3aa48361e03748935cc6083c60223c99ce767d Mon Sep 17 00:00:00 2001 From: ychenfo Date: Thu, 2 Dec 2021 02:54:11 +0800 Subject: [PATCH] nac3parser: modify parser to handle negative integer edge cases --- nac3parser/src/python.lalrpop | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nac3parser/src/python.lalrpop b/nac3parser/src/python.lalrpop index 3a7c6b81..7cfdf3a3 100644 --- a/nac3parser/src/python.lalrpop +++ b/nac3parser/src/python.lalrpop @@ -5,7 +5,7 @@ use std::iter::FromIterator; -use crate::ast; +use crate::ast::{self, Constant}; use crate::fstring::parse_located_fstring; use crate::function::{ArgumentList, parse_args, parse_params}; use crate::error::LexicalError; @@ -916,7 +916,17 @@ Factor: ast::Expr = { => ast::Expr { location, custom: (), - node: ast::ExprKind::UnaryOp { operand: Box::new(e), op } + node: { + match (&op, &e.node) { + (ast::Unaryop::USub, ast::ExprKind::Constant { value: Constant::Int(val), kind }) => { + ast::ExprKind::Constant { + value: Constant::Int(-val), + kind: kind.clone() + } + } + _ => ast::ExprKind::UnaryOp { operand: Box::new(e), op } + } + } }, Power, };