Esempio n. 1
0
bool ScriptParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
{
    if (keyword==Scanner::K_while || keyword==Scanner::K_if || keyword==Scanner::K_elseif)
    {
        mControlParser.reset();
        if (mControlParser.parseKeyword (keyword, loc, scanner))
            scanner.scan (mControlParser);

        mControlParser.appendCode (mOutput.getCode());

        return true;
    }

    /// \todo add an option to disable this nonsense
    if (keyword==Scanner::K_endif)
    {
        // surplus endif
        getErrorHandler().warning ("endif without matching if/elseif", loc);

        SkipParser skip (getErrorHandler(), getContext());
        scanner.scan (skip);
        return true;
    }

    if (keyword==Scanner::K_end && mEnd)
    {
        return false;
    }

    mLineParser.reset();
    if (mLineParser.parseKeyword (keyword, loc, scanner))
        scanner.scan (mLineParser);

    return true;
}
Esempio n. 2
0
    bool ControlParser::parseWhileBody (int keyword, const TokenLoc& loc, Scanner& scanner)
    {
        if (keyword==Scanner::K_endwhile)
        {
            Codes loop;

            Codes expr;
            mExprParser.append (expr);

            Generator::jump (loop, -static_cast<int> (mCodeBlock.size()+expr.size()));

            std::copy (expr.begin(), expr.end(), std::back_inserter (mCode));

            Codes skip;

            Generator::jumpOnZero (skip, mCodeBlock.size()+loop.size()+1);

            std::copy (skip.begin(), skip.end(), std::back_inserter (mCode));

            std::copy (mCodeBlock.begin(), mCodeBlock.end(), std::back_inserter (mCode));

            Codes loop2;

            Generator::jump (loop2, -static_cast<int> (mCodeBlock.size()+expr.size()+skip.size()));

            if (loop.size()!=loop2.size())
                throw std::logic_error (
                    "internal compiler error: failed to generate a while loop");

            std::copy (loop2.begin(), loop2.end(), std::back_inserter (mCode));

            mState = WhileEndwhileState;
            return true;
        }
        else if (keyword==Scanner::K_if || keyword==Scanner::K_while)
        {
            // nested
            ControlParser parser (getErrorHandler(), getContext(), mLocals, mLiterals);

            if (parser.parseKeyword (keyword, loc, scanner))
                scanner.scan (parser);

            parser.appendCode (mCodeBlock);

            return true;
        }
        else
        {
            mLineParser.reset();
            if (mLineParser.parseKeyword (keyword, loc, scanner))
                scanner.scan (mLineParser);

            return true;
        }
    }
Esempio n. 3
0
    void ExprParser::parseArguments (const std::string& arguments, Scanner& scanner,
        std::vector<Interpreter::Type_Code>& code, bool invert)
    {
        ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true);
        StringParser stringParser (getErrorHandler(), getContext(), mLiterals);
        
        std::stack<std::vector<Interpreter::Type_Code> > stack;
        
        for (std::string::const_iterator iter (arguments.begin()); iter!=arguments.end();
            ++iter)
        {
            if (*iter=='S' || *iter=='c')
            {
                stringParser.reset();
                if (*iter=='c') stringParser.smashCase();
                scanner.scan (stringParser);            
                
                if (invert)
                {
                    std::vector<Interpreter::Type_Code> tmp;
                    stringParser.append (tmp);
                    
                    stack.push (tmp);
                }
                else
                    stringParser.append (code);
            }
            else
            {
                parser.reset();    
                scanner.scan (parser);

                std::vector<Interpreter::Type_Code> tmp;

                char type = parser.append (tmp);

                if (type!=*iter)
                    Generator::convert (tmp, type, *iter);
                    
                if (invert)
                    stack.push (tmp);
                else
                    std::copy (tmp.begin(), tmp.end(), std::back_inserter (code));
            }
        }
        
        while (!stack.empty())
        {
            std::vector<Interpreter::Type_Code>& tmp = stack.top();
        
            std::copy (tmp.begin(), tmp.end(), std::back_inserter (code));
        
            stack.pop();
        }
    }    
Esempio n. 4
0
int main(int argc, char** argv)
{
	if (argc<3)
	{
		cout << "Not enough arguments. You must provide a valid infile." << endl;
		return 0;
	}

	ofstream out;
  out.open(argv[2]);
	Scanner* s = new Scanner();
	try
	{
 		s->scan(argv[1]);
		print_toks(s->toks,out);
		out << "Total Tokens = " << s->toks.size() << endl;
  	out.close();
		delete s;
	}
	catch (exception& e)
	{
		print_toks(s->toks, out);
		// cout << e.what();
		out << e.what();
		out.close();
		delete s;
	}
}
Esempio n. 5
0
bool Compiler::QuickFileParser::parseName (const std::string& name, const TokenLoc& loc,
    Scanner& scanner)
{
    SkipParser skip (getErrorHandler(), getContext());
    scanner.scan (skip);
    return true;
}
Esempio n. 6
0
    bool FileParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
    {
        if (code==Scanner::S_newline)
        {
            if (mState==BeginState)
            {
                // ignore empty lines
                return true;
            }

            if (mState==BeginCompleteState)
            {
                // parse the script body
                mScriptParser.reset();

                scanner.scan (mScriptParser);

                mState = EndNameState;
                scanner.allowNameStartingwithDigit();
                return true;
            }

            if (mState==EndCompleteState || mState==EndNameState)
            {
                // we are done here -> ignore the rest of the script
                return false;
            }
        }

        return Parser::parseSpecial (code, loc, scanner);
    }
