def get_parsy_error_location(error, file_path): line, column = parsy.line_info_at(error.stream, error.index) return IssueLocation( line=line, column=column, file_path=file_path, )
def program_parser(prog: str) -> ast.Node: try: result = program.parse(prog) return result except P.ParseError as e: l, c = P.line_info_at(e.stream, e.index) errors.add_error( errors.Error(start=ast.Position(line=l + 1, column=c), end=ast.Position(line=l + 1, column=c), kind=errors.ParseKind.ParserError, message=str(e))) return ast.Nothing()
def test_line_info_at(self): text = "abc\ndef" self.assertEqual(line_info_at(text, 0), (0, 0)) self.assertEqual(line_info_at(text, 2), (0, 2)) self.assertEqual(line_info_at(text, 3), (0, 3)) self.assertEqual(line_info_at(text, 4), (1, 0)) self.assertEqual(line_info_at(text, 7), (1, 3)) self.assertRaises(ValueError, lambda: line_info_at(text, 8))