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_swprintf.h"
42 void Parser::parseFile(const std::wstring& fileName, const std::wstring& progName)
44 // Calling Parse state machine in C with global values
45 // Must be locked to avoid concurrent access
47 if (getParseTrace() == true)
49 ParserSingleInstance::enableParseTrace();
53 ParserSingleInstance::disableParseTrace();
55 ParserSingleInstance::parseFile(fileName, progName);
56 this->setExitStatus(ParserSingleInstance::getExitStatus());
57 this->setControlStatus(ParserSingleInstance::getControlStatus());
58 if (getExitStatus() == Parser::Succeded)
60 this->setTree(ParserSingleInstance::getTree());
64 this->setErrorMessage(ParserSingleInstance::getErrorMessage());
70 /** \brief parse the given file name */
71 void ParserSingleInstance::parseFile(const std::wstring& fileName, const std::wstring& progName)
73 yylloc.first_line = yylloc.last_line = 1;
74 yylloc.first_column = yylloc.last_column = 1;
76 _wfopen_s(&yyin, fileName.c_str(), L"r");
78 char* pstTemp = wide_string_to_UTF8(fileName.c_str());
79 yyin = fopen(pstTemp, "r");
85 wchar_t szError[bsiz];
86 os_swprintf(szError, bsiz, _W("%ls: Cannot open file %ls.\n").c_str(), L"parser", fileName.c_str());
87 throw ast::ScilabError(szError, 999, *new Location());
91 ParserSingleInstance::disableStrictMode();
92 // Parser::getInstance()->enableStrictMode();
93 ParserSingleInstance::setFileName(fileName);
94 ParserSingleInstance::setProgName(progName);
96 ParserSingleInstance::setExitStatus(Parser::Succeded);
97 ParserSingleInstance::resetControlStatus();
98 ParserSingleInstance::resetErrorMessage();
103 void Parser::parse(wchar_t *command)
105 // Calling Parse state machine in C with global values
106 // Must be locked to avoid concurrent access
108 if (getParseTrace() == true)
110 ParserSingleInstance::enableParseTrace();
114 ParserSingleInstance::disableParseTrace();
117 char* pstCommand = wide_string_to_UTF8(command);
118 ParserSingleInstance::parse(pstCommand);
119 this->setExitStatus(ParserSingleInstance::getExitStatus());
120 this->setControlStatus(ParserSingleInstance::getControlStatus());
121 if (getExitStatus() == Parser::Succeded)
123 this->setTree(ParserSingleInstance::getTree());
127 this->setErrorMessage(ParserSingleInstance::getErrorMessage());
130 if (getControlStatus() == AllControlClosed && get_last_token() != YYEOF)
132 //set parser last token to EOF
140 /** \brief parse the given file command */
141 void ParserSingleInstance::parse(char *command)
143 size_t len = strlen(command);
145 yylloc.first_line = yylloc.last_line = 1;
146 yylloc.first_column = yylloc.last_column = 1;
148 char szFile[MAX_PATH];
149 char* pstTmpDIr = getTMPDIR();
150 sprintf(szFile, "%s\\%s", pstTmpDIr, "command.temp");
157 fopen_s(&yyin, szFile, "w");
158 fwrite(command, sizeof(char), len, yyin);
160 fopen_s(&yyin, szFile, "r");
164 char szFile[PATH_MAX];
165 char* pstTmpDIr = "/tmp";
166 sprintf(szFile, "%s/%s", getTMPDIR(), "command.temp");
172 yyin = fopen(szFile, "w");
173 fwrite(command, 1, len, yyin);
175 yyin = fopen(szFile, "r");
181 yyin = fmemopen(command, len, "r");
185 ParserSingleInstance::disableStrictMode();
186 ParserSingleInstance::setFileName(L"prompt");
187 ParserSingleInstance::setExitStatus(Parser::Succeded);
188 ParserSingleInstance::resetControlStatus();
189 ParserSingleInstance::resetErrorMessage();
199 //reopen a file to prevents max file opened.
200 fopen_s(&fileLocker, szFile, "w");
203 fileLocker = fopen(szFile, "w");
207 /** \brief put the asked line in codeLine */
208 char *ParserSingleInstance::getCodeLine(int line, char **codeLine)
214 ** WARNING : *codeLine will be allocated by getline
215 ** so it must be manually freed !
217 for (i = 1 ; i <= line ; ++i)
219 fgets(*codeLine, 4096, yyin);
224 std::wstring& ParserSingleInstance::getErrorMessage(void)
226 return _error_message;
229 void ParserSingleInstance::appendErrorMessage(std::wstring message)
231 _error_message += message;
234 /** \brief enable Bison trace mode */
235 void ParserSingleInstance::enableParseTrace(void)
240 /** \brief disable Bison trace mode */
241 void ParserSingleInstance::disableParseTrace(void)
246 std::wstring ParserSingleInstance::_file_name;
247 std::wstring ParserSingleInstance::_prog_name;
248 std::wstring ParserSingleInstance::_error_message;
249 bool ParserSingleInstance::_strict_mode = false;
250 bool ParserSingleInstance::_stop_on_first_error = false;
251 ast::Exp* ParserSingleInstance::_the_program = NULL;
252 Parser::ParserStatus ParserSingleInstance::_exit_status = Parser::Succeded;
253 std::list<Parser::ControlStatus> ParserSingleInstance::_control_status;
254 FILE* ParserSingleInstance::fileLocker = NULL;