Esempio n. 7
0
void ScannerPage::runScanner ()
{
  QString s;
  config.getData(Config::ScannerPath, s);
  QString s2("*");
  SymbolDialog *dialog = new SymbolDialog(this,
                                          s,
  					  s,
					  s2,
					  Q3FileDialog::ExistingFiles);
  dialog->setCaption(tr("Select scanners to run"));

  int rc = dialog->exec();

  if (rc == QDialog::Accepted)
  {
    QStringList l = dialog->selectedFiles();
    int loop;
    QDir dir;
    for (loop = 0; loop < (int) l.count(); loop++)
    {
      QFileInfo fi(l[loop]);
      Scanner *sdialog = new Scanner(fi.fileName(), chartIndex);
      connect(sdialog, SIGNAL(exitScanner()), this, SLOT(refreshList()));
      connect(sdialog, SIGNAL(message(QString)), this, SIGNAL(message(QString)));
      sdialog->show();
      sdialog->scan();
      delete sdialog;
    }
  }

  delete dialog;
}
Esempio n. 8
0
    void LineParser::parseExpression (Scanner& scanner, const TokenLoc& loc)
    {
        mExprParser.reset();

        if (!mExplicit.empty())
        {
            mExprParser.parseName (mExplicit, loc, scanner);
            if (mState==MemberState)
                mExprParser.parseSpecial (Scanner::S_member, loc, scanner);
            else
                mExprParser.parseSpecial (Scanner::S_ref, loc, scanner);
        }

        scanner.scan (mExprParser);

        char type = mExprParser.append (mCode);
        mState = EndState;

        switch (type)
        {
            case 'l':

                Generator::report (mCode, mLiterals, "%g");
                break;

            case 'f':

                Generator::report (mCode, mLiterals, "%f");
                break;

            default:

                throw std::runtime_error ("unknown expression result type");
        }
    }
Esempio n. 9
0
bool Compiler::QuickFileParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
{
    if (keyword==Scanner::K_end)
        return false;

    if (keyword==Scanner::K_short || keyword==Scanner::K_long || keyword==Scanner::K_float)
    {
        mDeclarationParser.reset();
        scanner.putbackKeyword (keyword, loc);
        scanner.scan (mDeclarationParser);
        return true;
    }

    SkipParser skip (getErrorHandler(), getContext());
    scanner.scan (skip);
    return true;
}
Esempio n. 10
0
bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc,
                              Scanner& scanner)
{
    mLineParser.reset();
    if (mLineParser.parseName (name, loc, scanner))
        scanner.scan (mLineParser);

    return true;
}
Esempio n. 11
0
bool Compiler::QuickFileParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
{
    if (code!=Scanner::S_newline)
    {
        SkipParser skip (getErrorHandler(), getContext());
        scanner.scan (skip);
    }

    return true;
}
Esempio n. 12
0
Status* Parser::parseProgram(const string& code) {
    Scanner scanner;
    try {
         Status* s = scanner.scan(code, _tokens);
         if (s) {
             return s;
         }
    } catch (ErrorInfoHolder* error) {
        return new Status(error->getMessage(), error->getPosition());
    }
    return parseTopLevel();
}
Esempio n. 13
0
    bool ControlParser::parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner)
    {
        if (mState==IfBodyState || mState==IfElseifBodyState || mState==IfElseBodyState ||
            mState==WhileBodyState)
        {
            scanner.putbackName (name, loc);
            mLineParser.reset();
            scanner.scan (mLineParser);
            return true;
        }

        return Parser::parseName (name, loc, scanner);
    }
Esempio n. 14
0
    bool ControlParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
    {
        if (mState==StartState)
        {
            if (keyword==Scanner::K_if || keyword==Scanner::K_elseif)
            {
                if (keyword==Scanner::K_elseif)
                    getErrorHandler().warning ("elseif without matching if", loc);

                mExprParser.reset();
                scanner.scan (mExprParser);

                mState = IfEndState;
                return true;
            }
            else if (keyword==Scanner::K_while)
            {
                mExprParser.reset();
                scanner.scan (mExprParser);

                mState = WhileEndState;
                return true;
            }
        }
        else if (mState==IfBodyState || mState==IfElseifBodyState || mState==IfElseBodyState ||
            mState==IfElseJunkState)
        {
            if (parseIfBody (keyword, loc, scanner))
                return true;
        }
        else if (mState==WhileBodyState)
        {
            if ( parseWhileBody (keyword, loc, scanner))
                return true;
        }

        return Parser::parseKeyword (keyword, loc, scanner);
    }
