feat: various update

This commit is contained in:
PoiScript 2023-11-24 14:57:45 +08:00
parent be32dc24e0
commit 3c2c8b28fd
No known key found for this signature in database
GPG key ID: 22C2B1249D99985E
7 changed files with 73 additions and 54 deletions

View file

@ -133,11 +133,8 @@ fn headline_stars(input: Input) -> IResult<Input, Input, ()> {
if level == 0 {
return Err(nom::Err::Error(()));
}
// followed by eof, new line, or whitespace
else if matches!(
bytes.get(level),
None | Some(b'\n') | Some(b'\r') | Some(b' ')
) {
// headline stars must be followed by space
else if matches!(bytes.get(level), Some(b' ')) {
Ok(input.take_split(level))
} else {
Err(nom::Err::Error(()))
@ -232,7 +229,7 @@ fn headline_priority_node(input: Input) -> IResult<Input, (GreenElement, Input),
#[test]
fn parse() {
use crate::{ast::Headline, tests::to_ast};
use crate::{ast::Headline, tests::to_ast, ParseConfig};
let to_headline = to_ast::<Headline>(headline_node);
@ -307,6 +304,16 @@ fn parse() {
NEW_LINE@11..12 "\n"
"###
);
let config = &ParseConfig::default();
assert!(headline_node(("_ ", config).into()).is_err());
assert!(headline_node(("*", config).into()).is_err());
assert!(headline_node((" * ", config).into()).is_err());
assert!(headline_node(("**", config).into()).is_err());
assert!(headline_node(("**\n", config).into()).is_err());
assert!(headline_node(("**\r", config).into()).is_err());
assert!(headline_node(("**\t", config).into()).is_err());
}
#[test]

View file

@ -2,7 +2,7 @@ use memchr::{memchr, memchr2};
use nom::{
branch::alt,
bytes::complete::{tag, take},
character::complete::{alphanumeric1, digit1, space0},
character::complete::{alphanumeric1, digit1, space0, space1},
combinator::{cond, map, opt, recognize, verify},
sequence::{preceded, tuple},
AsBytes, IResult, InputLength, InputTake,
@ -10,7 +10,7 @@ use nom::{
use super::{
combinator::{
at_token, blank_lines, colon2_token, l_bracket_token, line_starts_iter, node,
at_token, blank_lines, colon2_token, eol_or_eof, l_bracket_token, line_starts_iter, node,
r_bracket_token, GreenElement,
},
element::element_node,
@ -82,25 +82,23 @@ fn list_item_node<'a>(
preceded(digit1, tag(".")),
preceded(digit1, tag(")")),
)),
space0,
alt((space1, eol_or_eof)),
)))(input)?;
// bullet must ends with whitespace,
if !(bullet
.s
.bytes()
.last()
.map(|b| b == b' ' || b == b'\t')
.unwrap_or(true)
// or input should be a line end
|| input
.s
.bytes()
.next()
.map(|b| b == b'\r' || b == b'\n')
.unwrap_or(true))
{
return Err(nom::Err::Error(()));
if input.is_empty() {
return Ok((
input,
(
false,
node(
LIST_ITEM,
[
indent.token(LIST_ITEM_INDENT),
bullet.token(LIST_ITEM_BULLET),
],
),
),
));
}
let is_ordered = bullet.s.starts_with(|c: char| c.is_ascii_digit());
@ -289,8 +287,6 @@ fn parse() {
LIST_ITEM@0..2
LIST_ITEM_INDENT@0..0 ""
LIST_ITEM_BULLET@0..2 "1)"
LIST_ITEM_CONTENT@2..2
PARAGRAPH@2..2
"###
);
@ -301,8 +297,6 @@ fn parse() {
LIST_ITEM@0..2
LIST_ITEM_INDENT@0..0 ""
LIST_ITEM_BULLET@0..2 "+ "
LIST_ITEM_CONTENT@2..2
PARAGRAPH@2..2
"###
);
@ -312,10 +306,7 @@ fn parse() {
LIST@0..2
LIST_ITEM@0..2
LIST_ITEM_INDENT@0..0 ""
LIST_ITEM_BULLET@0..1 "-"
LIST_ITEM_CONTENT@1..2
PARAGRAPH@1..2
BLANK_LINE@1..2 "\n"
LIST_ITEM_BULLET@0..2 "-\n"
"###
);
@ -594,6 +585,13 @@ fn parse() {
"###
);
to_list("- ");
to_list("-\t");
to_list("-\r");
to_list("-\t\n");
to_list("-\r\n");
to_list("-");
let config = &ParseConfig::default();
assert!(list_node(("-a", config).into()).is_err());