summaryrefslogtreecommitdiff
path: root/python/common.py
blob: 5ce2eb318ee5d7f74bab34344eccaefb7b5c0942 (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(python, 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 []