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 }