ratex_parser/functions/
math.rs1use std::collections::HashMap;
2
3use crate::error::{ParseError, ParseResult};
4use crate::functions::{define_function_full, FunctionContext, FunctionSpec};
5use crate::parse_node::{Mode, ParseNode, StyleStr};
6
7pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
8 define_function_full(
9 map,
10 &["\\(", "$"],
11 "styling",
12 0, 0, None,
13 false, true, false, false, false,
14 handle_math_switch,
15 );
16
17 define_function_full(
18 map,
19 &["\\)", "\\]"],
20 "text",
21 0, 0, None,
22 false, true, false, false, false,
23 handle_math_close,
24 );
25}
26
27fn handle_math_switch(
28 ctx: &mut FunctionContext,
29 _args: Vec<ParseNode>,
30 _opt_args: Vec<Option<ParseNode>>,
31) -> ParseResult<ParseNode> {
32 let outer_mode = ctx.parser.mode;
33 ctx.parser.switch_mode(Mode::Math);
34 let close = if ctx.func_name == "\\(" { "\\)" } else { "$" };
35 let body = ctx.parser.parse_expression(false, Some(close))?;
36 ctx.parser.expect(close, true)?;
37 ctx.parser.switch_mode(outer_mode);
38
39 Ok(ParseNode::Styling {
40 mode: ctx.parser.mode,
41 style: StyleStr::Text,
42 body,
43 loc: None,
44 })
45}
46
47fn handle_math_close(
48 ctx: &mut FunctionContext,
49 _args: Vec<ParseNode>,
50 _opt_args: Vec<Option<ParseNode>>,
51) -> ParseResult<ParseNode> {
52 Err(ParseError::msg(format!("Mismatched {}", ctx.func_name)))
53}