| # Copyright 2019 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Error and warning message support for Emboss. |
| |
| This module exports the error, warn, and note functions, which return a _Message |
| representing the error, warning, or note, respectively. The format method of |
| the returned object can be used to render the message with source code snippets. |
| |
| Throughout Emboss, messages are passed around as lists of lists of _Messages. |
| Each inner list represents a group of messages which should either all be |
| printed, or not printed; i.e., an error message and associated informational |
| messages. For example, to indicate both a duplicate definition error and a |
| warning that a field is a reserved word, one might return: |
| |
| return [ |
| [ |
| error.error(file_name, location, "Duplicate definition), |
| error.note(original_file_name, original_location, |
| "Original definition"), |
| ], |
| [ |
| error.warn(file_name, location, "Field name is a C reserved word.") |
| ], |
| ] |
| """ |
| |
| from compiler.util import ir_data_utils |
| from compiler.util import parser_types |
| |
| # Error levels; represented by the strings that will be included in messages. |
| ERROR = "error" |
| WARNING = "warning" |
| NOTE = "note" |
| |
| # Colors; represented by the terminal escape sequences used to switch to them. |
| # These work out-of-the-box on Unix derivatives (Linux, *BSD, Mac OS X), and |
| # work on Windows using colorify. |
| BLACK = "\033[0;30m" |
| RED = "\033[0;31m" |
| GREEN = "\033[0;32m" |
| YELLOW = "\033[0;33m" |
| BLUE = "\033[0;34m" |
| MAGENTA = "\033[0;35m" |
| CYAN = "\033[0;36m" |
| WHITE = "\033[0;37m" |
| BRIGHT_BLACK = "\033[0;1;30m" |
| BRIGHT_RED = "\033[0;1;31m" |
| BRIGHT_GREEN = "\033[0;1;32m" |
| BRIGHT_YELLOW = "\033[0;1;33m" |
| BRIGHT_BLUE = "\033[0;1;34m" |
| BRIGHT_MAGENTA = "\033[0;1;35m" |
| BRIGHT_CYAN = "\033[0;1;36m" |
| BRIGHT_WHITE = "\033[0;1;37m" |
| BOLD = "\033[0;1m" |
| RESET = "\033[0m" |
| |
| |
| def _copy(location): |
| location = ir_data_utils.copy(location) |
| if not location: |
| location = parser_types.make_location((0, 0), (0, 0)) |
| return location |
| |
| |
| def error(source_file, location, message): |
| """Returns an object representing an error message.""" |
| return _Message(source_file, _copy(location), ERROR, message) |
| |
| |
| def warn(source_file, location, message): |
| """Returns an object representing a warning.""" |
| return _Message(source_file, _copy(location), WARNING, message) |
| |
| |
| def note(source_file, location, message): |
| """Returns and object representing an informational note.""" |
| return _Message(source_file, _copy(location), NOTE, message) |
| |
| |
| class _Message(object): |
| """_Message holds a human-readable message.""" |
| |
| __slots__ = ("location", "source_file", "severity", "message") |
| |
| def __init__(self, source_file, location, severity, message): |
| self.location = location |
| self.source_file = source_file |
| self.severity = severity |
| self.message = message |
| |
| def format(self, source_code): |
| """Formats the _Message for display. |
| |
| Arguments: |
| source_code: A dict of file names to source texts. This is used to |
| render source snippets. |
| |
| Returns: |
| A list of tuples. |
| |
| The first element of each tuple is an escape sequence used to put a Unix |
| terminal into a particular color mode. For use in non-Unix-terminal |
| output, the string will match one of the color names exported by this |
| module. |
| |
| The second element is a string containing text to show to the user. |
| |
| The text will not end with a newline character, nor will it include a |
| RESET color element. |
| |
| To show non-colorized output, simply write the second element of each |
| tuple, then a newline at the end. |
| |
| To show colorized output, write both the first and second element of each |
| tuple, then a newline at the end. Before exiting to the operating system, |
| a RESET sequence should be emitted. |
| """ |
| # TODO(bolms): Figure out how to get Vim, Emacs, etc. to parse Emboss error |
| # messages. |
| severity_colors = { |
| ERROR: (BRIGHT_RED, BOLD), |
| WARNING: (BRIGHT_MAGENTA, BOLD), |
| NOTE: (BRIGHT_BLACK, WHITE), |
| } |
| |
| result = [] |
| if self.location.is_synthetic: |
| pos = "[compiler bug]" |
| else: |
| pos = parser_types.format_position(self.location.start) |
| source_name = self.source_file or "[prelude]" |
| if not self.location.is_synthetic and self.source_file in source_code: |
| source_lines = source_code[self.source_file].splitlines() |
| source_line = source_lines[self.location.start.line - 1] |
| else: |
| source_line = "" |
| lines = self.message.splitlines() |
| for i in range(len(lines)): |
| line = lines[i] |
| # This is a little awkward, but we want to suppress the final newline in |
| # the message. This newline is final if and only if it is the last line |
| # of the message and there is no source snippet. |
| if i != len(lines) - 1 or source_line: |
| line += "\n" |
| result.append((BOLD, "{}:{}: ".format(source_name, pos))) |
| if i == 0: |
| severity = self.severity |
| else: |
| severity = NOTE |
| result.append((severity_colors[severity][0], "{}: ".format(severity))) |
| result.append((severity_colors[severity][1], line)) |
| if source_line: |
| result.append((WHITE, source_line + "\n")) |
| indicator_indent = " " * (self.location.start.column - 1) |
| if self.location.start.line == self.location.end.line: |
| indicator_caret = "^" * max( |
| 1, self.location.end.column - self.location.start.column |
| ) |
| else: |
| indicator_caret = "^" |
| result.append((BRIGHT_GREEN, indicator_indent + indicator_caret)) |
| return result |
| |
| def __repr__(self): |
| return ( |
| "Message({source_file!r}, make_location(({start_line!r}, " |
| "{start_column!r}), ({end_line!r}, {end_column!r}), " |
| "{is_synthetic!r}), {severity!r}, {message!r})" |
| ).format( |
| source_file=self.source_file, |
| start_line=self.location.start.line, |
| start_column=self.location.start.column, |
| end_line=self.location.end.line, |
| end_column=self.location.end.column, |
| is_synthetic=self.location.is_synthetic, |
| severity=self.severity, |
| message=self.message, |
| ) |
| |
| def __eq__(self, other): |
| return ( |
| self.__class__ == other.__class__ |
| and self.location == other.location |
| and self.source_file == other.source_file |
| and self.severity == other.severity |
| and self.message == other.message |
| ) |
| |
| def __ne__(self, other): |
| return not self == other |
| |
| |
| def split_errors(errors): |
| """Splits errors into (user_errors, synthetic_errors). |
| |
| Arguments: |
| errors: A list of lists of _Message, which is a list of bundles of |
| associated messages. |
| |
| Returns: |
| (user_errors, synthetic_errors), where both user_errors and |
| synthetic_errors are lists of lists of _Message. synthetic_errors will |
| contain all bundles that reference any synthetic source_location, and |
| user_errors will contain the rest. |
| |
| The intent is that user_errors can be shown to end users, while |
| synthetic_errors should generally be suppressed. |
| """ |
| synthetic_errors = [] |
| user_errors = [] |
| for error_block in errors: |
| if any(message.location.is_synthetic for message in error_block): |
| synthetic_errors.append(error_block) |
| else: |
| user_errors.append(error_block) |
| return user_errors, synthetic_errors |
| |
| |
| def filter_errors(errors): |
| """Returns the non-synthetic errors from `errors`.""" |
| return split_errors(errors)[0] |
| |
| |
| def format_errors(errors, source_codes, use_color=False): |
| """Formats error messages with source code snippets.""" |
| result = [] |
| for error_group in errors: |
| assert error_group, "Found empty error_group!" |
| for message in error_group: |
| if use_color: |
| result.append( |
| "".join(e[0] + e[1] + RESET for e in message.format(source_codes)) |
| ) |
| else: |
| result.append("".join(e[1] for e in message.format(source_codes))) |
| return "\n".join(result) |
| |
| |
| def make_error_from_parse_error(file_name, parse_error): |
| return [ |
| error( |
| file_name, |
| parse_error.token.source_location, |
| "{code}\n" |
| "Found {text!r} ({symbol}), expected {expected}.".format( |
| code=parse_error.code or "Syntax error", |
| text=parse_error.token.text, |
| symbol=parse_error.token.symbol, |
| expected=", ".join(parse_error.expected_tokens), |
| ), |
| ) |
| ] |