Initialize Tizen 2.3
[external/ragel.git] / test / langtrans_ruby.sh
1 #!/bin/bash
2 #
3
4 file=$1
5
6 [ -f $file ] || exit 1
7
8 # Get the machine name.
9 machine=`sed -n 's/^[\t ]*machine[\t ]*\([a-zA-Z_0-9]*\)[\t ]*;[\t ]*$/\1/p' \
10         $file | tr '[A-Z]' '[a-z]'`
11
12 # Make a temporary version of the test case using the Ruby language translations.
13 sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin langtrans_ruby.txl > $file.pr
14
15 # Begin writing out the test case.
16 cat << EOF
17 #
18 # @LANG: ruby
19 # @GENERATED: yes
20 EOF
21
22 grep '@ALLOW_GENFLAGS:' $file | sed 's/^ *\*/#/' | sed 's/-G.//g'
23 grep '@ALLOW_MINFLAGS:' $file | sed 's/^ *\*/#/'
24
25 cat << EOF
26 #
27
28 EOF
29
30 # Write out the machine specification.
31 sed -n '/^%%{$/,/^}%%/{s/^/\t/;p}' $file.pr
32
33 # Write out the init and execute routines.
34 cat << EOF
35
36         %% write data;
37
38         def run_machine( data )
39                 p = 0
40                 pe = data.length
41                 eof = data.length
42                 cs = 0;
43 EOF
44
45 # Write the data declarations
46 sed -n '/^%%$/q;{s/^/\t/;p}' $file.pr
47
48 # Write the data initializations
49 sed -n '0,/^%%$/d; /^%%{$/q; {s/^/\t\t/;p}' $file.pr
50
51 cat << EOF
52
53                 %% write init;
54                 %% write exec;
55                 if cs >= ${machine}_first_final
56                         puts "ACCEPT"
57                 else
58                         puts "FAIL"
59                 end
60         end
61
62 EOF
63
64 # Write out the test data.
65 sed -n '0,/\/\* _____INPUT_____/d; /_____INPUT_____ \*\//q; p;' $file | awk '
66 BEGIN {
67         print " inp = ["
68 }
69 {
70         print "         " $0 ","
71 }
72 END {
73         print " ]"
74         print ""
75         print " inplen = " NR ";"
76 }'
77
78
79 # Write out the main routine.
80 cat << EOF
81
82         inp.each { |str| 
83                 run_machine(str.unpack("c*"))
84         }
85
86 EOF
87
88 # Write out the expected output.
89 echo "=begin _____OUTPUT_____"
90
91 sed -n '/\/\* _____OUTPUT_____/,/_____OUTPUT_____ \*\//{/_____OUTPUT_____/d;p;};' $file
92
93 echo "=end _____OUTPUT_____"
94
95 # Don't need this language-specific file anymore.
96 rm $file.pr