2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
4 * Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
18 #include "parser_private.hxx"
19 #include "scilabexception.hxx"
23 #include "charEncoding.h"
24 #include "sci_malloc.h"
29 #include "sci_tmpdir.h"
31 #include "localization.h"
32 #include "os_string.h"
41 extern int yylex_destroy();
43 void Parser::cleanup()
48 void Parser::parseFile(const std::wstring& fileName, const std::wstring& progName)
50 // Calling Parse state machine in C with global values
51 // Must be locked to avoid concurrent access
53 if (getParseTrace() == true)
55 ParserSingleInstance::enableParseTrace();
59 ParserSingleInstance::disableParseTrace();
61 ParserSingleInstance::parseFile(fileName, progName);
62 this->setExitStatus(ParserSingleInstance::getExitStatus());
63 this->setControlStatus(ParserSingleInstance::getControlStatus());
64 if (getExitStatus() == Parser::Succeded)
66 this->setTree(ParserSingleInstance::getTree());
70 this->setErrorMessage(ParserSingleInstance::getErrorMessage());
76 /** \brief parse the given file name */
77 void ParserSingleInstance::parseFile(const std::wstring& fileName, const std::wstring& progName)
79 yylloc.first_line = yylloc.last_line = 1;
80 yylloc.first_column = yylloc.last_column = 1;
82 _wfopen_s(&yyin, fileName.c_str(), L"r");
84 char* pstTemp = wide_string_to_UTF8(fileName.c_str());
85 yyin = fopen(pstTemp, "r");
91 wchar_t szError[bsiz];
92 os_swprintf(szError, bsiz, _W("%ls: Cannot open file %ls.\n").c_str(), L"parser", fileName.c_str());
93 throw ast::ScilabError(szError, 999, *new Location());
97 ParserSingleInstance::disableStrictMode();
98 // Parser::getInstance()->enableStrictMode();
99 ParserSingleInstance::setFileName(fileName);
100 ParserSingleInstance::setProgName(progName);
102 ParserSingleInstance::setExitStatus(Parser::Succeded);
103 ParserSingleInstance::resetControlStatus();
104 ParserSingleInstance::resetErrorMessage();
109 void Parser::parse(char *command)
111 // Calling Parse state machine in C with global values
112 // Must be locked to avoid concurrent access
114 if (getParseTrace() == true)
116 ParserSingleInstance::enableParseTrace();
120 ParserSingleInstance::disableParseTrace();
123 ParserSingleInstance::parse(command);
124 this->setExitStatus(ParserSingleInstance::getExitStatus());
125 this->setControlStatus(ParserSingleInstance::getControlStatus());
126 if (getExitStatus() == Parser::Succeded)
128 this->setTree(ParserSingleInstance::getTree());
132 this->setErrorMessage(ParserSingleInstance::getErrorMessage());
135 if (getControlStatus() == AllControlClosed && get_last_token() != YYEOF)
137 //set parser last token to EOF
144 void Parser::parse(wchar_t *command)
146 char* pstCommand = wide_string_to_UTF8(command);
151 /** \brief parse the given file command */
152 void ParserSingleInstance::parse(char *command)
154 size_t len = strlen(command);
156 yylloc.first_line = yylloc.last_line = 1;
157 yylloc.first_column = yylloc.last_column = 1;
159 char szFile[MAX_PATH];
160 char* pstTmpDIr = getTMPDIR();
161 os_sprintf(szFile, "%s\\%s", pstTmpDIr, "command.temp");
168 fopen_s(&yyin, szFile, "w");
169 fwrite(command, sizeof(char), len, yyin);
171 fopen_s(&yyin, szFile, "r");
175 char szFile[PATH_MAX];
176 char* pstTmpDIr = "/tmp";
177 sprintf(szFile, "%s/%s", getTMPDIR(), "command.temp");
183 yyin = fopen(szFile, "w");
184 fwrite(command, 1, len, yyin);
186 yyin = fopen(szFile, "r");
192 yyin = fmemopen(command, len, "r");
196 ParserSingleInstance::disableStrictMode();
197 ParserSingleInstance::setFileName(L"prompt");
198 ParserSingleInstance::setExitStatus(Parser::Succeded);
199 ParserSingleInstance::resetControlStatus();
200 ParserSingleInstance::resetErrorMessage();
210 //reopen a file to prevents max file opened.
211 fopen_s(&fileLocker, szFile, "w");
214 fileLocker = fopen(szFile, "w");
218 /** \brief put the asked line in codeLine */
219 char *ParserSingleInstance::getCodeLine(int line, char **codeLine)
225 ** WARNING : *codeLine will be allocated by getline
226 ** so it must be manually freed !
228 for (i = 1 ; i <= line ; ++i)
230 fgets(*codeLine, 4096, yyin);
235 std::wstring& ParserSingleInstance::getErrorMessage(void)
237 return _error_message;
240 void ParserSingleInstance::appendErrorMessage(std::wstring message)
242 _error_message += message;
245 /** \brief enable Bison trace mode */
246 void ParserSingleInstance::enableParseTrace(void)
251 /** \brief disable Bison trace mode */
252 void ParserSingleInstance::disableParseTrace(void)
257 std::wstring ParserSingleInstance::_file_name;
258 std::wstring ParserSingleInstance::_prog_name;
259 std::wstring ParserSingleInstance::_error_message;
260 bool ParserSingleInstance::_strict_mode = false;
261 bool ParserSingleInstance::_stop_on_first_error = false;
262 ast::Exp* ParserSingleInstance::_the_program = NULL;
263 Parser::ParserStatus ParserSingleInstance::_exit_status = Parser::Succeded;
264 std::list<Parser::ControlStatus> ParserSingleInstance::_control_status;
265 FILE* ParserSingleInstance::fileLocker = NULL;