ea964572f83f1342651850b571407ff97e46575c
[xonotic/xonotic-maps.pk3dir.git] / scripts / shader-parser.subr
1 #!/bin/sh
2
3 # simple shader parsing "framework"
4
5 shader_normalize()
6 {
7         echo "$1" | sed 's/\.\(tga\|jpg\|png\)$//'
8 }
9
10 parse_shaderstage_pre()
11 {
12         : replace this function after including
13 }
14 parse_shaderstage_line()
15 {
16         : replace this function after including
17 }
18 parse_shaderstage_post()
19 {
20         : replace this function after including
21 }
22 parse_shaderstage()
23 {
24         parse_shaderstage_pre
25         while read shaderparser_L shaderparser_A1 shaderparser_Aother; do
26                 case "`echo "$shaderparser_L" | tr A-Z a-z`" in
27                         '{')
28                                 err "brace nesting error in $parsing_shader"
29                                 ;;
30                         '}')
31                                 break
32                                 ;;
33                         *)
34                                 parse_shaderstage_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
35                                 ;;
36                 esac
37         done
38         parse_shaderstage_post
39 }
40
41 parse_shader_pre()
42 {
43         : replace this function after including
44 }
45 parse_shader_line()
46 {
47         : replace this function after including
48 }
49 parse_shader_post()
50 {
51         : replace this function after including
52 }
53 parse_shader()
54 {
55         parse_shader_pre
56         while read shaderparser_L shaderparser_A1 shaderparser_Aother; do
57                 case "`echo "$shaderparser_L" | tr A-Z a-z`" in
58                         '{')
59                                 parse_shaderstage
60                                 ;;
61                         '}')
62                                 break
63                                 ;;
64                         *)
65                                 parse_shader_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
66                                 ;;
67                 esac
68         done
69         parse_shader_post
70 }
71
72 parse_shaderfile_pre()
73 {
74         : replace this function after including
75 }
76 parse_shaderfile_post()
77 {
78         : replace this function after including
79 }
80 parse_shaderfile()
81 {
82         parse_shaderfile_pre
83         while read shaderparser_L; do
84                 case "$shaderparser_L" in
85                         '{')
86                                 parse_shader
87                                 ;;
88                         '{')
89                                 err "brace nesting error in $parsing_shader"
90                                 ;;
91                         '')
92                                 ;;
93                         *)
94                                 parsing_shader="`shader_normalize "$shaderparser_L"`"
95                                 if [ x"$shaderparser_L" != x"$parsing_shader" ]; then
96                                         warn "normalized shader name $shaderparser_L to $parsing_shader"
97                                 fi
98                                 ;;
99                 esac
100         done
101         parse_shaderfile_post
102 }
103
104 shaderparser_strip_comments()
105 {
106         sed 's,//.*,,g; s,\r, ,g; s,\shaderparser_t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
107 }
108
109 parse_shaders()
110 {
111         shaderparser_t=`mktemp || echo ".temp"`
112         for shaderparser_X in *.shader; do
113                 shaderparser_strip_comments < "$shaderparser_X" > "$shaderparser_t"
114                 parsing_shaderfile="$shaderparser_X"
115                 parse_shaderfile < "$shaderparser_t"
116         done
117         rm -f "$shaderparser_t"
118 }