diff --git a/muppet/format.py b/muppet/format.py index 870dfd32253dc3186e065b8b3b714e1bf11702cd..8c52eb44e562c10e9670770b8cf47aeb685c5d28 100644 --- a/muppet/format.py +++ b/muppet/format.py @@ -58,6 +58,11 @@ def keyword(x: str) -> Tag: return tag(x, 'keyword', x) +def operator(op: str) -> Tag: + """Tag string as an operator.""" + return tag(op, 'operator') + + def print_hash(hash: list[HashEntry], indent: int, context: Context) -> Tag: @@ -271,7 +276,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: decls += [declare_var(name)] if 'value' in data: decls += [ - ' ', '=', ' ', + ' ', operator('='), ' ', # TODO this is a declaration parse(data.get('value'), indent+1, context), ] @@ -711,7 +716,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: ind(indent+1), tag(key, 'parameter'), ' '*pad, - ' ', '=>', ' ', + ' ', operator('=>'), ' ', parse(value, indent+3, context), ',', '\n', ] @@ -722,7 +727,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: ind(indent+1), tag('*', 'parameter', 'splat'), ' '*pad, - ' ', '=>', ' ', + ' ', operator('=>'), ' ', parse(value, indent+2, context), ',', '\n', ] @@ -751,7 +756,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: ind(indent+1), tag(key, 'parameter'), ' '*pad, - ' ', '=>', ' ', + ' ', operator('=>'), ' ', parse(value, indent+3, context), ',', '\n', ] @@ -762,7 +767,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: ind(indent+1), tag(key, 'parameter'), ' '*pad, - ' ', '+>', ' ', + ' ', operator('+>'), ' ', parse(value, indent+2, context), ',', '\n', ] @@ -773,7 +778,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: ind(indent+1), tag('*', 'parameter', 'splat'), ' '*pad, - ' ', '=>', ' ', + ' ', operator('=>'), ' ', parse(value, indent+2, context), ',', '\n', ] @@ -830,90 +835,90 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: case ['!', x]: return tag([ - '!', ' ', + operator('!'), ' ', parse(x, indent, context), ]) case ['!=', a, b]: return tag([ parse(a, indent, context), - ' ', '!=', ' ', + ' ', operator('!='), ' ', parse(b, indent, context), ]) case ['+', a, b]: return tag([ parse(a, indent, context), - ' ', '+', ' ', + ' ', operator('+'), ' ', parse(b, indent, context), ]) case ['-', a, b]: return tag([ parse(a, indent, context), - ' ', '-', ' ', + ' ', operator('-'), ' ', parse(b, indent, context), ]) case ['-', a]: return tag([ - '-', ' ', + operator('-'), ' ', parse(a, indent, context), ]) case ['*', a, b]: return tag([ parse(a, indent, context), - ' ', '*', ' ', + ' ', operator('*'), ' ', parse(b, indent, context), ]) case ['%', a, b]: return tag([ parse(a, indent, context), - ' ', '%', ' ', + ' ', operator('%'), ' ', parse(b, indent, context), ]) case ['<<', a, b]: return tag([ parse(a, indent, context), - ' ', '<<', ' ', + ' ', operator('<<'), ' ', parse(b, indent, context), ]) case ['>>', a, b]: return tag([ parse(a, indent, context), - ' ', '>>', ' ', + ' ', operator('>>'), ' ', parse(b, indent, context), ]) case ['>=', a, b]: return tag([ parse(a, indent, context), - ' ', '>=', ' ', + ' ', operator('>='), ' ', parse(b, indent, context), ]) case ['<=', a, b]: return tag([ parse(a, indent, context), - ' ', '<=', ' ', + ' ', operator('<='), ' ', parse(b, indent, context), ]) case ['>', a, b]: return tag([ parse(a, indent, context), - ' ', '>', ' ', + ' ', operator('>'), ' ', parse(b, indent, context), ]) case ['<', a, b]: return tag([ parse(a, indent, context), - ' ', '<', ' ', + ' ', operator('<'), ' ', parse(b, indent, context), ]) @@ -922,7 +927,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: parse(left, indent, context), '\n', ind(indent), - '~>', ' ', + operator('~>'), ' ', parse(right, indent, context) ]) @@ -931,7 +936,7 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: parse(left, indent, context), '\n', ind(indent), - '->', ' ', + operator('->'), ' ', parse(right, indent, context), ]) @@ -940,49 +945,49 @@ def parse(form: Any, indent: int, context: list[str]) -> Tag: parse(left, indent, context), '\n', ind(indent), - '.', + operator('.'), parse(right, indent+1, context), ]) case ['/', a, b]: return tag([ parse(a, indent, context), - ' ', '/', ' ', + ' ', operator('/'), ' ', parse(b, indent, context), ]) case ['=', field, value]: return tag([ parse(field, indent, ['declaration'] + context), - ' ', '=', ' ', + ' ', operator('='), ' ', parse(value, indent, context), ], 'declaration') case ['==', a, b]: return tag([ parse(a, indent, context), - ' ', '==', ' ', + ' ', operator('=='), ' ', parse(b, indent, context), ]) case ['=~', a, b]: return tag([ parse(a, indent, context), - ' ', '=~', ' ', + ' ', operator('=~'), ' ', parse(b, indent, context), ]) case ['!~', a, b]: return tag([ parse(a, indent, context), - ' ', '!~', ' ', + ' ', operator('!~'), ' ', parse(b, indent, context), ]) case ['?', condition, cases]: return tag([ parse(condition, indent, context), - ' ', '?', ' ', '{', '\n', + ' ', operator('?'), ' ', '{', '\n', print_hash(cases, indent+1, context), ind(indent), '}',