diff options
| author | Philipp A | 2020-10-31 15:39:08 +0100 |
|---|---|---|
| committer | GitHub | 2020-10-31 15:39:08 +0100 |
| commit | 8252a895ed0d23f23065be5000c45429031a2c6f (patch) | |
| tree | ea5e75cc430223f90e35bb4eaedb44f45e9b02a7 | |
| parent | 9d506af852e253cf49095c330183075272de1f79 (diff) | |
| download | rust-rst-8252a895ed0d23f23065be5000c45429031a2c6f.tar.bz2 | |
Fix comment conversion (#27)
| -rw-r--r-- | parser/src/conversion/block.rs | 20 | ||||
| -rw-r--r-- | parser/src/rst.pest | 27 | ||||
| -rw-r--r-- | parser/src/tests.rs | 44 |
3 files changed, 42 insertions, 49 deletions
diff --git a/parser/src/conversion/block.rs b/parser/src/conversion/block.rs index f5697fc..e23cce2 100644 --- a/parser/src/conversion/block.rs +++ b/parser/src/conversion/block.rs @@ -236,23 +236,11 @@ fn convert_raw_directive(pair: Pair<Rule>) -> Result<e::Raw, Error> { Ok(raw_block) } -fn convert_comment_block(pair: Pair<Rule>) -> String { - let iter = pair.into_inner(); - let block = iter.skip(1).next().unwrap(); - let text = block.into_inner().map(|l| match l.as_rule() { - Rule::comment_line_blank => "", - Rule::comment_line => l.as_str(), - _ => unreachable!(), - }.into()).collect::<Vec<&str>>().join("\n"); - text -} - fn convert_comment(pair: Pair<Rule>) -> Result<e::Comment, Error> { - let block = pair.into_inner().skip(1).next().unwrap(); - let children = block.into_inner().map(|l| match l.as_rule() { - Rule::comment_title => String::from(l.as_str()), - Rule::comment_block => convert_comment_block(l), + let lines = pair.into_inner().map(|l| match l.as_rule() { + Rule::comment_line_blank => "\n", + Rule::comment_line => l.as_str(), _ => unreachable!(), }.into()).collect(); - Ok(e::Comment::with_children(children)) + Ok(e::Comment::with_children(lines)) } diff --git a/parser/src/rst.pest b/parser/src/rst.pest index 4329e84..ed0236e 100644 --- a/parser/src/rst.pest +++ b/parser/src/rst.pest @@ -102,9 +102,9 @@ raw_directive = { blank_line+ ~ PEEK[..-1] ~ PUSH(" " ~ POP) ~ raw_block ~ DROP } raw_output_format = { (!NEWLINE ~ ANY)+ } -raw_block = { raw_line ~ (raw_line_blank* ~ PEEK[..] ~ raw_line)* } -raw_line_blank = { " "* ~ NEWLINE } -raw_line = { (!NEWLINE ~ ANY)+ ~ NEWLINE } +raw_block = { raw_line ~ (raw_line_blank* ~ PEEK[..] ~ raw_line)* } +raw_line_blank = { " "* ~ NEWLINE } +raw_line = { (!NEWLINE ~ ANY)+ ~ NEWLINE } // Admonition. A directive. The generic one has a title @@ -115,13 +115,22 @@ admonition_content = _{ PEEK[..-1] ~ PUSH(" " ~ POP) ~ hanging_block ~ block* } // Comments. -block_comment = { - ".." ~ PUSH(" "*) ~ comment_title? ~ NEWLINE? ~ comment_block? ~ (" "* ~ NEWLINE)* ~ DROP +block_comment = { + ".." ~ + ( // Without title + (" "* ~ NEWLINE)+ ~ PUSH(" "+) ~ comment_hanging ~ DROP + // or with title + | PUSH(" "+) ~ comment_line ~ + (comment_line_blank* ~ PEEK[..-1] ~ PUSH(" " ~ POP) ~ comment_hanging)? ~ + DROP + // or empty + | " "* ~ NEWLINE + ) ~ + (" "* ~ NEWLINE)* } -comment_title = { (!NEWLINE ~ ANY)+ } -comment_block = { (comment_line_blank* ~ PEEK[..] ~ comment_line)+ } -comment_line_blank = { " "* ~ NEWLINE } -comment_line = { " "+ ~ (!NEWLINE ~ ANY)+ ~ NEWLINE } +comment_hanging = _{ comment_line ~ (comment_line_blank* ~ PEEK[..] ~ comment_line)* } +comment_line_blank = { " "* ~ NEWLINE } +comment_line = { (!NEWLINE ~ ANY)+ ~ NEWLINE } /* diff --git a/parser/src/tests.rs b/parser/src/tests.rs index b4b9e19..5a9ee9a 100644 --- a/parser/src/tests.rs +++ b/parser/src/tests.rs @@ -248,46 +248,42 @@ fn comments() { followed by a non-blank line and another one. +.. +.. Comments can also be + run-in like this ", rule: Rule::document, tokens: [ block_comment(0, 22, [ - comment_title(3, 20), + comment_line(3, 21), ]), block_comment(22, 43, [ - comment_block(25, 42, [ - comment_line(25, 42), - ]) + comment_line(28, 42), ]), block_comment(43, 63, [ - comment_block(46, 63, [ - comment_line(46, 63), - ]) + comment_line(49, 63), ]), block_comment(63, 81, [ - comment_block(66, 81, [ - comment_line(66, 81), - ]) + comment_line(69, 81), ]), block_comment(81, 99, [ - comment_block(84, 99, [ - comment_line(84, 99), - ]) + comment_line(87, 99), ]), block_comment(99, 121, [ - comment_block(102, 121, [ - comment_line(102, 121), - ]) + comment_line(105, 121), ]), block_comment(121, 216, [ - comment_title(124, 138), - comment_block(139, 216, [ - comment_line_blank(139, 140), - comment_line(141, 163), - comment_line(164, 195), - comment_line_blank(195, 196), - comment_line(197, 216), - ]) + comment_line(124, 139), + comment_line_blank(139, 140), + comment_line(143, 163), + comment_line(166, 195), + comment_line_blank(195, 196), + comment_line(199, 216), + ]), + block_comment(216, 219), + block_comment(219, 263, [ + comment_line(222, 243), + comment_line(246, 263), ]), ] }; |
