Skip to content

Commit f351b97

Browse files
fix review items
1 parent 9410ba3 commit f351b97

3 files changed

Lines changed: 61 additions & 29 deletions

File tree

plantuml/parser/integration_test/src/test_error_view.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ impl ErrorView for ActivityParserError {
201201
fn project(&self, base_dir: &Path) -> ProjectedError {
202202
match self {
203203
ActivityParserError::Base(e) => e.project(base_dir),
204-
ActivityParserError::NotImplemented => {
204+
ActivityParserError::InvalidStatement(message) => {
205205
let _ = base_dir;
206-
ProjectedError::new("NotImplemented")
206+
ProjectedError::new("InvalidStatement").with_field("message", message.clone())
207207
}
208208
}
209209
}

plantuml/parser/puml_parser/src/activity_diagram/src/activity_parser.rs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ use puml_utils::LogLevel;
3434
pub 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

4141
impl 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,

plantuml/parser/puml_parser/src/grammar/activity.pest

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,18 @@ fork_start_stmt = {
164164
}
165165
fork_again_stmt = { "fork again" }
166166

167+
end_fork_kw = { "end fork" }
168+
169+
end_merge_kw = { "end merge" }
170+
171+
fork_end_keyword = { end_fork_kw | end_merge_kw }
172+
167173
fork_end_stmt = {
168-
("end fork" | "end merge")
174+
fork_end_keyword
169175
~ fork_modifier?
170176
}
171177

172-
fork_modifier = {
178+
fork_modifier = ${
173179
"{"
174180
~ ("and" | "or")
175181
~ "}"

0 commit comments

Comments
 (0)