-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocessor.hx
81 lines (69 loc) · 2.22 KB
/
Preprocessor.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package om.glsl;
using StringTools;
/**
GLSL preprocessor statement.
*/
class Preprocessor {
public var name : String;
public var args : Array<String>;
public var value : String;
public function new( name : String, ?args : Array<String>, ?value : String ) {
this.name = name;
this.args = args;
this.value = value;
}
public function toString() : String {
var buf = new StringBuf();
buf.add( '#define ' );
buf.add( name );
if( args != null && args.length > 0 ) {
buf.add( '(' );
buf.add( args.join(',') );
buf.add( ')' );
}
if( value != null && value.length > 0 ) {
buf.add( ' ' );
buf.add( value );
}
return buf.toString();
}
public static function parse( str : String ) : Preprocessor {
var split = ~/\s+/.split( str.trim() );
if( split[0] != '#define' )
return throw 'invalid preprocessor';
if( split[1].indexOf( '(' ) == -1 ) {
var name : String = null;
var value : String = null;
var expr = ~/^([a-zA-Z_]+)(\s+(.+))$/;
if( expr.match( split[1]) ) {
name = expr.matched(1);
value = expr.matched(3);
} else {
name = split[1];
value = split.slice(2).join(' ').trim();
}
return new Preprocessor( name, value );
} else {
var content = split.slice(1).join(' ').trim();
var argsStart : Int = null;
var argsEnd : Int = null;
for( i in 0...content.length ) {
switch content.charAt(i) {
case '(': argsStart = i;
case ')': argsEnd = i;
case ' ','\t': if( argsEnd != null )
break;
}
}
return new Preprocessor(
content.substr( 0, argsStart ),
content.substring( argsStart + 1, argsEnd ).split(',').map( StringTools.trim ),
content.substr( argsEnd+1 ).trim()
);
}
}
/*
public static function find( name : String ) : Preprocessor {
}
*/
}