summaryrefslogtreecommitdiff
path: root/local/bin/ocaml-rapper-extract-type.awk
blob: 42235703e763964ad90c184f365e869b38e0e5e5 (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
BEGIN {
    # Set the initial state
  inside_rapper = 0
  statement_name = ""
  generated_type = ""
  add_bracket = 0
}

# Extract function name so that we can use that
# for the type name
/let [a-zA-Z0-9_]+ =/ && inside_rapper == 0 {
  statement_name = $2
}

# If [%rapper, begin extraction
/\[%rapper/ && inside_rapper == 0 {
  inside_rapper = 1
  add_bracket = 1
  printf "type " statement_name "_result = \n"
}

inside_rapper == 1 && /@[a-z?]+{[a-z_0-9]+}/g {
    from = 0
    # Remove everything before "@" and after "}"
    pos = match ($0, /@[a-z?]+{[a-z_0-9]+}/, val)
    while (0 < pos) {

        # Split the line based on curly braces
        split(val[0], parts, /{/)

        # Extract the @type
        # Remove the @
        type = substr(parts[1], 2)

        # Remove the last character of key
        key = substr(parts[2], 1, length(parts[2])-1)

        # ? = optional
        if (substr(type, length(type)) == "?") {
            type = substr(type, 1, length(type) - 1) " option"
        }

        if (add_bracket == 0){
          print " ; " key " : " type
        }

        else if (add_bracket == 1) {
          print " { " key " : " type
          add_bracket = 0
        }
        from += pos + val[0, "length"]
        pos = match( substr( $0, from ), /@[a-z?]+{[a-z_0-9]+}/, val )
    }
}

# If we see |sql], set inside_rapper to 0
/\|sql}/ && inside_rapper == 1 {
   printf " }\n"
   inside_rapper = 0
 }