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
use chumsky::{
prelude::{choice, end, filter, just, take_until, Simple},
text::{ident, keyword, newline, TextParser},
Parser,
};
#[derive(Debug)]
pub struct Lua;
impl Lua {
pub fn parse(src: &str) -> Result<String, Vec<Simple<char>>> {
Ok(Self::lex(src)?.join("\n"))
}
pub fn lex(src: &str) -> Result<Vec<String>, Vec<Simple<char>>> {
let dotted = ident()
.then(just('.').or(just(':')))
.then(ident())
.map(|((m, k), f)| format!("{m}{k}{f}"));
let expr = dotted.padded().then_ignore(just('='));
let func = keyword("function").padded();
let local = keyword("local").padded();
let node = choice((
just("---")
.then(filter(|c| *c != '\n').repeated().collect::<String>())
.map(|(s, x)| format!("{s}{x}")),
local.ignore_then(choice((
func.clone()
.ignore_then(ident())
.map(|x| format!("---@func {x} private")),
ident()
.padded()
.then_ignore(just('='))
.map(|x| format!("---@expr {x} private")),
))),
func.ignore_then(dotted)
.map(|x| format!("---@func {x} public")),
expr.map(|x| format!("---@expr {x} public")),
keyword("return")
.ignore_then(ident().padded())
.then_ignore(end())
.map(|x| format!("---@export {x}")),
))
.map(Some);
let misc = take_until(newline()).to(None);
choice((node.padded(), misc))
.repeated()
.flatten()
.parse(src)
}
}