3
3
4
4
from . import ast
5
5
from .errors import ParseError
6
- from .stream import EOL , FluentParserStream
6
+ from .stream import EOL , FluentParserStream , Location
7
7
8
8
R = TypeVar ("R" , bound = ast .SyntaxNode )
9
9
@@ -15,15 +15,15 @@ def decorated(
15
15
if not self .with_spans :
16
16
return fn (self , ps , * args , ** kwargs )
17
17
18
- start = ps .index
18
+ start = ps .current_location
19
19
node = fn (self , ps , * args , ** kwargs )
20
20
21
21
# Don't re-add the span if the node already has it. This may happen
22
22
# when one decorated function calls another decorated function.
23
23
if node .span is not None :
24
24
return node
25
25
26
- end = ps .index
26
+ end = ps .current_location
27
27
node .add_span (start , end )
28
28
return node
29
29
@@ -85,7 +85,7 @@ def parse(self, source: str) -> ast.Resource:
85
85
res = ast .Resource (entries )
86
86
87
87
if self .with_spans :
88
- res .add_span (0 , ps .index )
88
+ res .add_span (0 , ps .current_location )
89
89
90
90
return res
91
91
@@ -112,29 +112,31 @@ def parse_entry(self, source: str) -> ast.EntryType:
112
112
113
113
def get_entry_or_junk (self , ps : FluentParserStream ) -> ast .EntryType :
114
114
entry_start_index = ps .index
115
+ entry_start_location = ps .current_location
115
116
116
117
try :
117
118
entry = self .get_entry (ps )
118
119
ps .expect_line_end ()
119
120
return entry
120
121
except ParseError as err :
121
122
error_index = ps .index
123
+ error_location = ps .current_location
122
124
123
125
ps .skip_to_next_entry_start (entry_start_index )
124
126
next_entry_start_index = ps .index
125
127
if next_entry_start_index < error_index :
126
128
# The position of the error must be inside of the Junk's span.
127
- error_index = next_entry_start_index
129
+ error_location = ps . current_location
128
130
129
131
# Create a Junk instance
130
132
slice = ps .string [entry_start_index :next_entry_start_index ]
131
133
junk = ast .Junk (slice )
132
134
if self .with_spans :
133
- junk .add_span (entry_start_index , next_entry_start_index )
135
+ junk .add_span (entry_start_location , ps . current_location )
134
136
annot = ast .Annotation (
135
137
err .code , list (err .args ) if err .args else None , err .message
136
138
)
137
- annot .add_span (error_index , error_index )
139
+ annot .add_span (error_location , error_location )
138
140
junk .add_annotation (annot )
139
141
return junk
140
142
@@ -380,24 +382,24 @@ def get_pattern(self, ps: FluentParserStream, is_block: bool) -> ast.Pattern:
380
382
if is_block :
381
383
# A block pattern is a pattern which starts on a new line. Measure
382
384
# the indent of this first line for the dedentation logic.
383
- blank_start = ps .index
385
+ blank_start = ps .current_location
384
386
first_indent = ps .skip_blank_inline ()
385
- elements .append (self .Indent (first_indent , blank_start , ps .index ))
387
+ elements .append (self .Indent (first_indent , blank_start , ps .current_location ))
386
388
common_indent_length = len (first_indent )
387
389
else :
388
390
# Should get fixed by the subsequent min() operation
389
391
common_indent_length = cast (int , float ("infinity" ))
390
392
391
393
while ps .current_char :
392
394
if ps .current_char == EOL :
393
- blank_start = ps .index
395
+ blank_start = ps .current_location
394
396
blank_lines = ps .peek_blank_block ()
395
397
if ps .is_value_continuation ():
396
398
ps .skip_to_peek ()
397
399
indent = ps .skip_blank_inline ()
398
400
common_indent_length = min (common_indent_length , len (indent ))
399
401
elements .append (
400
- self .Indent (blank_lines + indent , blank_start , ps .index )
402
+ self .Indent (blank_lines + indent , blank_start , ps .current_location )
401
403
)
402
404
continue
403
405
@@ -421,7 +423,7 @@ def get_pattern(self, ps: FluentParserStream, is_block: bool) -> ast.Pattern:
421
423
return ast .Pattern (dedented )
422
424
423
425
class Indent (ast .SyntaxNode ):
424
- def __init__ (self , value : str , start : int , end : int ):
426
+ def __init__ (self , value : str , start : Location , end : Location ):
425
427
super (FluentParser .Indent , self ).__init__ ()
426
428
self .value = value
427
429
self .add_span (start , end )
@@ -454,8 +456,8 @@ def dedent(
454
456
sum = ast .TextElement (prev .value + element .value )
455
457
if self .with_spans :
456
458
sum .add_span (
457
- cast (ast .Span , prev .span ).start ,
458
- cast (ast .Span , element .span ).end ,
459
+ cast (ast .Span , prev .span ).start_location ,
460
+ cast (ast .Span , element .span ).end_location ,
459
461
)
460
462
trimmed [- 1 ] = sum
461
463
continue
@@ -466,8 +468,8 @@ def dedent(
466
468
text_element = ast .TextElement (element .value )
467
469
if self .with_spans :
468
470
text_element .add_span (
469
- cast (ast .Span , element .span ).start ,
470
- cast (ast .Span , element .span ).end ,
471
+ cast (ast .Span , element .span ).start_location ,
472
+ cast (ast .Span , element .span ).end_location ,
471
473
)
472
474
element = text_element
473
475
0 commit comments