Move bold out to its own module.
This commit is contained in:
		
							parent
							
								
									6352f92ebc
								
							
						
					
					
						commit
						0b35491047
					
				
							
								
								
									
										75
									
								
								src/parser/bold.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								src/parser/bold.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,75 @@ | ||||
| use crate::parser::parser_with_context::parser_with_context; | ||||
| 
 | ||||
| use super::combinator::context_many_till; | ||||
| use super::error::CustomError; | ||||
| use super::error::MyError; | ||||
| use super::parser_context::ChainBehavior; | ||||
| use super::parser_context::ContextElement; | ||||
| use super::parser_context::ExitMatcherNode; | ||||
| use super::text::symbol; | ||||
| use super::text::Bold; | ||||
| use super::text::Res; | ||||
| use super::text::TextElement; | ||||
| use super::text_element_parser::_in_section; | ||||
| use super::text_element_parser::_preceded_by_whitespace; | ||||
| use super::text_element_parser::flat_text_element; | ||||
| use super::Context; | ||||
| use nom::branch::alt; | ||||
| use nom::bytes::complete::tag; | ||||
| use nom::combinator::map; | ||||
| use nom::combinator::peek; | ||||
| use nom::combinator::recognize; | ||||
| use nom::sequence::tuple; | ||||
| 
 | ||||
| pub fn flat_bold<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Bold<'s>> { | ||||
|     let bold_start = parser_with_context!(context_bold_start)(&context); | ||||
|     let parser_context = context | ||||
|         .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { | ||||
|             exit_matcher: ChainBehavior::AndParent(Some(&context_bold_end)), | ||||
|         })) | ||||
|         .with_additional_node(ContextElement::Context("bold")); | ||||
|     let (remaining, captured) = recognize(tuple((bold_start, |i| { | ||||
|         context_many_till(&parser_context, flat_text_element, context_bold_end)(i) | ||||
|     })))(i)?; | ||||
|     let ret = Bold { contents: captured }; | ||||
|     Ok((remaining, ret)) | ||||
| } | ||||
| 
 | ||||
| fn can_start_bold<'s, 'r>(context: Context<'r, 's>) -> bool { | ||||
|     _preceded_by_whitespace(context) && !_in_section(context, "bold") | ||||
| } | ||||
| 
 | ||||
