package com.mattunderscore.specky;

import org.antlr.v4.runtime.tree.TerminalNode;

import com.mattunderscore.specky.model.ConstructionMethod;
import com.mattunderscore.specky.parser.Specky;
import com.mattunderscore.specky.parser.Specky.OptsContext;
import com.mattunderscore.specky.parser.Specky.String_valueContext;

/**
 * Parser utilities. Support working with the generated parser.
 *
 * @author Matt Champion 24/01/2017
 */
/*package*/ final class ParserUtils {
    private ParserUtils() {
    }

    /**
     * @return the unpacked string
     */
    static String toValue(String_valueContext stringValue) {
        if (stringValue == null) {
            return null;
        }

        final TerminalNode multiline = stringValue.MULTILINE_STRING_LITERAL();
        if (multiline != null) {
            return toValue(multiline);
        }
        else {
            return toValue(stringValue.StringLiteral());
        }
    }

    /**
     * @return the unpacked string
     */
    static String toValue(TerminalNode node) {
        if (node == null) {
            return null;
        }
        else if (node.getSymbol().getType() == Specky.StringLiteral) {
            final String literal = node.getText();
            return literal.substring(1, literal.length() - 1);
        }
        else if (node.getSymbol().getType() == Specky.MULTILINE_STRING_LITERAL) {
            final String literal = node.getText();
            return literal.substring(3, literal.length() - 3);
        }
        else {
            throw new IllegalArgumentException("Node cannot be made into a string." + node);
        }
    }

    /**
     * @return the construction method
     */
    static ConstructionMethod toConstructionDesc(OptsContext options) {
        if (options == null || options.construction() == null) {
            return ConstructionMethod.CONSTRUCTOR;
        }

        if (options.construction().CONSTRUCTOR() != null) {
            return ConstructionMethod.CONSTRUCTOR;
        }
        else if (options.construction().MUTABLE_BUILDER() != null) {
            return ConstructionMethod.MUTABLE_BUILDER;
        }
        else if (options.construction().IMMUTABLE_BUILDER() != null) {
            return ConstructionMethod.IMMUTABLE_BUILDER;
        }
        else if (options.construction().FROM_DEFAULTS() != null) {
            return ConstructionMethod.FROM_DEFAULTS;
        }
        else {
            throw new IllegalArgumentException("Unsupported type");
        }
    }

    /**
     * @return if with property methods should be generated
     */
    static boolean withModifications(Specky.OptsContext options) {
        return !(options == null || options.WITH_MODIFICATION() == null);
    }
}

@@ -1,12 +1,1508 @@
+/* Copyright %C2%A9 2017 Matthew Champion All rights reserved.%0A%0ARedistribution and use in source and binary forms, with or without%0Amodification, are permitted provided that the following conditions are met:%0A * Redistributions of source code must retain the above copyright%0A      notice, this list of conditions and the following disclaimer.%0A * Redistributions in binary form must reproduce the above copyright%0A      notice, this list of conditions and the following disclaimer in the%0A      documentation and/or other materials provided with the distribution.%0A * Neither the name of mattunderscore.com nor the%0A      names of its contributors may be used to endorse or promote products%0A      derived from this software without specific prior written permission.%0A%0ATHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22 AND%0AANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED%0AWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE%0ADISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY%0ADIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES%0A(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;%0ALOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND%0AON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT%0A(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS%0ASOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */%0A%0A
 package com.

Add missing licence header.