#!/usr/bin/python


"""
This catches an opening brace followed by a space, but it allows it if a
comment begins there, such as "{ //  comment".
"""

error_msg = "No space after opening brace allowed!"
strip_comments_and_strings = True
strip_macros = True

import re
class EvalMatches( object ):
    REGEXP = re.compile(r"""(?x)
\{([ ]\s*.*)
""")

    def __call__(self, lines, fn):
        errors = []
        curline = 0
        for l in lines:
            l = l.rstrip()
            curline += 1
            m = self.REGEXP.search(l)
            if m:
                g = m.group(1)
                if not len(g): continue
                if g[-1] == '\\': continue

                errors.append( (fn, curline, error_msg))
        return errors


evaluate_matches = EvalMatches()


forbidden = [
    '{ /',
    '{ / /',
    '{ h',
    '{ "h"',
]

allowed = [
    "if(blah) {                \\",
    '\tif ((m_readyflags | thisflag) != 3) {  //  codepath a',
    r'// { This is ok',
    r'/// {{{ Like this',
    r'/* {{{ And this this */',
    r'{ // And this also',
    r'{',
    r'{/',
    r'{/ /',
    r'{h',
    r'{"h"',
    "namespace Widelands {\n",
    "//  Do { what we want in comments",
    "buffer[i] = '{'",
    r"   // FIXME instead of replacing < with { in chat messages.",
    "{ /* check number of arguments */",
    "{ // check number of arguments",
]
