1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_parse.cpp
27 // desc: chuck parser interface
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
33 #include "chuck_parse.h"
34 #include "chuck_errmsg.h"
40 static char g_filename
[1024] = "";
48 //-----------------------------------------------------------------------------
49 // name: open_cat_ck()
51 //-----------------------------------------------------------------------------
52 FILE * open_cat_ck( c_str fname
)
55 if( !(fd
= fopen( fname
, "rb" )) )
56 if( !strstr( fname
, ".ck" ) && !strstr( fname
, ".CK" ) )
58 strcat( fname
, ".ck" );
59 fd
= fopen( fname
, "rb" );
67 //-----------------------------------------------------------------------------
68 // name: chuck_parse()
70 //-----------------------------------------------------------------------------
71 t_CKBOOL
chuck_parse( c_constr fname
, FILE * fd
, c_constr code
)
79 fprintf( stderr
, "[chuck](via parser): (internal) both fd and code specified!\n" );
93 fwrite( code
, sizeof(char), strlen(code
), fd
);
97 // use code from memory buffer if its available
101 strcpy( g_filename, fname );
103 if( EM_reset( g_filename, NULL ) == FALSE ) goto cleanup;
105 // TODO: clean g_program
111 // load string (yy_scan_string will copy the C string)
112 YY_BUFFER_STATE ybs = yy_scan_string( code );
113 if( !ybs ) goto cleanup;
116 if( !( yyparse() == 0 ) ) goto cleanup;
118 // delete the lexer buffer
119 yy_delete_buffer( ybs );
125 strcpy( g_filename
, fname
);
129 fd
= open_cat_ck( g_filename
);
130 if( !fd
) strcpy( g_filename
, fname
);
135 if( EM_reset( g_filename
, fd
) == FALSE
) goto cleanup
;
138 // TODO: if( yyin ) { fclose( yyin ); yyin = NULL; }
141 if( !fd
) { fd
= fopen( g_filename
, "r" ); if( fd
) clo
= TRUE
; }
143 if( !fd
) { EM_error2( 0, "no such file or directory" ); goto cleanup
; }
145 else fseek( fd
, 0, SEEK_SET
);
151 if( yyin
== NULL
) goto cleanup
;
153 // TODO: clean g_program
157 if( !(yyparse( ) == 0) ) goto cleanup
;
165 if( clo
) fclose( fd
);
173 //------------------------------------------------------------------------------
174 // name: reset_parse()
176 //------------------------------------------------------------------------------
180 EM_change_file( NULL
);
186 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
190 t_CKBOOL
SyntaxQuery::parseLine( const std::string
& line
, SyntaxTokenList
& tokens
)
192 // clear the token list