# Path: qutepart/syntax/parser.py
# class StringDetect(AbstractRule):
#     """Public attributes:
#         string
#     """
#     def __init__(self, abstractRuleParams, string):
#         AbstractRule.__init__(self, abstractRuleParams)
#         self.string = string
# 
#     def shortId(self):
#         return 'StringDetect(%s)' % self.string
# 
#     def _tryMatch(self, textToMatchObject):
#         if self.string is None:
#             return None
# 
#         if self.dynamic:
#             string = self._makeDynamicSubsctitutions(self.string, textToMatchObject.contextData)
#             if not string:
#                 return None
#         else:
#             string = self.string
# 
#         if textToMatchObject.text.startswith(string):
#             return RuleTryMatchResult(self, len(string))
# 
#         return None
# 
#     @staticmethod
#     def _makeDynamicSubsctitutions(string, contextData):
#         """For dynamic rules, replace %d patterns with actual strings
#         Python function, which is used by C extension.
#         """
#         def _replaceFunc(escapeMatchObject):
#             stringIndex = escapeMatchObject.group(0)[1]
#             index = int(stringIndex)
#             if index < len(contextData):
#                 return contextData[index]
#             else:
#                 return escapeMatchObject.group(0)  # no any replacements, return original value
# 
#         return _numSeqReplacer.sub(_replaceFunc, string)
# 
# class RegExpr(AbstractRule):
#     """ Public attributes:
#         regExp
#         wordStart
#         lineStart
#     """
#     def __init__(self, abstractRuleParams,
#                  string, insensitive, minimal, wordStart, lineStart):
#         AbstractRule.__init__(self, abstractRuleParams)
#         self.string = string
#         self.insensitive = insensitive
#         self.minimal = minimal
#         self.wordStart = wordStart
#         self.lineStart = lineStart
# 
#         if self.dynamic:
#             self.regExp = None
#         else:
#             self.regExp = self._compileRegExp(string, insensitive, minimal)
# 
# 
#     def shortId(self):
#         return 'RegExpr( %s )' % self.string
# 
#     def _tryMatch(self, textToMatchObject):
#         """Tries to parse text. If matched - saves data for dynamic context
#         """
#         # Special case. if pattern starts with \b, we have to check it manually,
#         # because string is passed to .match(..) without beginning
#         if self.wordStart and \
#            (not textToMatchObject.isWordStart):
#                 return None
# 
#         #Special case. If pattern starts with ^ - check column number manually
#         if self.lineStart and \
#            textToMatchObject.currentColumnIndex > 0:
#             return None
# 
#         if self.dynamic:
#             string = self._makeDynamicSubsctitutions(self.string, textToMatchObject.contextData)
#             regExp = self._compileRegExp(string, self.insensitive, self.minimal)
#         else:
#             regExp = self.regExp
# 
#         if regExp is None:
#             return None
# 
#         wholeMatch, groups = self._matchPattern(regExp, textToMatchObject.text)
#         if wholeMatch is not None:
#             count = len(wholeMatch)
#             return RuleTryMatchResult(self, count, groups)
#         else:
#             return None
# 
#     @staticmethod
#     def _makeDynamicSubsctitutions(string, contextData):
#         """For dynamic rules, replace %d patterns with actual strings
#         Escapes reg exp symbols in the pattern
#         Python function, used by C code
#         """
#         def _replaceFunc(escapeMatchObject):
#             stringIndex = escapeMatchObject.group(0)[1]
#             index = int(stringIndex)
#             if index < len(contextData):
#                 return re.escape(contextData[index])
#             else:
#                 return escapeMatchObject.group(0)  # no any replacements, return original value
# 
#         return _numSeqReplacer.sub(_replaceFunc, string)
# 
#     @staticmethod
#     def _compileRegExp(string, insensitive, minimal):
#         """Compile regular expression.
#         Python function, used by C code
# 
#         NOTE minimal flag is not supported here, but supported on PCRE
#         """
#         flags = 0
#         if insensitive:
#             flags = re.IGNORECASE
# 
#         string = string.replace('[_[:alnum:]]', '[\\w\\d]') # ad-hoc fix for C++ parser
#         string = string.replace('[:digit:]', '\\d')
#         string = string.replace('[:blank:]', '\\s')
# 
#         try:
#             return re.compile(string, flags)
#         except (re.error, AssertionError) as ex:
#             _logger.warning("Invalid pattern '%s': %s", string, str(ex))
#             return None
# 
#     @staticmethod
#     def _matchPattern(regExp, string):
#         """Try to match pattern.
#         Returns tuple (whole match, groups) or (None, None)
#         Python function, used by C code
#         """
#         match = regExp.match(string)
#         if match is not None and match.group(0):
#             return match.group(0), (match.group(0), ) + match.groups()
#         else:
#             return None, None


import unittest
import sys
import os.path
from qutepart.syntax.parser import StringDetect, RegExpr
#!/usr/bin/env python3


topLevelPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, topLevelPath)
sys.path.insert(0, os.path.join(topLevelPath, 'build/lib.linux-x86_64-3.4/'))
sys.path.insert(0, os.path.join(topLevelPath, 'build/lib.linux-x86_64-3.5/'))


class TestCase(unittest.TestCase):
    def test_StringDetect(self):
        self.assertEqual(StringDetect._makeDynamicSubsctitutions('a%1c%3', ['a', '|']),
                         'a|c%3')

    def test_RegExp(self):

