added simple interface to the parser
[swftools.git] / lib / as3 / compiler.c
1 /* compiler.h
2
3    Compiler for parsing Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008/2009 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include "tokenizer.h"
25 #include "files.h"
26 #include "parser.h"
27 #include "parser.tab.h"
28 #include "compiler.h"
29 #include <errno.h>
30
31 /* flex/bison definitions */
32 extern void avm2_set_in (FILE *  in_str );
33 extern int avm2_parse();
34 extern int avm2_lex_destroy();
35
36 void as3_setverbosity(int level)
37 {
38     as3_verbosity=level;
39 }
40 void as3_add_include_dir(char*dir)
41 {
42     add_include_dir(dir);
43 }
44
45 static char registry_initialized = 0;
46 static char parser_initialized = 0;
47
48 void as3_parse_file(char*filename) 
49 {
50     if(!registry_initialized) {
51         registry_initialized = 1;
52         registry_init();
53     }
54     if(!parser_initialized) {
55         parser_initialized = 1;
56         initialize_parser();
57     }
58
59     char*fullfilename = enter_file(filename, 0);
60     FILE*fi = fopen(fullfilename, "rb");
61     if(!fi) {
62         syntaxerror("Couldn't find file %s: %s", fullfilename, strerror(errno));
63     }
64     avm2_set_in(fi);
65     initialize_file(filename);
66     avm2_parse();
67     avm2_lex_destroy();
68     fclose(fi);
69     finish_file();
70 }
71
72 static void*as3code = 0;
73 void* as3_getcode()
74 {
75     if(parser_initialized) {
76         parser_initialized = 0;
77         as3code = finish_parser();
78     }
79     return as3code;
80 }
81 char* as3_getglobalclass()
82 {
83     return as3_globalclass;
84 }
85
86 void as3_destroy() 
87 {
88     if(parser_initialized) {
89         parser_initialized = 0;
90         swf_FreeABC(finish_parser());
91     }
92     if(as3_globalclass) {
93         free(as3_globalclass);as3_globalclass=0;
94     }
95 }
96