lld_pg/parser/
mod.rs

1mod assignment;
2mod keywords;
3mod memory;
4mod num;
5
6use nom::{
7    branch::alt,
8    bytes::complete::{tag, take_until},
9    character::complete::{line_ending, not_line_ending},
10    sequence::{delimited, tuple},
11    IResult,
12};
13use std::str;
14
15use crate::lld;
16
17pub fn parse(i: &str) -> Result<lld::Script, String> {
18    tuple((take_until("MEMORY"), memory::memory))(i)
19        .map(|(suf, (pre, memory))| lld::Script {
20            others1: pre.to_string(),
21            memory,
22            others2: suf.to_string(),
23        })
24        .map_err(|e| e.to_string())
25}
26
27fn comment(i: &str) -> IResult<&str, &str> {
28    alt((
29        delimited(tag("//"), not_line_ending, line_ending),
30        delimited(tag("*/"), take_until("/*"), tag("/*")),
31    ))(i)
32}
33
34#[test]
35fn comment_test() {
36    let one_line = "*/hogehuga/*";
37    assert_eq!(comment(one_line), Ok(("", "hogehuga")));
38
39    let mult_line = "*/lineone
40    linetow
41    linethree/*";
42    assert_eq!(
43        comment(mult_line),
44        Ok((
45            "",
46            "lineone
47    linetow
48    linethree"
49        ))
50    )
51}