use crate::lst_parser::{
    error::SyntaxError,
    grammar::expression::{atom, expression},
    CompletedMarker, Marker, Parser,
    SyntaxKind::*,
};

pub fn attr_set(p: &mut Parser) -> CompletedMarker {
    let start = p.start("attr_set_start");
    assert!(p.eat(L_BRACE));

    loop {
        if attr(p).is_some() {
            // TODO: handle others
            if p.eat(COMMA) {
                continue;
            } else if p.eat(R_BRACE) {
                return start.complete(p, ATTR_SET);
            }
            // TODO: check for newline  and stuff following that for recov of others
        } else if p.eat(R_BRACE) {
            return start.complete(p, ATTR_SET);
        }
    }
}

fn attr(p: &mut Parser) -> Option<CompletedMarker> {
    if p.at(IDENT) {
        let attr_start = p.start("attr");
        let attr_name_start = p.start("attr_name");
        p.do_bump();
        attr_name_start.complete(p, ATTR_NAME);

        // TODO: handle comma, expr/atom, other
        p.eat(COLON);

        // TODO: handle failed expr parser too
        let attr_value = p.start("attr_value");
        let _ = expression(p, false);
        attr_value.complete(p, ATTR_VALUE);
        Some(attr_start.complete(p, ATTR))
    } else {
        None
    }
}
