webwire_cli/idl/
include.rs1use nom::{
2 bytes::complete::{tag, take_while1},
3 character::complete::char,
4 combinator::map,
5 sequence::{preceded, terminated},
6 IResult,
7};
8
9use crate::common::FilePosition;
10
11use super::{
12 common::{ws, ws1},
13 Span,
14};
15
16#[derive(Clone, Debug, PartialEq)]
17pub struct Include {
18 pub filename: String,
19 pub position: FilePosition,
20}
21
22pub fn parse_include(input: Span) -> IResult<Span, Include> {
23 preceded(
24 ws,
25 map(
26 preceded(
27 terminated(tag("include"), ws1),
28 terminated(parse_filename, char(';')),
29 ),
30 |filename| Include {
31 filename: filename.to_string(),
32 position: filename.into(),
33 },
34 ),
35 )(input)
36}
37
38pub fn parse_filename(input: Span) -> IResult<Span, Span> {
39 take_while1(|c| c != ';')(input)
40}
41
42#[test]
43fn test_parse_include() {
44 use super::common::assert_parse;
45 use super::*;
46 let content = "include common.idl;";
47 assert_parse(
48 parse_include(Span::new(content)),
49 Include {
50 filename: String::from("common.idl"),
51 position: FilePosition { line: 1, column: 9 },
52 },
53 );
54}
55
56#[test]
57fn test_parse_include_with_directory() {
58 use super::common::assert_parse;
59 use super::*;
60 let content = "include a/b/c.idl;";
61 assert_parse(
62 parse_include(Span::new(content)),
63 Include {
64 filename: String::from("a/b/c.idl"),
65 position: FilePosition { line: 1, column: 9 },
66 },
67 );
68}