pub fn parse(
        input: &str,
        init_globals: &[String],
        override_schema: Option<&str>,
        span_registry: &mut Registry,
    ) -> Result<Self, S<Error>> {
        let mut alloc = Allocator {
            override_schema: [None; 2],
            map: HashMap::new(),
            span_registry,
        };
        if let Some(schema) = override_schema {
            alloc.set_schema_name(schema).span_err(Span::default())?;
        }
        let mut template = Self {
            global_exprs: init_globals
                .iter()
                .map(|init_global_input| {
                    let pairs = TemplateParser::parse(Rule::stmt, init_global_input).span_err(Span::default())?;
                    Ok(alloc.stmt_from_pairs(pairs)?.span(Span::default()))
                })
                .collect::<Result<_, _>>()?,
            ..Self::default()
        };

        let pairs = TemplateParser::parse(Rule::create_table, input).span_err(Span::default())?;
        let mut table_map = HashMap::new();
        let mut expected_child_name = None::<S<QName>>;

        for pair in pairs {
            let span = pair.as_span();
            match pair.as_rule() {
                Rule::EOI => {}
                Rule::stmt => template.global_exprs.push(
                    alloc
                        .expr_binary_from_pairs(pair.into_inner())?
                        .span(alloc.register(span)),
                ),
                Rule::single_table => {
                    let table = alloc.table_from_pairs(pair.into_inner())?;
                    let table_name = table.name.unique_name();
                    if let Some(child_name) = &expected_child_name {
                        if child_name.inner.unique_name() != &*table_name {
                            return Err(Error::DerivedTableNameMismatch {
                                for_each_row: child_name.inner.table_name(true).to_owned(),
                                create_table: table.name.table_name(true).to_owned(),
                            }
                            .span(child_name.span));
                        }
                    }
                    table_map.insert(table_name.to_owned(), template.tables.len());
                    template.tables.push(table);
                }
                Rule::dependency_directive => {
                    // register the next table as derived from the specified parent table.
                    let child_index = template.tables.len();
                    let DependencyDirective { parent, child, count } =
                        alloc.dependency_directive_from_pairs(pair.into_inner())?;
                    if let Some(parent_index) = table_map.get(parent.inner.unique_name()) {
                        template.tables[*parent_index].derived.push((child_index, count));
                        expected_child_name = Some(child);
                    } else {
                        return Err(Error::UnknownParentTable {
                            parent: parent.inner.table_name(true).to_owned(),
                        }
                        .span(parent.span));
                    }
                }
                r => unreachable!("Unexpected rule {:?}", r),
            }
        }

        template.variables_count = alloc.map.len();
        Ok(template)
    }
/// Parses a raw string into a structured template.
Parses a raw string into a structured template.
Parses a raw string into a structured template .
pub fn parse(
        input: &str,
        init_globals: &[String],
        override_schema: Option<&str>,
        span_registry: &mut Registry,
    ) -> Result<Self, S<Error>> {
        let mut alloc = Allocator {
            override_schema: [None; 2],
            map: HashMap::new(),
            span_registry,
        };
        if let Some(schema) = override_schema {
            alloc.set_schema_name(schema).span_err(Span::default())?;
        }
        let mut template = Self {
            global_exprs: init_globals
                .iter()
                .map(|init_global_input| {
                    let pairs = TemplateParser::parse(Rule::stmt, init_global_input).span_err(Span::default())?;
                    Ok(alloc.stmt_from_pairs(pairs)?.span(Span::default()))
                })
                .collect::<Result<_, _>>()?,
            ..Self::default()
        };
        let pairs = TemplateParser::parse(Rule::create_table, input).span_err(Span::default())?;
        let mut table_map = HashMap::new();
        let mut expected_child_name = None::<S<QName>>;
        for pair in pairs {
            let span = pair.as_span();
            match pair.as_rule() {
                Rule::EOI => {}
                Rule::stmt => template.global_exprs.push(
                    alloc
                        .expr_binary_from_pairs(pair.into_inner())?
                        .span(alloc.register(span)),
                ),
                Rule::single_table => {
                    let table = alloc.table_from_pairs(pair.into_inner())?;
                    let table_name = table.name.unique_name();
                    if let Some(child_name) = &expected_child_name {
                        if child_name.inner.unique_name() != &*table_name {
                            return Err(Error::DerivedTableNameMismatch {
                                for_each_row: child_name.inner.table_name(true).to_owned(),
                                create_table: table.name.table_name(true).to_owned(),
                            }
                            .span(child_name.span));
                        }
                    }
                    table_map.insert(table_name.to_owned(), template.tables.len());
                    template.tables.push(table);
                }
                Rule::dependency_directive => {
                    // register the next table as derived from the specified parent table.
                    let child_index = template.tables.len();
                    let DependencyDirective { parent, child, count } =
                        alloc.dependency_directive_from_pairs(pair.into_inner())?;
                    if let Some(parent_index) = table_map.get(parent.inner.unique_name()) {
                        template.tables[*parent_index].derived.push((child_index, count));
                        expected_child_name = Some(child);
                    } else {
                        return Err(Error::UnknownParentTable {
                            parent: parent.inner.table_name(true).to_owned(),
                        }
                        .span(parent.span));
                    }
                }
                r => unreachable!("Unexpected rule {:?}", r),
            }
        }
        template.variables_count = alloc.map.len();
        Ok(template)
    }
