Working with Michelson code

Expanding macros

class pytezos.michelson.macros.PxrNode(depth, annots, args, is_root)
property annots

Alias for field number 1

property args

Alias for field number 2

property depth

Alias for field number 0

property is_root

Alias for field number 3

pytezos.michelson.macros.build_pxr_tree(pxr_macro, pxr_annots)pytezos.michelson.macros.PxrNode[source]
pytezos.michelson.macros.dip_n(instr, depth=1)[source]
pytezos.michelson.macros.expand_assert(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_assert_cmpx(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_assert_left(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_assert_none(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_assert_right(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_assert_some(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_assert_x(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_caxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_cdxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_cmpx(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_cxr(prim, annots) → list[source]
pytezos.michelson.macros.expand_dixp(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_duxp(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_fail(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_if_right(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_if_some(*args, **kwargs) → dict[source]
pytezos.michelson.macros.expand_ifcmpx(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_ifx(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_macro(prim, annots, args, internal=False, extra=None)[source]

Expands Michelson macro.

Parameters
  • prim – macro name

  • annots – annotations (optional)

  • args – arguments (optional)

  • internal – this function is called during another mocro expansion

  • extra – list of ignored primitives

Returns

Code sequence (Micheline expression)

pytezos.michelson.macros.expand_map_car(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_map_caxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_map_cdr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_map_cdxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_map_cxr(prim, annots, args)[source]
pytezos.michelson.macros.expand_pxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_set_car(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_set_caxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_set_cdr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_set_cdxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_set_cxr(prim, annots)[source]
pytezos.michelson.macros.expand_unpair(*args, **kwargs) → list[source]
pytezos.michelson.macros.expand_unpxr(*args, **kwargs) → list[source]
pytezos.michelson.macros.expr(**kwargs) → dict[source]
pytezos.michelson.macros.get_field_annots(annots)[source]
pytezos.michelson.macros.get_map_cxr_annots(annots) → Tuple[str, list][source]
pytezos.michelson.macros.get_var_annots(annots)[source]
pytezos.michelson.macros.macro(regexp)[source]
pytezos.michelson.macros.seq(instr=None) → list[source]
pytezos.michelson.macros.skip_nones(array)[source]
pytezos.michelson.macros.traverse_pxr_tree(prim, annots, produce)[source]

Parsing AST

class pytezos.michelson.grammar.MichelsonParser(debug=False, write_tables=False, extra_primitives=None)[source]

Customizable Michelson parser

p_annot(p)[source]

annot : ANNOT

p_annots(p)[source]

annots : annot | empty

p_annots_list(p)[source]

annots : annots annot

p_arg_byte(p)[source]

arg : BYTE

p_arg_group(p)[source]

arg : LEFT_PAREN expr RIGHT_PAREN

p_arg_int(p)[source]

arg : INT

p_arg_prim(p)[source]

arg : PRIM

p_arg_str(p)[source]

arg : STR

p_arg_subseq(p)[source]

arg : LEFT_CURLY instr RIGHT_CURLY

p_args(p)[source]

args : arg | empty

p_args_list(p)[source]

args : args arg

p_empty(p)[source]

empty :

p_error(p)[source]
p_expr(p)[source]

expr : PRIM annots args

p_instr(p)[source]

instr : expr | empty

p_instr_byte(p)[source]

instr : BYTE

p_instr_int(p)[source]

instr : INT

p_instr_list(p)[source]

instr : instr SEMI instr

p_instr_str(p)[source]

instr : STR

p_instr_subseq(p)[source]

instr : LEFT_CURLY instr RIGHT_CURLY

parse(code)[source]

Parse Michelson source.

Parameters

code – Michelson source

Returns

Micheline expression

tokens = ('INT', 'BYTE', 'STR', 'ANNOT', 'PRIM', 'LEFT_CURLY', 'RIGHT_CURLY', 'LEFT_PAREN', 'RIGHT_PAREN', 'SEMI')
exception pytezos.michelson.grammar.MichelsonParserError(token: ply.lex.LexToken, message=None)[source]
class pytezos.michelson.grammar.Sequence[source]
class pytezos.michelson.grammar.SimpleMichelsonLexer[source]
t_ANNOT = '[:@%]+([_0-9a-zA-Z\\.]*)?'
t_BYTE = '0x[A-Fa-f0-9]*'
t_INT = '-?[0-9]+'
t_LEFT_CURLY = '\\{'
t_LEFT_PAREN = '\\('
t_PRIM = '[A-Za-z][A-Za-z0-9_]+'
t_RIGHT_CURLY = '\\}'
t_RIGHT_PAREN = '\\)'
t_SEMI = ';'
t_STR = '\\"(\\\\.|[^\\"])*\\"'
t_error(t)[source]
t_ignore = ' \t\r\n\x0c'
t_ignore_COMMENT = '#[^\\n]*'
t_ignore_MULTI_COMMENT = '/\\*.*?\\*/'
tokens = ('INT', 'BYTE', 'STR', 'ANNOT', 'PRIM', 'LEFT_CURLY', 'RIGHT_CURLY', 'LEFT_PAREN', 'RIGHT_PAREN', 'SEMI')

Code formatter

exception pytezos.michelson.formatter.MichelsonFormatterError[source]
pytezos.michelson.formatter.format_timestamp(timestamp: int) → str[source]

Format unix timestamp.

Parameters

timestamp – Unix timestamp (seconds)

pytezos.michelson.formatter.micheline_to_michelson(data, inline=False, wrap=False) → str[source]

Converts micheline expression into formatted Michelson source.

Parameters
  • data – Micheline expression

  • inline – produce single line, used for tezos-client arguments (False by default)

  • wrap – ensure expression is wrapped in brackets