Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed Macros #250

Open
robcxyz opened this issue Jan 1, 2024 · 0 comments
Open

Typed Macros #250

robcxyz opened this issue Jan 1, 2024 · 0 comments
Labels
proposal Proposal for changes in syntax / behaviour wip A work in progress proposal

Comments

@robcxyz
Copy link
Collaborator

robcxyz commented Jan 1, 2024

Typed Macros

Allow creation of macros from within tackle files

Overview

from typing import Callable

from tackle import Context



KEY_MACROS: list[Callable[[Context, str], dict]]

VALUE_MACROS: list[Callable[[Context, str], dict]]

Macro Types

  • key(context: Context, key: str, value: Any) -> (key: str, value: Any)

  • value(context: Context, key: str, value: Any) -> (key: str, value: Any)

Macros:

  • var_hook_macro(args: list) -> list

    • First arg has

    • value macro

  • blocks_macro(context: 'Context')

    • Rewrite a key to a value dict
  • compact_hook_call_macro(context: 'Context', element: str) -> dict

    • String values to dict values
  • list_to_var_macro(context: 'Context', element: list) -> dict

    • Hooks with keys and list value get turned into var hooks
  • function_field_to_parseable_macro(func_dict: dict, context: 'Context', func_name: str) -> dict

    • Takes the fields in a function and updates them based on if they have hooks and things

Order

Every key:

  • Check for arrows in last chars

    • Signifies block / compact / var hooks

    • value is:

      • string

        • compact / var
      • dict

        • block

      • list

        • list to var

Key Macros

Compact Hook
key->: value
key:

  ->: value
Special Keys
  • return

  • assert

  • import

  • merge

  • raise

  • exit

  • print

  • cmd

  • set

  • append

  • pop

  • delete

  • update

  • generate

  • gen

return
return->: "{{value}}"

return:

  ->: "{{value}}"
return: # Key doesn't matter as value is the only data exported

  ->: return

  value: "{{value}}"
return->:

  "{{value}}": "{{key}}"

return:

  ->:

    "{{value}}": "{{key}}"
return:

  ->: return

  value:

    "{{value}}": "{{key}}"
import
import->: foo/bar

import:

  ->: foo/bar
import->:

  - foo/bar

  - foo/bar2
import:

  ->: import

  providers:

    - foo/bar
assert
assert->: foo==bar

assert:

  ->: foo==bar
assert->:

  - foo==bar

  - foo==bar2
assert:

  ->: assert

  skip_output: true

  providers:

    - foo==bar
merge
merge->:

  foo: bar

merge:

  ->: literal  

  ->: var

  merge: true

  input:

    foo: bar
Expand Keys
key:

  ->: value --a-key foo
key:

  ->: value

  a_key: foo
Block
key:

  ->:

    if: foo == 'bar'

    key: value  
key:

  ->: block

  if: foo == 'bar'

  key: value  

Note: Block hook maps unknown kwargs to items.

Macro could alternatively do this:

key:

  ->: block

  if: foo == 'bar'

  items:

    key: value  

Issues

  • input_dict

    • Old macros didn't only just return a new value but would re-write the input

    • This is because later in the parsing we use the input dict to grab some data

      • parse_sub_context from new_hook from ...

        • If you don't rewrite the input data then you can't access

        • This call can likely be avoided as I am not sure wtf is happening there

        • Looks like we can grab the new context to parse from

@robcxyz robcxyz changed the title tmp Typed Macros Jan 1, 2024
@robcxyz robcxyz added proposal Proposal for changes in syntax / behaviour wip A work in progress proposal labels Jan 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposal for changes in syntax / behaviour wip A work in progress proposal
Projects
None yet
Development

No branches or pull requests

1 participant