mlog: move code for printing code with a caret to the mlog module

We need this outside the constructor for the ParseException class, so
let's pull it out. mlog seemed like a good place since it's a text
formatting function, and has no dependencies.
pull/11179/head
Dylan Baker 2 years ago committed by Eli Schwartz
parent b9ef662926
commit 7460ab20cd
  1. 11
      mesonbuild/mlog.py
  2. 2
      mesonbuild/mparser.py

@ -495,3 +495,14 @@ def stop_pager() -> None:
pass
log_pager.wait()
log_pager = None
def code_line(text: str, line: str, colno: int) -> str:
"""Print a line with a caret pointing to the colno
:param text: A message to display before the line
:param line: The line of code to be pointed to
:param colno: The column number to point at
:return: A formatted string of the text, line, and a caret
"""
return f'{text}\n{line}\n{" " * colno}^'

@ -47,7 +47,7 @@ def decode_match(match: T.Match[str]) -> str:
class ParseException(MesonException):
def __init__(self, text: str, line: str, lineno: int, colno: int) -> None:
# Format as error message, followed by the line with the error, followed by a caret to show the error column.
super().__init__("{}\n{}\n{}".format(text, line, '{}^'.format(' ' * colno)))
super().__init__(mlog.code_line(text, line, colno))
self.lineno = lineno
self.colno = colno

Loading…
Cancel
Save