Import from my private repository. Snapshot after version 5.16, immediately
[external/ragel.git] / test / langtrans_java.sh
1 #!/bin/bash
2 #
3
4 file=$1
5
6 [ -f $file ] || exit 1
7 root=${file%.rl}
8 class=${root}_java
9
10 # Make a temporary version of the test case the Java language translations.
11 sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin langtrans_java.txl - $class > $file.pr
12
13 # Begin writing out the test case.
14 cat << EOF
15 /*
16  * @LANG: java
17  * @ALLOW_GENFLAGS: -T0
18  * @GENERATED: yes
19  */
20
21 class $class
22 {
23 EOF
24
25 # Write the data declarations
26 sed -n '/^%%$/q;{s/^/\t/;p}' $file.pr
27
28 # Write out the machine specification.
29 sed -n '/^%%{$/,/^}%%/{s/^/\t/;p}' $file.pr
30
31 # Write out the init and execute routines.
32 cat << EOF
33
34         int cs;
35         %% write data;
36
37         void init()
38         {
39 EOF
40
41 sed -n '1,/^%%$/d; /^%%{$/q; {s/^/\t\t/;p}' $file.pr
42
43 cat << EOF
44                 %% write init;
45         }
46
47         void exec( char data[], int len )
48         {
49                 int p = 0;
50                 int pe = len;
51                 %% write exec;
52         }
53
54         void finish( )
55         {
56                 %% write eof;
57                 if ( cs >= ${class}_first_final )
58                         System.out.println( "ACCEPT" );
59                 else
60                         System.out.println( "FAIL" );
61         }
62
63 EOF
64
65 # Write out the test data.
66 sed -n '1,/\/\* _____INPUT_____/d; /_____INPUT_____ \*\//q; p;' $file | awk '
67 BEGIN {
68         print " static final String inp[] = {"
69 }
70 {
71         print "         " $0 ","
72 }
73 END {
74         print " };"
75         print ""
76         print " static final int inplen = " NR ";"
77 }'
78
79
80 # Write out the main routine.
81 cat << EOF
82
83         public static void main (String[] args)
84         {
85                 $class machine = new $class();
86                 for ( int i = 0; i < inplen; i++ ) {
87                         machine.init();
88                         machine.exec( inp[i].toCharArray(), inp[i].length() );
89                         machine.finish();
90                 }
91         }
92 }
93
94 EOF
95
96 # Write out the expected output.
97 sed -n '/\/\* _____OUTPUT_____/,/_____OUTPUT_____ \*\//p;' $file
98
99 # Don't need this language-specific file anymore.
100 rm $file.pr