@@ -12,7 +12,7 @@ use crate::{
1212 } ,
1313 Backend ,
1414 } ,
15- parser:: parse ,
15+ parser:: { dump_tracks , Parser } ,
1616 time, BufLines ,
1717} ;
1818
@@ -57,15 +57,21 @@ impl Backend for FixupBackend {
5757 parser_input : & BufLines , out : & mut Out , settings : Self :: BackendSettings ,
5858 ) -> BackendResult {
5959 if let Some ( dump) = settings. dump {
60- let ( parse_time, parsed) = time ( || parse ( parser_input) ) ;
60+ let ( parse_time, parsed) = time ( || Parser :: parse ( parser_input) ) ;
6161 let mut r = BackendResult :: new ( vec ! [ ] , None , Some ( parse_time) , None ) ;
62+ let parsed = match parsed {
63+ Ok ( x) => x,
64+ Err ( ( y, x) ) => {
65+ r. err = Some ( y) ;
66+ x
67+ }
68+ } ;
6269 match dump {
6370 FixupDumpOptions :: TickStream => writeln ! ( out, "{:?}" , parsed. tick_stream) . unwrap ( ) ,
6471 FixupDumpOptions :: PrettyTracks => {
65- writeln ! ( out, "{}" , parsed. dump_tracks ( ) ) . unwrap ( )
72+ writeln ! ( out, "{}" , dump_tracks ( & parsed. as_ref ( ) ) ) . unwrap ( )
6673 }
6774 }
68- r. err = parsed. error ;
6975 return r;
7076 }
7177
@@ -76,81 +82,66 @@ impl Backend for FixupBackend {
7682 let mut location_tracker = LocationTracker :: new ( ) ;
7783 loop {
7884 let parse_start = Instant :: now ( ) ;
79- let parsed = parse ( & parser_input) ;
85+ let r = Parser :: parse ( & parser_input) ;
8086 parse_time = parse_start. elapsed ( ) ;
81- match & parsed. error {
82- None => break ,
83- Some ( err) => {
84- location_tracker. add ( err. main_location . clone ( ) ) ;
85- if location_tracker. is_same ( ) {
86- let lines =
87- err. main_location . get_line_idx ( ) . map ( |x| x..=x) . unwrap_or ( 0 ..=0 ) ;
88- return BackendResult :: new (
89- diagnostics,
90- Some ( BackendError :: fixup_failed ( err. main_location . clone ( ) , lines) ) ,
91- Some ( parse_time) ,
92- Some ( fixup_start. elapsed ( ) ) ,
93- ) ;
94- }
95- match err. kind {
96- BackendErrorKind :: IOError ( _) | BackendErrorKind :: FmtError ( _) => {
97- return BackendResult :: new (
98- diagnostics,
99- parsed. error ,
100- Some ( parse_time) ,
101- None ,
102- ) ;
103- }
104- BackendErrorKind :: BendOnInvalid => { } // todo: bendOnInvalid fixup: remove the bend
105- BackendErrorKind :: InvalidStringName => { }
106- BackendErrorKind :: EmptyScore => { }
107- BackendErrorKind :: BothSlotsMultiChar => { } // todo: fix BothSlotsMultichar errors
108- BackendErrorKind :: FretTooLarge => { } // todo: fix FretTooLarge errors (add
109- // space between)
110- BackendErrorKind :: MultiBothSlotsFilled => {
111- let Some ( ( line_idx, char_idx) ) = err
112- . main_location
113- . get_line_idx ( )
114- . zip ( err. main_location . get_char_idx ( ) )
115- else {
116- continue ;
117- } ;
118- // we just replace both with a rest to be sure
119- // todo: use a better strategy by checking the actual ticks
120- let line_mut = parser_input[ line_idx] . to_mut ( ) ;
121- line_mut. replace_range ( char_idx..char_idx + 1 , "-" ) ;
122- line_mut. replace_range ( char_idx + 1 ..char_idx + 2 , "-" ) ;
123- diagnostics. push ( Diagnostic :: info (
124- err. main_location . clone ( ) ,
125- DiagnosticKind :: FormatReplacedInvalid ,
126- ) ) ;
127- }
128- BackendErrorKind :: NoClosingBarline => {
129- let l_idx = err. main_location . get_line_idx ( ) . unwrap ( ) ;
130- parser_input[ l_idx] . to_mut ( ) . push ( '|' ) ;
131- diagnostics. push ( Diagnostic :: info (
132- err. main_location . clone ( ) ,
133- DiagnosticKind :: FormatAddedBarline ,
134- ) )
135- }
136- BackendErrorKind :: FixupFailed => unreachable ! ( ) ,
137- BackendErrorKind :: Parse3InvalidCharacter ( _) => {
138- let Some ( ( line_idx, char_idx) ) = err
139- . main_location
140- . get_line_idx ( )
141- . zip ( err. main_location . get_char_idx ( ) )
142- else {
143- continue ;
144- } ;
145- parser_input[ line_idx]
146- . to_mut ( )
147- . replace_range ( char_idx + 1 ..char_idx + 2 , "-" ) ;
148- diagnostics. push ( Diagnostic :: info (
149- err. main_location . clone ( ) ,
150- DiagnosticKind :: FormatReplacedInvalid ,
151- ) )
152- }
87+ let Err ( ( err, _) ) = r else { break } ;
88+
89+ location_tracker. add ( err. main_location . clone ( ) ) ;
90+ if location_tracker. is_same ( ) {
91+ let lines = err. main_location . get_line_idx ( ) . map ( |x| x..=x) . unwrap_or ( 0 ..=0 ) ;
92+ return BackendResult :: new (
93+ diagnostics,
94+ Some ( BackendError :: fixup_failed ( err. main_location . clone ( ) , lines) ) ,
95+ Some ( parse_time) ,
96+ Some ( fixup_start. elapsed ( ) ) ,
97+ ) ;
98+ }
99+ match err. kind {
100+ BackendErrorKind :: IOError ( _) | BackendErrorKind :: FmtError ( _) => {
101+ return BackendResult :: new ( diagnostics, Some ( err) , Some ( parse_time) , None ) ;
102+ }
103+ BackendErrorKind :: BendOnInvalid => { } // TODO: bendOnInvalid fixup: remove the bend
104+ BackendErrorKind :: InvalidStringName => { }
105+ BackendErrorKind :: EmptyScore => { }
106+ BackendErrorKind :: BothSlotsMultiChar => { } // TODO: fix BothSlotsMultichar errors
107+ BackendErrorKind :: FretTooLarge => { } // TODO: fix FretTooLarge errors (add
108+ // space between)
109+ BackendErrorKind :: MultiBothSlotsFilled => {
110+ let Some ( ( line_idx, char_idx) ) =
111+ err. main_location . get_line_idx ( ) . zip ( err. main_location . get_char_idx ( ) )
112+ else {
113+ continue ;
114+ } ;
115+ // we just replace both with a rest to be sure
116+ // TODO: use a better strategy by checking the actual ticks
117+ let line_mut = parser_input[ line_idx] . to_mut ( ) ;
118+ line_mut. replace_range ( char_idx..char_idx + 1 , "-" ) ;
119+ line_mut. replace_range ( char_idx + 1 ..char_idx + 2 , "-" ) ;
120+ diagnostics. push ( Diagnostic :: info (
121+ err. main_location . clone ( ) ,
122+ DiagnosticKind :: FormatReplacedInvalid ,
123+ ) ) ;
124+ }
125+ BackendErrorKind :: NoClosingBarline => {
126+ let l_idx = err. main_location . get_line_idx ( ) . unwrap ( ) ;
127+ parser_input[ l_idx] . to_mut ( ) . push ( '|' ) ;
128+ diagnostics. push ( Diagnostic :: info (
129+ err. main_location . clone ( ) ,
130+ DiagnosticKind :: FormatAddedBarline ,
131+ ) )
132+ }
133+ BackendErrorKind :: FixupFailed => unreachable ! ( ) ,
134+ BackendErrorKind :: Parse3InvalidCharacter ( _) => {
135+ let Some ( ( line_idx, char_idx) ) =
136+ err. main_location . get_line_idx ( ) . zip ( err. main_location . get_char_idx ( ) )
137+ else {
138+ continue ;
153139 } ;
140+ parser_input[ line_idx] . to_mut ( ) . replace_range ( char_idx + 1 ..char_idx + 2 , "-" ) ;
141+ diagnostics. push ( Diagnostic :: info (
142+ err. main_location . clone ( ) ,
143+ DiagnosticKind :: FormatReplacedInvalid ,
144+ ) )
154145 }
155146 }
156147 }
0 commit comments