Implement entity parser.
This commit is contained in:
		
							parent
							
								
									21f46d09e6
								
							
						
					
					
						commit
						754e9ff7d7
					
				
							
								
								
									
										50
									
								
								src/parser/entity.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/parser/entity.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,50 @@ | ||||
| use nom::branch::alt; | ||||
| use nom::bytes::complete::tag; | ||||
| use nom::bytes::complete::tag_no_case; | ||||
| use nom::character::complete::satisfy; | ||||
| use nom::character::complete::space0; | ||||
| use nom::combinator::eof; | ||||
| use nom::combinator::peek; | ||||
| use nom::combinator::recognize; | ||||
| 
 | ||||
| use super::Context; | ||||
| use crate::error::Res; | ||||
| use crate::parser::object::Entity; | ||||
| use crate::parser::parser_with_context::parser_with_context; | ||||
| use crate::parser::util::get_consumed; | ||||
| 
 | ||||
| #[tracing::instrument(ret, level = "debug")] | ||||
| pub fn entity<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Entity<'s>> { | ||||
|     let (remaining, _) = tag("\\")(input)?; | ||||
|     let (remaining, entity_name) = name(context, remaining)?; | ||||
|     let (remaining, _) = alt(( | ||||
|         tag("{}"), | ||||
|         peek(recognize(parser_with_context!(entity_end)(context))), | ||||
|     ))(remaining)?; | ||||
|     let (remaining, _) = space0(remaining)?; | ||||
| 
 | ||||
|     let source = get_consumed(input, remaining); | ||||
|     Ok(( | ||||
|         remaining, | ||||
|         Entity { | ||||
|             source, | ||||
|             entity_name, | ||||
|         }, | ||||
|     )) | ||||
| } | ||||
| 
 | ||||
| #[tracing::instrument(ret, level = "debug")] | ||||
| fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { | ||||
|     // TODO: This should be defined by org-entities and optionally org-entities-user
 | ||||
| 
 | ||||
|     // TODO: Add the rest of the entities, this is a very incomplete list
 | ||||
|     let (remaining, proto) = alt((alt((tag_no_case("delta"), tag_no_case("pi"))),))(input)?; | ||||
|     Ok((remaining, proto)) | ||||
| } | ||||
| 
 | ||||
| #[tracing::instrument(ret, level = "debug")] | ||||
| fn entity_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { | ||||
|     let (remaining, _) = alt((eof, recognize(satisfy(|c| !c.is_alphabetic()))))(input)?; | ||||
| 
 | ||||
|     Ok((remaining, ())) | ||||
| } | ||||
| @ -1 +1,3 @@ | ||||
| foo <<<*bar* baz>>> lorem ipsum *bar* baz dolar. | ||||
| foo \Delta bar | ||||
| foo \pibar | ||||
| foo \pi{}bar | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander