malachi/parser/
command.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024 Taylan Gökkaya
3
4// This file is licensed under the terms of Apache-2.0 License.
5
6use nom::Finish;
7
8use super::{
9	capture::{
10		parse_capture,
11		parse_group,
12		parse_priority_group,
13	},
14	literal::parse_literal,
15	prelude::*,
16	Segment,
17	SyntaxError,
18};
19
20pub fn parse_segment(input: &'_ str) -> IResult<&'_ str, Segment<'_>> {
21	alt((
22		// First try parsing a priority group  `[]`.
23		map(parse_priority_group, Segment::PriorityGroup),
24		// Or a normal group `{}`.
25		map(parse_group, Segment::Group),
26		// Or a single capture `<>`.
27		map(parse_capture, Segment::Capture),
28		// If all fails, it's a literal.
29		map(parse_literal, Segment::Text),
30	))(input)
31}
32
33fn parse_cmd(input: &'_ str) -> IResult<&'_ str, Vec<Segment<'_>>> {
34	many0(wrap_space0(parse_segment))(input)
35}
36
37pub fn parse_command(input: &'_ str) -> Result<Vec<Segment<'_>>, SyntaxError> {
38	parse_cmd(input)
39		.finish()
40		.map_err(|e| SyntaxError::from_nom(e, input))
41		.map(|tup| tup.1)
42}