1 /*
2 GUIDO Library
3 Copyright (C) 2008 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #ifdef WIN32
25 # pragma warning (disable : 4786 4244 4018 4065)
26 # pragma warning (disable : 4996)
27 # pragma warning (disable : 4102)
28 #endif
29
30 //#define GLDEBUG
31
32 #ifdef GLDEBUG
33 # define YYDEBUG 1
34 #endif
35 #include "glangparse.cpp"
36
37 #include <iostream>
38 #include "glparser.h"
39
40 #define yyin glangin
41 #define yyrestart glangrestart
42 #define yy_delete_buffer glang_delete_buffer
43 #define yy_scan_string glang_scan_string
44
45 #ifdef GLDEBUG
46 extern int yydebug;
47 # define pdebug yydebug = 1
48 #else
49 # define pdebug
50 #endif
51
52 guidolang::glangreader * gGLReader;
53
54 namespace guidolang
55 {
56
parse(FILE * fd)57 int glparser::parse (FILE *fd)
58 {
59 if (!fd) {
60 gGLReader->error("Invalid file descriptor", 0 );
61 return(-1);
62 }
63 yyin = fd;
64 pdebug;
65
66 glanglineno = 0;
67 int res = yyparse();
68 yyrestart(yyin);
69 BEGIN(INITIAL);
70 return res;
71 }
72
parse(const char * filename)73 int glparser::parse (const char *filename)
74 {
75 int res;
76 if( !filename ) return -1; // parse error
77 FILE * fd = fopen(filename,"r");
78 if (fd == NULL){
79 gGLReader->error("Cannot not open file", 0 );
80 return(-1);
81 }
82 pdebug;
83 res = parse(fd);
84 fclose(fd);
85 return res;
86 }
87
readstring(const char * buffer,glangreader * r)88 bool glparser::readstring (const char * buffer, glangreader * r)
89 {
90 gGLReader = r;
91 if (!*buffer) return false; // error for empty buffers
92
93 YY_BUFFER_STATE b;
94 b = yy_scan_string (buffer); // Copy string into new buffer and Switch buffers
95 pdebug;
96 glanglineno = 1;
97 int ret = yyparse(); // parse the string
98 yy_delete_buffer(b); // delete the new buffer
99 BEGIN(INITIAL); // and return to INITIAL state
100 return ret==0;
101 }
102
readfile(FILE * fd,glangreader * r)103 bool glparser::readfile (FILE* fd, glangreader * r)
104 {
105 gGLReader = r;
106 return parse (fd) == 0;
107 }
108
readfile(const char * file,glangreader * r)109 bool glparser::readfile (const char * file, glangreader * r)
110 {
111 gGLReader = r;
112 return parse (file) == 0;
113 }
114
115 } // namespace
116
117 #ifdef MAIN
118
119 using namespace std;
120 using namespace guidolang;
121
122 #define catOp(str,a,b,c) *str+="("; *str+=*a; *str+=b; *str+=*c; *str+=")"
123
124 class testreader : public glangreader
125 {
126 public:
newIDExpr(const char * id,SGLExpr * e)127 virtual void newIDExpr (const char *id, SGLExpr* e)
128 { cout << "new identified expression: " << id << " = " << *e << endl; }
129
newNamedExpr(const char * name)130 virtual SGLExpr* newNamedExpr (const char *name)
131 { cout << "newNamedExpr: " << name << endl; return new SGLExpr(name); }
132
newGroupExpr(SGLExpr * e)133 virtual SGLExpr* newGroupExpr (SGLExpr* e)
134 { cout << "newGroupedExpr: " << *e << endl; SGLExpr* str = new string; *str+="<"; *str+=*e; *str+=">";return str; }
135
newScoreExpr(const char * gmn)136 virtual SGLExpr* newScoreExpr (const char *gmn)
137 { return new SGLExpr(gmn); }
138
newBinaryExpr(const char * name,SGLExpr * e1,SGLExpr * e2)139 virtual SGLExpr* newBinaryExpr (const char * name, SGLExpr* e1, SGLExpr* e2)
140 { SGLExpr* str = new string(); catOp(str, e1, name, e2); cout << "newBinaryExpr: " << *str << endl; return str; }
141
newAbstractExpr(const char * name,SGLExpr * e1,SGLExpr * e2)142 virtual SGLExpr* newAbstractExpr (const char * name, SGLExpr* e1, SGLExpr* e2)
143 { SGLExpr* str = new string(); catOp(str, e1, name, e2); cout << "newAbstractExpr: " << *str << endl; return str; }
144
error(const char * msg,int lineno)145 int error (const char* msg, int lineno)
146 { cerr << msg << " on line " << lineno << endl; return 0; }
147 };
148
149
main(int argc,char * argv[])150 int main (int argc, char * argv[])
151 {
152 if (argc > 1) {
153 testreader r;
154 glparser glp;
155 return glp.readfile (argv[1], &r) ? 0 : 1;
156 }
157 return 0;
158 }
159 #endif
160
161