Last active
August 29, 2015 14:02
-
-
Save nadako/c85c7aad16e5d9c7ab2b to your computer and use it in GitHub Desktop.
HXML parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using StringTools; | |
typedef Build = { | |
target:Target, | |
output:String, | |
classPaths:Array<String>, | |
main:String, | |
debug:Bool, | |
libs:Array<String>, | |
defines:Map<String, String>, | |
types:Array<String>, | |
} | |
@:enum abstract Target(String) to String { | |
var As3 = "as3"; | |
var Cs = "cs"; | |
var Cpp = "cpp"; | |
var Java = "java"; | |
var Js = "js"; | |
var Neko = "neko"; | |
var Php = "php"; | |
var Python = "python"; | |
var Swf = "swf"; | |
} | |
enum HxmlLine { | |
Comment(s:String); | |
Simple(s:String); | |
Param(s:String, p:String); | |
} | |
class Main { | |
static function main() { | |
var src = sys.io.File.getContent("sample.hxml"); | |
var lines = readLines(src); | |
var build = parseLines(lines); | |
trace(build); | |
} | |
static function parseLines(lines:Array<HxmlLine>):Build { | |
var build:Build = { | |
target: null, | |
output: null, | |
main: null, | |
classPaths: [], | |
libs: [], | |
debug: false, | |
defines: new Map(), | |
types: [], | |
}; | |
var typeRegex = ~/^([a-z]\w*\.)*[A-Z]\w*$/; | |
for (line in lines) { | |
switch (line) { | |
case Comment(_): | |
case Simple("-debug"): | |
build.debug = true; | |
case Param(s = ("-as3" | "-cs" | "-cpp" | "-java" | "-js" | "-neko" | "-php" | "-python" | "-swf"), output): | |
build.target = cast s.substr(1); | |
build.output = output; | |
case Param("-main", main): | |
build.main = main; | |
case Param("-cp", path): | |
build.classPaths.push(path); | |
case Param("-lib", name): | |
build.libs.push(name); | |
case Param("-D", s): | |
var idx = s.indexOf("="); | |
if (idx == -1) { | |
build.defines[s] = "1"; | |
} else { | |
var def = s.substr(0, idx); | |
var value = s.substr(idx + 1); | |
build.defines[def] = value; | |
} | |
case Simple(type) if (typeRegex.match(type)): | |
build.types.push(type); | |
default: | |
trace("Unsupported " + line); | |
} | |
} | |
return build; | |
} | |
static function readLines(src:String):Array<HxmlLine> { | |
var result = []; | |
var srcLines = ~/[\n\r]+/g.split(src); | |
for (line in srcLines) { | |
line = line.trim(); | |
if (line.length == 0) | |
continue; | |
switch (line.fastCodeAt(0)) { | |
case "#".code: | |
result.push(Comment(line.substr(1).trim())); | |
case "-".code: | |
var idx = line.indexOf(" "); | |
if (idx != -1) | |
result.push(Param(line.substr(0, idx), line.substr(idx).trim())); | |
else | |
result.push(Simple(line)); | |
default: | |
result.push(Simple(line)); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment