summaryrefslogtreecommitdiff
path: root/tests/testmaker.sh
blob: 2fe54782bccf88626def3234e8e539470d7c0368 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
#
# testmaker.sh - asssemble tests from backend-independent rulesets and
# backend-dependent boilerplate.  Generates both a Flex source file
# and an input text for it.
#
# The single argument is a testfile name to be generated.
# With the -d option, dump to stdourather than crating the file.
#
# To add a new back end named "foo", append "|foo" to the
# third case arm marked "# Add new back ends on this line".

if [ "$1" = -d ] ; then
    shift
    outdev=/dev/stdout
    filter=cat
else
    outdev="$1"
    filter=m4
fi

testfile=$1

trap 'rm -f /tmp/testmaker$$' EXIT INT QUIT

# we do want word splitting, so we won't put double quotes around it
# shellcheck disable=2046
set $(echo "${testfile}" | tr '.' ' ')
for last; do :; done
if [ "${last}" != "l" ]
then
    echo "$0: Don't know how to make anything but a .l file: ${last}" >&2
    exit 1
fi

# ditto
# shellcheck disable=2046
set -- $(echo "${1}" | tr '_' ' ')
stem=$1
options=""
backend=nr
for part in "$@"; do
    case ${part} in
        nr) backend=nr; ;;
        r) backend=r; options="${options} reentrant";;
        c99|go) backend=${part}; options="${options} emit=\"${part}\"" ;;	# Add new back ends on this line
        ser) serialization=yes ;;
        ver) serialization=yes; verification=yes; options="${options} tables-verify" ;;
	Ca) options="${options} align" ;;
	Ce) options="${options} ecs" ;;
	Cf) options="${options} full" ;;
	CxF|Cxf) options="${options} fast" ;;
	Cm) options="${options} meta-ecs";;
	Cem) options="${options} ecs meta-ecs" ;;
	Cae) options="${options} align ecs" ;;
	Caef) options="${options} align ecs full" ;;
	CaexF|Caexf) options="${options} algin ecs fast" ;;
	Cam) options="${options} align meta-ecs" ;;
	Caem) options="${options} align ecs meta-ecs" ;;
    esac
done
# Special case: C99 back end uses same boilerplate as reentrant C.
case ${backend} in c99) backend=r ;; esac

m4def() {
    define="${1}"
    value="${2}"
    # we'll be careful, I promise
    printf "define(\`%s', \`${value}')dnl\n" "${define}"
}

(
    m4def M4_TEST_BACKEND "${backend}"
    if [ -n "${verification}" ] ; then
        m4def M4_TEST_TABLE_VERIFICATION
    fi
    if [ -n "${serialization}" ] ; then
        options="${options} tables-file=\"${testfile%.l}.tables\""
        m4def M4_TEST_TABLE_SERIALIZATION
    fi
    if [ -z "${options}" ] ; then
        m4def M4_TEST_OPTIONS
    else
        m4def M4_TEST_OPTIONS "%%option${options}\n"
    fi
    cat testmaker.m4
    echo "M4_TEST_PREAMBLE\`'dnl"
    echo "M4_TEST_OPTIONS\`'dnl"
    sed <"${stem}.rules" -e "/###/Q0"
    echo "%%"
    echo "M4_TEST_POSTAMBLE\`'dnl"
) | "${filter}" >"${outdev}"

if [ "${outdev}" != /dev/stdout ] && [ ! -f "${stem}.txt" ]
then
    sed <"${stem}.rules" -e "1,/###/d" >"${stem}.txt"
fi

# end