<commit_before>package org.jetbrains.haskell.debugger.parser

import org.json.simple.JSONObject
import org.json.simple.JSONArray
import org.json.simple.parser.JSONParser
import java.util.ArrayList

/**
 * Converts Json objects to appropriate types to use then in commands
 *
 * @author <NAME>
 */
public class JSONConverter {
    class object {
        private val WRONG_OUTPUT_MSG = "Wrong JSON output occured while handling command"
        private val INFO_TAG = "info"
        private val BREAK_LIST_FOR_LINE_INFO = "break list for line"
        private val BREAKS_TAG = "breaks"
        private val BREAK_INDEX_TAG = "index"
        private val SRC_SPAN_TAG = "src_span"

        private val CONNECTED_MSG = "connected to port"

        private val WARNING_MSG = "warning"
        private val EXCEPTION_MSG = "exception"

        private val PAUSED_MSG = "paused"
        private val FINISHED_MSG = "finished"
        private val STOPPED_AT_FUNC_TAG = "function"

        private val BREAKPOINT_SET_MSG = "breakpoint was set"
        private val BREAKPOINT_NOT_SET_MSG = "breakpoint was not set"

        private val BREAKPOINT_REMOVED_MSG = "breakpoint was removed"
        private val BREAKPOINT_NOT_REMOVED_MSG = "breakpoint was not removed"

        private val BACK_MSG = "stepped back"
        private val FORWARD_MSG = "stepped forward"
        private val HISTORY_MSG = "got history"

        private val EXPRESSION_TYPE_MSG = "expression type"

        private val EVALUATED_MSG = "evaluated"

        public fun checkExceptionFromJSON(json: JSONObject): ExceptionResult? =
            switchInfoOrNull(json) {
                case(WARNING_MSG, EXCEPTION_MSG) { ExceptionResult(json.getString("message")) }
            }

        public fun breakpointCommandResultFromJSON(json: JSONObject): BreakpointCommandResult? =
            switchInfoOrThrow(json, WRONG_OUTPUT_MSG + " - set breakpoint") {
                case(BREAKPOINT_SET_MSG) {
                    BreakpointCommandResult(json.getInt("index"), filePositionFromJSON(json.getObject("src_span"))!!)
                }
                case(BREAKPOINT_NOT_SET_MSG) { null }
            }

        public fun stoppedAtFromJSON(json: JSONObject): HsStackFrameInfo? =
            switchInfoOrThrow(json, WRONG_OUTPUT_MSG + " - flow command") {
                case(PAUSED_MSG) {
                    HsStackFrameInfo(filePositionFromJSON(json.getObject("src_span")),
                                     localBindingListFromJSONArray(json.getArray("vars")).list,
                                     json.getString(STOPPED_AT_FUNC_TAG))
                }
                case(FINISHED_MSG) { null }
            }

        public fun moveHistResultFromJSON(json: JSONObject): MoveHistResult? =
            switchInfoOrThrow(json, WRONG_OUTPUT_MSG + " - move through history") {
                case(BACK_MSG, FORWARD_MSG) {
                    MoveHistResult(filePositionFromJSON(json.getObject("src_span")),<commit_msg>Added handling of wrong console input<commit_after>package org.jetbrains.haskell.debugger.parser

import org.json.simple.JSONObject
import org.json.simple.JSONArray
import org.json.simple.parser.JSONParser
import java.util.ArrayList

/**
 * Converts Json objects to appropriate types to use then in commands
 *
 * @author <NAME>
 */
public class JSONConverter {
    class object {
        private val WRONG_OUTPUT_MSG = "Wrong JSON output occured while handling command"
        private val INFO_TAG = "info"
        private val BREAK_LIST_FOR_LINE_INFO = "break list for line"
        private val BREAKS_TAG = "breaks"
        private val BREAK_INDEX_TAG = "index"
        private val SRC_SPAN_TAG = "src_span"

        private val CONNECTED_MSG = "connected to port"

        private val WARNING_MSG = "warning"
        private val EXCEPTION_MSG = "exception"

        private val PAUSED_MSG = "paused"
        private val FINISHED_MSG = "finished"
        private val STOPPED_AT_FUNC_TAG = "function"

        private val BREAKPOINT_SET_MSG = "breakpoint was set"
        private val BREAKPOINT_NOT_SET_MSG = "breakpoint was not set"

        private val BREAKPOINT_REMOVED_MSG = "breakpoint was removed"
        private val BREAKPOINT_NOT_REMOVED_MSG = "breakpoint was not removed"

        private val BACK_MSG = "stepped back"
        private val FORWARD_MSG = "stepped forward"
        private val HISTORY_MSG = "got history"

        private val EXPRESSION_TYPE_MSG = "expression type"

        private val EVALUATED_MSG = "evaluated"

        public fun checkExceptionFromJSON(json: JSONObject): ExceptionResult? =
            switchInfoOrNull(json) {
                case(WARNING_MSG, EXCEPTION_MSG) { ExceptionResult(json.getString("message")) }
            }

        public fun breakpointCommandResultFromJSON(json: JSONObject): BreakpointCommandResult? =
            switchInfoOrThrow(json, WRONG_OUTPUT_MSG + " - set breakpoint") {
                case(BREAKPOINT_SET_MSG) {
                    BreakpointCommandResult(json.getInt("index"), filePositionFromJSON(json.getObject("src_span"))!!)
                }
                case(BREAKPOINT_NOT_SET_MSG) { null }
            }

        public fun stoppedAtFromJSON(json: JSONObject): HsStackFrameInfo? =
            switchInfoOrThrow(json, WRONG_OUTPUT_MSG + " - flow command") {
                case(PAUSED_MSG) {
                    HsStackFrameInfo(filePositionFromJSON(json.getObject("src_span")),
                                     localBindingListFromJSONArray(json.getArray("vars")).list,
                                     json.getString(STOPPED_AT_FUNC_TAG))
                }
                case(FINISHED_MSG) { null }
                case(EXCEPTION_MSG) { null }
                case(WARNING_MSG) { null }
            }

        public fun moveHistResultFromJSON(json: JSONObject): MoveHistResult? =
            switchInfoOrThrow(json, WRONG_OUTPUT_MSG + " - move through history") {
                case(BACK_MSG, FORWARD_MSG) {
                    MoveHistResult(filePositionFromJSON(json.getObject("src_span")),
{'fraction_non_alphanumeric': 0.07661953304833936, 'fraction_numerical': 0.0, 'mean_word_length': 2.5121247113163974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1088098', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1644, 'n_words': 384}