def test_unclosed_size_definition_at_semicolon(): with pytest.raises(SyntaxError) as error: parse('Integer2d data[5, 10;') assert error.value.lineno == 1 assert str( error.value ) == "Datablock size specification is missing a closing ] (<string>, line 1)"
def update_code(self): print "lodaing chestnut" from chestnut.backends.walnut import WalnutBackend from chestnut.exceptions import CompilerException from chestnut.parser import parse, FullFirstMatchException from chestnut.symboltable import SymbolTable print "parsing" # Time to get an AST, but that might fail and we want nice lines + context information code = str(self.chestnutWindow.toPlainText()) try: ast = parse(code) except FullFirstMatchException, e: code_lines = code.split('\n') lineno = e.kargs['lineno'] def valid_line(lineno): return lineno > 0 and lineno < len(code_lines) import textwrap error = "" error += 'Error:\n' error += '\n'.join(map(lambda s: ' ' + s, textwrap.wrap(str(e), 76))) + '\n' error += 'Context:\n' for offset in [-2, -1, 0, 1, 2]: if valid_line(lineno+offset): if offset == 0: error += ' ->' else: error += ' ' error += '%s: %s\n' % (lineno+offset, code_lines[lineno+offset - 1]) self.astWindow.setPlainText(error) return
def update_code(self): print "lodaing chestnut" from chestnut.backends.walnut import WalnutBackend from chestnut.exceptions import CompilerException from chestnut.parser import parse, FullFirstMatchException from chestnut.symboltable import SymbolTable print "parsing" # Time to get an AST, but that might fail and we want nice lines + context information code = str(self.chestnutWindow.toPlainText()) try: ast = parse(code) except FullFirstMatchException, e: code_lines = code.split('\n') lineno = e.kargs['lineno'] def valid_line(lineno): return lineno > 0 and lineno < len(code_lines) import textwrap error = "" error += 'Error:\n' error += '\n'.join( map(lambda s: ' ' + s, textwrap.wrap(str(e), 76))) + '\n' error += 'Context:\n' for offset in [-2, -1, 0, 1, 2]: if valid_line(lineno + offset): if offset == 0: error += ' ->' else: error += ' ' error += '%s: %s\n' % (lineno + offset, code_lines[lineno + offset - 1]) self.astWindow.setPlainText(error) return
def test_unclosed_block_definition_at_eof(): with pytest.raises(SyntaxError) as error: parse('{ int x;') assert error.value.lineno == 1 assert str(error.value) == 'block is missing a closing } (<string>, line 1)'
def test_unclosed_size_definition_at_semicolon(): with pytest.raises(SyntaxError) as error: parse('Integer2d data[5, 10;') assert error.value.lineno == 1 assert str(error.value) == "Datablock size specification is missing a closing ] (<string>, line 1)"
def test_unclosed_block_definition_at_eof(): with pytest.raises(SyntaxError) as error: parse('{ int x;') assert error.value.lineno == 1 assert str( error.value) == 'block is missing a closing } (<string>, line 1)'