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,
13 0,
14 None,
15 false,
16 true,
17 false,
18 false,
19 false,
20 handle_math_switch,
21 );
22
23 define_function_full(
24 map,
25 &["\\)", "\\]"],
26 "text",
27 0,
28 0,
29 None,
30 false,
31 true,
32 false,
33 false,
34 false,
35 handle_math_close,
36 );
37}
38
39fn handle_math_switch(
40 ctx: &mut FunctionContext,
41 _args: Vec<ParseNode>,
42 _opt_args: Vec<Option<ParseNode>>,
43) -> ParseResult<ParseNode> {
44 let outer_mode = ctx.parser.mode;
45 ctx.parser.switch_mode(Mode::Math);
46 let close = if ctx.func_name == "\\(" { "\\)" } else { "$" };
47 let body = ctx.parser.parse_expression(false, Some(close))?;
48 ctx.parser.expect(close, true)?;
49 ctx.parser.switch_mode(outer_mode);
50
51 Ok(ParseNode::Styling {
52 mode: ctx.parser.mode,
53 style: StyleStr::Text,
54 body,
55 loc: None,
56 })
57}
58
59fn handle_math_close(
60 ctx: &mut FunctionContext,
61 _args: Vec<ParseNode>,
62 _opt_args: Vec<Option<ParseNode>>,
63) -> ParseResult<ParseNode> {
64 Err(ParseError::msg(format!("Mismatched {}", ctx.func_name)))
65}