From 732a562a1cf7bba5216b0ae61ce00b1b4f46d10a Mon Sep 17 00:00:00 2001 From: Aleh Arol Date: Sun, 31 May 2020 00:01:19 +0300 Subject: [PATCH 1/2] Use token type equality as a fallback when matching error examples --- lark/exceptions.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lark/exceptions.py b/lark/exceptions.py index cf03746..4cbe4bf 100644 --- a/lark/exceptions.py +++ b/lark/exceptions.py @@ -39,7 +39,7 @@ class UnexpectedInput(LarkError): """ assert self.state is not None, "Not supported for this exception" - candidate = None + candidate = (None, False) for label, example in examples.items(): assert not isinstance(example, STRING_TYPE) @@ -51,12 +51,16 @@ class UnexpectedInput(LarkError): try: if ut.token == self.token: # Try exact match first return label + + if (ut.token.type == self.token.type) and not candidate[-1]: # Fallback to token types match + candidate = label, True + except AttributeError: pass - if not candidate: - candidate = label + if not candidate[0]: + candidate = label, False - return candidate + return candidate[0] class UnexpectedCharacters(LexError, UnexpectedInput): From e6daf51f255e8bb72153cb5157f03dfe38216052 Mon Sep 17 00:00:00 2001 From: Aleh Arol Date: Sun, 28 Jun 2020 18:07:00 +0300 Subject: [PATCH 2/2] Make token type check fallback disabled by default --- lark/exceptions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lark/exceptions.py b/lark/exceptions.py index 4cbe4bf..6dd31be 100644 --- a/lark/exceptions.py +++ b/lark/exceptions.py @@ -32,7 +32,7 @@ class UnexpectedInput(LarkError): after = text[pos:end].split('\n', 1)[0] return before + after + '\n' + ' ' * len(before) + '^\n' - def match_examples(self, parse_fn, examples): + def match_examples(self, parse_fn, examples, token_type_match_fallback=False): """ Given a parser instance and a dictionary mapping some label with some malformed syntax examples, it'll return the label for the example that bests matches the current error. @@ -52,8 +52,10 @@ class UnexpectedInput(LarkError): if ut.token == self.token: # Try exact match first return label - if (ut.token.type == self.token.type) and not candidate[-1]: # Fallback to token types match - candidate = label, True + if token_type_match_fallback: + # Fallback to token types match + if (ut.token.type == self.token.type) and not candidate[-1]: + candidate = label, True except AttributeError: pass