Example #1
0
int main(void) {
	JSParser* parser = new JSParser();
	parser->registerDelegate("printf", js_printf);
	
	// Parse the javascript
	string* code = new string();
	code->append("var s = ('false' == ('1' == '10'));");
	code->append("var a = 'hello, ' + 'world!';");
	code->append("printf(a); printf(s);");
	code->append("function doIt(){ var r = 20 + 1 + 5 + 30 - (6*2); printf(r); }");
	code->append("doIt();");
	code->append("doIt();");
	code->append("function myFunc ( first, second ){ printf(first + second); }");
	code->append("myFunc('starcraft ', 'love');");
	code->append("function addLove() { return 'love'; }");
	code->append("var t = (addLove() + 'craft'); printf(t);");
	code->append("var obj = function() { printf('constructor'); };");
	code->append("obj.prototype.method = function(i){ printf('method' + i); };");
	code->append("var instance = new obj();");
	code->append("var other = new obj();");
	code->append("instance.method('a12');");
	code->append("other.method('a2a');");
	code->append("function invoke(callback, b) { printf('in function '); callback.call(); b.call(); }");
	code->append("function cd() { printf('callback!'); }");
	code->append("invoke(function() { printf('wat wat'); }, function(){ printf('other'); });");
	code->append("function ident() { return true; }");
	code->append("if (true && (false || true)) { printf('in the method'); }");
	code->append("");
	code->append("var player = function() { this.id = '100'; }");
	code->append("player.prototype.print = function() { printf(this.id); this.id = '10'; }");
	code->append("var p1 = new player();");
	code->append("var p2 = new player();");
	code->append("p1.print();");
	code->append("p2.print();");
	code->append("p1.print();");
	
	List<Token*> tokens = tokenize(code->toString());
	// Print some debug information
	for ( int i = 0; i < tokens.getLength(); i++ ) {
		printf(tokens.getAt(i)->val);
	}
	
	printf("\n\n");
	parser->parse(code->toString());
	
	// Terminate program.
	printf("\n");
	return 0;
}
Example #2
0
void IndexerJobEsprima::index()
{
    JSParser parser;
    if (!parser.init()) {
        error() << "Can't init JSParser for" << mSourceInformation.sourceFile();
        return;
    }
    if (isAborted())
        return;
    String dump;
    if (!parser.parse(mSourceInformation.sourceFile(), &mData->symbols, &mData->symbolNames,
                      mType == Dump ? 0 : &mData->dependencies, mType == Dump ? &dump : 0)) {
        error() << "Can't parse" << mSourceInformation.sourceFile();
    }
    mParseTime = time(0);

    if (mType == Dump) {
        dump += "\n";
        {
            Log stream(&dump);
            stream << "symbols:\n";
            for (SymbolMap::const_iterator it = mData->symbols.begin(); it != mData->symbols.end(); ++it) {
                stream << it->first << it->second.toString(0) << '\n';
            }

            stream << "symbolnames:\n";
            for (SymbolNameMap::const_iterator it = mData->symbolNames.begin(); it != mData->symbolNames.end(); ++it) {
                stream << it->first << it->second << '\n';
            }

            assert(id() != -1);
        }
        write(dump);

        mData->symbols.clear();
        mData->symbolNames.clear();
    } else {
        mData->message = String::format<128>("%s in %dms. (%d syms, %d symNames, %d refs)",
                                             mSourceInformation.sourceFile().toTilde().constData(),
                                             static_cast<int>(mTimer.elapsed()) / 1000, mData->symbols.size(), mData->symbolNames.size(), mData->references.size());
    }
}