Inst ExtManager::parseExtInstMnemo(Scanner& scanner, Brigantine& bw, int* vx) const
{
    assert(scanner.peek().kind() == EExtInstName);

    // Parse mnemo prefix adding suffices until there is an extension which can handle it.
    // This prefix has the form <vendor>_<extension>

    string prefix = scanner.scan().text();
    while (!isMnemoPrefix(prefix) && scanner.peek().kind() == EExtInstSuff) 
    {
        prefix += scanner.scan().text();
    }

    // Parse remaining part of instruction mnemo (typically in the form <opcode>_<suff>)

    if (const Extension* e = getByPrefix(prefix)) return e->parseInstMnemo(prefix, scanner, bw, vx);

    // Enabled extensions failed to parse this mnemo.
    // Search for a disabled extension which might have handled it
    const char* s = 0;
    for (unsigned i = 0; i < size(); ++i)
    {
        if (!isEnabled[i] && (s = extension[i]->matchInstMnemo(prefix)) != 0)
        {
            scanner.syntaxError(string("Instruction \"") + s + "\" cannot be used (extension \"" + extension[i]->getName() + "\" is not enabled)");
            return Inst();
        }
    }

    // Extensions (including disabled) cannot handle this mnemo

    if (!hasEnabled()) scanner.scan(); // show offending token
    scanner.syntaxError("Undefined instruction");

    return Inst();
}
Esempio n. 16
0
bool ScriptParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
{
    if (code==Scanner::S_newline) // empty line
        return true;

    if (code==Scanner::S_open) /// \todo Option to switch this off
    {
        scanner.putbackSpecial (code, loc);
        return parseKeyword (Scanner::K_if, loc, scanner);
    }

    mLineParser.reset();
    if (mLineParser.parseSpecial (code, loc, scanner))
        scanner.scan (mLineParser);

    return true;
}
Esempio n. 17
0
Inst parseMnemoGcnMem(unsigned opCode, Scanner& scanner, Brigantine& bw, int* vx) 
{
    OptionalU const vector  = scanner.tryEatToken(EMVector);
    string    const segment = scanner.scan().text();

    if (segment != AMD_GCN_SEGMENT_SUFF) scanner.syntaxError("missing or invalid segment name");

    int vx2 = -1;
    InstMem res = parseMnemoMem(opCode, scanner, bw, &vx2);
    if (res.segment() != BRIG_SEGMENT_FLAT) scanner.syntaxError("invalid segment");
    if (vx2 != 1) scanner.syntaxError("invalid vX suffix");

    res.segment() = BRIG_SEGMENT_AMD_GCN;
    if (vx) *vx = vector.isInitialized() ? vector.value() : 1;

    return res;
}
Esempio n. 18
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QCoreApplication::setOrganizationDomain("ahlforn.net");
    QCoreApplication::setApplicationName("Reddit Scanner");
    QCoreApplication::setApplicationVersion("0.1.1");

    QSettings settings;
    QCommandLineParser parser;

    parser.setApplicationDescription("Scans reddit/subreddits for images.");
    parser.addHelpOption();
    parser.addVersionOption();

    parser.addPositionalArgument("source", "Url of subreddit");
    parser.addPositionalArgument("destination", "path for files to be stored in.");

    QCommandLineOption optionLimit(QStringList() << "l" << "limit", "Limit on downloaded images. Default = 100", "Limit");
    QCommandLineOption optionID(QStringList() << "i" << "id", "Client ID for Imgur.", "ClientID");

    parser.addOption(optionLimit);
    parser.addOption(optionID);

    parser.process(a);

    const QStringList args = parser.positionalArguments();

    QString url = (args.length() > 0 && !args.at(0).isEmpty()) ? args.at(0) : settings.value("url").toString();
    QString path = (args.length() > 0 && !args.at(1).isEmpty()) ? args.at(1) : settings.value("path").toString();
    int limit = (!parser.value(optionLimit).isEmpty()) ? parser.value(optionLimit).toInt() : settings.value("limit", 100).toInt();
    QString id = (!parser.value(optionID).isEmpty()) ? parser.value(optionID) : settings.value("clientID").toString();

    Scanner scanner;
    scanner.setUrl(QUrl(url + ".json"));
    scanner.setLimit(limit);
    scanner.setClientID(id);
    scanner.setPath(path);
    scanner.scan();

    return a.exec();
}
Esempio n. 19
0
int main()
{
  Scanner scanner;

  if (!scanner.scan()) {
    std::cout << "scan fail" << std::endl;
    std::cout << scanner.get_root() << std::endl;
    return 0;
  }
  
  std::map<std::string, std::string> uri_class_table = scanner.get_uri_class_table();
  std::map<std::pair<std::string, std::string>, std::string> uri_method_table = scanner.get_uri_method_table();
  std::map<std::string, std::string>::iterator iter = uri_class_table.begin();
  std::map<std::pair<std::string, std::string>, std::string>::iterator it = uri_method_table.begin();

  for (; iter != uri_class_table.end(); iter++)
    std::cout << "URI:" << iter->first << "\tCLASS:" << iter->second << std::endl;

  for (; it != uri_method_table.end(); it++)
    std::cout << "PATH: " << it->first.first << "  METHOD: " << it->first.second << "  FUNCTION:" << it->second << std::endl;
}
Esempio n. 20
0
string compile() {

    // tokenize it:
    cout << "\nListing Source code:\n\n"<< sourceCode << "\n\n";
    
    Scanner scanner;
    TokenList* list = scanner.scan(sourceCode);
    cout << "\n\nTokens:\n" << *list;
    
    
    // now parse the tokenlist into a syntax tree
    ParseTableReader parserTableReader;
    Parser* p = parserTableReader.createParser(list, serializedParseTable);
    
    cout << "\nParsing tokenlist now:\n";
	
	string ret = "";
    
    try{
        SyntaxTree* syntaxtree = p->parse();
        
        
        cout << "\n\nSyntaxTree created:\n" << *syntaxtree;
        
        cout << "\n\nGenerating AST:\n";
        
        ASTGenerator astGen(syntaxtree);
        AST::CodeProgram* p = astGen.generate();
		
		ret = p->code();
        
        cout << "Compiled ASM:\n" << ret << endl;
        
    }catch(GrammarException* ex){
        cout << "\n\nGrammarException: " << ex->what() << "\n";
    }
    
    return ret;
}
Esempio n. 21
0
int main(int argc, char* argv[])
{
	string inputfile = argv[1];
	string outputfile = argv[2];

	ifstream in;
	in.open(argv[1]);
	ofstream out;
	out.open(argv[2]);

	Scanner scanner;

	while(!in.eof())
	{
		scanner.scan(in, out);
		if(scanner.final == true)
		{
			break;
		}
	}
	for(unsigned int i = 0; i < scanner.tokens.size(); i++)
	{
		Token temp = scanner.tokens[i];
		//out << "(" << temp.getKind() << "," << temp.getValue() << "," << temp.getLine() << ")\n";
	}


	if(scanner.final == true)
	{
		//out << "Input Error on line " << scanner.number << "\n";
	}
	else
	{
		//out << "Total Tokens = " << scanner.tokens.size() << endl;
	}

	try
	{
Esempio n. 22
0
    bool ControlParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
    {
        if (code==Scanner::S_newline)
        {
            switch (mState)
            {
                case IfEndState: mState = IfBodyState; return true;
                case IfElseifEndState: mState = IfElseifBodyState; return true;
                case IfElseEndState: mState = IfElseBodyState; return true;
                case IfElseJunkState: mState = IfElseBodyState; return true;

                case WhileEndState: mState = WhileBodyState; return true;

                case IfBodyState:
                case IfElseifBodyState:
                case IfElseBodyState:
                case WhileBodyState:

                    return true; // empty line

                case IfEndifState:
                case WhileEndwhileState:

                    return false;

                default: ;
            }
        }
        else if (code==Scanner::S_open && mState==IfElseJunkState)
        {
            SkipParser skip (getErrorHandler(), getContext());
            scanner.scan (skip);
            mState = IfElseBodyState;
            return true;
        }

        return Parser::parseSpecial (code, loc, scanner);
    }
Esempio n. 23
0
//int main(int argc, char** argv)
int main()
{
	
	vector<Token> ex_list;
	//ex_list = sc.scan(argv[1]);
	ex_list = sc.scan("parcertest1.txt");
	//sc.scan("parcertest1.txt", "parceresport.txt");
	p.get_vector(ex_list);
	 
	DLP vect = p.datalogprogram("DLPEXPORT.txt");
	
	w.importDLP(vect);
	//string temp = argv[2];
	string temp = "a.txt";
	ofstream out(temp);
	w.doWork(out);
	w.factEval(out);
	w.printfacts(out);
	w.further_work(out);

	// out.close();


	//vector<Predicate> sh = vect.getSchemes();


	/*
	for (int i = 0; i < sh.size(); i++)
	{
		cout << sh[i].toString() << endl;
	}*/
	 
	
	//system("pause");
	return 0;
}
Esempio n. 24
0
    int ExprParser::parseArguments (const std::string& arguments, Scanner& scanner,
        std::vector<Interpreter::Type_Code>& code)
    {
        bool optional = false;
        int optionalCount = 0;

        ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true);
        StringParser stringParser (getErrorHandler(), getContext(), mLiterals);

        std::stack<std::vector<Interpreter::Type_Code> > stack;

        for (std::string::const_iterator iter (arguments.begin()); iter!=arguments.end();
            ++iter)
        {
            if (*iter=='/')
            {
                optional = true;
            }
            else if (*iter=='S' || *iter=='c' || *iter=='x')
            {
                stringParser.reset();

                if (optional || *iter=='x')
                    stringParser.setOptional (true);

                if (*iter=='c') stringParser.smashCase();
                scanner.scan (stringParser);

                if (optional && stringParser.isEmpty())
                    break;

                if (*iter!='x')
                {
                    std::vector<Interpreter::Type_Code> tmp;
                    stringParser.append (tmp);

                    stack.push (tmp);

                    if (optional)
                        ++optionalCount;
                }
            }
            else
            {
                parser.reset();

                if (optional || *iter == 'X')
                    parser.setOptional (true);

                scanner.scan (parser);

                if (optional && parser.isEmpty())
                    break;

                if (*iter != 'X')
                {
                    std::vector<Interpreter::Type_Code> tmp;

                    char type = parser.append (tmp);

                    if (type!=*iter)
                        Generator::convert (tmp, type, *iter);

                    stack.push (tmp);

                    if (optional)
                        ++optionalCount;
                }
            }
        }

        while (!stack.empty())
        {
            std::vector<Interpreter::Type_Code>& tmp = stack.top();

            std::copy (tmp.begin(), tmp.end(), std::back_inserter (code));

            stack.pop();
        }

        return optionalCount;
    }
Esempio n. 25
0
    bool LineParser::parseName (const std::string& name, const TokenLoc& loc,
        Scanner& scanner)
    {
        if (mState==PotentialEndState)
        {
            getErrorHandler().warning ("stray string argument (ignoring it)", loc);
            mState = EndState;
            return true;
        }

        if (mState==SetState)
        {
            std::string name2 = Misc::StringUtils::lowerCase (name);
            mName = name2;

            // local variable?
            char type = mLocals.getType (name2);
            if (type!=' ')
            {
                mType = type;
                mState = SetLocalVarState;
                return true;
            }

            type = getContext().getGlobalType (name2);
            if (type!=' ')
            {
                mType = type;
                mState = SetGlobalVarState;
                return true;
            }

            mState = SetPotentialMemberVarState;
            return true;
        }

        if (mState==SetMemberVarState)
        {
            mMemberName = Misc::StringUtils::lowerCase (name);
            std::pair<char, bool> type = getContext().getMemberType (mMemberName, mName);

            if (type.first!=' ')
            {
                mState = SetMemberVarState2;
                mType = type.first;
                mReferenceMember = type.second;
                return true;
            }

            getErrorHandler().error ("unknown variable", loc);
            SkipParser skip (getErrorHandler(), getContext());
            scanner.scan (skip);
            return false;
        }

        if (mState==MessageState || mState==MessageCommaState)
        {
            std::string arguments;

            for (std::size_t i=0; i<name.size(); ++i)
            {
                if (name[i]=='%')
                {
                    ++i;
                    if (i<name.size())
                    {
                        if (name[i]=='G' || name[i]=='g')
                        {
                            arguments += "l";
                        }
                        else if (name[i]=='S' || name[i]=='s')
                        {
                            arguments += 'S';
                        }
                        else if (name[i]=='.' || name[i]=='f')
                        {
                            arguments += 'f';
                        }
                    }
                }
            }

            if (!arguments.empty())
            {
                mExprParser.reset();
                mExprParser.parseArguments (arguments, scanner, mCode);
            }

            mName = name;
            mButtons = 0;

            mState = MessageButtonState;
            return true;
        }

        if (mState==MessageButtonState || mState==MessageButtonCommaState)
        {
            Generator::pushString (mCode, mLiterals, name);
            mState = MessageButtonState;
            ++mButtons;
            return true;
        }

        if (mState==BeginState && getContext().isId (name))
        {
            mState = PotentialExplicitState;
            mExplicit = Misc::StringUtils::lowerCase (name);
            return true;
        }

        if (mState==BeginState && mAllowExpression)
        {
            std::string name2 = Misc::StringUtils::lowerCase (name);

            char type = mLocals.getType (name2);

            if (type!=' ')
            {
                scanner.putbackName (name, loc);
                parseExpression (scanner, loc);
                return true;
            }

            type = getContext().getGlobalType (name2);

            if (type!=' ')
            {
                scanner.putbackName (name, loc);
                parseExpression (scanner, loc);
                return true;
            }
        }

        return Parser::parseName (name, loc, scanner);
    }
Esempio n. 26
0
    bool LineParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
    {
        if (mState==SetMemberVarState)
        {
            mMemberName = loc.mLiteral;
            std::pair<char, bool> type = getContext().getMemberType (mMemberName, mName);

            if (type.first!=' ')
            {
                mState = SetMemberVarState2;
                mType = type.first;
                mReferenceMember = type.second;
                return true;
            }
        }

        if (mState==SetPotentialMemberVarState && keyword==Scanner::K_to)
        {
            getErrorHandler().warning ("unknown variable (ignoring set instruction)", loc);
            SkipParser skip (getErrorHandler(), getContext());
            scanner.scan (skip);
            return false;
        }

        if (mState==SetState)
        {
            // allow keywords to be used as variable names when assigning a value to a variable.
            return parseName (loc.mLiteral, loc, scanner);
        }

        if (mState==BeginState || mState==ExplicitState)
        {
            switch (keyword)
            {
                case Scanner::K_enable:

                    Generator::enable (mCode, mLiterals, mExplicit);
                    mState = PotentialEndState;
                    return true;

                case Scanner::K_disable:

                    Generator::disable (mCode, mLiterals, mExplicit);
                    mState = PotentialEndState;
                    return true;

                case Scanner::K_startscript:

                    mExprParser.parseArguments ("c", scanner, mCode);
                    Generator::startScript (mCode, mLiterals, mExplicit);
                    mState = EndState;
                    return true;

                case Scanner::K_stopscript:

                    mExprParser.parseArguments ("c", scanner, mCode);
                    Generator::stopScript (mCode);
                    mState = EndState;
                    return true;
            }

            // check for custom extensions
            if (const Extensions *extensions = getContext().getExtensions())
            {
                std::string argumentType;

                bool hasExplicit = mState==ExplicitState;
                if (extensions->isInstruction (keyword, argumentType, hasExplicit))
                {
                    if (!hasExplicit && mState==ExplicitState)
                    {
                        getErrorHandler().warning ("stray explicit reference (ignoring it)", loc);
                        mExplicit.clear();
                    }

                    int optionals = 0;

                    try
                    {
                        // workaround for broken positioncell instructions.
                        /// \todo add option to disable this
                        std::auto_ptr<ErrorDowngrade> errorDowngrade (0);
                        if (Misc::StringUtils::lowerCase (loc.mLiteral)=="positioncell")
                            errorDowngrade.reset (new ErrorDowngrade (getErrorHandler()));

                        std::vector<Interpreter::Type_Code> code;
                        optionals = mExprParser.parseArguments (argumentType, scanner, code);
                        mCode.insert (mCode.end(), code.begin(), code.end());
                        extensions->generateInstructionCode (keyword, mCode, mLiterals,
                            mExplicit, optionals);
                    }
                    catch (const SourceException& exception)
                    {
                        // Ignore argument exceptions for positioncell.
                        /// \todo add option to disable this
                        if (Misc::StringUtils::lowerCase (loc.mLiteral)=="positioncell")
                        {
                            SkipParser skip (getErrorHandler(), getContext());
                            scanner.scan (skip);
                            return false;
                        }

                        throw;
                    }

                    mState = EndState;
                    return true;
                }
            }

            if (mAllowExpression)
            {
                if (keyword==Scanner::K_getdisabled || keyword==Scanner::K_getdistance)
                {
                    scanner.putbackKeyword (keyword, loc);
                    parseExpression (scanner, loc);
                    mState = EndState;
                    return true;
                }

                if (const Extensions *extensions = getContext().getExtensions())
                {
                    char returnType;
                    std::string argumentType;

                    bool hasExplicit = !mExplicit.empty();

                    if (extensions->isFunction (keyword, returnType, argumentType, hasExplicit))
                    {
                        if (!hasExplicit && !mExplicit.empty())
                        {
                            getErrorHandler().warning ("stray explicit reference (ignoring it)", loc);
                            mExplicit.clear();
                        }

                        scanner.putbackKeyword (keyword, loc);
                        parseExpression (scanner, loc);
                        mState = EndState;
                        return true;
                    }
                }
            }
        }

        if (mState==ExplicitState)
        {
            // drop stray explicit reference
            getErrorHandler().warning ("stray explicit reference (ignoring it)", loc);
            mState = BeginState;
            mExplicit.clear();
        }

        if (mState==BeginState)
        {
            switch (keyword)
            {
                case Scanner::K_short:
                case Scanner::K_long:
                case Scanner::K_float:
                {
                    if (!getContext().canDeclareLocals())
                    {
                        getErrorHandler().error (
                            "local variables can't be declared in this context", loc);
                        SkipParser skip (getErrorHandler(), getContext());
                        scanner.scan (skip);
                        return true;
                    }

                    DeclarationParser declaration (getErrorHandler(), getContext(), mLocals);
                    if (declaration.parseKeyword (keyword, loc, scanner))
                        scanner.scan (declaration);

                    return false;
                }

                case Scanner::K_set: mState = SetState; return true;
                case Scanner::K_messagebox: mState = MessageState; return true;

                case Scanner::K_return:

                    Generator::exit (mCode);
                    mState = EndState;
                    return true;

                case Scanner::K_stopscript:

                    mExprParser.parseArguments ("c", scanner, mCode);
                    Generator::stopScript (mCode);
                    mState = EndState;
                    return true;

                case Scanner::K_else:

                    getErrorHandler().warning ("stray else (ignoring it)", loc);
                    mState = EndState;
                    return true;

                case Scanner::K_endif:

                    getErrorHandler().warning ("stray endif (ignoring it)", loc);
                    mState = EndState;
                    return true;

                case Scanner::K_begin:

                    getErrorHandler().warning ("stray begin (ignoring it)", loc);
                    mState = EndState;
                    return true;
            }
        }
        else if (mState==SetLocalVarState && keyword==Scanner::K_to)
        {
            mExprParser.reset();
            scanner.scan (mExprParser);

            std::vector<Interpreter::Type_Code> code;
            char type = mExprParser.append (code);

            Generator::assignToLocal (mCode, mLocals.getType (mName),
                mLocals.getIndex (mName), code, type);

            mState = EndState;
            return true;
        }
        else if (mState==SetGlobalVarState && keyword==Scanner::K_to)
        {
            mExprParser.reset();
            scanner.scan (mExprParser);

            std::vector<Interpreter::Type_Code> code;
            char type = mExprParser.append (code);

            Generator::assignToGlobal (mCode, mLiterals, mType, mName, code, type);

            mState = EndState;
            return true;
        }
        else if (mState==SetMemberVarState2 && keyword==Scanner::K_to)
        {
            mExprParser.reset();
            scanner.scan (mExprParser);

            std::vector<Interpreter::Type_Code> code;
            char type = mExprParser.append (code);

            Generator::assignToMember (mCode, mLiterals, mType, mMemberName, mName, code, type,
                !mReferenceMember);

            mState = EndState;
            return true;
        }

        if (mAllowExpression)
        {
            if (keyword==Scanner::K_getsquareroot || keyword==Scanner::K_menumode ||
                keyword==Scanner::K_random || keyword==Scanner::K_scriptrunning ||
                keyword==Scanner::K_getsecondspassed)
            {
                scanner.putbackKeyword (keyword, loc);
                parseExpression (scanner, loc);
                mState = EndState;
                return true;
            }
        }

        return Parser::parseKeyword (keyword, loc, scanner);
    }
int main(int argc, char *argv[])
{
    using namespace std;
    bool print_tokens = false, print_ast = false, no_run = false; 
    // Refer whether tokens and ast need to be printed. 
    int name_count = 0;
    // Count the filename inputed.
    string filename;
    // Save the name of file.
    string raw;

    if (argc < 2)
    {
        // A correct call of rune must given 2 or more args;
        show_help();
        return -2;
    }
    else
    {
        for (int i = 1; i < argc; ++i)
        {
            if (argv[i][0] == '-')
            {
                switch(argv[i][1])
                {
                case 't':
                    print_tokens = true;
                    break;
                case 'a':
                    print_ast = true;
                    break;
                case 'n':
                    no_run = true;
                    break;
                default:
                    cout << "unrecognized command line option \'"
                         << argv[i] << '\'';  
                    show_help();
                    return -2;
                }
            }
            else
            {
                filename = argv[i];
                name_count++;
            }
        }
        if (name_count != 1)
        {
            // if more than 1 filenames were inputed, show help and exit.
            show_help();
            return -2;
        }
    }
    raw = read_all_from_file(filename);
    if (raw.size() == 0)
    {
        // **There is a pitfall.
        // **I'm not sure how to get known the existance of file.
        // **So, deal as same when file not exist and empty.
        cout << "file not exist or nothing contained in file.";
        return -3;
    }
    try
    {
        Preprocessor pre;
        vector<string> lines = pre.preprocess(raw);
        // Raw string preprocessed to decommented lines.
        Scanner scn;
        vector<Token> tokens = scn.scan(lines);
        // Scan the lines to a vector of Tokens.
        // Token is defined in "tokenEnum.h".
        if (print_tokens)
        {
            show_tokens(tokens);
        }
        // print Tokens if has option "-tokens".
        Parser psr;
        auto ast = psr.parse(tokens);
        // Parse the Tokens to an Abstract Syntax Tree.
        // AST is defined in "syntexTreeEnum.h".
        if (print_ast)
        {
            cout << "SYNTAX TREE:\n";
            psr.show_tree(ast);
        }
        // print AST if has option "-ast".
        if (!no_run)
        {
            Interpreter itp;
            itp.execute(ast);
            // Interprete the AST.
        }
    } 
    catch (string msg)
    {
        cout << msg << endl;
        return -1;
        // print error message.
    }
	return 0;
}
Esempio n. 28
0
    bool ControlParser::parseIfBody (int keyword, const TokenLoc& loc, Scanner& scanner)
    {
        if (keyword==Scanner::K_endif || keyword==Scanner::K_elseif ||
            keyword==Scanner::K_else)
        {
            std::pair<Codes, Codes> entry;

            if (mState!=IfElseBodyState)
                mExprParser.append (entry.first);

            std::copy (mCodeBlock.begin(), mCodeBlock.end(),
                std::back_inserter (entry.second));

            mIfCode.push_back (entry);

            mCodeBlock.clear();

            if (keyword==Scanner::K_endif)
            {
                // store code for if-cascade
                Codes codes;

                for (IfCodes::reverse_iterator iter (mIfCode.rbegin());
                    iter!=mIfCode.rend(); ++iter)
                {
                    Codes block;

                    if (iter!=mIfCode.rbegin())
                        Generator::jump (iter->second, codes.size()+1);

                    if (!iter->first.empty())
                    {
                        // if or elseif
                        std::copy (iter->first.begin(), iter->first.end(),
                            std::back_inserter (block));
                        Generator::jumpOnZero (block, iter->second.size()+1);
                    }

                    std::copy (iter->second.begin(), iter->second.end(),
                        std::back_inserter (block));

                   std::swap (codes, block);

                   std::copy (block.begin(), block.end(), std::back_inserter (codes));
                }

                std::copy (codes.begin(), codes.end(), std::back_inserter (mCode));

                mIfCode.clear();
                mState = IfEndifState;
            }
            else if (keyword==Scanner::K_elseif)
            {
                mExprParser.reset();
                scanner.scan (mExprParser);

                mState = IfElseifEndState;
            }
            else if (keyword==Scanner::K_else)
            {
                mState = IfElseJunkState; /// \todo should be IfElseEndState; add an option for that
            }

            return true;
        }
        else if (keyword==Scanner::K_if || keyword==Scanner::K_while)
        {
            // nested
            ControlParser parser (getErrorHandler(), getContext(), mLocals, mLiterals);

            if (parser.parseKeyword (keyword, loc, scanner))
                scanner.scan (parser);

            parser.appendCode (mCodeBlock);

            return true;
        }
        else
        {
            mLineParser.reset();
            if (mLineParser.parseKeyword (keyword, loc, scanner))
                scanner.scan (mLineParser);

            return true;
        }
    }
Esempio n. 29
0
    bool LineParser::parseName (const std::string& name, const TokenLoc& loc,
        Scanner& scanner)
    {
        if (mState==ShortState || mState==LongState || mState==FloatState)
        {
            if (!getContext().canDeclareLocals())
            {
                getErrorHandler().error ("local variables can't be declared in this context", loc);
                SkipParser skip (getErrorHandler(), getContext());
                scanner.scan (skip);
                return false;
            }

            std::string name2 = Misc::StringUtils::lowerCase (name);

            char type = mLocals.getType (name2);

            if (type!=' ')
            {
                /// \todo add option to make re-declared local variables an error
                getErrorHandler().warning ("can't re-declare local variable", loc);
                SkipParser skip (getErrorHandler(), getContext());
                scanner.scan (skip);
                mState = EndState;
                return true;
            }

            mLocals.declare (mState==ShortState ? 's' : (mState==LongState ? 'l' : 'f'),
                name2);

            mState = EndState;
            return true;
        }

        if (mState==SetState)
        {
            std::string name2 = Misc::StringUtils::lowerCase (name);
            mName = name2;

            // local variable?
            char type = mLocals.getType (name2);
            if (type!=' ')
            {
                mType = type;
                mState = SetLocalVarState;
                return true;
            }

            type = getContext().getGlobalType (name2);
            if (type!=' ')
            {
                mType = type;
                mState = SetGlobalVarState;
                return true;
            }

            mState = SetPotentialMemberVarState;
            return true;
        }

        if (mState==SetMemberVarState)
        {
            mMemberName = name;
            char type = getContext().getMemberType (mMemberName, mName);

            if (type!=' ')
            {
                mState = SetMemberVarState2;
                mType = type;
                return true;
            }

            getErrorHandler().error ("unknown variable", loc);
            SkipParser skip (getErrorHandler(), getContext());
            scanner.scan (skip);
            return false;
        }

        if (mState==MessageState || mState==MessageCommaState)
        {
            std::string arguments;

            for (std::size_t i=0; i<name.size(); ++i)
            {
                if (name[i]=='%')
                {
                    ++i;
                    if (i<name.size())
                    {
                        if (name[i]=='G' || name[i]=='g')
                        {
                            arguments += "l";
                        }
                        else if (name[i]=='S' || name[i]=='s')
                        {
                            arguments += 'S';
                        }
                        else if (name[i]=='.' || name[i]=='f')
                        {
                            arguments += 'f';
                        }
                    }
                }
            }

            if (!arguments.empty())
            {
                mExprParser.reset();
                mExprParser.parseArguments (arguments, scanner, mCode, true);
            }

            mName = name;
            mButtons = 0;

            mState = MessageButtonState;
            return true;
        }

        if (mState==MessageButtonState || mState==MessageButtonCommaState)
        {
            Generator::pushString (mCode, mLiterals, name);
            mState = MessageButtonState;
            ++mButtons;
            return true;
        }

        if (mState==BeginState && getContext().isId (name))
        {
            mState = PotentialExplicitState;
            mExplicit = Misc::StringUtils::lowerCase (name);
            return true;
        }

        if (mState==BeginState && mAllowExpression)
        {
            std::string name2 = Misc::StringUtils::lowerCase (name);

            char type = mLocals.getType (name2);

            if (type!=' ')
            {
                scanner.putbackName (name, loc);
                parseExpression (scanner, loc);
                return true;
            }

            type = getContext().getGlobalType (name2);

            if (type!=' ')
            {
                scanner.putbackName (name, loc);
                parseExpression (scanner, loc);
                return true;
            }
        }

        return Parser::parseName (name, loc, scanner);
    }
Esempio n. 30
0
    bool LineParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
    {
        if (mState==BeginState || mState==ExplicitState)
        {
            switch (keyword)
            {
                case Scanner::K_enable:

                    Generator::enable (mCode, mLiterals, mExplicit);
                    mState = EndState;
                    return true;

                case Scanner::K_disable:

                    Generator::disable (mCode, mLiterals, mExplicit);
                    mState = EndState;
                    return true;
            }

            // check for custom extensions
            if (const Extensions *extensions = getContext().getExtensions())
            {
                std::string argumentType;

                if (extensions->isInstruction (keyword, argumentType, mState==ExplicitState))
                {
                    int optionals = mExprParser.parseArguments (argumentType, scanner, mCode, true);

                    extensions->generateInstructionCode (keyword, mCode, mLiterals, mExplicit, optionals);
                    mState = EndState;
                    return true;
                }
            }

            if (mAllowExpression)
            {
                if (keyword==Scanner::K_getdisabled || keyword==Scanner::K_getdistance)
                {
                    scanner.putbackKeyword (keyword, loc);
                    parseExpression (scanner, loc);
                    mState = EndState;
                    return true;
                }

                if (const Extensions *extensions = getContext().getExtensions())
                {
                    char returnType;
                    std::string argumentType;

                    if (extensions->isFunction (keyword, returnType, argumentType,
                        !mExplicit.empty()))
                    {
                        scanner.putbackKeyword (keyword, loc);
                        parseExpression (scanner, loc);
                        mState = EndState;
                        return true;
                    }
                }
            }
        }

        if (mState==BeginState)
        {
            switch (keyword)
            {
                case Scanner::K_short: mState = ShortState; return true;
                case Scanner::K_long: mState = LongState; return true;
                case Scanner::K_float: mState = FloatState; return true;
                case Scanner::K_set: mState = SetState; return true;
                case Scanner::K_messagebox: mState = MessageState; return true;

                case Scanner::K_return:

                    Generator::exit (mCode);
                    mState = EndState;
                    return true;

                case Scanner::K_startscript:

                    mExprParser.parseArguments ("c", scanner, mCode, true);
                    Generator::startScript (mCode);
                    mState = EndState;
                    return true;

                case Scanner::K_stopscript:

                    mExprParser.parseArguments ("c", scanner, mCode, true);
                    Generator::stopScript (mCode);
                    mState = EndState;
                    return true;
            }
        }
        else if (mState==SetLocalVarState && keyword==Scanner::K_to)
        {
            mExprParser.reset();
            scanner.scan (mExprParser);

            std::vector<Interpreter::Type_Code> code;
            char type = mExprParser.append (code);

            Generator::assignToLocal (mCode, mLocals.getType (mName),
                mLocals.getIndex (mName), code, type);

            mState = EndState;
            return true;
        }
        else if (mState==SetGlobalVarState && keyword==Scanner::K_to)
        {
            mExprParser.reset();
            scanner.scan (mExprParser);

            std::vector<Interpreter::Type_Code> code;
            char type = mExprParser.append (code);

            Generator::assignToGlobal (mCode, mLiterals, mType, mName, code, type);

            mState = EndState;
            return true;
        }
        else if (mState==SetMemberVarState2 && keyword==Scanner::K_to)
        {
            mExprParser.reset();
            scanner.scan (mExprParser);

            std::vector<Interpreter::Type_Code> code;
            char type = mExprParser.append (code);

            Generator::assignToMember (mCode, mLiterals, mType, mMemberName, mName, code, type);

            mState = EndState;
            return true;
        }

        if (mAllowExpression)
        {
            if (keyword==Scanner::K_getsquareroot || keyword==Scanner::K_menumode ||
                keyword==Scanner::K_random || keyword==Scanner::K_scriptrunning ||
                keyword==Scanner::K_getsecondspassed)
            {
                scanner.putbackKeyword (keyword, loc);
                parseExpression (scanner, loc);
                mState = EndState;
                return true;
            }
        }

        return Parser::parseKeyword (keyword, loc, scanner);
    }