# Path: qutepart/syntax/parser.py
# class StringDetect(AbstractRule):
#     """Public attributes:
#         string
#     """
#     def __init__(self, abstractRuleParams, string):
#         AbstractRule.__init__(self, abstractRuleParams)
#         self.string = string
# 
#     def shortId(self):
#         return 'StringDetect(%s)' % self.string
# 
#     def _tryMatch(self, textToMatchObject):
#         if self.string is None:
#             return None
# 
#         if self.dynamic:
#             string = self._makeDynamicSubsctitutions(self.string, textToMatchObject.contextData)
#             if not string:
#                 return None
#         else:
#             string = self.string
# 
#         if textToMatchObject.text.startswith(string):
#             return RuleTryMatchResult(self, len(string))
# 
#         return None
# 
#     @staticmethod
#     def _makeDynamicSubsctitutions(string, contextData):
#         """For dynamic rules, replace %d patterns with actual strings
#         Python function, which is used by C extension.
#         """
#         def _replaceFunc(escapeMatchObject):
#             stringIndex = escapeMatchObject.group(0)[1]
#             index = int(stringIndex)
#             if index < len(contextData):
#                 return contextData[index]
#             else:
#                 return escapeMatchObject.group(0)  # no any replacements, return original value
# 
#         return _numSeqReplacer.sub(_replaceFunc, string)
# 
# class RegExpr(AbstractRule):
#     """ Public attributes:
#         regExp
#         wordStart
#         lineStart
#     """
#     def __init__(self, abstractRuleParams,
#                  string, insensitive, minimal, wordStart, lineStart):
#         AbstractRule.__init__(self, abstractRuleParams)
#         self.string = string
#         self.insensitive = insensitive
#         self.minimal = minimal
#         self.wordStart = wordStart
#         self.lineStart = lineStart
# 
#         if self.dynamic:
#             self.regExp = None
#         else:
#             self.regExp = self._compileRegExp(string, insensitive, minimal)
# 
# 
#     def shortId(self):
#         return 'RegExpr( %s )' % self.string
# 
#     def _tryMatch(self, textToMatchObject):
#         """Tries to parse text. If matched - saves data for dynamic context
#         """
#         # Special case. if pattern starts with \b, we have to check it manually,
#         # because string is passed to .match(..) without beginning
#         if self.wordStart and \
#            (not textToMatchObject.isWordStart):
#                 return None
# 
#         #Special case. If pattern starts with ^ - check column number manually
#         if self.lineStart and \
#            textToMatchObject.currentColumnIndex > 0:
#             return None
# 
#         if self.dynamic:
#             string = self._makeDynamicSubsctitutions(self.string, textToMatchObject.contextData)
#             regExp = self._compileRegExp(string, self.insensitive, self.minimal)
#         else:
#             regExp = self.regExp
# 
#         if regExp is None:
#             return None
# 
#         wholeMatch, groups = self._matchPattern(regExp, textToMatchObject.text)
#         if wholeMatch is not None:
#             count = len(wholeMatch)
#             return RuleTryMatchResult(self, count, groups)
#         else:
#             return None
# 
#     @staticmethod
#     def _makeDynamicSubsctitutions(string, contextData):
#         """For dynamic rules, replace %d patterns with actual strings
#         Escapes reg exp symbols in the pattern
#         Python function, used by C code
#         """
#         def _replaceFunc(escapeMatchObject):
#             stringIndex = escapeMatchObject.group(0)[1]
#             index = int(stringIndex)
#             if index < len(contextData):
#                 return re.escape(contextData[index])
#             else:
#                 return escapeMatchObject.group(0)  # no any replacements, return original value
# 
#         return _numSeqReplacer.sub(_replaceFunc, string)
# 
#     @staticmethod
#     def _compileRegExp(string, insensitive, minimal):
#         """Compile regular expression.
#         Python function, used by C code
# 
#         NOTE minimal flag is not supported here, but supported on PCRE
#         """
#         flags = 0
#         if insensitive:
#             flags = re.IGNORECASE
# 
#         string = string.replace('[_[:alnum:]]', '[\\w\\d]') # ad-hoc fix for C++ parser
#         string = string.replace('[:digit:]', '\\d')
#         string = string.replace('[:blank:]', '\\s')
# 
#         try:
#             return re.compile(string, flags)
#         except (re.error, AssertionError) as ex:
#             _logger.warning("Invalid pattern '%s': %s", string, str(ex))
#             return None
# 
#     @staticmethod
#     def _matchPattern(regExp, string):
#         """Try to match pattern.
#         Returns tuple (whole match, groups) or (None, None)
#         Python function, used by C code
#         """
#         match = regExp.match(string)
#         if match is not None and match.group(0):
#             return match.group(0), (match.group(0), ) + match.groups()
#         else:
#             return None, None


# Path: tests/test_syntax/test_dynamic_substitution.py
import unittest
import sys
import os.path
from qutepart.syntax.parser import StringDetect, RegExpr

#!/usr/bin/env python3


topLevelPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, topLevelPath)
sys.path.insert(0, os.path.join(topLevelPath, 'build/lib.linux-x86_64-3.4/'))
sys.path.insert(0, os.path.join(topLevelPath, 'build/lib.linux-x86_64-3.5/'))


class TestCase(unittest.TestCase):
    def test_StringDetect(self):
        self.assertEqual(StringDetect._makeDynamicSubsctitutions('a%1c%3', ['a', '|']),
                         'a|c%3')

    def test_RegExp(self):

        self.assertEqual(RegExpr._makeDynamicSubsctitutions('a%1c%3', ['a', '|']),