Skip to content

Commit fc12c32

Browse files
committed
allow puml without final newline
1 parent e5bcbeb commit fc12c32

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ IGNORED = _{ (WHITESPACE | COMMENT)+ }
3131

3232
// Entry Rule
3333
file = { SOI ~ line* ~ EOI }
34+
line_end = _{ NEWLINE | &EOI }
3435

3536
line= _{
3637
includesub_line
3738
| include_line
3839
| sub_block
3940
| text_line
41+
| NEWLINE
4042
}
4143

4244
// ---------------- Include ----------------
43-
include_line = { include_directive ~ NEWLINE }
45+
include_line = { include_directive ~ line_end }
4446
include_directive = {
4547
include_keyword ~ include_path
4648
}
@@ -52,7 +54,7 @@ include_keyword = {
5254
include_path = @{ (!"!" ~(ASCII_ALPHANUMERIC | "_" | "-" | "." | "/" | " "))+ }
5355

5456
// --------------- IncludeSub ---------------
55-
includesub_line = { includesub_directive ~ NEWLINE }
57+
includesub_line = { includesub_directive ~ line_end }
5658
includesub_directive = {
5759
"!includesub" ~ include_path ~ include_suffix ~ !include_suffix
5860
}
@@ -65,18 +67,19 @@ include_index = @{ ASCII_DIGIT+ }
6567

6668
// ---------------- SubBlock ----------------
6769
sub_block = {
68-
startsub_directive ~ NEWLINE ~ sub_content* ~ endsub_directive ~ NEWLINE
70+
startsub_directive ~ NEWLINE ~ sub_content* ~ endsub_directive ~ line_end
6971
}
7072
startsub_directive = { "!startsub" ~ ((include_index | include_label)) }
7173
endsub_directive = { "!endsub" }
7274
sub_content = _{
7375
includesub_line
7476
| include_line
7577
| text_line
78+
| NEWLINE
7679
}
7780

7881
// ---------------- Text Line ----------------
79-
text_line = { (!("!" ~ preprocess_keyword) ~ (!NEWLINE ~ ANY)*) ~ NEWLINE }
82+
text_line = { (!("!" ~ preprocess_keyword) ~ (!NEWLINE ~ ANY)+) ~ line_end }
8083
preprocess_keyword = {
8184
include_keyword
8285
| "includesub"

plantuml/parser/puml_parser/src/preprocessor/src/include/include_parser.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ mod tests {
245245
assert_eq!(path, "path/to/file");
246246
}
247247

248+
#[test]
249+
fn test_parse_include_without_trailing_newline() {
250+
let input = "@startuml\n!include path/to/file";
251+
252+
let result = IncludeParser::parse(Rule::file, input);
253+
254+
assert!(
255+
result.is_ok(),
256+
"expected parser to accept !include at EOF without trailing newline"
257+
);
258+
}
259+
248260
#[test]
249261
fn test_parse_includesub_directive() {
250262
let input = "!includesub path/to/file!2";
@@ -257,6 +269,18 @@ mod tests {
257269
assert!(matches!(suffix, IncludeSuffix::Index(2)));
258270
}
259271

272+
#[test]
273+
fn test_parse_includesub_without_trailing_newline() {
274+
let input = "@startuml\n!includesub path/to/file!2";
275+
276+
let result = IncludeParser::parse(Rule::file, input);
277+
278+
assert!(
279+
result.is_ok(),
280+
"expected parser to accept !includesub at EOF without trailing newline"
281+
);
282+
}
283+
260284
#[test]
261285
fn test_parse_file_io_error() {
262286
let mut service = IncludeParserService;
@@ -267,4 +291,28 @@ mod tests {
267291
Err(IncludeParseError::Base(BaseParseError::IoError { .. }))
268292
));
269293
}
294+
295+
#[test]
296+
fn test_parse_file_without_trailing_newline() {
297+
let input = "@startuml\nclass User {}\n@enduml";
298+
299+
let result = IncludeParser::parse(Rule::file, input);
300+
301+
assert!(
302+
result.is_ok(),
303+
"expected parser to accept EOF without trailing newline"
304+
);
305+
}
306+
307+
#[test]
308+
fn test_parse_sub_block_without_trailing_newline() {
309+
let input = "!startsub label\nclass User {}\n!endsub";
310+
311+
let result = IncludeParser::parse(Rule::file, input);
312+
313+
assert!(
314+
result.is_ok(),
315+
"expected parser to accept !endsub at EOF without trailing newline"
316+
);
317+
}
270318
}

0 commit comments

Comments
 (0)