From 31c00b27094c1ed7c4a1f817c3d14d57cc19458f Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Thu, 16 May 2024 08:55:22 -0500 Subject: Add ocaml-rapper-script --- local/bin/ocaml-rapper-extract-type.awk | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 local/bin/ocaml-rapper-extract-type.awk diff --git a/local/bin/ocaml-rapper-extract-type.awk b/local/bin/ocaml-rapper-extract-type.awk new file mode 100755 index 0000000..4223570 --- /dev/null +++ b/local/bin/ocaml-rapper-extract-type.awk @@ -0,0 +1,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 + } + + -- cgit v1.2.3