<commit_before>package com.github.alexfalappa.nbspringboot.cfgprops.parser;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.openide.util.Pair;
import org.parboiled.Action;
import org.parboiled.BaseParser;
import org.parboiled.Context;
import org.parboiled.Rule;
import org.parboiled.support.StringBuilderVar;
import org.parboiled.support.ValueStack;
import org.parboiled.support.Var;

/**
 * Spring Boot configuration properties parser based on Parboiled library.
 * <p>
 * This parser implements a grammar accepting the Java Properties format (with some minor exceptions) and adding dot separated keys, array
 * notation ({@code array[index]=value}.
 * <p>
 * Differences with base Java Properties syntax:
 * <ul>
 * <li>values must be explicitly separated from keys by a <tt>=</tt> (equal sign) or <tt>:</tt> (colon), first occurring whitespace as
 * separator is not supported.
 * </ul>
 *
 * @author <NAME>
 */
public class CfgPropsParboiled extends BaseParser<String> {

    private Properties parsedProps = new Properties();
    private Map<Integer, Pair<String, String>> propLines = new HashMap<>();

    public Properties getParsedProps() {
        return parsedProps;
    }

    public Map<Integer, Pair<String, String>> getPropLines() {
        return propLines;
    }

    public void reset() {
        parsedProps.clear();
        propLines.clear();
    }

    Action<String> actionStoreProp = new Action<String>() {
        @Override
        public boolean run(Context<java.lang.String> context) {
            final ValueStack<String> stack = context.getValueStack();
            int line = context.getPosition().line;
            int size = stack.size();
            switch (size) {
                case 1:
                    String propName = stack.pop();
                    parsedProps.setProperty(propName, "");
                    propLines.put(line, Pair.of(propName, ""));
                    break;
                case 2:
                    // NOTE: stack popping order below is important!
                    final String propValue = stack.pop();
                    propName = stack.pop();
                    parsedProps.setProperty(propName, propValue);
                    propLines.put(line, Pair.of(propName, propValue));
                    break;
                default:
                    throw new IllegalStateException("Zero or more than 2 values on the parsing stack");
            }
            return true;
        }

        @Override
        public java.lang.String toString() {
            return "StorePropsAction";
        }

    };

    Rule cfgProps() {
        return Sequence(
                ZeroOrMore(
                        FirstOf(
                                kvPair(),
                                comment(),
                                whitespace(),
                                eolChar()
                        )
                ),
                EOI
        );<commit_msg>Parser now skips false entries due to recover from syntax errors.<commit_after>package com.github.alexfalappa.nbspringboot.cfgprops.parser;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.openide.util.Pair;
import org.parboiled.Action;
import org.parboiled.BaseParser;
import org.parboiled.Context;
import org.parboiled.Rule;
import org.parboiled.support.StringBuilderVar;
import org.parboiled.support.ValueStack;
import org.parboiled.support.Var;

/**
 * Spring Boot configuration properties parser based on Parboiled library.
 * <p>
 * This parser implements a grammar accepting the Java Properties format (with some minor exceptions) and adding dot separated keys, array
 * notation ({@code array[index]=value}.
 * <p>
 * Differences with base Java Properties syntax:
 * <ul>
 * <li>values must be explicitly separated from keys by a <tt>=</tt> (equal sign) or <tt>:</tt> (colon), first occurring whitespace as
 * separator is not supported.
 * </ul>
 *
 * @author <NAME>
 */
public class CfgPropsParboiled extends BaseParser<String> {

    private Properties parsedProps = new Properties();
    private Map<Integer, Pair<String, String>> propLines = new HashMap<>();

    public Properties getParsedProps() {
        return parsedProps;
    }

    public Map<Integer, Pair<String, String>> getPropLines() {
        return propLines;
    }

    public void reset() {
        parsedProps.clear();
        propLines.clear();
    }

    Action<String> actionStoreProp = new Action<String>() {
        @Override
        public boolean run(Context<java.lang.String> context) {
            final ValueStack<String> stack = context.getValueStack();
            int line = context.getPosition().line;
            int size = stack.size();
            if (!context.hasError()) {
                switch (size) {
                    case 1:
                        String propName = stack.pop();
                        parsedProps.setProperty(propName, "");
                        propLines.put(line, Pair.of(propName, ""));
                        break;
                    case 2:
                        // NOTE: stack popping order below is important!
                        final String propValue = stack.pop();
                        propName = stack.pop();
                        parsedProps.setProperty(propName, propValue);
                        propLines.put(line, Pair.of(propName, propValue));
                        break;
                    default:
                        throw new IllegalStateException(String.format("Cannot manage %d values on the parsing stack", size));
                }
            } else {
                stack.clear();
            }
            return true;
        }

        @Override
        public java.lang.String toString() {
            return "StorePropsAction";
        }

    };

    Rule cfgProps() {
        return Sequence(
                ZeroOrMore(
                        FirstOf(
                                kvPair(),
                                comment(),
                                whitespace(),
                                eolChar()
                        )
                ),
                EOI
        );
{'fraction_non_alphanumeric': 0.09644751647645074, 'fraction_numerical': 0.0008037293039704227, 'mean_word_length': 1.7752007136485282, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '538343', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1504, 'n_words': 429}