package net.ranides.assira.ui.parsers;

import lombok.experimental.UtilityClass;
import net.ranides.assira.grammar.G4Selector;
import net.ranides.assira.grammar.G4SelectorBuilder;
import net.ranides.assira.text.StringUtils;

@UtilityClass
public class ParsePrimitives {

    public static final G4Selector<Integer> INT = new G4SelectorBuilder<Integer>()
        .withName("Primitives:INT")
        .withParser(GrammarBase.PARSER)
        .mapRule("numberInt", node -> {
            return Integer.parseInt(node.text());
        })
        .mapRule("numberHex", node -> {
            return Integer.parseInt(node.text().substring(2));
        })
        .prepare();

    public static final G4Selector<Double> REAL = new G4SelectorBuilder<Double>()
        .withName("Primitives:REAL")
        .withParser(GrammarBase.PARSER)
        .mapRule("numberReal", node -> {
            return Double.parseDouble(node.text());
        })
        .mapRule("numberPct", node -> {
            return Double.parseDouble(StringUtils.substr(node.text(), 0, -1));
        })
        .prepare();

    public static final G4Selector<Number> NUMBER = new G4SelectorBuilder<Number>()
        .withName("Primitives:NUMBER")
        .withParser(GrammarBase.PARSER)
        .inherit(INT)
        .inherit(REAL)
        .prepare();

    public static final G4Selector<String> CODE = new G4SelectorBuilder<String>()
        .withName("Primitives:CODE")
        .withParser(GrammarBase.PARSER)
        .mapRule("codeHex", node -> {
            return node.text();
        })
        .prepare();

}
