Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
added address parsing in tx parameters and named address recognition …
Browse files Browse the repository at this point in the history
…in type parameters
  • Loading branch information
RIg410 committed Jan 18, 2022
1 parent 6f8704d commit 742fdb4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dove/src/tx/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl TryFrom<(&AddressDeclarations, CallDeclarationCmd)> for CallDeclaration {
if let Some(tp) = cmd.type_parameters {
call.set_tp_params(
tp.iter()
.map(|tp| parse_tp_param(tp))
.map(|tp| parse_tp_param(addr_map, tp))
.collect::<Result<_, _>>()?,
);
}
Expand Down
14 changes: 7 additions & 7 deletions dove/src/tx/fn_call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::str::FromStr;
use std::fmt::Debug;
use anyhow::Error;
use dialect::get_context;
use move_symbol_pool::Symbol;
use diem_types::account_config::diem_root_address;
use move_core_types::account_address::AccountAddress;
Expand Down Expand Up @@ -361,15 +362,14 @@ fn parse_address(
arg_value: &str,
addr_map: &AddressDeclarations,
) -> Result<AccountAddress, Error> {
Ok(if arg_value.starts_with("0x") {
AccountAddress::from_hex_literal(arg_value)
.map_err(|err| parse_err(&Type::Address, arg_value, err))?
} else {
addr_map
let ctx = get_context();
match ctx.parse_address(arg_value) {
Ok(addr) => Ok(addr),
Err(_) => addr_map
.get(&Symbol::from(arg_value))
.and_then(|addr| *addr)
.ok_or_else(|| anyhow!("Failed to find address with name:{}", arg_value))?
})
.ok_or_else(|| anyhow!("Failed to find address with name:{}", arg_value)),
}
}

fn parse_err<D: Debug>(tp: &Type, value: &str, err: D) -> Error {
Expand Down
20 changes: 13 additions & 7 deletions dove/src/tx/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub(crate) fn parse_call(addr_map: &AddressDeclarations, call: &str) -> Result<C
.map_err(|err| anyhow!("{}\n\n{:?}", ERROR_MESSAGE, err))?;

let mut call = parse_call_body(addr_map, &mut ctx)?;
call.set_tp_params(parse_type_params(&mut ctx)?);
call.set_tp_params(parse_type_params(addr_map, &mut ctx)?);
call.set_args(parse_args(&mut ctx)?);
Ok(call)
}
Expand Down Expand Up @@ -203,7 +203,10 @@ fn parse_call_body(addr_map: &AddressDeclarations, ctx: &mut Context) -> Result<
})
}

fn parse_type_params(ctx: &mut Context) -> Result<Vec<TypeTag>, Error> {
fn parse_type_params(
addr_map: &AddressDeclarations,
ctx: &mut Context,
) -> Result<Vec<TypeTag>, Error> {
let error_message = "Invalid call script format: Invalid type parameters format.\n\n\
Use pattern:\n\
SCRIPT_FUNCTION_NAME<TYPE1, TYPE2, ...>(PARAM1, PARAM2, ...)\
Expand All @@ -229,7 +232,7 @@ fn parse_type_params(ctx: &mut Context) -> Result<Vec<TypeTag>, Error> {

let type_str = ctx.tokens.content().to_string();
type_parameter.push(
parse_type_param(ctx)
parse_type_param(addr_map, ctx)
.map_err(|_| anyhow!("{}\n\nUnknown: {}", &error_message, type_str))?,
);
}
Expand Down Expand Up @@ -295,25 +298,28 @@ fn parse_args(ctx: &mut Context) -> Result<Vec<String>, Error> {
}
}

