<commit_before>package org.bychan.core.dynamic;

import org.jetbrains.annotations.NotNull;

import java.util.regex.Pattern;

public class TokenDefinitionBuilder<N> {
    private String pattern;
    private DynamicNudParseAction<N> nud;
    private DynamicLedParseAction<N> led;
    private boolean parsed;
    private String tokenName;
    private int leftBindingPower = 1;
    private final TokenDefinitionOwner<N> tokenDefinitionOwner;
    private TokenKey tokenKey;

    public TokenDefinitionBuilder<N> matchesString(String text) {
        return matchesPattern(Pattern.quote(text));
    }

    public TokenDefinitionBuilder<N> matchesPattern(String pattern) {
        this.pattern = pattern;
        return this;
    }

    public TokenDefinitionBuilder(@NotNull TokenDefinitionOwner<N> tokenDefinitionOwner) {
        this.tokenDefinitionOwner = tokenDefinitionOwner;
        parsed = true;
    }

    public TokenDefinitionBuilder<N> nud(DynamicNudParseAction<N> nud) {
        this.nud = nud;
        return this;
    }

    @NotNull
    public TokenDefinition<N> build() {
        if (pattern == null) {
            throw new IllegalStateException("No matching pattern has been set");
        }
        if (tokenName == null) {
            tokenName = "token" + tokenDefinitionOwner.increaseUnnamedTokenCounter();
        }
        TokenDefinition<N> token = new TokenDefinition<>(Pattern.compile(pattern), nud, led, tokenName, parsed, leftBindingPower);
        tokenDefinitionOwner.tokenBuilt(token);
        return token;
    }

    public TokenDefinitionBuilder<N> led(DynamicLedParseAction<N> led) {
        this.led = led;
        return this;
    }

    public TokenDefinitionBuilder<N> ignoredWhenParsing() {
        this.parsed = false;
        return this;
    }

    public TokenDefinitionBuilder<N> named(String name) {
        this.tokenName = name;
        tokenKey = new TokenKey(tokenName);
        return this;
    }

    @NotNull
    public TokenDefinitionBuilder<N> matchesWhitespace() {
        return matchesPattern("\\s+");
    }

    public TokenDefinitionBuilder<N> leftBindingPower(int leftBindingPower) {
        this.leftBindingPower = leftBindingPower;
        return this;
    }

    @NotNull
    public TokenDefinitionBuilder<N> ignoreWhenParsing() {
        return ignoredWhenParsing();
    }

    @NotNull
    public TokenKey getKey() {
        if (tokenName == null) {
            throw new IllegalStateException("You must set a name first");
        }
        return tokenKey;
    }

}
<commit_msg>minor<commit_after>package org.bychan.core.dynamic;

import org.jetbrains.annotations.NotNull;

import java.util.regex.Pattern;

public class TokenDefinitionBuilder<N> {
    private String pattern;
    private DynamicNudParseAction<N> nud;
    private DynamicLedParseAction<N> led;
    private boolean parsed;
    private String tokenName;
    private int leftBindingPower = 1;
    private final TokenDefinitionOwner<N> tokenDefinitionOwner;
    private TokenKey tokenKey;

    public TokenDefinitionBuilder<N> matchesString(String text) {
        return matchesPattern(Pattern.quote(text));
    }

    public TokenDefinitionBuilder<N> matchesPattern(String pattern) {
        this.pattern = pattern;
        return this;
    }

    public TokenDefinitionBuilder(@NotNull TokenDefinitionOwner<N> tokenDefinitionOwner) {
        this.tokenDefinitionOwner = tokenDefinitionOwner;
        parsed = true;
    }

    public TokenDefinitionBuilder<N> nud(DynamicNudParseAction<N> nud) {
        this.nud = nud;
        return this;
    }

    @NotNull
    public TokenDefinition<N> build() {
        if (pattern == null) {
            throw new IllegalStateException("No matching pattern has been set");
        }
        if (tokenName == null) {
            tokenName = "token" + tokenDefinitionOwner.increaseUnnamedTokenCounter();
        }
        TokenDefinition<N> token = new TokenDefinition<>(Pattern.compile(pattern), nud, led, tokenName, parsed, leftBindingPower);
        tokenDefinitionOwner.tokenBuilt(token);
        return token;
    }

    public TokenDefinitionBuilder<N> led(DynamicLedParseAction<N> led) {
        this.led = led;
        return this;
    }

    public TokenDefinitionBuilder<N> ignoredWhenParsing() {
        this.parsed = false;
        return this;
    }

    public TokenDefinitionBuilder<N> named(String name) {
        this.tokenName = name;
        tokenKey = new TokenKey(tokenName);
        return this;
    }

    @NotNull
    public TokenDefinitionBuilder<N> matchesWhitespace() {
        return matchesPattern("\\s+");
    }

    public TokenDefinitionBuilder<N> leftBindingPower(int leftBindingPower) {
        this.leftBindingPower = leftBindingPower;
        return this;
    }

    @NotNull
    public TokenDefinitionBuilder<N> ignoreWhenParsing() {
        return ignoredWhenParsing();
    }

    @NotNull
    public TokenKey getKey() {
        if (tokenKey == null) {
            throw new IllegalStateException("You must set a name first");
        }
        return tokenKey;
    }

}

{'fraction_non_alphanumeric': 0.08712793219002563, 'fraction_numerical': 0.0003942440370589395, 'mean_word_length': 2.894090560245587, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6272112', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1336, 'n_words': 322}