string JSFormater::fObject(){ string rtn="{"; get_next_token(); if(is_tag(cur_token,"}")) return rtn.append("}"); rtn.append(fId()); if(!is_tag(cur_token,":")) throw 1; else{ rtn.append(" : "); get_next_token(); } rtn.append(fAssignExpr()); while(is_tag(cur_token,",")){ rtn.append(", "); get_next_token(); rtn.append(fId()); if(!is_tag(cur_token,":")) throw 1; else{ rtn.append(" : "); get_next_token(); } rtn.append(fAssignExpr()); } rtn.push_back('}'); get_next_token(); return rtn; }
string JSFormater::fDeclare(){ string rtn="var "; get_next_token(); rtn.append(fId()); bool inl1,inl2=false; if(is_tag(cur_token,"=")){ get_next_token(); rtn.append(" = ").append(fAssignExpr()); inl1=true; } while(is_tag(cur_token,",")){ string tmp_v; rtn.push_back(','); get_next_token(); tmp_v.append(fId()); if(is_tag(cur_token,"=")){ inl2=true; get_next_token(); tmp_v.append(" = ").append(fAssignExpr()); }else inl2=false; if(inl1==true || inl2==true){ rtn.append("\n "); }else rtn.push_back(' '); rtn.append(tmp_v); inl1=inl2, inl2=false; } if(is_tag(cur_token,";")){ rtn.append(";"); get_next_token(); } return rtn; }
string JSFormater::fExpr(){ string rtn; rtn.append(fAssignExpr()); while(is_tag(cur_token,",")){ get_next_token(); rtn.append(fAssignExpr()); } if(is_tag(cur_token,";")){ rtn.push_back(';'); get_next_token(); } return rtn; }
string JSFormater::fFunction(){ string rtn="function "; get_next_token(); if(is_tag(cur_token,"id")){ rtn.append(fId()); } if(is_tag(cur_token,"(")){ rtn.push_back('('); get_next_token(); if(!is_tag(cur_token,")")){ while(true){ if(is_tag(cur_token,"id")){ rtn.append(cur_token.value); get_next_token(); }else throw 1; if(is_tag(cur_token,",")){ rtn.append(", "); get_next_token(); }else if(is_tag(cur_token,")")) break; else throw 1; } } rtn.push_back(')'); get_next_token(); } if(is_tag(cur_token,"{")){ rtn.append(fBlock()); }else throw 1; return rtn; }
string JSFormater::fId(){ string rtn=cur_token.value; if(!is_tag(cur_token,"id")) throw 1; else{ get_next_token(); return rtn; } }
string JSFormater::fStatement(){ if(is_tag(cur_token,"function")){ return fFunction(); }else if(is_tag(cur_token,"var")){ return fDeclare(); }else if(is_tag(cur_token,"for")){ return fFor(); } //... do while... else if(is_tag(cur_token,"{")){ return fBlock(); }else if(is_tag(cur_token,";")){ string rtn=cur_token.value; get_next_token(); return rtn; }else{ return fExpr(); } }
string JSFormater::fBlock(){ string rtn="{"; get_next_token(); while(!is_tag(cur_token,"}")){ rtn.append(fStatement()); } rtn.append("}"); get_next_token(); return rtn; }
string JSFormater::fFor(){ string rtn="for"; if(!is_tag(get_next_token(),"(")) throw 1; rtn.push_back('('); if(is_tag(get_next_token(),"var")){ rtn.append(fDeclare()); }else{ rtn.append(fExpr()); } if(is_tag(cur_token,";")) rtn.push_back(';'); else throw 1; rtn.append(fAssignExpr()); if(is_tag(cur_token,";")) rtn.push_back(';'); else throw 1; }
string JSFormater::fArray(){ string rtn="["; get_next_token(); if(is_tag(cur_token,"]")){ rtn.push_back(']'); get_next_token(); return rtn; } rtn.append(fAssignExpr()); while(is_tag(cur_token,",")){ rtn.append(", "); get_next_token(); rtn.append(fAssignExpr()); } if(!is_tag(cur_token,"]")) throw 1; else{ rtn.push_back(']'); get_next_token(); } return rtn; }
string JSFormater::fPriExpr(){ string rtn; if(is_tag(cur_token,"function")){ return fFunction(); }else if(is_tag(cur_token,"{")){ return fObject(); }else if(is_tag(cur_token,"[")){ return fArray(); }else if(is_tag(cur_token,"(")){ rtn.append("("); get_next_token(); rtn.append(fExpr()); if(!is_tag(cur_token,")")) throw 1; else{ rtn.append(")"); get_next_token(); } }else{ rtn=cur_token.value; get_next_token(); } return rtn; }
string JSFormater::fAssignExpr(){ string rtn; static const char* tmp0[9]={"+","-","~","!","delete","typeof", "void","++","--"}; if(is_tags(cur_token,tmp0,9)){ rtn.append(cur_token.value); get_next_token(); } rtn.append(fLeftExpr()); static const char* tmp1[32]={"=","*=","/=","%=","+=", "-=","^=","&=","|=",">>=",">>>=","<<=", "+","-","*","/","&","%","|" ,"<<",">>",">>>","<","<=",">",">=", "instanceof","in","==","!=","===","!=="}; if(is_tag(cur_token,"++")||is_tag(cur_token,"--")){ rtn.append(cur_token.value); get_next_token(); } else if(is_tags(cur_token,tmp1,32)){ rtn.append(" ").append(cur_token.value).append(" "); get_next_token(); rtn.append(fAssignExpr()); }else if(is_tag(cur_token,"?")){ rtn.append(" ? "); get_next_token(); rtn.append(fAssignExpr()); if(!is_tag(cur_token,":")) throw 1; else get_next_token(); rtn.append(" : "); rtn.append(fAssignExpr()); } return rtn; }
string JSFormater::fLeftExpr(){ string rtn; while(is_tag(cur_token,"new")){ rtn.append("new "); get_next_token(); } rtn.append(fPriExpr()); while(true){ if(is_tag(cur_token,"(")){ rtn.append("("); get_next_token(); if(!is_tag(cur_token,")")){ rtn.append(fAssignExpr()); while(is_tag(cur_token,",")){ rtn.append(", "); get_next_token(); rtn.append(fAssignExpr()); } } if(!is_tag(cur_token,")")) throw 1; else{ rtn.append(")"); get_next_token(); } }else if(is_tag(cur_token,".")){ get_next_token(); rtn.append(".").append(fId()); }else if(is_tag(cur_token,"[")){ rtn.append("["); get_next_token(); rtn.append(fExpr()); if(!is_tag(cur_token,"]")) throw 1; else{ rtn.append("]"); get_next_token(); } }else break; } return rtn; }
void mustache_render(pmustache m) { char c; if (text_parsed_init(m) != true) { perror("Unable to initialize text_parsed\n"); exit(-1); } while ((c = m->text_get_char(m)) != EOF) { //Avoid passing spaces to is_tag if (isspace(c) || is_tag(m, &c) == false) { text_parsed_add_char(m, &c); } } //while }
OBJECT InsertSym(FULL_CHAR *str, unsigned char xtype, FILE_POS *xfpos, unsigned char xprecedence, BOOLEAN xindefinite, BOOLEAN xrecursive, unsigned xpredefined, OBJECT xenclosing, OBJECT xbody) { register int sum, rlen; register unsigned char *x; OBJECT p, q, s, tmp, link, entry, plink; int len; debug3(DST, DD, "InsertSym( %s, %s, in %s )", Image(xtype), str, SymName(xenclosing)); if( !LexLegalName(str) ) Error(29, 3, "invalid symbol name %s", WARN, xfpos, str); New(s, xtype); FposCopy(fpos(s), *xfpos); has_body(s) = FALSE; filter(s) = nilobj; use_invocation(s) = nilobj; imports(s) = nilobj; imports_encl(s) = FALSE; right_assoc(s) = TRUE; precedence(s) = xprecedence; indefinite(s) = xindefinite; recursive(s) = xrecursive; predefined(s) = xpredefined; enclosing(s) = xenclosing; sym_body(s) = xbody; base_uses(s) = nilobj; uses(s) = nilobj; marker(s) = nilobj; cross_sym(s) = nilobj; is_extern_target(s) = FALSE; uses_extern_target(s)= FALSE; visible(s) = FALSE; uses_galley(s) = FALSE; horiz_galley(s) = ROWM; has_compulsory(s) = 0; is_compulsory(s) = FALSE; uses_count(s) = 0; dirty(s) = FALSE; if( enclosing(s) != nilobj && type(enclosing(s)) == NPAR ) dirty(s) = dirty(enclosing(s)) = TRUE; has_par(s) = FALSE; has_lpar(s) = FALSE; has_rpar(s) = FALSE; if( is_par(type(s)) ) has_par(enclosing(s)) = TRUE; if( type(s) == LPAR ) has_lpar(enclosing(s)) = TRUE; if( type(s) == RPAR ) has_rpar(enclosing(s)) = TRUE; /* assign a code letter between a and z to any NPAR symbol */ if( type(s) == NPAR ) { if( LastDown(enclosing(s)) != enclosing(s) ) { Child(tmp, LastDown(enclosing(s))); if( type(tmp) == NPAR ) { if( npar_code(tmp) == 'z' || npar_code(tmp) == ' ' ) npar_code(s) = ' '; else npar_code(s) = npar_code(tmp)+1; } else npar_code(s) = 'a'; } else npar_code(s) = 'a'; } has_target(s) = FALSE; force_target(s) = FALSE; if( !StringEqual(str, KW_TARGET) ) is_target(s) = FALSE; else { is_target(s) = has_target(enclosing(s)) = TRUE; /* if @Target is found after @Key, take note of external target */ if( has_key(enclosing(s)) && xbody != nilobj && is_cross(type(xbody)) ) { if( LastDown(xbody) != Down(xbody) ) { OBJECT sym; Child(sym, Down(xbody)); if( type(sym) == CLOSURE ) { is_extern_target(actual(sym)) = TRUE; uses_extern_target(actual(sym)) = TRUE; } } } } has_tag(s) = is_tag(s) = FALSE; has_key(s) = is_key(s) = FALSE; has_optimize(s) = is_optimize(s) = FALSE; has_merge(s) = is_merge(s) = FALSE; has_enclose(s) = is_enclose(s) = FALSE; if( enclosing(s) != nilobj && type(enclosing(s)) == LOCAL ) { if( StringEqual(str, KW_TAG) ) is_tag(s) = has_tag(enclosing(s)) = dirty(enclosing(s)) = TRUE; if( StringEqual(str, KW_OPTIMIZE) ) is_optimize(s) = has_optimize(enclosing(s)) = TRUE; if( StringEqual(str, KW_KEY) ) { is_key(s) = has_key(enclosing(s)) = dirty(enclosing(s)) = TRUE; /* if @Key is found after @Target, take note of external target */ for( link=Down(enclosing(s)); link!=enclosing(s); link=NextDown(link) ) { Child(p, link); if( is_target(p) && sym_body(p)!=nilobj && is_cross(type(sym_body(p))) ) { OBJECT sym; Child(sym, Down(sym_body(p))); if( type(sym) == CLOSURE ) { is_extern_target(actual(sym)) = TRUE; uses_extern_target(actual(sym)) = TRUE; } } } } if( StringEqual(str, KW_MERGE) ) is_merge(s) = has_merge(enclosing(s)) = TRUE; if( StringEqual(str, KW_ENCLOSE) ) is_enclose(s) = has_enclose(enclosing(s)) = TRUE; } if( StringEqual(str, KW_FILTER) ) { if( type(s) != LOCAL || enclosing(s) == StartSym ) Error(29, 4, "%s must be a local definition", WARN, &fpos(s), str); else if( !has_rpar(enclosing(s)) ) Error(29, 14, "%s must lie within a symbol with a right parameter", WARN, &fpos(s), KW_FILTER); else { filter(enclosing(s)) = s; precedence(enclosing(s)) = FILTER_PREC; } } if( type(s) == RPAR && has_body(enclosing(s)) && (is_tag(s) || is_key(s) || is_optimize(s)) ) Error(29, 5, "a body parameter may not be named %s", WARN, &fpos(s), str); if( type(s) == RPAR && has_target(enclosing(s)) && (is_tag(s) || is_key(s) || is_optimize(s)) ) Error(29, 6, "the right parameter of a galley may not be called %s", WARN, &fpos(s), str); len = StringLength(str); hash(str, len, sum); ifdebug(DST, D, sym_spread[sum]++; sym_count++); entry = (OBJECT) &symtab[sum]; for( plink = Down(entry); plink != entry; plink = NextDown(plink) ) { Child(p, plink); if( length(p) == len && StringEqual(str, string(p)) ) { for( link = Down(p); link != p; link = NextDown(link) ) { Child(q, link); if( enclosing(s) == enclosing(q) ) { Error(29, 7, "symbol %s previously defined at%s", WARN, &fpos(s), str, EchoFilePos(&fpos(q)) ); if( AltErrorFormat ) { Error(29, 13, "symbol %s previously defined here", WARN, &fpos(q), str); } break; } } goto wrapup; } } /* need a new OBJECT as well as s */ NewWord(p, WORD, len, xfpos); length(p) = len; StringCopy(string(p), str); Link(entry, p); wrapup: Link(p, s); if( enclosing(s) != nilobj ) Link(enclosing(s), s); debug2(DST, DD, "InsertSym Link(%s, %s) and returning.", SymName(enclosing(s)), SymName(s)); return s; } /* end InsertSym */