'Checks that the filters used in the given Mako expression are valid
for the given context. Adds violation to results if there is a problem.
Arguments:
mako_template: The contents of the Mako template.
expression: A Mako Expression.
context: The context of the page in which the expression was found
(e.g. javascript, html).
has_page_default: True if the page is marked as default, False
otherwise.
results: A list of results into which violations will be added.'

def _check_expression_and_filters(self, mako_template, expression, context, has_page_default, results):

    if (context == 'unknown'):
        results.violations.append(ExpressionRuleViolation(Rules.mako_unknown_context, expression))
        return
    filters_regex = re.compile('\\|([.,\\w\\s]*)\\}')
    filters_match = filters_regex.search(expression.expression)
    if (filters_match is None):
        python_code = expression.expression[2:(-1)]
    else:
        python_code = expression.expression[2:filters_match.start()]
    self._check_expression_python(python_code, (expression.start_index + 2), has_page_default, results)
    if (filters_match is None):
        if (context == 'javascript'):
            results.violations.append(ExpressionRuleViolation(Rules.mako_invalid_js_filter, expression))
        return
    filters = filters_match.group(1).replace('    ', '').split(',')
    if (filters == ['n', 'decode.utf8']):
        pass
    elif (context == 'html'):
        if (filters == ['h']):
            if has_page_default:
                results.violations.append(ExpressionRuleViolation(Rules.mako_unwanted_html_filter, expression))
        else:
            results.violations.append(ExpressionRuleViolation(Rules.mako_invalid_html_filter, expression))
    elif (context == 'javascript'):
        self._check_js_expression_not_with_html(mako_template, expression, results)
        if (filters == ['n', 'dump_js_escaped_json']):
            pass
        elif (filters == ['n', 'js_escaped_string']):
            self._check_js_string_expression_in_quotes(mako_template, expression, results)
        else:
            results.violations.append(ExpressionRuleViolation(Rules.mako_invalid_js_filter, expression))