pub(crate) fn parse_tp_param(tp: &str) -> Result<TypeTag, Error> {
pub(crate) fn parse_tp_param(addr_map: &AddressDeclarations, tp: &str) -> Result<TypeTag, Error> {
let mut lexer = Lexer::new(tp, FileHash::new("tp"));
let mut env = CompilationEnv::new(Flags::empty(), Default::default());
let mut ctx = Context::new(&mut env, &mut lexer);

ctx.tokens
.advance()
.map_err(|err| Error::msg(format!("{:?}", err)))?;
parse_type_param(&mut ctx)
parse_type_param(addr_map, &mut ctx)
}

/// parse type params
///
/// u8 => TypeTag::U8
/// u64 => TypeTag::U64
/// ...
pub(crate) fn parse_type_param(ctx: &mut Context) -> Result<TypeTag, Error> {
pub(crate) fn parse_type_param(
addr_map: &AddressDeclarations,
ctx: &mut Context,
) -> Result<TypeTag, Error> {
let ty = parse_type(ctx).map_err(|err| Error::msg(format!("{:?}", err)))?;
unwrap_spanned_ty(ty)
unwrap_spanned_ty(addr_map, ty)
}

pub(crate) fn parse_vec<E>(tkn: &str, tp_name: &str) -> Result<Vec<E>, Error>
Expand Down
1 change: 1 addition & 0 deletions lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ move-symbol-pool = { git = "https://github.com/pontem-network/diem.git", branch
move-ir-types = { git = "https://github.com/pontem-network/diem.git", branch = "v1.5.1-pre" }
move-binary-format = { git = "https://github.com/pontem-network/diem.git", branch = "v1.5.1-pre" }
move-command-line-common = { git = "https://github.com/pontem-network/diem.git", branch = "v1.5.1-pre" }
move-package = { git = "https://github.com/pontem-network/diem.git", branch = "v1.5.1-pre" }

# third-party dependencies
anyhow = "1.0.45"
Expand Down
25 changes: 16 additions & 9 deletions lang/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use move_core_types::account_address::AccountAddress;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::{StructTag, TypeTag};
use move_compiler::parser::ast::{LeadingNameAccess_, NameAccessChain_, Type, Type_};
use move_package::source_package::parsed_manifest::AddressDeclarations;

pub fn unwrap_spanned_ty(ty: Type) -> Result<TypeTag, Error> {
fn unwrap_spanned_ty_(ty: Type, this: Option<AccountAddress>) -> Result<TypeTag, Error> {
pub fn unwrap_spanned_ty(addr_map: &AddressDeclarations, ty: Type) -> Result<TypeTag, Error> {
fn unwrap_spanned_ty_(
addr_map: &AddressDeclarations,
ty: Type,
this: Option<AccountAddress>,
) -> Result<TypeTag, Error> {
let st = match ty.value {
Type_::Apply(ma, mut ty_params) => {
match (ma.value, this) {
Expand All @@ -18,7 +23,7 @@ pub fn unwrap_spanned_ty(ty: Type) -> Result<TypeTag, Error> {
"address" => TypeTag::Address,
"signer" => TypeTag::Signer,
"Vec" if ty_params.len() == 1 => TypeTag::Vector(
unwrap_spanned_ty_(ty_params.pop().unwrap(), this)
unwrap_spanned_ty_(addr_map, ty_params.pop().unwrap(), this)
.unwrap()
.into(),
),
Expand All @@ -29,12 +34,14 @@ pub fn unwrap_spanned_ty(ty: Type) -> Result<TypeTag, Error> {
(NameAccessChain_::Two(_, _), None) => {
bail!("Could not parse input: type without module address");
}
(NameAccessChain_::Three(access, name), this) => {
(NameAccessChain_::Three(access, name), _this) => {
let (addr, m_name) = access.value;
let address = match addr.value {
LeadingNameAccess_::AnonymousAddress(addr) => AccountAddress::new(addr.into_bytes()),
LeadingNameAccess_::Name(name) => {
this.ok_or_else(|| anyhow!("Could not parse input: unsupported named address. Name '{}'.", name))?
addr_map.get(&name.value)
.and_then(|addr|addr.to_owned())
.ok_or_else(|| anyhow!("Could not parse input: unsupported named address. Name '{}'.", name))?
}
};
TypeTag::Struct(StructTag {
Expand All @@ -43,7 +50,7 @@ pub fn unwrap_spanned_ty(ty: Type) -> Result<TypeTag, Error> {
name: Identifier::new(name.value.as_str())?,
type_params: ty_params
.into_iter()
.map(|ty| unwrap_spanned_ty_(ty, Some(address)))
.map(|ty| unwrap_spanned_ty_(addr_map, ty, Some(address)))
.map(|res| match res {
Ok(st) => st,
Err(err) => panic!("{:?}", err),
Expand All @@ -65,7 +72,7 @@ pub fn unwrap_spanned_ty(ty: Type) -> Result<TypeTag, Error> {
name: Identifier::new(name.value.as_str())?,
type_params: ty_params
.into_iter()
.map(|ty| unwrap_spanned_ty_(ty, Some(address)))
.map(|ty| unwrap_spanned_ty_(addr_map, ty, Some(address)))
.map(|res| match res {
Ok(st) => st,
Err(err) => panic!("{:?}", err),
Expand All @@ -83,7 +90,7 @@ pub fn unwrap_spanned_ty(ty: Type) -> Result<TypeTag, Error> {
Ok(st)
}

unwrap_spanned_ty_(ty, None)
unwrap_spanned_ty_(addr_map, ty, None)
}

#[cfg(test)]
Expand All @@ -105,7 +112,7 @@ mod tests {
let mut context = Context::new(&mut env, &mut lexer);
let ty = parse_type(&mut context)
.map_err(|err| anyhow!("Query parsing error:\n\t{:?}", err))?;
unwrap_spanned_ty(ty)
unwrap_spanned_ty(&Default::default(), ty)
}

#[test]
Expand Down

0 comments on commit 742fdb4

Please sign in to comment.