package net.ranides.assira.ui.parsers;

import lombok.experimental.UtilityClass;
import net.ranides.assira.grammar.G4Node;
import net.ranides.assira.grammar.G4Parser;
import net.ranides.assira.grammar.properties.G4NodeProperty;
import net.ranides.grammars.generated.UiGrammarParser;
import net.ranides.grammars.generated.UiGrammarLexer;

import javax.swing.*;

@UtilityClass
public class GrammarBase {

    public static final G4NodeProperty<JComponent> COMPONENT = G4NodeProperty.of(JComponent.class, "component");

    public static final G4Parser PARSER = G4Parser.builder()
        .lexer(UiGrammarLexer.class)
        .parser(UiGrammarParser.class)
        .build();

    public static JComponent component(G4Node node) {
        return node.get(GrammarBase.COMPONENT).orElseThrow(() -> new IllegalArgumentException("There is no associated JComponent with node + " + node));
    }

    public static JComponent root(G4Node node) {
        return component(node).getRootPane();
    }

}
