<commit_before>/* Copyright © 2016 <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of mattunderscore.com nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.mattunderscore.specky;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.UnbufferedTokenStream;

import com.mattunderscore.specky.dsl.SpecBuilder;
import com.mattunderscore.specky.dsl.model.DSLSpecDesc;
import com.mattunderscore.specky.parser.Specky;
import com.mattunderscore.specky.parser.SpeckyLexer;

/**
 * @author <NAME> on 03/07/2016
 */
public final class SpeckyDSLParsingContext {
    private final AtomicBoolean consumed = new AtomicBoolean(false);
    private final List<InputStream> streamsToParse;

    /*package*/ SpeckyDSLParsingContext(List<InputStream> streamsToParse) {
        this.streamsToParse = streamsToParse;
    }

    /**
     * Parse the input streams and return a model generating context.
     * @throws IOException if there is a problem with the streams
     */
    @SuppressWarnings("PMD.PrematureDeclaration")
    public SpeckyModelGeneratingContext parse() throws IOException, SpeckyParsingException {
        if (consumed.compareAndSet(false, true)) {
            final List<Specky.SpecContext> specContexts = new ArrayList<>();
            for (final InputStream inputStream : streamsToParse) {
                try {
                    final CharStream stream = new ANTLRInputStream(inputStream);
                    final SpeckyLexer lexer = new SpeckyLexer(stream);
                    final Specky parser = new Specky(new UnbufferedTokenStream<CommonToken>(lexer));
                    final ErrorCountingListener errorCountingListener = new ErrorCountingListener();
                    parser.addErrorListener(errorCountingListener);
                    final Specky.SpecContext specContext = parser.spec();
                    if (errorCountingListener.hasErrors()) {
                        throw new SpeckyParsingException();
                    }

                    specContexts.add(specContext);
                }
                finally {
                    inputStream.close();
                }
            }

            final List<DSLSpecDesc> specs = new ArrayList<>();
            for (final Specky.SpecContext specContext : specContexts) {
                final SpecBuilder specBuilder = new SpecBuilder();
                for (final DSLSpecDesc specDesc : specBuilder.build(specContext)) {
                    specs.add(specDesc);
                }
            }

            return new SpeckyModelGeneratingContext(specs);
        }<commit_msg>Attempt to parse all files before failing with an exception.<commit_after>/* Copyright © 2016 <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of mattunderscore.com nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.mattunderscore.specky;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.UnbufferedTokenStream;

import com.mattunderscore.specky.dsl.SpecBuilder;
import com.mattunderscore.specky.dsl.model.DSLSpecDesc;
import com.mattunderscore.specky.parser.Specky;
import com.mattunderscore.specky.parser.SpeckyLexer;

/**
 * @author <NAME> on 03/07/2016
 */
public final class SpeckyDSLParsingContext {
    private final AtomicBoolean consumed = new AtomicBoolean(false);
    private final List<InputStream> streamsToParse;

    /*package*/ SpeckyDSLParsingContext(List<InputStream> streamsToParse) {
        this.streamsToParse = streamsToParse;
    }

    /**
     * Parse the input streams and return a model generating context.
     * @throws IOException if there is a problem with the streams
     */
    public SpeckyModelGeneratingContext parse() throws IOException, SpeckyParsingException {
        if (consumed.compareAndSet(false, true)) {
            final List<Specky.SpecContext> specContexts = new ArrayList<>();
            final ErrorCountingListener errorCountingListener = new ErrorCountingListener();
            for (final InputStream inputStream : streamsToParse) {
                try {
                    final CharStream stream = new ANTLRInputStream(inputStream);
                    final SpeckyLexer lexer = new SpeckyLexer(stream);
                    final Specky parser = new Specky(new UnbufferedTokenStream<CommonToken>(lexer));
                    parser.addErrorListener(errorCountingListener);
                    final Specky.SpecContext specContext = parser.spec();

                    specContexts.add(specContext);
                }
                finally {
                    inputStream.close();
                }
            }

            if (errorCountingListener.hasErrors()) {
                throw new SpeckyParsingException();
            }

            final List<DSLSpecDesc> specs = new ArrayList<>();
            for (final Specky.SpecContext specContext : specContexts) {
                final SpecBuilder specBuilder = new SpecBuilder();
                for (final DSLSpecDesc specDesc : specBuilder.build(specContext)) {
                    specs.add(specDesc);
                }
            }

            return new SpeckyModelGeneratingContext(specs);
        }
{'fraction_non_alphanumeric': 0.06401633437424933, 'fraction_numerical': 0.003843382176315157, 'mean_word_length': 3.054040895813048, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '1222248', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2031, 'n_words': 740}