Esempio n. 1
0
 private bool ReadFunctionBody(StringReader p, bool reqName, out FunctionCode func)
 {
     var saved = p.Position;
     if (p.ThisWord("function"))
     {
         var jsfn = new FunctionCode();
         // esse Reader pode ser chamado para declaracao de funcoes ou para expressoes de funcoes anonimas
         // reqName diferencia esses dois usos, um tem nome o outro nao
         // ex.: function X(){} ou function(){}
         if (reqName)
             Expect(p, ReadIdent(p, out jsfn.Name), "func name");
         ReqText(p, "(");
         var plist = new List<string>();
         string pn;
         while (ReadIdent(p, out pn))
         {
             plist.Add(pn);
             if (!p.ThisText(","))
                 break;
         }
         jsfn.Params = plist.Count > 0 ? plist.ToArray() : null;
         ReqText(p, ")");
         ReqText(p, "{");
         ReadCodes(p, ref jsfn.Code);
         ReqText(p, "}");
         func = jsfn;
         return true;
     }
     func = null;
     return false;
 }
Esempio n. 2
0
 private bool ReadClosure(StringReader p, out FunctionCode func)
 {
     var saved = p.Position;
     var plist = new List<string>();
     string pn;
     if (p.ThisText("("))
     {
         while (ReadIdent(p, out pn))
         {
             plist.Add(pn);
             if (!p.ThisText(","))
                 break;
         }
         if (!p.ThisText(")"))
             goto Mistaken;
     }
     else if (ReadIdent(p, out pn))
         plist.Add(pn);
     if (p.ThisText(":"))
     {
         var jsfn = new FunctionCode();
         jsfn.Params = plist.Count > 0 ? plist.ToArray() : null;
         if (p.ThisText("{"))
         {
             ReadCodes(p, ref jsfn.Code);
             ReqText(p, "}");
         }
         else
         {
             ExprPiece arg;
             ReqVal(p, out arg);
             AddCode(ref jsfn.Code, new ReturnCode { Arg = arg });
         }
         func = jsfn;
         return true;
     }
     Mistaken:
     p.Position = saved;
     func = null;
     return false;
 }