move to c++11 remove console writes
This commit is contained in:
parent
9347c8742d
commit
9cf23d1010
33
3rdparty/python-console/Interpreter.cpp
vendored
33
3rdparty/python-console/Interpreter.cpp
vendored
@ -1,7 +1,7 @@
|
||||
#include "Interpreter.h"
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <boost/format.hpp>
|
||||
#include <memory>
|
||||
|
||||
PyThreadState* Interpreter::MainThreadState = NULL;
|
||||
|
||||
@ -27,9 +27,6 @@ Interpreter::Interpreter( )
|
||||
|
||||
Interpreter::~Interpreter( )
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "delete interpreter\n";
|
||||
#endif
|
||||
PyEval_AcquireThread( m_threadState );
|
||||
Py_EndInterpreter( m_threadState );
|
||||
PyEval_ReleaseLock( );
|
||||
@ -60,6 +57,15 @@ Interpreter::test( )
|
||||
PyEval_ReleaseThread( m_threadState );
|
||||
}
|
||||
|
||||
template<typename ... Args>
|
||||
std::string string_format( const std::string& format, Args ... args )
|
||||
{
|
||||
size_t size = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
|
||||
std::unique_ptr<char[]> buf( new char[ size ] );
|
||||
std::snprintf( buf.get(), size, format.c_str(), args ... );
|
||||
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
|
||||
}
|
||||
|
||||
std::string
|
||||
Interpreter::interpret( const std::string& command, int* errorCode )
|
||||
{
|
||||
@ -69,15 +75,9 @@ Interpreter::interpret( const std::string& command, int* errorCode )
|
||||
PyObject* py_result;
|
||||
PyObject* dum;
|
||||
std::string res;
|
||||
#ifndef NDEBUG
|
||||
std::cout << "interpreting (" << command << ")\n";
|
||||
#endif
|
||||
py_result = Py_CompileString(command.c_str(), "<stdin>", Py_single_input);
|
||||
if ( py_result == 0 )
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "Huh?\n";
|
||||
#endif
|
||||
if ( PyErr_Occurred( ) )
|
||||
{
|
||||
*errorCode = 1;
|
||||
@ -110,13 +110,7 @@ const std::list<std::string>& Interpreter::suggest( const std::string& hint )
|
||||
PyEval_AcquireThread( m_threadState );
|
||||
m_suggestions.clear();
|
||||
int i = 0;
|
||||
std::string command = boost::str(
|
||||
boost::format("sys.completer.complete('%1%', %2%)\n")
|
||||
% hint
|
||||
% i);
|
||||
#ifndef NDEBUG
|
||||
std::cout << command << "\n";
|
||||
#endif
|
||||
std::string command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i);
|
||||
std::string res;
|
||||
do
|
||||
{
|
||||
@ -129,10 +123,7 @@ const std::list<std::string>& Interpreter::suggest( const std::string& hint )
|
||||
res = GetResultString( m_threadState );
|
||||
GetResultString( m_threadState ) = "";
|
||||
++i;
|
||||
command = boost::str(
|
||||
boost::format("sys.completer.complete('%1%', %2%)\n")
|
||||
% hint
|
||||
% i);
|
||||
command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i);
|
||||
if (res.size())
|
||||
{
|
||||
// throw away the newline
|
||||
|
@ -20,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
#include "ParseHelper.h"
|
||||
#include <iostream>
|
||||
|
||||
ParseHelper::BlockParseState::
|
||||
BlockParseState( ParseHelper& parent ):
|
||||
@ -47,10 +46,6 @@ process(const std::string& str)
|
||||
bool isIndented = PeekIndent( str, &ind );
|
||||
if ( isIndented )
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "current line indent: ";
|
||||
print( ind );
|
||||
#endif
|
||||
// check if indent matches
|
||||
if ( ind.Token != indent.Token )
|
||||
{
|
||||
@ -61,17 +56,14 @@ process(const std::string& str)
|
||||
parent.stateStack.pop_back( );
|
||||
if ( !parent.stateStack.size( ) )
|
||||
break;
|
||||
boost::shared_ptr<BlockParseState> parseState =
|
||||
boost::dynamic_pointer_cast<BlockParseState>(
|
||||
std::shared_ptr<BlockParseState> parseState =
|
||||
std::dynamic_pointer_cast<BlockParseState>(
|
||||
parent.stateStack.back( ));
|
||||
found = ( ind.Token == parseState->indent.Token );
|
||||
}
|
||||
|
||||
if ( ! found )
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "indent mismatch\n";
|
||||
#endif
|
||||
parent.reset( );
|
||||
ParseMessage msg( 1, "IndentationError: unexpected indent");
|
||||
parent.broadcast( msg );
|
||||
@ -85,11 +77,11 @@ process(const std::string& str)
|
||||
if ( str[str.size()-1] == ':' )
|
||||
{
|
||||
parent.commandBuffer.push_back( str );
|
||||
//parent.inBlock = (boost::dynamic_pointer_cast<BlockParseState>(
|
||||
//parent.inBlock = (std::dynamic_pointer_cast<BlockParseState>(
|
||||
// parent.stateStack.back()));
|
||||
|
||||
//expectingIndent = true;
|
||||
boost::shared_ptr<ParseState> parseState(
|
||||
std::shared_ptr<ParseState> parseState(
|
||||
new BlockParseState( parent ) );
|
||||
parent.stateStack.push_back( parseState );
|
||||
return true;
|
||||
@ -98,7 +90,7 @@ process(const std::string& str)
|
||||
if ( str[str.size()-1] == '\\' )
|
||||
{
|
||||
parent.commandBuffer.push_back( str );
|
||||
boost::shared_ptr<ParseState> parseState(
|
||||
std::shared_ptr<ParseState> parseState(
|
||||
new ContinuationParseState( parent ) );
|
||||
parent.stateStack.push_back( parseState );
|
||||
return true;
|
||||
@ -107,7 +99,7 @@ process(const std::string& str)
|
||||
if (BracketParseState::HasOpenBrackets( str ))
|
||||
{
|
||||
// FIXME: Every parse state should have its own local buffer
|
||||
boost::shared_ptr<ParseState> parseState(
|
||||
std::shared_ptr<ParseState> parseState(
|
||||
new BracketParseState( parent, str ) );
|
||||
parent.stateStack.push_back( parseState );
|
||||
return true;
|
||||
@ -121,18 +113,12 @@ process(const std::string& str)
|
||||
if ( str.size() )
|
||||
{
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "Expected indented block\n";
|
||||
#endif
|
||||
parent.reset( );
|
||||
ParseMessage msg( 1, "IndentationError: expected an indented block" );
|
||||
parent.broadcast( msg );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
std::cout << "Leaving block\n";
|
||||
#endif
|
||||
parent.stateStack.pop_back();
|
||||
parent.flush( );
|
||||
parent.reset( );
|
||||
@ -146,8 +132,6 @@ initializeIndent(const std::string& str)
|
||||
bool expectingIndent = (indent.Token == "");
|
||||
if ( !expectingIndent )
|
||||
{
|
||||
std::cout << "already initialized indent: ";
|
||||
print( indent );
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -155,9 +139,6 @@ initializeIndent(const std::string& str)
|
||||
bool isIndented = parent.PeekIndent( str, &ind );
|
||||
if ( !isIndented )
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "Expected indented block\n";
|
||||
#endif
|
||||
parent.reset( );
|
||||
ParseMessage msg( 1, "IndentationError: expected an indented block" );
|
||||
parent.broadcast( msg );
|
||||
@ -167,9 +148,5 @@ initializeIndent(const std::string& str)
|
||||
//parent.currentIndent = ind;
|
||||
//parent.indentStack.push_back( parent.currentIndent );
|
||||
//parent.expectingIndent = false;
|
||||
#ifndef NDEBUG
|
||||
std::cout << "initializing indent: ";
|
||||
print( ind );
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ THE SOFTWARE.
|
||||
#include "ParseHelper.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
const std::string ParseHelper::BracketParseState::OpeningBrackets = "[({";
|
||||
const std::string ParseHelper::BracketParseState::ClosingBrackets = "])}";
|
||||
|
||||
@ -64,23 +64,17 @@ ParseHelper::BracketParseState::BracketParseState( ParseHelper& parent, const st
|
||||
ParseState( parent )
|
||||
{
|
||||
bool hasOpenBrackets = LoadBrackets( firstLine, &brackets );
|
||||
assert( hasOpenBrackets );
|
||||
//assert( hasOpenBrackets );
|
||||
m_buffer.push_back( firstLine );
|
||||
}
|
||||
|
||||
bool ParseHelper::BracketParseState::process(const std::string& str)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "(BracketParseState) processing " << str << "\n";
|
||||
#endif
|
||||
// update brackets stack
|
||||
for (int i = 0; i < str.size(); ++i)
|
||||
{
|
||||
if (OpeningBrackets.find_first_of(str[i]) != std::string::npos)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "push " << str[i] << "\n";
|
||||
#endif
|
||||
brackets.push_back(str[i]);
|
||||
}
|
||||
else
|
||||
@ -88,9 +82,6 @@ bool ParseHelper::BracketParseState::process(const std::string& str)
|
||||
size_t t = ClosingBrackets.find_first_of(str[i]);
|
||||
if (t != std::string::npos)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "pop " << str[i] << "\n";
|
||||
#endif
|
||||
// reset state if unmatched brackets seen
|
||||
if (t != OpeningBrackets.find_first_of(brackets.back()))
|
||||
{
|
||||
|
37
3rdparty/python-console/ParseHelper.cpp
vendored
37
3rdparty/python-console/ParseHelper.cpp
vendored
@ -28,26 +28,6 @@ THE SOFTWARE.
|
||||
#include <cstdlib>
|
||||
#include "ParseListener.h"
|
||||
|
||||
#ifndef NDEBUG
|
||||
void print(const ParseHelper::Indent& indent)
|
||||
{
|
||||
std::string str = indent.Token;
|
||||
for (int i = 0; i < str.size(); ++i)
|
||||
{
|
||||
switch (str.at(i))
|
||||
{
|
||||
case ' ':
|
||||
str[i] = 's';
|
||||
break;
|
||||
case '\t':
|
||||
str[i] = 't';
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << str << "\n";
|
||||
}
|
||||
#endif
|
||||
|
||||
ParseHelper::Indent::
|
||||
Indent( )
|
||||
{ }
|
||||
@ -93,15 +73,11 @@ ParseHelper::ParseHelper( )
|
||||
|
||||
void ParseHelper::process( const std::string& str )
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "processing: (" << str << ")\n";
|
||||
#endif
|
||||
|
||||
std::string top;
|
||||
commandBuffer.push_back(str);
|
||||
//std::string top = commandBuffer.back();
|
||||
//commandBuffer.pop_back();
|
||||
boost::shared_ptr<ParseState> blockStatePtr;
|
||||
std::shared_ptr<ParseState> blockStatePtr;
|
||||
while (stateStack.size())
|
||||
{
|
||||
top = commandBuffer.back();
|
||||
@ -122,9 +98,6 @@ void ParseHelper::process( const std::string& str )
|
||||
broadcast( std::string() );
|
||||
return;
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
std::cout << "now processing: (" << top << ")\n";
|
||||
#endif
|
||||
|
||||
{ // check for unexpected indent
|
||||
Indent ind;
|
||||
@ -142,7 +115,7 @@ void ParseHelper::process( const std::string& str )
|
||||
// enter indented block state
|
||||
if ( top[top.size()-1] == ':' )
|
||||
{
|
||||
boost::shared_ptr<ParseState> parseState(
|
||||
std::shared_ptr<ParseState> parseState(
|
||||
new BlockParseState( *this ) );
|
||||
stateStack.push_back( parseState );
|
||||
return;
|
||||
@ -150,7 +123,7 @@ void ParseHelper::process( const std::string& str )
|
||||
|
||||
if ( top[top.size()-1] == '\\' )
|
||||
{
|
||||
boost::shared_ptr<ParseState> parseState(
|
||||
std::shared_ptr<ParseState> parseState(
|
||||
new ContinuationParseState( *this ) );
|
||||
stateStack.push_back( parseState );
|
||||
return;
|
||||
@ -160,7 +133,7 @@ void ParseHelper::process( const std::string& str )
|
||||
{
|
||||
// FIXME: Every parse state should have its own local buffer
|
||||
commandBuffer.pop_back( );
|
||||
boost::shared_ptr<ParseState> parseState(
|
||||
std::shared_ptr<ParseState> parseState(
|
||||
new BracketParseState( *this, top ) );
|
||||
stateStack.push_back( parseState );
|
||||
return;
|
||||
@ -198,7 +171,7 @@ void ParseHelper::reset( )
|
||||
bool ParseHelper::isInContinuation( ) const
|
||||
{
|
||||
return (stateStack.size()
|
||||
&& (boost::dynamic_pointer_cast<ContinuationParseState>(
|
||||
&& (std::dynamic_pointer_cast<ContinuationParseState>(
|
||||
stateStack.back())));
|
||||
}
|
||||
|
||||
|
4
3rdparty/python-console/ParseHelper.h
vendored
4
3rdparty/python-console/ParseHelper.h
vendored
@ -25,7 +25,7 @@ THE SOFTWARE.
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <memory>
|
||||
#include "ParseMessage.h"
|
||||
|
||||
class ParseListener;
|
||||
@ -120,7 +120,7 @@ protected:
|
||||
// TODO: Create a ContinuationParseState to handle this
|
||||
bool inContinuation;
|
||||
std::vector< ParseListener* > listeners;
|
||||
std::vector< boost::shared_ptr< ParseState > > stateStack;
|
||||
std::vector< std::shared_ptr< ParseState > > stateStack;
|
||||
std::vector< std::string > commandBuffer;
|
||||
|
||||
public:
|
||||
|
@ -5,7 +5,7 @@ int main( int argc, char *argv[] )
|
||||
{
|
||||
std::string commands[] = {
|
||||
"from time import time,ctime\n",
|
||||
"print 'Today is',ctime(time())\n"
|
||||
"print('Today is',ctime(time()))\n"
|
||||
};
|
||||
Interpreter::Initialize( );
|
||||
Interpreter* interpreter = new Interpreter;
|
||||
|
Loading…
Reference in New Issue
Block a user