asdl 1.0.1

Parser for ASDL format. Describes the abstract syntax of compiler intermediate representations and other tree-like data structures
Documentation
{%- macro type(ty) -%}
{% set type_name = ty.id | camel %}
#[derive(PartialEq, Eq, Hash, Debug)]
pub(crate) struct {{type_name}}<'a> {
    {%- for f in ty.fields %}
    {{ self::field(f = f) }},
    {%- endfor %}
}
impl<'a> {{type_name}}<'a> {

    pub(crate) fn new({{ self::args(fs = ty.fields) }}) -> Self {
        {{type_name}}{ {{ self::names(fs = ty.fields) }} }
    }
}
{%- endmacro type -%}

{%- macro names(fs) -%}
{% for f in fs -%}
{{ f.id | snake  }}
{%- if not loop.last -%}, {% endif -%}
{%- endfor -%}
{%- endmacro args -%}

{%- macro args(fs) -%}
{% for f in fs -%}
{{ self::arg(f = f) }}
{%- if not loop.last -%}, {% endif -%}
{%- endfor -%}
{%- endmacro args -%}

{%- macro arg(f) -%}
{%- set field_name = f.id | snake -%}
    {{field_name}}: {{ self::field_type(f = f) }}
{%- endmacro field -%}

{%- macro field(f) -%}
{%- set field_name = f.id | snake -%}
    pub(crate) {{field_name}}: {{ self::field_type(f = f) }}
{%- endmacro field -%}

{%- macro field_type(f) -%}
    {%- if f.is_optional -%}
    Option<{{ self::rust_type(name = f.type_id) }}>
    {%- elif f.is_repeated -%}
    Vec<{{ self::rust_type(name = f.type_id) }}>
    {%- else -%}
    {{ self::rust_type(name = f.type_id) }}
    {%- endif -%}
{%- endmacro field -%}

{%- macro rust_type(name) -%}
    {%- if name == 'str'  -%}
        &'a str
    {%- else -%}
        {{ name | camel }}<'a>
    {%- endif -%}
{%- endmacro rust_type -%}

{%- macro id_type(ty) -%}
{%- set type_name = ty.id | camel -%}
{%- set filed = ty.fields.0 -%}
{%- set field_type = self::field_type(f = filed) -%}
{%- set method_name = filed.id | snake -%}
#[derive(PartialEq, Eq, Hash, Debug)]
pub(crate) struct {{type_name}}<'a>(pub(crate) {{ field_type }});

impl<'a> ToString for {{type_name}}<'a> {

    #[allow(dead_code)]
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}
{%- endmacro type -%}