1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use super::{line::PoLine, reader::PoReader, unescape::Unescaper};
use crate::error::Error;
use regex::Regex;
use std::{collections::HashMap, io::Read};

/// Object used for the creation of a PO reader
///
/// This structure is used for initializing a parser.
///
/// The reader is created by the method `[PoReader::parse](#method.parse)`.
pub struct PoParser {
    map_re: Regex,
    map_check_re: Regex,
    message_re: Regex,
    comment_re: Regex,
    unescaper: Unescaper,
}

impl PoParser {
    pub fn new() -> PoParser {
        PoParser {
            map_re: Regex::new(r"(\S+?)\s*=\s*(.*?)\s*;").unwrap(),
            map_check_re: Regex::new(r"^\s*(\S+?\s*=\s*.*?\s*;\s*)*$").unwrap(),
            message_re: Regex::new(
                r#"^\s*(?:#(~?\|?))?\s*(msgctxt|msgid|msgid_plural|msgstr(?:\[(?:0|[1-9]\d*)\])?)?\s*"(.*)"\s*$"#,
            )
            .unwrap(),
            comment_re: Regex::new(r#"^\s*#(.)?\s*(.*)$"#).unwrap(),
            unescaper: Unescaper::new(),
        }
    }

    pub fn parse<R: Read>(&self, reader: R) -> Result<PoReader<R>, Error> {
        PoReader::new(reader, self)
    }

    pub(crate) fn parse_map<'a>(&self, text: &'a str) -> Result<HashMap<&'a str, &'a str>, Error> {
        if self.map_check_re.is_match(text) {
            Ok(self
                .map_re
                .captures_iter(text)
                .filter_map(|c| c.get(1).and_then(|v1| c.get(2).map(|v2| (v1.as_str(), v2.as_str()))))
                .collect())
        } else {
            Err(Error::Unexpected(0, format!("Bad value list definition: `{}`", text)))
        }
    }

    pub(super) fn parse_line(&self, line: &str, n: usize) -> Result<PoLine, ()> {
        if !line.contains(|c: char| !c.is_whitespace()) {
            Ok(PoLine::Blank)
        } else if let Some(c) = self.message_re.captures(line) {
            let string = self
                .unescaper
                .unescape(c.get(3).map(|m| m.as_str()).unwrap_or_default());
            let flags = c.get(1).map(|x| x.as_str().to_string()).unwrap_or_default();

            Ok(match c.get(2) {
                None => PoLine::Continuation(n, flags, string),
                Some(m) => {
                    let tag = if flags.ends_with('|') {
                        String::from("|") + m.as_str()
                    } else {
                        m.as_str().to_string()
                    };

                    PoLine::Message(n, flags, tag, string)
                }
            })
        } else {
            self.comment_re.captures(line).map_or(Err(()), |c| {
                Ok(PoLine::Comment(
                    n,
                    c.get(1).and_then(|m| m.as_str().chars().next()).unwrap_or(' '),
                    c.get(2).map(|m| m.as_str().to_string()).unwrap_or_default(),
                ))
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct TestCase {
        source: &'static str,
        target: Result<PoLine, ()>,
    }

    impl TestCase {
        fn test(&self, parser: &PoParser) {
            assert_eq!(
                parser.parse_line(self.source, 123),
                self.target,
                "Error for source: `{}`",
                self.source
            );
        }
    }

    #[test]
    fn test_func_parse_map() {
        let parser = PoParser::new();

        match parser.parse_map("key=value") {
            Err(err) => assert_eq!(
                format!("{:?}", err),
                "Unexpected error: Bad value list definition: `key=value`"
            ),
            v => panic!("Unexpected result: {:?}", v),
        }

        assert_eq!(
            parser.parse_map("     xyz=abc \t ; a=b;c=  d;   e  \t = f;"),
            Ok(vec![("xyz", "abc"), ("a", "b"), ("c", "d"), ("e", "f"),]
                .into_iter()
                .collect::<HashMap<_, _>>())
        );
    }

    #[test]
    fn test_func_parse_line() {
        let parser = PoParser::new();
        let cases = vec![
            TestCase {
                source: "---",
                target: Err(()),
            },
            TestCase {
                source: "\"--",
                target: Err(()),
            },
            TestCase {
                source: "msgid \"--",
                target: Err(()),
            },
            TestCase {
                source: "msgxx \"--\"",
                target: Err(()),
            },
            TestCase {
                source: "-# Something",
                target: Err(()),
            },
            TestCase {
                source: "",
                target: Ok(PoLine::Blank),
            },
            TestCase {
                source: "      ",
                target: Ok(PoLine::Blank),
            },
            TestCase {
                source: "   \t\r\n   ",
                target: Ok(PoLine::Blank),
            },
            TestCase {
                source: r#"msgid "hello\n\tworld""#,
                target: Ok(PoLine::Message(
                    123,
                    String::new(),
                    String::from("msgid"),
                    String::from("hello\n\tworld"),
                )),
            },
            TestCase {
                source: r#"#| msgstr[3] "hello\n\tworld""#,
                target: Ok(PoLine::Message(
                    123,
                    String::from("|"),
                    String::from("|msgstr[3]"),
                    String::from("hello\n\tworld"),
                )),
            },
            TestCase {
                source: r#""hello\n\tworld""#,
                target: Ok(PoLine::Continuation(123, String::new(), String::from("hello\n\tworld"))),
            },
            TestCase {
                source: r#"#| "path: xx\\yy""#,
                target: Ok(PoLine::Continuation(
                    123,
                    String::from("|"),
                    String::from("path: xx\\yy"),
                )),
            },
            TestCase {
                source: "#, My comment",
                target: Ok(PoLine::Comment(123, ',', String::from("My comment"))),
            },
            TestCase {
                source: "# Another comment",
                target: Ok(PoLine::Comment(123, ' ', String::from("Another comment"))),
            },
            TestCase {
                source: "#$ Special comment",
                target: Ok(PoLine::Comment(123, '$', String::from("Special comment"))),
            },
        ];

        for case in cases {
            case.test(&parser);
        }
    }
}