# Path: Popen_noblock.py
# ON_POSIX = 'posix' in sys.builtin_module_names
# def enqueue_output(out, queue):
#     def __init__(self, *args, **kwargs):
# class Popen_async(object):
# 
# Path: memoize.py
# def memoize(f):
#     """ Memoization decorator for a function taking one or more arguments. """
#     class memodict(dict):
#         def __getitem__(self, *key, **kwkey):
#             return dict.__getitem__(self, (tuple(key), tuple((k, kwkey[k]) for k in sorted(kwkey.keys()))))
# 
#         def __missing__(self, key):
#             args, kwargs = key
#             self[key] = ret = f(*args, **dict(kwargs))
#             return ret
# 
#     return memodict().__getitem__
# 
# Path: file_util.py
# def clean_v_file(file_name):
#     clean_extra_coq_files(file_name, extra_exts=('.v',))
# 
# Path: util.py
# def re_escape(*args, **kwargs):
#     ret = re.escape(*args, **kwargs)
#     for ch in ':"':
#         ret = ret.replace('\\' + ch, ch)
#     return ret
# 
# Path: custom_arguments.py
# LOG_ALWAYS=None
# 
# Path: coq_version.py
# def group_coq_args(args, coqc_help, topname=None, is_coq_makefile=False):
#     bindings, unrecognized_bindings = group_coq_args_split_recognized(args, coqc_help, topname=topname, is_coq_makefile=is_coq_makefile)
#     return bindings + [tuple([v]) for v in unrecognized_bindings]
# 
# def get_coqc_help(coqc_prog, **kwargs):
#     kwargs['log']('Running command: "%s"' % '" "'.join([coqc_prog, "-q", "--help"]), level=2)
#     return get_coqc_help_helper(coqc_prog)


import os, sys, tempfile, subprocess, re, time, math, glob, threading, shutil, traceback
import util
from Popen_noblock import Popen_async, Empty
from memoize import memoize
from file_util import clean_v_file
from util import re_escape
from custom_arguments import LOG_ALWAYS
from coq_version import group_coq_args, get_coqc_help
from __future__ import with_statement, print_function

__all__ = ["has_error", "get_error_line_number", "get_error_byte_locations", "make_reg_string", "get_coq_output", "get_error_string", "get_timeout", "reset_timeout", "reset_coq_output_cache", "is_timeout"]

ACTUAL_ERROR_REG_STRING = '(?!Warning)(?!The command has indeed failed with message:)' # maybe we should just require Error or Timeout?
DEFAULT_PRE_PRE_ERROR_REG_STRING = 'File "[^"]+", line ([0-9]+), characters [0-9-]+:\n'
DEFAULT_PRE_ERROR_REG_STRING = DEFAULT_PRE_PRE_ERROR_REG_STRING + ACTUAL_ERROR_REG_STRING
DEFAULT_PRE_ERROR_REG_STRING_WITH_BYTES = 'File "[^"]+", line ([0-9]+), characters ([0-9]+)-([0-9]+):\n' + ACTUAL_ERROR_REG_STRING
DEFAULT_ERROR_REG_STRING = DEFAULT_PRE_ERROR_REG_STRING + '((?:.|\n)+)'
DEFAULT_ERROR_REG_STRING_WITH_BYTES = DEFAULT_PRE_ERROR_REG_STRING_WITH_BYTES + '((?:.|\n)+)'
DEFAULT_ERROR_REG_STRING_GENERIC = DEFAULT_PRE_PRE_ERROR_REG_STRING + '(%s)'

def clean_output(output):
    return util.normalize_newlines(output)


# Path: Popen_noblock.py
# ON_POSIX = 'posix' in sys.builtin_module_names
# def enqueue_output(out, queue):
#     def __init__(self, *args, **kwargs):
# class Popen_async(object):
# 
# Path: memoize.py
# def memoize(f):
#     """ Memoization decorator for a function taking one or more arguments. """
#     class memodict(dict):
#         def __getitem__(self, *key, **kwkey):
#             return dict.__getitem__(self, (tuple(key), tuple((k, kwkey[k]) for k in sorted(kwkey.keys()))))
# 
#         def __missing__(self, key):
#             args, kwargs = key
#             self[key] = ret = f(*args, **dict(kwargs))
#             return ret
# 
#     return memodict().__getitem__
# 
# Path: file_util.py
# def clean_v_file(file_name):
#     clean_extra_coq_files(file_name, extra_exts=('.v',))
# 
# Path: util.py
# def re_escape(*args, **kwargs):
#     ret = re.escape(*args, **kwargs)
#     for ch in ':"':
#         ret = ret.replace('\\' + ch, ch)
#     return ret
# 
# Path: custom_arguments.py
# LOG_ALWAYS=None
# 
# Path: coq_version.py
# def group_coq_args(args, coqc_help, topname=None, is_coq_makefile=False):
#     bindings, unrecognized_bindings = group_coq_args_split_recognized(args, coqc_help, topname=topname, is_coq_makefile=is_coq_makefile)
#     return bindings + [tuple([v]) for v in unrecognized_bindings]
# 
# def get_coqc_help(coqc_prog, **kwargs):
#     kwargs['log']('Running command: "%s"' % '" "'.join([coqc_prog, "-q", "--help"]), level=2)
#     return get_coqc_help_helper(coqc_prog)


# Path: diagnose_error.py
import os, sys, tempfile, subprocess, re, time, math, glob, threading, shutil, traceback
import util
from Popen_noblock import Popen_async, Empty
from memoize import memoize
from file_util import clean_v_file
from util import re_escape
from custom_arguments import LOG_ALWAYS
from coq_version import group_coq_args, get_coqc_help

from __future__ import with_statement, print_function

__all__ = ["has_error", "get_error_line_number", "get_error_byte_locations", "make_reg_string", "get_coq_output", "get_error_string", "get_timeout", "reset_timeout", "reset_coq_output_cache", "is_timeout"]

ACTUAL_ERROR_REG_STRING = '(?!Warning)(?!The command has indeed failed with message:)' # maybe we should just require Error or Timeout?
DEFAULT_PRE_PRE_ERROR_REG_STRING = 'File "[^"]+", line ([0-9]+), characters [0-9-]+:\n'
DEFAULT_PRE_ERROR_REG_STRING = DEFAULT_PRE_PRE_ERROR_REG_STRING + ACTUAL_ERROR_REG_STRING
DEFAULT_PRE_ERROR_REG_STRING_WITH_BYTES = 'File "[^"]+", line ([0-9]+), characters ([0-9]+)-([0-9]+):\n' + ACTUAL_ERROR_REG_STRING
DEFAULT_ERROR_REG_STRING = DEFAULT_PRE_ERROR_REG_STRING + '((?:.|\n)+)'
DEFAULT_ERROR_REG_STRING_WITH_BYTES = DEFAULT_PRE_ERROR_REG_STRING_WITH_BYTES + '((?:.|\n)+)'
DEFAULT_ERROR_REG_STRING_GENERIC = DEFAULT_PRE_PRE_ERROR_REG_STRING + '(%s)'

def clean_output(output):
    return util.normalize_newlines(output)


@memoize