vyperlang / vyper

Pythonic Smart Contract Language for the EVM
https://vyperlang.org
Other
4.86k stars 793 forks source link

imported type in tuple declaration leads to parse error #3898

Open cyberthirst opened 7 months ago

cyberthirst commented 7 months ago

Version Information

What's your issue about?

Parsing:

    lib1 = """
struct MyStruct:
    a: uint256
    b: uint256
    """
    main = """
import lib1

initializes: lib1

@external
def foo() -> (uint256, lib1.MyStruct):
    return 42, lib1.MyStruct(a=1, b=2)
    """

leads to:

        except EOFError:
            pass
        except UnexpectedCharacters as e:
            # In the contextual lexer, UnexpectedCharacters can mean that the terminal is defined, but not in the current context.
            # This tests the input against the global context, to provide a nicer error.
            try:
                last_token = lexer_state.last_token  # Save last_token. Calling root_lexer.next_token will change this to the wrong token
                token = self.root_lexer.next_token(lexer_state, parser_state)
>               raise UnexpectedToken(token, e.allowed, state=parser_state, token_history=[last_token], terminals_by_name=self.root_lexer.terminals_by_name)
E               lark.exceptions.UnexpectedToken: Unexpected token Token('DOT', '.') at line 7, column 28.
E               Expected one of: 
E                       * LSQB
E                       * COMMA
E                       * RPAR
E               Previous tokens: [Token('NAME', 'lib1')]

venv/lib/python3.11/site-packages/lark/lexer.py:674: UnexpectedToken

How can it be fixed?

Not sure, but it seems that the tuple_def in the grammar isn't correct because it doesn't allow for imported types:

tuple_def: "(" ( NAME | array_def | dyn_array_def | tuple_def ) ( "," ( NAME | array_def | dyn_array_def | tuple_def ) )* [","] ")"

smth like this might work? didn't test

tuple_def: "(" ( NAME | array_def | dyn_array_def | tuple_def | imported_type ) ( "," ( NAME | array_def | dyn_array_def | tuple_def | imported_type ) )* [","] ")"
charles-cooper commented 7 months ago

Note: only happens from get_contract in the test suite