pub fn parse ( input : & str , init_globals : & [ String ] , override_schema : Option < & str > , span_registry : & mut Registry , ) -> Result < Self , S < Error > > { let mut alloc = Allocator { override_schema : [ None ; 2 ] , map : HashMap :: new ( ) , span_registry , } ; if let Some ( schema ) = override_schema { alloc . set_schema_name ( schema ) . span_err ( Span :: default ( ) ) ? ; } let mut template = Self { global_exprs : init_globals . iter ( ) . map ( | init_global_input | { let pairs = TemplateParser :: parse ( Rule :: stmt , init_global_input ) . span_err ( Span :: default ( ) ) ? ; Ok ( alloc . stmt_from_pairs ( pairs ) ? . span ( Span :: default ( ) ) ) } ) . collect :: < Result < _ , _ > > ( ) ? , .. Self :: default ( ) } ; let pairs = TemplateParser :: parse ( Rule :: create_table , input ) . span_err ( Span :: default ( ) ) ? ; let mut table_map = HashMap :: new ( ) ; let mut expected_child_name = None :: < S < QName > > ; for pair in pairs { let span = pair . as_span ( ) ; match pair . as_rule ( ) { Rule :: EOI => { } Rule :: stmt => template . global_exprs . push ( alloc . expr_binary_from_pairs ( pair . into_inner ( ) ) ? . span ( alloc . register ( span ) ) , ) , Rule :: single_table => { let table = alloc . table_from_pairs ( pair . into_inner ( ) ) ? ; let table_name = table . name . unique_name ( ) ; if let Some ( child_name ) = & expected_child_name { if child_name . inner . unique_name ( ) != & * table_name { return Err ( Error :: DerivedTableNameMismatch { for_each_row : child_name . inner . table_name ( true ) . to_owned ( ) , create_table : table . name . table_name ( true ) . to_owned ( ) , } . span ( child_name . span ) ) ; } } table_map . insert ( table_name . to_owned ( ) , template . tables . len ( ) ) ; template . tables . push ( table ) ; } Rule :: dependency_directive => { // register the next table as derived from the specified parent table. let child_index = template . tables . len ( ) ; let DependencyDirective { parent , child , count } = alloc . dependency_directive_from_pairs ( pair . into_inner ( ) ) ? ; if let Some ( parent_index ) = table_map . get ( parent . inner . unique_name ( ) ) { template . tables [ * parent_index ] . derived . push ( ( child_index , count ) ) ; expected_child_name = Some ( child ) ; } else { return Err ( Error :: UnknownParentTable { parent : parent . inner . table_name ( true ) . to_owned ( ) , } . span ( parent . span ) ) ; } } r => unreachable ! ( " " r ) , } } template . variables_count = alloc . map . len ( ) ; Ok ( template ) }
Parses a raw string into a structured template.
Parses a raw string into a structured template .
input
&str
init_globals
&[String]
override_schema
Option<&str>
span_registry
&mut Registry
input
&str
init_globals
&[String]
override_schema
Option<&str>
span_registry
&mut Registry