38
38
Dict ,
39
39
Iterator ,
40
40
List ,
41
- Mapping ,
42
41
Optional ,
43
- Sequence ,
44
42
Tuple ,
45
43
Union ,
46
44
)
47
45
48
- from lark .exceptions import UnexpectedCharacters , UnexpectedEOF , UnexpectedInput
49
-
50
46
from .base_types import Shape
51
47
from .config import get_enable_function_call_precompute
52
48
@@ -563,48 +559,47 @@ class ParserInputContext(ErrorContext, ABC):
563
559
"""
564
560
565
561
text : str
562
+ index : int
563
+
564
+ def print_line (self , builder : MessageBuilder ) -> None :
565
+ text = self .text
566
+ index = self .index
567
+
568
+ line_start = text .rfind ("\n " , 0 , self .index )
569
+ if line_start != - 1 :
570
+ text = text [line_start + 1 :]
571
+ index -= line_start + 1
572
+
573
+ line_end = text .find ("\n " )
574
+ if line_end != - 1 :
575
+ text = text [:line_end ]
566
576
567
- def print_line (self , builder : MessageBuilder , line : int , column : int ) -> None :
568
- if line > 0 :
569
- line_content = self .text .split ("\n " )[line - 1 ]
570
- builder .add_columned_line ("Line:" , f'"{ line_content } "' )
571
- if column > 0 :
572
- builder .add_columned_line ("" , " " * column + "^" )
577
+ builder .add_columned_line ("Line:" , f'"{ text } "' )
578
+ builder .add_columned_line ("" , " " * (index + 1 ) + "^" )
573
579
574
580
575
581
@dataclass (frozen = True )
576
- class LarkUnexpectedInputContext (ParserInputContext ):
582
+ class UnexpectedInputContext (ParserInputContext ):
577
583
"""
578
- An error was caused by an `UnexpectedInput` error from `Lark` .
584
+ An error was caused by a malformed specification or docstring .
579
585
"""
580
586
581
- error : UnexpectedInput
582
- terminal_descriptions : Mapping [ str , str ]
587
+ expected : Tuple [ str , ...]
588
+ is_eof : bool
583
589
584
590
def print (self , builder : MessageBuilder ) -> None :
585
- self .print_line (
586
- builder ,
587
- getattr (self .error , "line" , - 1 ),
588
- getattr (self .error , "column" , - 1 ),
589
- )
590
-
591
- expected : Sequence [str ] = getattr (self .error , "accepts" , [])
592
- if not expected :
593
- expected = getattr (self .error , "expected" , [])
594
- expected = sorted (expected )
595
- expected_key = "Expected:" if len (expected ) <= 1 else "Expected one of:"
596
- for expected_name in expected :
597
- expected_value = self .terminal_descriptions .get (expected_name , expected_name )
598
- builder .add_columned_line (expected_key , expected_value )
599
- expected_key = ""
591
+ self .print_line (builder )
600
592
601
- if isinstance (self .error , UnexpectedCharacters ):
602
- builder .add_line ("Found unexpected character." )
603
- if isinstance (self .error , UnexpectedEOF ):
593
+ if self .is_eof :
604
594
builder .add_line ("Found unexpected end of input." )
595
+ else :
596
+ builder .add_line ("Found unexpected character." )
605
597
606
- def __hash__ (self ) -> int :
607
- return hash ((self .error , * sorted (self .terminal_descriptions .items ())))
598
+ expected = sorted (set (self .expected ))
599
+ expected_key = "Expected:" if len (self .expected ) <= 1 else "Expected one of:"
600
+ for expected_value in expected :
601
+ builder .add_columned_line (expected_key , expected_value )
602
+ expected_key = ""
608
603
609
604
610
605
@dataclass (frozen = True )
@@ -613,11 +608,9 @@ class MultipleElementBoolContext(ParserInputContext):
613
608
An error was caused by trying to use a multi-element argument specification as a bool.
614
609
"""
615
610
616
- line : int
617
- column : int
618
-
619
611
def print (self , builder : MessageBuilder ) -> None :
620
- self .print_line (builder , self .line , self .column )
612
+ self .print_line (builder )
613
+
621
614
builder .add_line (
622
615
"Argument references that evaluate to multiple values are not supported for boolean"
623
616
" expressions."
0 commit comments