csml_interpreter/parser/
parse_scope.rs1use crate::data::{ast::*, tokens::*};
2use crate::parser::parse_braces::parse_r_brace;
3use crate::parser::{
4 parse_actions::parse_root_functions,
5 parse_comments::comment,
6 state_context::count_commands,
7 tools::{get_interval, parse_error},
8};
9use nom::{
10 bytes::complete::tag,
11 error::{ContextError, ParseError},
12 multi::fold_many0,
13 sequence::delimited,
14 sequence::preceded,
15 *,
16};
17
18pub fn parse_root<'a, E>(s: Span<'a>) -> IResult<Span<'a>, Block, E>
23where
24 E: ParseError<Span<'a>> + ContextError<Span<'a>>,
25{
26 let result = fold_many0(
27 parse_root_functions,
28 Block::default,
29 |mut acc: Block, mut command| {
30 let mut index = acc.commands_count;
31
32 let mut instruction_info = InstructionInfo { index, total: 0 };
33
34 count_commands(&mut command, &mut index, &mut instruction_info);
35
36 acc.commands.push((command, instruction_info));
37 acc.commands_count = index;
38 acc
39 },
40 )(s);
41
42 result
43}
44
45pub fn parse_implicit_scope<'a, E>(s: Span<'a>) -> IResult<Span<'a>, Block, E>
46where
47 E: ParseError<Span<'a>> + ContextError<Span<'a>>,
48{
49 let mut acc = Block::default();
50 let (s, item) = parse_root_functions(s)?;
51
52 let instruction_info = InstructionInfo { index: 0, total: 0 };
53
54 acc.commands.push((item, instruction_info));
55 Ok((s, acc))
56}
57
58pub fn parse_scope<'a, E>(s: Span<'a>) -> IResult<Span<'a>, Block, E>
59where
60 E: ParseError<Span<'a>> + ContextError<Span<'a>>,
61{
62 let (start, _) = get_interval(s)?;
63
64 parse_error(
65 start,
66 s,
67 delimited(
68 preceded(comment, tag(L_BRACE)),
69 parse_root,
70 preceded(comment, parse_r_brace),
71 ),
72 )
73}