OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / compile
1 #!/bin/sh
2 # Compiles all .rl files for Ohcount.
3 # Written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
4
5 # check to make sure we have ragel
6 if `type ragel > /dev/null 2> /dev/null`
7 then
8   echo "Found ragel, compiling..."
9 else
10   echo "ragel not found, aborting" >&2
11   exit 1
12 fi
13
14 for file in *.rl
15 do
16   echo "Compiling $file"
17   if [ -z "`grep '#EMBED' $file`" ]
18   then
19     # no embedded language
20     ragel $file -o "`cut -d '.' -f1 <<< "$file"`.h"
21   else
22     # embedded language(s)
23     elangs=`grep -o "#EMBED([a-z_][a-z_]*)" $file | 
24       sed -e 's/#EMBED(\([a-z_][a-z_]*\))/\1/'`
25
26     # embed all languages into this temporary file to be compiled
27     parser=${file}.tmp
28
29     # grab all lines before #EMBED statements
30     e_line=`grep -n -m 1 "#EMBED([a-z_][a-z_]*)" $file | cut -d ':' -f1`
31
32     sed -n -e "1,$e_line p" $file > $parser
33
34     # embed each language in the temporary file
35     for lang in $elangs
36     do
37       echo -e "}%%\n%%{\n" >> $parser
38
39       e_parser=${lang}.rl
40       # extract the embedded language contents for embedding
41       s_line=`grep -n "^%%{" $e_parser | cut -d ':' -f1`
42       e_line=`grep -n "^}%%" $e_parser | cut -d ':' -f1`
43       s_line=`expr $s_line + 1`
44       e_line=`expr $e_line - 1`
45       sed -n "$s_line,$e_line p;" $e_parser |
46         sed -e "s/^[ \t]*machine[^;][^;]*;//;
47                 s/^[ \t]*write[^;][^;]*;//;
48                 s/^[ \t]*include[^;][^;]*;//;" >> $parser
49     done
50
51     # grab all lines after #EMBED statements
52     s_line=`grep -n "#EMBED([a-z_][a-z_]*)" $file | cut -d ':' -f1 | sort -r |
53       grep -m 1 "^[0-9][0-9]*"`
54     sed -n -e "$s_line,$ p" $file >> $parser
55
56     ragel $parser -o "`cut -d '.' -f1 <<< "$file"`.h"
57     rm $parser
58   fi
59 done