diff options
author | Miodrag Milanovic <mmicko@gmail.com> | 2018-06-28 13:16:53 +0200 |
---|---|---|
committer | Miodrag Milanovic <mmicko@gmail.com> | 2018-06-28 17:57:26 +0200 |
commit | c63274342f1aaa3d1087c0df5ffdaccfd0afcd56 (patch) | |
tree | b04628f6c8c6768e2d5771737f3f13687cd0ed29 /3rdparty/python-console/ParseHelper.cpp | |
parent | 66670831b89d934ef00c47c0527137f6ec38a0b0 (diff) | |
download | nextpnr-c63274342f1aaa3d1087c0df5ffdaccfd0afcd56.tar.gz nextpnr-c63274342f1aaa3d1087c0df5ffdaccfd0afcd56.tar.bz2 nextpnr-c63274342f1aaa3d1087c0df5ffdaccfd0afcd56.zip |
initial import of python-console
Diffstat (limited to '3rdparty/python-console/ParseHelper.cpp')
-rw-r--r-- | 3rdparty/python-console/ParseHelper.cpp | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/3rdparty/python-console/ParseHelper.cpp b/3rdparty/python-console/ParseHelper.cpp new file mode 100644 index 00000000..8944a84e --- /dev/null +++ b/3rdparty/python-console/ParseHelper.cpp @@ -0,0 +1,225 @@ +/** +Copyright (c) 2014 Alex Tsui + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +#include "ParseHelper.h" +#include <iostream> +#include <fstream> +#include <string> +#include <sstream> +#include <vector> +#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( ) +{ } + +ParseHelper::Indent:: +Indent( const std::string& indent ): + Token( indent ) +{ } + +ParseHelper::ParseState:: +ParseState( ParseHelper& parent_ ): parent( parent_ ) +{ } + +ParseHelper::ParseState:: +~ParseState( ) +{ } + +bool ParseHelper::PeekIndent( const std::string& str, Indent* indent ) +{ + if ( !str.size() || ! isspace(str[0]) ) + return false; + + int nonwhitespaceIndex = -1; + for (int i = 0; i < str.size(); ++i) + { + if (!isspace(str[i])) + { + nonwhitespaceIndex = i; + break; + } + } + if (nonwhitespaceIndex == -1) + { + return false; + } + std::string indentToken = str.substr(0, nonwhitespaceIndex); + indent->Token = indentToken; + return true; +} + +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; + while (stateStack.size()) + { + top = commandBuffer.back(); + commandBuffer.pop_back(); + blockStatePtr = stateStack.back(); + if (blockStatePtr->process(top)) + return; + } + + if ( ! commandBuffer.size() ) + return; + + // standard state + top = commandBuffer.back(); + if ( !top.size() ) + { + reset( ); + broadcast( std::string() ); + return; + } +#ifndef NDEBUG + std::cout << "now processing: (" << top << ")\n"; +#endif + + { // check for unexpected indent + Indent ind; + bool isIndented = PeekIndent( top, &ind ); + if ( isIndented && + ! isInContinuation( ) ) + { + reset( ); + ParseMessage msg( 1, "IndentationError: unexpected indent"); + broadcast( msg ); + return; + } + } + + // enter indented block state + if ( top[top.size()-1] == ':' ) + { + boost::shared_ptr<ParseState> parseState( + new BlockParseState( *this ) ); + stateStack.push_back( parseState ); + return; + } + + if ( top[top.size()-1] == '\\' ) + { + boost::shared_ptr<ParseState> parseState( + new ContinuationParseState( *this ) ); + stateStack.push_back( parseState ); + return; + } + + if (BracketParseState::HasOpenBrackets( top )) + { + // FIXME: Every parse state should have its own local buffer + commandBuffer.pop_back( ); + boost::shared_ptr<ParseState> parseState( + new BracketParseState( *this, top ) ); + stateStack.push_back( parseState ); + return; + } + + // handle single-line statement + flush( ); +} + +bool ParseHelper::buffered( ) const +{ + return commandBuffer.size( ) || stateStack.size( ); +} + +void ParseHelper::flush( ) +{ + std::stringstream ss; + for (int i = 0; i < commandBuffer.size(); ++i ) + { + ss << commandBuffer[i] << "\n"; + } + commandBuffer.clear(); + + broadcast( ss.str() ); + // TODO: feed string to interpreter +} + +void ParseHelper::reset( ) +{ +// inContinuation = false; + stateStack.clear( ); + commandBuffer.clear( ); +} + +bool ParseHelper::isInContinuation( ) const +{ + return (stateStack.size() + && (boost::dynamic_pointer_cast<ContinuationParseState>( + stateStack.back()))); +} + +void ParseHelper::subscribe( ParseListener* listener ) +{ + listeners.push_back( listener ); +} + +void ParseHelper::unsubscribeAll( ) +{ + listeners.clear( ); +} + +void ParseHelper::broadcast( const ParseMessage& msg ) +{ + // broadcast signal + for (int i = 0; i < listeners.size(); ++i) + { + if (listeners[i]) + { + listeners[i]->parseEvent(msg); + } + } +} |