diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-03-12 10:05:35 +0100 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-03-12 10:05:35 +0100 |
commit | a2ef91e85018538be2b82fa085320f518abe254a (patch) | |
tree | bf57e872dabdab4494f19b13f0314258cfffc5e6 /prolog | |
parent | e5674c50d1d4a25961d0244198dedbceb2a926e9 (diff) |
Prolog: split generic hint function into syntax check and the rest
Diffstat (limited to 'prolog')
-rw-r--r-- | prolog/common.py | 38 |
1 files changed, 16 insertions, 22 deletions
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 [] |