From 0ccbd89070bb159b34e4d6c69a0578da57f15de6 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Thu, 5 Jan 2023 23:06:36 +0900 Subject: [PATCH] Update: match the latest erg --- Cargo.lock | 12 ++--- crates/py2erg/convert.rs | 108 ++++++++++++++++++++++++++------------- src/analyze.rs | 11 +++- src/handle_err.rs | 4 +- 4 files changed, 91 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46141c9..089261b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -253,7 +253,7 @@ checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" [[package]] name = "els" version = "0.1.13" -source = "git+https://github.com/erg-lang/erg?branch=main#754fb2da1374613f50da5ded05c95399dc76d798" +source = "git+https://github.com/erg-lang/erg?branch=main#993bb13bb53dbeb366610cf58d57b3bbcab2b064" dependencies = [ "erg_common", "erg_compiler", @@ -274,7 +274,7 @@ dependencies = [ [[package]] name = "erg_common" version = "0.6.1" -source = "git+https://github.com/erg-lang/erg?branch=main#754fb2da1374613f50da5ded05c95399dc76d798" +source = "git+https://github.com/erg-lang/erg?branch=main#993bb13bb53dbeb366610cf58d57b3bbcab2b064" dependencies = [ "backtrace-on-stack-overflow", "hermit-abi", @@ -285,7 +285,7 @@ dependencies = [ [[package]] name = "erg_compiler" version = "0.6.1" -source = "git+https://github.com/erg-lang/erg?branch=main#754fb2da1374613f50da5ded05c95399dc76d798" +source = "git+https://github.com/erg-lang/erg?branch=main#993bb13bb53dbeb366610cf58d57b3bbcab2b064" dependencies = [ "erg_common", "erg_parser", @@ -294,7 +294,7 @@ dependencies = [ [[package]] name = "erg_parser" version = "0.6.1" -source = "git+https://github.com/erg-lang/erg?branch=main#754fb2da1374613f50da5ded05c95399dc76d798" +source = "git+https://github.com/erg-lang/erg?branch=main#993bb13bb53dbeb366610cf58d57b3bbcab2b064" dependencies = [ "erg_common", "unicode-xid 0.2.4", @@ -527,9 +527,9 @@ dependencies = [ [[package]] name = "object" -version = "0.30.0" +version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239da7f290cfa979f43f85a8efeee9a8a76d0827c356d37f9d3d7254d6b537fb" +checksum = "8d864c91689fdc196779b98dba0aceac6118594c2df6ee5d943eb6a8df4d107a" dependencies = [ "memchr", ] diff --git a/crates/py2erg/convert.rs b/crates/py2erg/convert.rs index beaaff0..32b65f5 100644 --- a/crates/py2erg/convert.rs +++ b/crates/py2erg/convert.rs @@ -63,7 +63,7 @@ impl BlockKind { } } -/// Variables are automatically rewritten with `python_compatible_mode`, +/// Variables are automatically rewritten with `py_compatible`, /// but types are rewritten here because they are complex components used inseparably in the Erg system. fn escape_name(name: String) -> String { match &name[..] { @@ -122,7 +122,12 @@ fn op_to_token(op: Operator) -> Token { } pub fn pyloc_to_ergloc(loc: PyLocation, cont_len: usize) -> erg_common::error::Location { - erg_common::error::Location::range(loc.row(), loc.column(), loc.row(), loc.column() + cont_len) + erg_common::error::Location::range( + loc.row() as u32, + loc.column() as u32, + loc.row() as u32, + (loc.column() + cont_len) as u32, + ) } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -322,17 +327,37 @@ impl ASTConverter { self.names.insert(name.clone(), info); name }; - let token = Token::new(TokenKind::Symbol, cont, loc.row(), loc.column() - 1); + let token = Token::new( + TokenKind::Symbol, + cont, + loc.row() as u32, + loc.column() as u32 - 1, + ); let name = VarName::new(token); - let dot = Token::new(TokenKind::Dot, ".", loc.row(), loc.column() - 1); + let dot = Token::new( + TokenKind::Dot, + ".", + loc.row() as u32, + loc.column() as u32 - 1, + ); Identifier::new(Some(dot), name) } // TODO: module member mangling fn convert_attr_ident(&mut self, name: String, loc: PyLocation) -> Identifier { - let token = Token::new(TokenKind::Symbol, name, loc.row(), loc.column() - 1); + let token = Token::new( + TokenKind::Symbol, + name, + loc.row() as u32, + loc.column() as u32 - 1, + ); let name = VarName::new(token); - let dot = Token::new(TokenKind::Dot, ".", loc.row(), loc.column() - 1); + let dot = Token::new( + TokenKind::Dot, + ".", + loc.row() as u32, + loc.column() as u32 - 1, + ); Identifier::new(Some(dot), name) } @@ -391,7 +416,8 @@ impl ASTConverter { } ExpressionType::Tuple { elements } => { let tmp = fresh_varname(); - let tmp_name = VarName::from_str_and_line((&tmp).into(), expr.location.row()); + let tmp_name = + VarName::from_str_and_line((&tmp).into(), expr.location.row() as u32); let tmp_expr = Expr::Accessor(Accessor::Ident(Identifier::new( Some(DOT), tmp_name.clone(), @@ -401,8 +427,8 @@ impl ASTConverter { let index = Literal::new(Token::new( TokenKind::NatLit, i.to_string(), - elem.location.row(), - elem.location.column() - 1, + elem.location.row() as u32, + elem.location.column() as u32 - 1, )); let (param, mut blocks) = self.convert_expr_to_param(elem); let sig = Signature::Var(VarSignature::new( @@ -426,8 +452,8 @@ impl ASTConverter { let token = Token::new( TokenKind::UBar, "_", - expr.location.row(), - expr.location.column() - 1, + expr.location.row() as u32, + expr.location.column() as u32 - 1, ); ( NonDefaultParamSignature::new(ParamPattern::Discard(token), None), @@ -461,8 +487,8 @@ impl ASTConverter { _other => TypeSpec::Infer(Token::new( TokenKind::UBar, "_", - expr.location.row(), - expr.location.column() - 1, + expr.location.row() as u32, + expr.location.column() as u32 - 1, )), } } @@ -487,8 +513,13 @@ impl ASTConverter { let last = elems.last().unwrap(); (last.location.row(), last.location.column()) }; - let l_brace = Token::new(l_kind, l_cont, expr_loc.row(), expr_loc.column() - 1); - let r_brace = Token::new(r_kind, r_cont, l_end, c_end); + let l_brace = Token::new( + l_kind, + l_cont, + expr_loc.row() as u32, + expr_loc.column() as u32 - 1, + ); + let r_brace = Token::new(r_kind, r_cont, l_end as u32, c_end as u32); (l_brace, r_brace) } @@ -512,7 +543,12 @@ impl ASTConverter { return Expr::Dummy(Dummy::new(vec![])); } }; - let token = Token::new(kind, cont, expr.location.row(), expr.location.column() - 1); + let token = Token::new( + kind, + cont, + expr.location.row() as u32, + expr.location.column() as u32 - 1, + ); Expr::Lit(Literal::new(token)) } ExpressionType::String { value } => { @@ -524,34 +560,34 @@ impl ASTConverter { let token = Token::new( TokenKind::StrLit, value, - expr.location.row(), - expr.location.column() - 2, + expr.location.row() as u32, + expr.location.column() as u32 - 2, ); Expr::Lit(Literal::new(token)) } ExpressionType::False => Expr::Lit(Literal::new(Token::new( TokenKind::BoolLit, "False", - expr.location.row(), - expr.location.column() - 1, + expr.location.row() as u32, + expr.location.column() as u32 - 1, ))), ExpressionType::True => Expr::Lit(Literal::new(Token::new( TokenKind::BoolLit, "True", - expr.location.row(), - expr.location.column() - 1, + expr.location.row() as u32, + expr.location.column() as u32 - 1, ))), ExpressionType::None => Expr::Lit(Literal::new(Token::new( TokenKind::NoneLit, "None", - expr.location.row(), - expr.location.column() - 1, + expr.location.row() as u32, + expr.location.column() as u32 - 1, ))), ExpressionType::Ellipsis => Expr::Lit(Literal::new(Token::new( TokenKind::EllipsisLit, "Ellipsis", - expr.location.row(), - expr.location.column() - 1, + expr.location.row() as u32, + expr.location.column() as u32 - 1, ))), ExpressionType::IfExpression { test, body, orelse } => { let block = self.convert_expr(*body); @@ -861,7 +897,7 @@ impl ASTConverter { let call_ident = Identifier::new(Some(DOT), VarName::from_static("__call__")); let params = Params::new(vec![], None, vec![], None); let class_ident = - Identifier::public_with_line(DOT, self.namespace.last().unwrap().into(), line); + Identifier::public_with_line(DOT, self.namespace.last().unwrap().into(), line as u32); let class_spec = TypeSpec::PreDeclTy(PreDeclTypeSpec::Simple(SimpleTypeSpec::new( class_ident, ConstArgs::empty(), @@ -1038,8 +1074,10 @@ impl ASTConverter { } ExpressionType::Tuple { elements } => { let tmp = fresh_varname(); - let tmp_name = - VarName::from_str_and_line((&tmp).into(), stmt.location.row()); + let tmp_name = VarName::from_str_and_line( + (&tmp).into(), + stmt.location.row() as u32, + ); let tmp_ident = Identifier::new(Some(DOT), tmp_name); let tmp_expr = Expr::Accessor(Accessor::Ident(tmp_ident.clone())); let sig = Signature::Var(VarSignature::new( @@ -1057,8 +1095,8 @@ impl ASTConverter { let index = Literal::new(Token::new( TokenKind::NatLit, i.to_string(), - elem.location.row(), - elem.location.column() - 1, + elem.location.row() as u32, + elem.location.column() as u32 - 1, )); let (param, mut blocks) = self.convert_expr_to_param(elem); let sig = Signature::Var(VarSignature::new( @@ -1309,8 +1347,8 @@ impl ASTConverter { let mod_name = Expr::Lit(Literal::new(Token::new( TokenKind::StrLit, cont, - stmt.location.row(), - stmt.location.column() - 1, + stmt.location.row() as u32, + stmt.location.column() as u32 - 1, ))); let args = Args::new(vec![PosArg::new(mod_name)], vec![], None); let call = import_acc.call_expr(args); @@ -1351,8 +1389,8 @@ impl ASTConverter { let mod_name = Expr::Lit(Literal::new(Token::new( TokenKind::StrLit, cont, - stmt.location.row(), - stmt.location.column() - 1, + stmt.location.row() as u32, + stmt.location.column() as u32 - 1, ))); let args = Args::new(vec![PosArg::new(mod_name)], vec![], None); let call = import_acc.call_expr(args); diff --git a/src/analyze.rs b/src/analyze.rs index 2f8cb79..2fb9e34 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -2,10 +2,12 @@ use erg_common::config::ErgConfig; use erg_common::error::{ErrorCore, ErrorKind, MultiErrorDisplay}; use erg_common::style::{BLUE, GREEN, RED, RESET, YELLOW}; use erg_common::traits::{Runnable, Stream}; +use erg_common::Str; use erg_compiler::artifact::{BuildRunnable, Buildable, CompleteArtifact, IncompleteArtifact}; use erg_compiler::context::ModuleContext; use erg_compiler::erg_parser::ast::AST; use erg_compiler::error::{CompileError, CompileErrors}; +use erg_compiler::global::SharedCompilerResource; use erg_compiler::lower::ASTLowerer; use py2erg::dump_decl_er; use py2erg::{CheckStatus, ShadowingMode}; @@ -53,6 +55,13 @@ impl Runnable for PythonAnalyzer { } impl Buildable for PythonAnalyzer { + fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self { + let mod_name = Str::rc(cfg.input.file_stem()); + Self { + cfg: cfg.copy(), + checker: ASTLowerer::new_with_cache(cfg, mod_name, shared), + } + } fn build(&mut self, code: String, mode: &str) -> Result { self.analyze(code, mode) } @@ -83,7 +92,7 @@ impl PythonAnalyzer { err.to_string(), 0, ErrorKind::SyntaxError, - erg_common::error::Location::Line(err.location.row()), + erg_common::error::Location::Line(err.location.row() as u32), ); let err = CompileError::new(core, self.cfg.input.clone(), "".into()); IncompleteArtifact::new(None, CompileErrors::from(err), CompileErrors::empty()) diff --git a/src/handle_err.rs b/src/handle_err.rs index ae6a9ae..479a6d1 100644 --- a/src/handle_err.rs +++ b/src/handle_err.rs @@ -22,8 +22,8 @@ fn filter_error(_ctx: &ModuleContext, error: CompileError) -> Option { let code = error.input.reread_lines( - error.core.loc.ln_begin().unwrap(), - error.core.loc.ln_end().unwrap(), + error.core.loc.ln_begin().unwrap_or(1) as usize, + error.core.loc.ln_end().unwrap_or(1) as usize, ); if code[0].trim().starts_with("\"\"\"") { None