kotlin_parser/parse/declaration/
annotation.rs1use crate::{
2 ast::*,
3 parse::expression::{
4 call::invocation_args_parser, path::path_parser,
5 },
6};
7use chumsky::prelude::*;
8
9pub fn annotations_parser(
10 expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
11) -> impl Parser<char, AnnotationSet, Error = Simple<char>> {
12 just('@')
13 .ignore_then(annotation_site_parser().then_ignore(just(':')).or_not())
14 .then(annotation_parser(expr_parser).repeated())
15 .map(|(site, annotations)| AnnotationSet { site, annotations })
16}
17
18pub fn file_annotations_parser(
19 expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
20) -> impl Parser<char, AnnotationSet, Error = Simple<char>> {
21 just('@')
22 .ignore_then(just("file:"))
23 .ignore_then(annotation_parser(expr_parser).repeated())
24 .map(|annotations| AnnotationSet {
25 site: None,
26 annotations,
27 })
28}
29
30pub fn annotation_site_parser(
31) -> impl Parser<char, AnnotationSite, Error = Simple<char>> {
32 choice((
33 just("field").to(AnnotationSite::Field),
34 just("property").to(AnnotationSite::Property),
35 just("get").to(AnnotationSite::Get),
36 just("set").to(AnnotationSite::Set),
37 just("receiver").to(AnnotationSite::Receiver),
38 just("param").to(AnnotationSite::Param),
39 just("setparam").to(AnnotationSite::SetParam),
40 just("delegate").to(AnnotationSite::Delegate),
41 ))
42}
43
44pub fn annotation_parser(
45 expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
46) -> impl Parser<char, Annotation, Error = Simple<char>> {
47 path_parser()
48 .then(
49 just('(')
50 .ignore_then(invocation_args_parser(expr_parser))
51 .then_ignore(just(')'))
52 .or_not(),
53 )
54 .map(|(path, args)| Annotation {
55 path,
56 args: args.unwrap_or_default(),
57 })
58}