/** * Run type checker when in a function scope. */ void runTypeChecker() { while(currentASTNode != NULL) { if(currentASTNode->ruleNum == 4) { // It is a function. goToFunctionScope(); } else { currentASTNode = currentASTNode->childNode; switch(currentASTNode->ruleNum) { case 7: break; case 8: checkAssignmentType1();break; case 9: checkAssignmentType2();break; case 10: goToIfScope();break; // Need to go into if scope. case 11: break; case 12: break; // No return type function } currentASTNode = currentASTNode->parentNode->sisterNode; //stmtsAndFunctionDefs_type1 nextStatement(); } } }
/** * Get the next statement to run type checker on. */ void nextStatement() { if(currentASTNode == NULL) return; if(currentASTNode->ruleNum == 6) { // It is a nullable. if(curScope == 0) { // We are in the main function scope and hence the end of the program. End here. currentASTNode = NULL; return; } else { // We are in some inner function and hence there might be other statements in the above function. Find it. while(currentASTNode->element.nontermValue != functionDef) { currentASTNode = currentASTNode->parentNode; } // We found the functionDef node. popTable(); currentASTNode = currentASTNode->parentNode->sisterNode; //stmtsAndFunctionDef_type1 nextStatement(); return; } } else { // More statements exist. currentASTNode = currentASTNode->childNode->childNode; //stmtorFunctionDef return; } }
void getIncludes(Node* n){ if(verbosity==shorter||verbosity==alle)return; if(n->id<0)return; if(eq("Release track",n->name))return; if(eq("Recording",n->name))return; if(eq("Document",n->name))return; if(eq("Cataloged instance",n->name))return; pf("getIncludes %d >>%s<<\n",n->id,n->name); Statement *s=0; int lookups=0; while((s=nextStatement(n,s))){ if(++lookups>50)break; // p(s); if(eq(s->Predicate()->name,"exclude")){ excluded.push_back(s->Object()->name); excludedIds.push_back(s->Object()->id); } if(eq(s->Predicate()->name,"include")){ included.push_back(s->Object()->name); includedIds.push_back(s->Object()->id); } } }
/* CENTRAL METHOD to parse and render html request*/ int handle(cchar* q0,int conn){ int len=(int)strlen(q0); if(len>1000){ p("checkSanity len>1000"); return 0;// SAFETY! } char* q=editable(q0); checkSanity(q,len); while(q[0]=='/')q++; enum result_format format = html;//txt; html DANGER WITH ROBOTS enum result_verbosity verbosity = normal; if (eq(q, "favicon.ico"))return 0; if(contains(q,"robots.txt")){ Writeline(conn,"User-agent: *\n"); Writeline("Disallow: /\n"); return 0; } char* jsonp=strstr(q,"jsonp");// ?jsonp=fun if(jsonp){ jsonp[-1]=0; jsonp+=6; format = json; } else jsonp=(char*)"parseResults"; if (endsWith(q, ".json")) { format = json; q[len-5]=0; } if (endsWith(q, ".xml")) { format = xml; q[len-4]=0; } if (endsWith(q, ".csv")||endsWith(q, ".tsv")) { format = csv; q[len-4]=0; } if (endsWith(q, ".txt")) { format = txt; q[len-4]=0; } if (endsWith(q, ".html")) { format = html; q[len-5]=0; } if (startsWith(q, ".js")) { q[len-3]=0; Writeline(conn, jsonp); Writeline(conn, "("); format = js; } // todo : dedup!! if (startsWith(q, "all/")) { cut_to(q," +"); cut_to(q," -"); q = q + 4; showExcludes=false; verbosity = alle; } if (startsWith(q, "long/")){ verbosity = longer; q = q + 5; } if (startsWith(q, "full/")) { verbosity = verbose; q = q + 5; } if (startsWith(q, "verbose/")) { verbosity = verbose; q = q + 8; } if (startsWith(q, "short/")) { verbosity = shorter; q = q + 6; } if (startsWith(q, "html/")) { format = html; if(!contains(q,".")&&!contains(q,":")) verbosity=verbose; q = q + 5; } if (startsWith(q, "plain/")) { format = txt; q = q + 6; } if (startsWith(q, "text/")) { format = txt; q = q + 5; } if (startsWith(q, "txt/")) { format = txt; q = q + 4; } if (startsWith(q, "xml/")) { format = xml; q = q + 4; } if (startsWith(q, "csv/")||startsWith(q, "tsv/")) { format = csv; q = q + 4; } if (startsWith(q, "json/")) { format = json; q = q + 5; } if (startsWith(q, "js/")) { q = q + 3; Writeline(conn, jsonp); Writeline(conn, "("); format = js; } if (startsWith(q, "long/")) { verbosity = longer; q = q + 5; } if (startsWith(q, "verbose/")) { verbosity = verbose; q = q + 8; } if (startsWith(q, "short/")) { verbosity = shorter; q = q + 6; } if (startsWith(q, "excludes/")||startsWith(q, "includes/")||startsWith(q, "excluded/")||startsWith(q, "included/")||startsWith(q, "showview/")) { showExcludes=true; verbosity=longer; q = q + 9; } else showExcludes=false; excluded.clear(); included.clear(); if(contains(q,"statement count")){Writeline(conn,itoa((int)context->statementCount).data());return 0;} if(contains(q,"node count")){Writeline(conn,itoa(context->nodeCount).data());return 0;} if (startsWith(q, "all/")) { cut_to(q," +"); cut_to(q," -"); q = q + 4; showExcludes=false; verbosity = alle; } // bool get_topic=false; bool get_topic=true; bool sort=false; if (startsWith(q, "ee/")||startsWith(q, "ee ")) { q[2]=' '; get_topic=true; } if (startsWith(q, "entities/")) { q[8]=' '; get_topic=true; // verbosity=longer; } if(hasWord(q)) loadView(q); if(contains(q,"exclude")||contains(q,"include")){ verbosity=normal; showExcludes=true; } p(q); // !!!!!!!!!!!!!!!!!!!!!!!!!!! // NodeVector all = parse(q); // <<<<<<<< HANDLE QUERY WITH NETBASE! // // !!!!!!!!!!!!!!!!!!!!!!!!!!! autoIds=false; int size=(int)all.size(); if(showExcludes){ for (int i = 0; i < size; i++) { // todo : own routine!!! Node* node = (Node*) all[i]; if(!contains(all,getAbstract(node->name))) all.push_back(getAbstract(node->name)); N parent= getType(node); if(parent){ if(!contains(all,parent))all.push_back(parent); N abs= getAbstract(parent->name); if(parent&&!contains(all,abs))all.push_back(abs); } } show(excluded); } const char* html_block="<!DOCTYPE html><html><head><META HTTP-EQUIV='CONTENT-TYPE' CONTENT='text/html; charset=UTF-8'/></head>"\ "<body><div id='netbase_results'></div>\n<script>var results="; // if((int)all.size()==0)Writeline("0"); // Writeline(conn,q); char buff[10000]; bool use_json= format == json || format == js || format == html; if (format == xml && (startsWith(q,"select")||contains(q," where "))){Writeline(conn,query2(q));return 0;} if (format == xml)Writeline(conn, "<results>\n"); if (format == html)Writeline(conn,html_block); if (use_json)Writeline(conn, "{\"results\":[\n"); const char* statement_format_xml = " <statement id='%d' subject=\"%s\" predicate=\"%s\" object=\"%s\" sid='%d' pid='%d' oid='%d'/>\n"; const char* statement_format_text = " $%d %s %s %s %d->%d->%d\n"; const char* statement_format_json = " { \"id\":%d, \"subject\":\"%s\", \"predicate\":\"%s\", \"object\":\"%s\", \"sid\":%d, \"pid\":%d, \"oid\":%d}"; const char* statement_format_csv = "%d\t%s\t%s\t%s\t%d\t%d\t%d\n"; const char* statement_format = 0; if (format == xml)statement_format = statement_format_xml; if (format == html)statement_format = statement_format_json; if (format == json)statement_format = statement_format_json; if (format == txt)statement_format = statement_format_text; if (format == csv)statement_format = statement_format_csv; const char* entity_format = 0; const char* entity_format_txt = "%s #%d (statements:%d) %s\n"; const char* entity_format_xml = "<entity name=\"%s\" id='%d' statementCount='%d' description='%s'>\n"; const char* entity_format_json = " {\"name\":\"%s\", \"id\":%d, \"statementCount\":%d, \"description\":\"%s\""; const char* entity_format_csv = "%s\t%d\t%d\t%s\n"; if(all.size()==1)entity_format_csv = "";//statements! if (format == xml)entity_format = entity_format_xml; if (format == txt)entity_format = entity_format_txt; if (format == csv)entity_format = entity_format_csv; if (use_json) entity_format = entity_format_json; Node* last=0; warnings=0; char* entity=0; if(startsWith(q,"all")){ entity=(char*)cut_to(q," "); entity=keep_to(entity,"limit"); } sortNodes(all); int count=(int)all.size(); int good=0; for (int i = 0; i < count && i<resultLimit; i++) { Node* node = (Node*) all[i]; if(!checkNode(node))continue; if(node->id==0)continue; if(last==node)continue; if(eq(node->name,"◊"))continue; last=node; if(verbosity ==normal && entity&& eq(entity,node->name))continue; char* text=getText(node); // if(use_json && get_topic){ // if(empty(text))continue;//! no description = no entity? BAD for amazon etc // if(isAbstract(node))continue; // N t=getTopic(node); // } good++; if (use_json)if(good>1)Writeline(conn, "},\n"); sprintf(buff, entity_format, node->name, node->id,node->statementCount,text); Writeline(conn, buff); // if(verbosity != alle && !get_topic) // loadView(node); bool got_topic=false; if(use_json && get_topic){ N c=getClass(node); N t=getTopic(node); N ty=getType(node); // if(!c)c=t; if(!t)t=ty; if(t==node)t=ty; if(t!=Entity && checkNode(t)){ got_topic=true; Writeline(conn, ",\n\t \"topicid\":"+itoa(t->id)); Writeline(conn, ", \"topic\":\""+string(t->name)+"\""); } if(c && c!=t){ Writeline(conn, ",\n\t \"classid\":"+itoa(c->id)); Writeline(conn, ", \"class\":\""+string(c->name)+"\""); } if(ty&& c!=ty && ty!=t){ Writeline(conn, ",\n\t \"typeid\":"+itoa(ty->id)); Writeline(conn, ", \"type\":\""+string(ty->name)+"\""); } } if(use_json)// && (verbosity==verbose||verbosity==shorter))// lol // just name Writeline(conn, ", \"kind\":"+itoa(node->kind)); if((use_json)&&!showExcludes&&node->statementCount>1 && getImage(node)!="") Writeline(", \"image\":\""+replace_all(replace_all(getImage(node,150,/*thumb*/true),"'","%27"),"\"","%22")+"\""); // if((use_json)&&getText(node)[0]!=0) // Writeline(", \"description\":\""+string(getText(node))+"\""); Statement* s = 0; if (format==csv|| verbosity == verbose || verbosity == longer|| verbosity == alle || showExcludes || ( all.size() == 1 && !(verbosity == shorter))) { int count=0; // Writeline(",image:\""+getImage(node->name)+"\""); if (use_json)Writeline(conn, ",\n\t \"statements\":[\n"); // sortStatements( deque<Statement*> statements;// sort while ((s = nextStatement(node, s))&&count++<lookupLimit){// resultLimit if (!checkStatement(s))break; // if(!got_topic &&( s->predicate==_Type|| s->predicate==_SuperClass)){ // addStatementToNode(node, s->id(), true);// next time // } if(get_topic &&!got_topic && verbosity != verbose && (s->predicate>100 || s->predicate<-100)) continue;// only important stuff here! // filter statements if(s->object==0)continue; // if(eq(s->Predicate()->name,"Offizielle Website") && !contains(s->Object()->name,"www")) // continue; if (s->subject==node->id and s->predicate!=4)//_instance statements.push_front(s); else statements.push_back(s); } // if(get_topic && verbosity!=shorter){ // NV topics=getTopics(node); // N s=topics[0]; // for (int j = 0; j < topics.size() && j<=resultLimit; j++) { // N s=topics[j]; // Temporary statement (node,topic,s) // statements.push_front(s); // } // } int good=0; for (int j = 0; j < statements.size() && j<=resultLimit; j++) { s=statements.at(j); // while ((s = nextStatement(node, s))&&count++<resultLimit) { if(format==csv&&all.size()>1)break;// entities vs statements p(s); if(verbosity!=alle&&checkHideStatement(s)){warnings++;continue;} fixLabels(s); if(!(verbosity==verbose||verbosity==alle) && (s->Predicate()==Instance||s->Predicate()==Type))continue; if(use_json && good>0)Writeline(conn, ",\n"); char* objectName=s->Object()->name; if(s->Predicate()==Instance){ N type=findProperty(s->Object(),Type->name,0,50); if( checkNode(type)) objectName=(char*)(concat(concat(objectName, ": "),type->name)); } sprintf(buff, statement_format, s->id(), s->Subject()->name, s->Predicate()->name, objectName, s->Subject()->id, s->Predicate()->id, s->Object()->id); Writeline(conn, buff); good++; } if (use_json)Writeline(conn, "]"); } if (format == xml)Writeline(conn, "</entity>\n"); // string img=getImage(node->name); // if(img!="")Writeline(conn,"<img src=\""+img+"\"/>"); } if (use_json || format == html || format == js)Writeline(conn,good>0?"}\n]}":"]}"); if (format == xml)Writeline(conn, "</results>\n"); if(format == js)Writeline(conn, ")");// jsonp const char* html_end=";\n</script>\n<script src='http://pannous.net/netbase.js'></script></body></html>\n"; if(format == html)Writeline(conn, html_end); // sprintf(buff, "<script src='/js/%s'></script>",q0); // Writeline(conn, buff); // } pf("Warnings/excluded: %d\n",warnings); return 0;// 0K }