Esempio n. 1
0
std::vector<std::pair<Tokens, std::string>> Lexer::lex(std::string input) {

    std::vector<std::string> allKeywords;
    for(auto& i: keywords)
        allKeywords.push_back(i.first);

    std::vector<std::string> splittedText;

    std::stringstream ss(input);
    std::string item;
    const char* delim = " ";

    while (std::getline(ss, item, *delim)) {
        if(item == "")
            continue;

        splittedText.push_back(item);
    }

    std::vector<std::pair<Tokens, std::string>> tokens;

    if(input.find_first_not_of(' ') == std::string::npos) // if only spaces
        tokens.push_back(std::make_pair(t_empty, ""));
    else {
        for(auto& i: splittedText) {
            if(hasToken(i))
                tokens.push_back(std::make_pair(keywords[i], i));

            else if(tokenIsName(i))
                tokens.push_back(std::make_pair(t_name, i));

            else if(tokenIsNumeral(i))
                tokens.push_back(std::make_pair(t_numeral, i));

            else
                tokens.push_back(std::make_pair(t_name_error, i));
        }
    }
    tokens.push_back(std::make_pair(t_end, "\n"));

    return tokens;

}
Esempio n. 2
0
bool Nordic::passToken(byte target){
  bool gotACK = false;
  if (hasToken()){
    byte data[2] = {0,0};
    Packet packet(conversation->getNextSeqNum(target), target, ID, TOKEN, data);

    conversation->update(packet);
    byte taddr[5] = {0,0,0,0,target};

    for (int t=TIMEOUT; t >= 0 && !gotACK; t--){
      Mirf.send(packet.pack());
      while (Mirf.isSending());
      Serial.println("Sent token");
      packet.print(DEC);
      byte ack[sizeof(Packet)];
      for (int i = 5; i >= 0; i--){
	if (Mirf.dataReady()){
	  break;
	}
	delay(10);
      }

      if (Mirf.dataReady()){
	Mirf.getData(ack);
	Packet ackPacket(ack); 

	if (ackPacket.getSequenceNumber() == packet.getSequenceNumber() &&
	    ackPacket.getSourceId() == packet.getTargetId() &&
	    ackPacket.getTargetId() == packet.getSourceId() &&
	    ackPacket.getType() == ACK){
	  Serial.println("Got ACK");
	  gotACK = true;
	  token = false;
	}
      }
    }
  }
  return gotACK;
}
Esempio n. 3
0
Token Scanner::getToken()
{
	/*
		Reads the next available character and dispatches to the
		appropriate FSA for them to parse. 
	*/		


	// do not return MP_COMMENT to the parser
	do 
	{	
		// detect EOF and skip whitespace
		hasToken();	
		char next = peek();	

		// reset the TOKEN and LEXEME variables, FSA will set new values
		_token = MP_NULL;
		_lexeme = "";

		// which FSA to call 
		if (next == '\'') // handle strings, start with ' (single quote)
			handleString();
		else if (isdigit(next))
			handleNumberic();
		else if (isalpha(next) || next=='_' ) // check for identifier
			handleWord();
		else if (next == '{' || next == '}') // handle comments first becuase {} are considered punctation
			handleComment();
		else if (ispunct(next))
			handleSymbol();
		else if (next == EOF) {
			_token = MP_EOF;
			_lexeme = get();
		}
	} while (_token == MP_COMMENT);

	return _token; 
};
Esempio n. 4
0
void
CLArgsPrivate::parse()
{
    {
        QStringList::iterator it = hasToken( QString::fromUtf8("version"), QString::fromUtf8("v") );
        if ( it != args.end() ) {
            QString msg = QObject::tr("%1 version %2 at commit %3 on branch %4 built on %4").arg( QString::fromUtf8(NATRON_APPLICATION_NAME) ).arg( QString::fromUtf8(NATRON_VERSION_STRING) ).arg( QString::fromUtf8(GIT_COMMIT) ).arg( QString::fromUtf8(GIT_BRANCH) ).arg( QString::fromUtf8(__DATE__) );
            std::cout << msg.toStdString() << std::endl;
            error = 1;

            return;
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("help"), QString::fromUtf8("h") );
        if ( it != args.end() ) {
            CLArgs::printUsage( args[0].toStdString() );
            error = 1;

            return;
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("background"), QString::fromUtf8("b") );
        if ( it != args.end() ) {
            isBackground = true;
            args.erase(it);
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("interpreter"), QString::fromUtf8("t") );
        if ( it != args.end() ) {
            isInterpreterMode = true;
            isBackground = true;
            std::cout << QObject::tr("Note: -t argument given, loading in command-line interpreter mode, only Python commands / scripts are accepted").toStdString()
                      << std::endl;
            args.erase(it);
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("render-stats"), QString::fromUtf8("s") );
        if ( it != args.end() ) {
            enableRenderStats = true;
            args.erase(it);
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_PROCESS_PID), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                breakpadProcessPID = it->toLongLong();
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the breakpad process executable file path").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_PROCESS_EXEC), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                breakpadProcessFilePath = *it;
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the breakpad process executable file path").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_CLIENT_FD_ARG), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                breakpadPipeClientID = it->toInt();
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the breakpad pipe client FD").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_PIPE_ARG), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                breakpadPipeFilePath = *it;
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the breakpad pipe path").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8(NATRON_BREAKPAD_COM_PIPE_ARG), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                breakpadComPipeFilePath = *it;
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the breakpad communication pipe path").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("export-docs"), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                exportDocsPath = *it;
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the doc dir path").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("IPCpipe"), QString() );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                ipcPipe = *it;
                args.erase(it);
            } else {
                std::cout << QObject::tr("You must specify the IPC pipe filename").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = hasToken( QString::fromUtf8("onload"), QString::fromUtf8("l") );
        if ( it != args.end() ) {
            ++it;
            if ( it != args.end() ) {
                defaultOnProjectLoadedScript = *it;
#ifdef __NATRON_UNIX__
                defaultOnProjectLoadedScript = AppManager::qt_tildeExpansion(defaultOnProjectLoadedScript);
#endif
                QFileInfo fi(defaultOnProjectLoadedScript);
                if ( !fi.exists() ) {
                    std::cout << QObject::tr("WARNING: --onload %1 ignored because the file does not exist.").arg(defaultOnProjectLoadedScript).toStdString() << std::endl;
                    defaultOnProjectLoadedScript.clear();
                } else {
                    defaultOnProjectLoadedScript = fi.canonicalFilePath();
                }

                args.erase(it);
                if ( !defaultOnProjectLoadedScript.endsWith( QString::fromUtf8(".py") ) ) {
                    std::cout << QObject::tr("The optional on project load script must be a Python script (.py).").toStdString() << std::endl;
                    error = 1;

                    return;
                }
            } else {
                std::cout << QObject::tr("--onload or -l specified, you must enter a script filename afterwards.").toStdString() << std::endl;
                error = 1;

                return;
            }
        }
    }

    {
        QStringList::iterator it = findFileNameWithExtension( QString::fromUtf8(NATRON_PROJECT_FILE_EXT) );
        if ( it == args.end() ) {
            it = findFileNameWithExtension( QString::fromUtf8("py") );
            if ( ( it == args.end() ) && !isInterpreterMode && isBackground ) {
                std::cout << QObject::tr("You must specify the filename of a script or %1 project. (.%2)").arg( QString::fromUtf8(NATRON_APPLICATION_NAME) ).arg( QString::fromUtf8(NATRON_PROJECT_FILE_EXT) ).toStdString() << std::endl;
                error = 1;

                return;
            }
            isPythonScript = true;
        }
        if ( it != args.end() ) {
            filename = *it;
#ifdef __NATRON_UNIX__
            filename = AppManager::qt_tildeExpansion(filename);
#endif
            QFileInfo fi(filename);
            if ( fi.exists() ) {
                filename = fi.canonicalFilePath();
            }
            args.erase(it);
        }
    }

    //Parse frame range
    for (QStringList::iterator it = args.begin(); it != args.end(); ++it) {
        if ( tryParseMultipleFrameRanges(*it, frameRanges) ) {
            args.erase(it);
            rangeSet = true;
            break;
        }
    }

    //Parse python commands
    for (;; ) {
        QStringList::iterator it = hasToken( QString::fromUtf8("cmd"), QString::fromUtf8("c") );
        if ( it == args.end() ) {
            break;
        }

        if (!isBackground) {
            std::cout << QObject::tr("You cannot use the -c option in interactive mode").toStdString() << std::endl;
            error = 1;

            return;
        }

        QStringList::iterator next = it;
        if ( next != args.end() ) {
            ++next;
        }
        if ( next == args.end() ) {
            std::cout << QObject::tr("You must specify a command when using the -c option").toStdString() << std::endl;
            error = 1;

            return;
        }

        pythonCommands.push_back( next->toStdString() );

        ++next;
        args.erase(it, next);
    } // for (;;)

    //Parse writers
    for (;; ) {
        QStringList::iterator it = hasToken( QString::fromUtf8("writer"), QString::fromUtf8("w") );
        if ( it == args.end() ) {
            break;
        }

        if (!isBackground || isInterpreterMode) {
            std::cout << QObject::tr("You cannot use the -w option in interactive or interpreter mode").toStdString() << std::endl;
            error = 1;

            return;
        }

        QStringList::iterator next = it;
        if ( next != args.end() ) {
            ++next;
        }
        if ( next == args.end() ) {
            std::cout << QObject::tr("You must specify the name of a Write node when using the -w option").toStdString() << std::endl;
            error = 1;

            return;
        }


        //Check that the name is conform to a Python acceptable script name
        std::string pythonConform = NATRON_PYTHON_NAMESPACE::makeNameScriptFriendly( next->toStdString() );
        if (next->toStdString() != pythonConform) {
            std::cout << QObject::tr("The name of the Write node specified is not valid: it cannot contain non alpha-numerical "
                                     "characters and must not start with a digit.").toStdString() << std::endl;
            error = 1;

            return;
        }

        CLArgs::WriterArg w;
        w.name = *next;

        QStringList::iterator nextNext = next;
        if ( nextNext != args.end() ) {
            ++nextNext;
        }
        if ( nextNext != args.end() ) {
            //Check for an optional filename
            if ( !nextNext->startsWith( QChar::fromLatin1('-') ) && !nextNext->startsWith( QString::fromUtf8("--") ) ) {
                w.filename = *nextNext;
#ifdef __NATRON_UNIX__
                w.filename = AppManager::qt_tildeExpansion(w.filename);
#endif
            }
        }

        writers.push_back(w);
        if ( nextNext != args.end() ) {
            ++nextNext;
        }
        args.erase(it, nextNext);
    } // for (;;)


    //Parse readers
    for (;; ) {
        QStringList::iterator it = hasToken( QString::fromUtf8("reader"), QString::fromUtf8("i") );
        if ( it == args.end() ) {
            break;
        }

        if (!isBackground || isInterpreterMode) {
            std::cout << QObject::tr("You cannot use the -i option in interactive or interpreter mode").toStdString() << std::endl;
            error = 1;

            return;
        }

        QStringList::iterator next = it;
        if ( next != args.end() ) {
            ++next;
        }
        if ( next == args.end() ) {
            std::cout << QObject::tr("You must specify the name of a Read node when using the -i option").toStdString() << std::endl;
            error = 1;

            return;
        }


        //Check that the name is conform to a Python acceptable script name
        std::string pythonConform = NATRON_PYTHON_NAMESPACE::makeNameScriptFriendly( next->toStdString() );
        if (next->toStdString() != pythonConform) {
            std::cout << QObject::tr("The name of the Read node specified is not valid: it cannot contain non alpha-numerical "
                                     "characters and must not start with a digit.").toStdString() << std::endl;
            error = 1;

            return;
        }

        CLArgs::ReaderArg r;
        r.name = *next;

        QStringList::iterator nextNext = next;
        if ( nextNext != args.end() ) {
            ++nextNext;
        }
        if ( nextNext == args.end() ) {
            std::cout << QObject::tr("You must specify the filename for the following Read node: ").toStdString()  << r.name.toStdString() << std::endl;
            error = 1;

            return;
        }


        //Check for  filename
        if ( !nextNext->startsWith( QChar::fromLatin1('-') ) && !nextNext->startsWith( QString::fromUtf8("--") ) ) {
            r.filename = *nextNext;
#ifdef __NATRON_UNIX__
            r.filename = AppManager::qt_tildeExpansion(r.filename);
#endif
        } else {
            std::cout << QObject::tr("You must specify the filename for the following Read node: ").toStdString()  << r.name.toStdString() << std::endl;
            error = 1;

            return;
        }


        readers.push_back(r);
        if ( nextNext != args.end() ) {
            ++nextNext;
        }
        args.erase(it, nextNext);
    } // for (;;)

    bool atLeastOneOutput = false;
    ///Parse outputs
    for (;; ) {
        QString indexStr;
        QStringList::iterator it  = hasOutputToken(indexStr);
        if (error > 0) {
            return;
        }
        if ( it == args.end() ) {
            break;
        }

        if (!isBackground) {
            std::cout << QObject::tr("You cannot use the -o option in interactive or interpreter mode").toStdString() << std::endl;
            error = 1;

            return;
        }

        CLArgs::WriterArg w;
        w.name = QString( QString::fromUtf8("Output%1") ).arg(indexStr);
        w.mustCreate = true;
        atLeastOneOutput = true;

        //Check for a mandatory file name
        QStringList::iterator next = it;
        if ( next != args.end() ) {
            ++next;
        }
        if ( next == args.end() ) {
            std::cout << QObject::tr("Filename is not optional with the -o option").toStdString() << std::endl;
            error = 1;

            return;
        }

        //Check for an optional filename
        if ( !next->startsWith( QChar::fromLatin1('-') ) && !next->startsWith( QString::fromUtf8("--") ) ) {
            w.filename = *next;
        }

        writers.push_back(w);

        QStringList::iterator endToErase = next;
        ++endToErase;
        args.erase(it, endToErase);
    }

    if (atLeastOneOutput && !rangeSet) {
        std::cout << QObject::tr("A frame range must be set when using the -o option").toStdString() << std::endl;
        error = 1;

        return;
    }
} // CLArgsPrivate::parse