aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/module/descriptor/annotation.lux
blob: ed54199741a1199d3a39778af125ad6ff4e11a39 (plain)
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
(;module:
  lux
  (lux (control codec
                monad)
       (data [text]
             (text format
                   ["l" lexer "l/" Monad<Lexer>])
             [number]
             error
             (coll [list "L/" Functor<List>])))
  ["&" ../common]
  [luxc ["&;" parser]])

(def: dummy-cursor Cursor ["" +0 +0])

(do-template [<name> <code>]
  [(def: <name> &;Signal <code>)]

  [ident-signal "@"]
  [bool-signal  "B"]
  [nat-signal   "N"]
  [int-signal   "I"]
  [deg-signal   "D"]
  [real-signal  "R"]
  [text-signal  "T"]
  [list-signal  "%"]
  [dict-signal  "#"]
  )

(def: (encode-ident [module name])
  (-> Ident Text)
  (format ident-signal
          module &;ident-separator name
          &;stop-signal))

(def: (encode-text value)
  (-> Text Text)
  (format text-signal
          (%t value)
          &;stop-signal))

(def: (encode-ann-value value)
  (-> Ann-Value Text)
  (case value
    (^template [<tag> <signal> <encoder>]
      (<tag> value)
      (format <signal>
              (<encoder> value)
              &;stop-signal))
    ([#;BoolA  bool-signal  %b]
     [#;NatA   nat-signal   %n]
     [#;IntA   int-signal   %i]
     [#;DegA   deg-signal   %d]
     [#;RealA  real-signal  %r]
     [#;TextA  text-signal  %t]
     [#;IdentA ident-signal %ident]
     [#;ListA  list-signal  (&;encode-list encode-ann-value)]
     [#;DictA  dict-signal  (&;encode-list (function [[k v]]
                                             (format (encode-text k)
                                                     (encode-ann-value v))))])))

(def: ann-value-decoder
  (l;Lexer Ann-Value)
  (with-expansions
    [<simple> (do-template [<tag> <lexer> <signal>]
                [(do l;Monad<Lexer>
                   [])])]
    ($_ l;either
        <simple>
        (|> ... (l;after (l;text bool-signal)))
        )))

(def: encode-anns
  (-> Anns Text)
  (&;encode-list (function [[ident value]]
                   (format (encode-ident ident)
                           (encode-ann-value value)))))

(struct: #export _ (Codec Text Anns)
  (def: encode encode-anns)
  (def: decode decode-anns))