summaryrefslogtreecommitdiff
path: root/python/common.py
blob: 39c323f54e8912816afea1a02811c1b18b2abeaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding=utf-8

import ast
from server.hints import Hint, HintSequence

hint_type = {
    'no_hint': Hint('no_hint'),
    'test_results': Hint('test_results'),
    'syntax_error': Hint('syntax_error'),
}

def hint(program):
    # Check program for syntax errors.
    try:
        tree = ast.parse(program, filename='user')
    except SyntaxError as ex:
        error_msg = '{}{}^\n{}'.format(ex.text, ' '*(ex.offset-1), ex.msg)
        return [{'id': 'syntax_error', 'args': {'lineno': ex.lineno, 'message': error_msg}}]
    return []