From a2ef91e85018538be2b82fa085320f518abe254a Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 12 Mar 2016 10:05:35 +0100 Subject: Prolog: split generic hint function into syntax check and the rest --- prolog/common.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'prolog') diff --git a/prolog/common.py b/prolog/common.py index 75006f7..5f16364 100644 --- a/prolog/common.py +++ b/prolog/common.py @@ -20,37 +20,31 @@ hint_type = { 'noncapitalised_variable_markup': HintPopup('noncapitalised_variable_markup'), } -def hint(code, aux_code): - +# Check program for syntax errors. +def check_syntax(code, aux_code): try: engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0) - - # Check program for syntax errors. if 'error' in map(operator.itemgetter(0), output): errors_msg = '\n'.join(['{}: {}'.format(m_type, m_text) for m_type, m_text in output]) return [{'id': 'syntax_error', 'args': {'messages': errors_msg}}] - - tokens = prolog.util.tokenize(code) - - # a,b,c are a bit dangerous when crossing the river exercise is being solved - # what about potential numbers after letters? - # this will have to be solved more generally - targets = [prolog.util.Token('NAME', 'x'), prolog.util.Token('NAME', 'y'), - prolog.util.Token('NAME', 'z'), prolog.util.Token('NAME', 'a'), - prolog.util.Token('NAME', 'b'), prolog.util.Token('NAME', 'c'), - prolog.util.Token('NAME', 'h'), prolog.util.Token('NAME', 't'), - prolog.util.Token('NAME', 'l'), prolog.util.Token('NAME', 's'), - prolog.util.Token('NAME', 'w'), prolog.util.Token('NAME', 'v')] - marks = [(t.pos, t.pos + len(t.val)) for t in tokens if t in targets] - if marks: - return [{'id': 'noncapitalised_variable_markup', 'start': m[0], 'end': m[1]} for m in marks] + \ - [{'id': 'noncapitalised_variable'}] - except socket.timeout as ex: + # TODO check if a timeout can actually happen here pass - finally: if engine_id is not None: prolog.engine.destroy(engine_id) + return [] + +def hint(code, aux_code): + tokens = prolog.util.tokenize(code) + + # a,b,c are a bit dangerous when crossing the river exercise is being solved + # what about potential numbers after letters? + # this will have to be solved more generally + targets = {'a', 'b', 'c', 'x', 'y', 'z', 'h', 't', 'l', 's', 'v', 'w'} + marks = [(t.pos, t.pos + len(t.val)) for t in tokens if t.type == 'NAME' and t.val in targets] + if marks: + return [{'id': 'noncapitalised_variable_markup', 'start': m[0], 'end': m[1]} for m in marks] + \ + [{'id': 'noncapitalised_variable'}] return [] -- cgit v1.2.1