55
66
77class FluentParser (object ):
8+ def __init__ (self , with_spans = True , with_annotations = True ):
9+ self .with_spans = with_spans
10+ self .with_annotations = with_annotations
11+
812 def parse (self , source ):
913 comment = None
1014
@@ -14,7 +18,7 @@ def parse(self, source):
1418 entries = []
1519
1620 while ps .current ():
17- entry = get_entry_or_junk (ps )
21+ entry = get_entry_or_junk (self , ps )
1822
1923 if isinstance (entry , ast .Comment ) and len (entries ) == 0 :
2024 comment = entry
@@ -28,28 +32,31 @@ def parse(self, source):
2832 def parse_entry (self , source ):
2933 ps = FTLParserStream (source )
3034 ps .skip_ws_lines ()
31- return get_entry_or_junk (ps )
35+ return get_entry_or_junk (self , ps )
3236
3337
34- def get_entry_or_junk (ps ):
38+ def get_entry_or_junk (self , ps ):
3539 entry_start_pos = ps .get_index ()
3640
3741 try :
3842 entry = get_entry (ps )
39- entry .add_span (entry_start_pos , ps .get_index ())
43+ if self .with_spans :
44+ entry .add_span (entry_start_pos , ps .get_index ())
4045 return entry
4146 except ParseError as err :
42- annot = ast .Annotation (err .code , err .args , err .message )
43- annot .add_span (ps .get_index (), ps .get_index ())
44-
47+ error_index = ps .get_index ()
4548 ps .skip_to_next_entry_start ()
4649 next_entry_start = ps .get_index ()
4750
4851 # Create a Junk instance
4952 slice = ps .get_slice (entry_start_pos , next_entry_start )
5053 junk = ast .Junk (slice )
51- junk .add_span (entry_start_pos , next_entry_start )
52- junk .add_annotation (annot )
54+ if self .with_spans :
55+ junk .add_span (entry_start_pos , next_entry_start )
56+ if self .with_annotations :
57+ annot = ast .Annotation (err .code , err .args , err .message )
58+ annot .add_span (error_index , error_index )
59+ junk .add_annotation (annot )
5360 return junk
5461
5562
0 commit comments