gray/src/gray_params.awk

70 lines
1.5 KiB
Awk
Raw Normal View History

#/usr/bin/env awk -f
# This script generates the very repetitive code for parsing the
# GRAY parameters from a string. The output is embedded
# into gray_params.f90 using the #include cpp directive.
BEGIN {
# set file to open
ARGV[1] = "src/gray_params.f90"
ARGC = 2
# start enum conversion switch
print "select case (value)"
}
# match an enum constant
/enumerator :: [A-Z0-9_]+/ {
# generate conversion case
print " case ('"$3"')"
print " temp = '"$5"'"
}
# match start of a parameters set
/type [[:alnum:]_]+_parameters$/ {
if (!done_enum) {
# close enum switch
print " case default"
print " temp = value"
print "end select\n"
# start params switch
print "select case (name)"
done_enum = 1
}
# set the current set
split($2, words, "_parameters")
if (words[1] != "gray") set = words[1]
# change separator for the next step
FS = "::"
}
# match a parameter
/ :: [[:alnum:]_]+/ {
if (!set) next
# strip comments
sub(/!.*$/, "")
for (i = 1; i <= split($2, ids, ","); i++) {
# extract identifier
match(ids[i], /^ *[[:alnum:]_]+/)
id = substr(ids[i], RSTART + 1, RLENGTH - 1)
# generate parameter case
print " case ('"set"."id"')"
print " read (temp, *, iostat=err) params%"set"%"id
print " if (err > 0) err = ERR_VALUE"
}
}
# match end of a parameters set
/end type$/ {
set = ""
FS = " "
}
END {
# close the parameters switch
print " case default"
print " ! unknown parameter"
print " err = ERR_UNKNOWN"
print "end select"
}