Skip to main content

Module parser_python

Module parser_python 

Source
Expand description

Python parser for Rúnar contracts (.runar.py).

Parses Python-style contract definitions using a hand-written tokenizer with INDENT/DEDENT tokens and recursive descent parser. Produces the same AST as the TypeScript parser.

§Expected format

from runar import SmartContract, Addr, Sig, PubKey, public, assert_, hash160, check_sig

class P2PKH(SmartContract):
    pub_key_hash: Addr

    def __init__(self, pub_key_hash: Addr):
        super().__init__(pub_key_hash)
        self.pub_key_hash = pub_key_hash

    @public
    def unlock(self, sig: Sig, pub_key: PubKey):
        assert_(hash160(pub_key) == self.pub_key_hash)
        assert_(check_sig(sig, pub_key))

Key mappings:

  • class Foo(SmartContract): -> contract
  • @public decorator -> Visibility::Public
  • self.prop -> PropertyAccess (like this.prop)
  • assert_(expr) or assert expr -> assert(expr)
  • // integer division -> Div in AST (OP_DIV)
  • and/or/not -> And/Or/Not operators
  • ==/!= -> StrictEq/StrictNe
  • Readonly[T] -> readonly property
  • for i in range(n): -> ForStatement
  • snake_case identifiers -> camelCase in AST

Functions§

parse_python
Parse a Python-format Rúnar contract source.