initial commit
[gaemu.git] / checker.d
blob428ae58b7f1b429cfcacb88a784ca3aa1e755d9a
1 module checker is aliced;
3 import gmlparser;
6 void loadScript (string name, bool warnings=false) {
7 import std.algorithm : startsWith;
8 import std.file : readText;
9 import std.path : baseName, extension;
10 import std.stdio;
11 import std.string : replace;
13 bool asXml = false;
14 auto s = readText(name);
15 s = s.replace("\r\n", "\n").replace("\r", "\n");
16 if (s.startsWith("<?xml")) {
17 import std.string : indexOf;
18 auto pos = s.indexOf(`<argument kind="STRING">`);
19 if (pos < 0) assert(0, "wtf?!");
20 s = s[pos+24..$];
21 pos = s.indexOf(`</argument>`);
22 if (pos < 0) assert(0, "wtfx?!");
23 s = s[0..pos];
24 asXml = true;
27 auto lex = new Lexer(s, name);
28 lex.xmlMode = asXml;
29 auto parser = new Parser(lex);
30 parser.strict = false;
31 parser.warnings = warnings;
32 bool asGmx = (name.extension == ".gmx");
33 try {
34 if (asGmx) {
35 while (lex.isKw(Keyword.Function)) {
36 auto loc = lex.loc;
37 lex.expect(Keyword.Function);
38 string fname = lex.expectId;
39 auto fnode = new NodeFunc(fname, loc);
40 Node st;
41 if (lex.isKw(Keyword.LCurly)) {
42 st = parser.parseCodeBlock();
43 } else {
44 st = parser.parseStatement();
46 if (auto b = cast(NodeBlock)st) {
47 fnode.ebody = b;
48 } else {
49 auto blk = new NodeBlock(st.loc);
50 blk.addStatement(st);
51 fnode.ebody = blk;
53 //if (exec.hasFunction(fnode.name)) throw new Exception("duplicate script '"~fnode.name~"' (from file '"~name~"')");
54 exec[fnode.name] = fnode;
56 } else {
57 string scname = name.baseName(".gml");
58 auto n = parser.parseFunctionBody(new NodeFunc(scname, lex.loc));
59 //writeln(n.toString);
60 //if (exec.hasFunction(n.name)) throw new Exception("duplicate script '"~n.name~"'");
61 exec[n.name] = n;
63 if (!lex.empty) throw new Exception("script '"~name~"' has some extra code");
64 return;
65 } catch (ErrorAt e) {
66 import std.stdio;
67 writeln("ERROR at ", e.loc, ": ", e.msg);
68 writeln(typeid(e).name, "@", e.file, "(", e.line, "): ", e.msg);
69 assert(0);
70 } catch (Exception e) {
71 import std.stdio;
72 writeln(typeid(e).name, "@", e.file, "(", e.line, "): ", e.msg);
73 assert(0);
78 NodeFunc parseScript (string code, string scname, bool strict=true) {
79 auto lex = new Lexer(code, scname);
80 lex.xmlMode = false;
81 auto parser = new Parser(lex, strict);
82 //parser.warnings = true;
83 try {
84 return parser.parseFunctionBody(new NodeFunc(scname, lex.loc));
85 } catch (ErrorAt e) {
86 import std.stdio;
87 writeln("ERROR at ", e.loc, ": ", e.msg);
88 writeln(typeid(e).name, "@", e.file, "(", e.line, "): ", e.msg);
89 writeln(code);
90 throw e;
91 } catch (Exception e) {
92 import std.stdio;
93 writeln(typeid(e).name, "@", e.file, "(", e.line, "): ", e.msg);
94 throw e;
96 assert(0);