| pub fn context_bold_start<'s, 'r>( | ||||
|     context: Context<'r, 's>, | ||||
|     input: &'s str, | ||||
| ) -> Res<&'s str, &'s str> { | ||||
|     if can_start_bold(context) { | ||||
|         recognize(bold_start)(input) | ||||
|     } else { | ||||
|         // TODO: Make this a specific error instead of just a generic MyError
 | ||||
|         return Err(nom::Err::Error(CustomError::MyError(MyError( | ||||
|             "Cannot start bold", | ||||
|         )))); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub fn context_bold_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { | ||||
|     let (remaining, actual_match) = recognize(bold_end)(input)?; | ||||
|     peek(alt(( | ||||
|         // Must have whitespace after the end asterisk or it must be the end of that section (as checked by the exit matcher)
 | ||||
|         tag(" "), | ||||
|         tag("\t"), | ||||
|         tag("\n"), | ||||
|         |i| context.check_exit_matcher(i), | ||||
|     )))(remaining)?; | ||||
| 
 | ||||
|     Ok((remaining, actual_match)) | ||||
| } | ||||
| 
 | ||||
| pub fn bold_start(input: &str) -> Res<&str, TextElement> { | ||||
|     map(symbol("*"), TextElement::Symbol)(input) | ||||
| } | ||||
| 
 | ||||
| pub fn bold_end(input: &str) -> Res<&str, TextElement> { | ||||
|     map(symbol("*"), TextElement::Symbol)(input) | ||||
| } | ||||
| @ -1,3 +1,4 @@ | ||||
| mod bold; | ||||
| mod combinator; | ||||
| mod error; | ||||
| mod list; | ||||
|  | ||||
| @ -100,14 +100,6 @@ fn blank_line(input: &str) -> Res<&str, BlankLine> { | ||||
|     )(input) | ||||
| } | ||||
| 
 | ||||
| pub fn bold_start(input: &str) -> Res<&str, TextElement> { | ||||
|     map(symbol("*"), TextElement::Symbol)(input) | ||||
| } | ||||
| 
 | ||||
| pub fn bold_end(input: &str) -> Res<&str, TextElement> { | ||||
|     map(symbol("*"), TextElement::Symbol)(input) | ||||
| } | ||||
| 
 | ||||
| pub fn link_start(input: &str) -> Res<&str, TextElement> { | ||||
|     map(symbol("["), TextElement::Symbol)(input) | ||||
| } | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| use crate::parser::parser_with_context::parser_with_context; | ||||
| use crate::parser::text::paragraph_end; | ||||
| 
 | ||||
| use super::bold::flat_bold; | ||||
| use super::combinator::context_many1; | ||||
| use super::combinator::context_many_till; | ||||
| use super::error::CustomError; | ||||
| @ -10,15 +11,12 @@ use super::parser_context::ChainBehavior; | ||||
| use super::parser_context::ContextElement; | ||||
| use super::parser_context::ContextTree; | ||||
| use super::parser_context::ExitMatcherNode; | ||||
| use super::text::bold_end; | ||||
| use super::text::bold_start; | ||||
| use super::text::line_break; | ||||
| use super::text::link_end; | ||||
| use super::text::link_start; | ||||
| use super::text::space; | ||||
| use super::text::span; | ||||
| use super::text::symbol; | ||||
| use super::text::Bold; | ||||
| use super::text::Link; | ||||
| use super::text::Paragraph; | ||||
| use super::text::Res; | ||||
| @ -26,11 +24,9 @@ use super::text::TextElement; | ||||
| use super::token::Token; | ||||
| use super::Context; | ||||
| use nom::branch::alt; | ||||
| use nom::bytes::complete::tag; | ||||
| use nom::combinator::eof; | ||||
| use nom::combinator::map; | ||||
| use nom::combinator::not; | ||||
| use nom::combinator::peek; | ||||
| use nom::combinator::recognize; | ||||
| use nom::sequence::tuple; | ||||
| use nom::IResult; | ||||
| @ -57,15 +53,11 @@ pub fn context_paragraph_end<'s, 'r>( | ||||
|     paragraph_end(input) | ||||
| } | ||||
| 
 | ||||
| fn can_start_bold<'s, 'r>(context: Context<'r, 's>) -> bool { | ||||
|     _preceded_by_whitespace(context) && !_in_section(context, "bold") | ||||
| } | ||||
| 
 | ||||
| fn can_start_link<'s, 'r>(context: Context<'r, 's>) -> bool { | ||||
|     !_in_section(context, "link") | ||||
| } | ||||
| 
 | ||||
| fn _in_section<'s, 'r, 'x>(context: Context<'r, 's>, section_name: &'x str) -> bool { | ||||
| pub fn _in_section<'s, 'r, 'x>(context: Context<'r, 's>, section_name: &'x str) -> bool { | ||||
|     for thing in context.iter() { | ||||
|         match thing.get_data() { | ||||
|             ContextElement::ExitMatcherNode(_) => {} | ||||
| @ -78,7 +70,7 @@ fn _in_section<'s, 'r, 'x>(context: Context<'r, 's>, section_name: &'x str) -> b | ||||
|     false | ||||
| } | ||||
| 
 | ||||
| fn _preceded_by_whitespace<'s, 'r>(context: Context<'r, 's>) -> bool { | ||||
| pub fn _preceded_by_whitespace<'s, 'r>(context: Context<'r, 's>) -> bool { | ||||
|     let mut context_iterator = context.iter().enumerate(); | ||||
|     loop { | ||||
|         if let Some((i, ctx)) = context_iterator.next() { | ||||
| @ -111,20 +103,6 @@ fn _preceded_by_whitespace<'s, 'r>(context: Context<'r, 's>) -> bool { | ||||
|     false | ||||
| } | ||||
| 
 | ||||
| pub fn context_bold_start<'s, 'r>( | ||||
|     context: Context<'r, 's>, | ||||
|     input: &'s str, | ||||
| ) -> Res<&'s str, &'s str> { | ||||
|     if can_start_bold(context) { | ||||
|         recognize(bold_start)(input) | ||||
|     } else { | ||||
|         // TODO: Make this a specific error instead of just a generic MyError
 | ||||
|         return Err(nom::Err::Error(CustomError::MyError(MyError( | ||||
|             "Cannot start bold", | ||||
|         )))); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub fn context_link_start<'s, 'r>( | ||||
|     context: Context<'r, 's>, | ||||
|     input: &'s str, | ||||
| @ -139,19 +117,6 @@ pub fn context_link_start<'s, 'r>( | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub fn context_bold_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { | ||||
|     let (remaining, actual_match) = recognize(bold_end)(input)?; | ||||
|     peek(alt(( | ||||
|         // Must have whitespace after the end asterisk or it must be the end of that section (as checked by the exit matcher)
 | ||||
|         tag(" "), | ||||
|         tag("\t"), | ||||
|         tag("\n"), | ||||
|         |i| context.check_exit_matcher(i), | ||||
|     )))(remaining)?; | ||||
| 
 | ||||
|     Ok((remaining, actual_match)) | ||||
| } | ||||
| 
 | ||||
| pub fn context_link_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { | ||||
|     let (remaining, actual_match) = recognize(link_end)(input)?; | ||||
|     Ok((remaining, actual_match)) | ||||
| @ -183,7 +148,7 @@ pub fn paragraph<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, P | ||||
|     )) | ||||
| } | ||||
| 
 | ||||
| fn flat_text_element<'s, 'r>( | ||||
| pub fn flat_text_element<'s, 'r>( | ||||
|     context: Context<'r, 's>, | ||||
|     i: &'s str, | ||||
| ) -> Res<&'s str, TextElement<'s>> { | ||||
| @ -204,20 +169,6 @@ fn flat_text_element<'s, 'r>( | ||||
|     ))(i) | ||||
| } | ||||
| 
 | ||||
| fn flat_bold<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Bold<'s>> { | ||||
|     let bold_start = parser_with_context!(context_bold_start)(&context); | ||||
|     let parser_context = context | ||||
|         .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { | ||||
|             exit_matcher: ChainBehavior::AndParent(Some(&context_bold_end)), | ||||
|         })) | ||||
|         .with_additional_node(ContextElement::Context("bold")); | ||||
|     let (remaining, captured) = recognize(tuple((bold_start, |i| { | ||||
|         context_many_till(&parser_context, flat_text_element, context_bold_end)(i) | ||||
|     })))(i)?; | ||||
|     let ret = Bold { contents: captured }; | ||||
|     Ok((remaining, ret)) | ||||
| } | ||||
| 
 | ||||
| fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link<'s>> { | ||||
|     let link_start = parser_with_context!(context_link_start)(&context); | ||||
|     let parser_context = context | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander