@@ -34,15 +34,15 @@ use puml_utils::LogLevel;
3434pub enum ActivityParserError {
3535 #[ error( transparent) ]
3636 Base ( #[ from] BaseParseError < Rule > ) ,
37- #[ error( "activity parser logic is not implemented yet " ) ]
38- NotImplemented ,
37+ #[ error( "invalid activity statement: {0} " ) ]
38+ InvalidStatement ( String ) ,
3939}
4040
4141impl ErrorLocation for ActivityParserError {
4242 fn error_location ( & self ) -> Option < ( usize , usize ) > {
4343 match self {
4444 Self :: Base ( base) => base. error_location ( ) ,
45- Self :: NotImplemented => None ,
45+ _ => None ,
4646 }
4747 }
4848}
@@ -108,27 +108,35 @@ impl PumlActivityParser {
108108 }
109109
110110 Ok ( ArrowStmt {
111- syntax : syntax. ok_or ( ActivityParserError :: NotImplemented ) ?,
111+ syntax : syntax. ok_or_else ( || {
112+ ActivityParserError :: InvalidStatement ( "missing arrow syntax" . to_string ( ) )
113+ } ) ?,
112114 label,
113115 } )
114116 }
115117
116118 fn parse_action_label (
117119 pair : pest:: iterators:: Pair < Rule > ,
120+ statement_kind : & str ,
118121 ) -> Result < String , ActivityParserError > {
119122 pair
120123 . into_inner ( )
121124 . find ( |inner| {
122125 matches ! ( inner. as_rule( ) , Rule :: action_text | Rule :: action_line_text)
123126 } )
124127 . map ( |inner| normalize_creole_text ( inner. as_str ( ) . trim ( ) ) )
125- . ok_or ( ActivityParserError :: NotImplemented )
128+ . ok_or_else ( || {
129+ ActivityParserError :: InvalidStatement ( format ! (
130+ "missing {} label" ,
131+ statement_kind,
132+ ) )
133+ } )
126134 }
127135
128136 fn parse_action_stmt (
129137 pair : pest:: iterators:: Pair < Rule > ,
130138 ) -> Result < ActionStmt , ActivityParserError > {
131- let label = Self :: parse_action_label ( pair) ?;
139+ let label = Self :: parse_action_label ( pair, "action" ) ?;
132140
133141 Ok ( ActionStmt {
134142 label,
@@ -138,7 +146,7 @@ impl PumlActivityParser {
138146 fn parse_backward_stmt (
139147 pair : pest:: iterators:: Pair < Rule > ,
140148 ) -> Result < BackwardStmt , ActivityParserError > {
141- let label = Self :: parse_action_label ( pair) ?;
149+ let label = Self :: parse_action_label ( pair, "backward" ) ?;
142150
143151 Ok ( BackwardStmt { label } )
144152 }
@@ -152,13 +160,18 @@ impl PumlActivityParser {
152160 }
153161
154162 fn parse_control_stmt (
155- _pair : pest:: iterators:: Pair < Rule > ,
163+ pair : pest:: iterators:: Pair < Rule > ,
156164 ) -> Result < ControlStmt , ActivityParserError > {
157- let kind = match _pair . as_str ( ) . trim ( ) {
165+ let kind = match pair . as_str ( ) . trim ( ) {
158166 "break" => ControlKind :: Break ,
159167 "kill" => ControlKind :: Kill ,
160168 "detach" => ControlKind :: Detach ,
161- _ => return Err ( ActivityParserError :: NotImplemented ) ,
169+ _ => {
170+ return Err ( ActivityParserError :: InvalidStatement ( format ! (
171+ "invalid control kind: {}" ,
172+ pair. as_str( ) . trim( ) ,
173+ ) ) )
174+ }
162175 } ;
163176
164177 Ok ( ControlStmt { kind } )
@@ -192,7 +205,9 @@ impl PumlActivityParser {
192205 }
193206
194207 Ok ( IfStartStmt {
195- condition : condition. ok_or ( ActivityParserError :: NotImplemented ) ?,
208+ condition : condition. ok_or_else ( || {
209+ ActivityParserError :: InvalidStatement ( "missing if condition" . to_string ( ) )
210+ } ) ?,
196211 label,
197212 } )
198213 }
@@ -232,7 +247,9 @@ impl PumlActivityParser {
232247 }
233248
234249 Ok ( WhileStartStmt {
235- condition : condition. ok_or ( ActivityParserError :: NotImplemented ) ?,
250+ condition : condition. ok_or_else ( || {
251+ ActivityParserError :: InvalidStatement ( "missing while condition" . to_string ( ) )
252+ } ) ?,
236253 label,
237254 } )
238255 }
@@ -281,7 +298,9 @@ impl PumlActivityParser {
281298 }
282299
283300 Ok ( RepeatWhileStmt {
284- condition : condition. ok_or ( ActivityParserError :: NotImplemented ) ?,
301+ condition : condition. ok_or_else ( || {
302+ ActivityParserError :: InvalidStatement ( "missing repeat while condition" . to_string ( ) )
303+ } ) ?,
285304 label,
286305 } )
287306 }
@@ -301,24 +320,29 @@ impl PumlActivityParser {
301320 fn parse_fork_end_stmt (
302321 pair : pest:: iterators:: Pair < Rule > ,
303322 ) -> Result < ForkEndStmt , ActivityParserError > {
304- let mut kind = ForkEndKind :: EndFork ;
323+ let kind = if pair. as_str ( ) . contains ( "merge" ) {
324+ ForkEndKind :: EndMerge
325+ } else {
326+ ForkEndKind :: EndFork
327+ } ;
305328 let mut modifier = None ;
306329
307330 for inner in pair. into_inner ( ) {
308331 match inner. as_rule ( ) {
309332 Rule :: fork_modifier => {
310- modifier = match inner. as_str ( ) . trim ( ) {
311- "{and}" => Some ( ForkModifier :: And ) ,
312- "{or}" => Some ( ForkModifier :: Or ) ,
313- _ => return Err ( ActivityParserError :: NotImplemented ) ,
333+ let text = inner. as_str ( ) ;
334+ modifier = if text. contains ( "and" ) {
335+ Some ( ForkModifier :: And )
336+ } else if text. contains ( "or" ) {
337+ Some ( ForkModifier :: Or )
338+ } else {
339+ return Err ( ActivityParserError :: InvalidStatement ( format ! (
340+ "invalid fork modifier: {}" ,
341+ text,
342+ ) ) ) ;
314343 } ;
315344 }
316- _ => {
317- let text = inner. as_str ( ) . trim ( ) ;
318- if text == "end merge" {
319- kind = ForkEndKind :: EndMerge ;
320- }
321- }
345+ _ => { }
322346 }
323347 }
324348
@@ -335,7 +359,9 @@ impl PumlActivityParser {
335359 . into_inner ( )
336360 . find ( |inner| inner. as_rule ( ) == Rule :: swimlane_text)
337361 . map ( |inner| normalize_creole_text ( inner. as_str ( ) . trim ( ) ) )
338- . ok_or ( ActivityParserError :: NotImplemented ) ?;
362+ . ok_or_else ( || {
363+ ActivityParserError :: InvalidStatement ( "missing swimlane name" . to_string ( ) )
364+ } ) ?;
339365
340366 Ok ( SwimlaneStmt {
341367 name,
0 commit comments