add a shader-preprocessor (like cpp)
[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                                 ;;
35                         *)
36                                 parse_shaderstage_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
37                                 ;;
38                 esac
39         done
40         parse_shaderstage_post
41 }
42
43 parse_shader_pre()
44 {
45         : replace this function after including
46 }
47 parse_shader_line()
48 {
49         : replace this function after including
50 }
51 parse_shader_post()
52 {
53         : replace this function after including
54 }
55 parse_shader()
56 {
57         parse_shader_pre
58         while read shaderparser_L shaderparser_A1 shaderparser_Aother; do
59                 case "`echo "$shaderparser_L" | tr A-Z a-z`" in
60                         '{')
61                                 parse_shaderstage
62                                 ;;
63                         '}')
64                                 break
65                                 ;;
66                         '')
67                                 ;;
68                         *)
69                                 parse_shader_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
70                                 ;;
71                 esac
72         done
73         parse_shader_post
74 }
75
76 parse_shaderfile_pre()
77 {
78         : replace this function after including
79 }
80 parse_shaderfile_post()
81 {
82         : replace this function after including
83 }
84 parse_shaderfile()
85 {
86         parse_shaderfile_pre
87         while read shaderparser_L; do
88                 case "$shaderparser_L" in
89                         '{')
90                                 parse_shader
91                                 ;;
92                         '{')
93                                 err "brace nesting error in $parsing_shader"
94                                 ;;
95                         '')
96                                 ;;
97                         *)
98                                 parsing_shader="`shader_normalize "$shaderparser_L"`"
99                                 if [ x"$shaderparser_L" != x"$parsing_shader" ]; then
100                                         warn "normalized shader name $shaderparser_L to $parsing_shader"
101                                 fi
102                                 ;;
103                 esac
104         done
105         parse_shaderfile_post
106 }
107
108 shaderparser_strip_comments()
109 {
110         sed 's,//.*,,g; s,\r, ,g; s,\shaderparser_t, ,g; s,  *, ,g; s, $,,; s,^ ,,; /^$/ d'
111 }
112
113 parse_shaders()
114 {
115         shaderparser_t=`mktemp || echo ".temp"`
116         for shaderparser_X in "$@"; do
117                 shaderparser_strip_comments < "$shaderparser_X" > "$shaderparser_t"
118                 parsing_shaderfile="$shaderparser_X"
119                 parse_shaderfile < "$shaderparser_t"
120         done
121         rm -f "$shaderparser_t"
122 }