
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2015, Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
import re
from .. import glsl
from ..util import logger


def remove_comments(code):
    """Remove C-style comment from GLSL code string."""

    pattern = r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*\n)"
    # first group captures quoted strings (double or single)
    # second group captures comments (//single-line or /* multi-line */)
    regex = re.compile(pattern, re.MULTILINE | re.DOTALL)

    def do_replace(match):
        # if the 2nd group (capturing comments) is not None,
        # it means we have captured a non-quoted (real) comment string.
        if match.group(2) is not None:
            return ""  # so we will return empty to remove the comment
        else:  # otherwise, we will return the 1st group
            return match.group(1)  # captured quoted-string

    return regex.sub(do_replace, code)


def remove_version(code):
    """ Remove any version directive """

    pattern = '\#\s*version[^\r\n]*\n'
    regex = re.compile(pattern, re.MULTILINE | re.DOTALL)
    return regex.sub('\n', code)


def merge_includes(code):
    """Merge all includes recursively."""

    # pattern = '\#\s*include\s*"(?P<filename>[a-zA-Z0-9\-\.\/]+)"[^\r\n]*\n'
    pattern = '\#\s*include\s*"(?P<filename>[a-zA-Z0-9\-\.\/]+)"'
    regex = re.compile(pattern)
    includes = []

    def replace(match):
        filename = match.group("filename")

        if filename not in includes:
            includes.append(filename)
            path = glsl.find(filename)
            if not path:
                logger.critical('"%s" not found' % filename)
                raise RuntimeError("File not found", filename)
            text = '\n// --- start of "%s" ---\n' % filename
            text += remove_comments(open(path).read())
            text += '// --- end of "%s" ---\n' % filename
            return text
        return ''

    # Limit recursion to depth 10
    for i in range(10):
        if re.search(regex, code):
            code = re.sub(regex, replace, code)
        else:
            break

    return code


def preprocess(code):
    """Preprocess a code by removing comments, version and merging includes."""

    if code:
        code = remove_comments(code)
        code = remove_version(code)
        code = merge_includes(code)
    return code

@@ -1147,208 +1147,8 @@
 )%0A%0A%0A
-def remove_version(code):%0A    %22%22%22 Remove any version directive %22%22%22%0A%0A    pattern = '%5C#%5Cs*version%5B%5E%5Cr%5Cn%5D*%5Cn'%0A    regex = re.compile(pattern, re.MULTILINE %7C re.DOTALL)%0A    return regex.sub('%5Cn', code)%0A%0A%0A
 def 
@@ -2304,44 +2304,8 @@
 de)%0A
-        code = remove_version(code)%0A
     

Remove version strip