Exemple #1
0
 public CodePiece Parse(string code)
 {
     var p = new JSReader(code);
     var block = new CodeBlock();
     ReadCodes(p, ref block);
     return block;
 }
Exemple #2
0
 private void AddCode(ref CodeBlock block, CodePiece code, bool insert = false)
 {
     if (block == null)
         block = new CodeBlock();
     if (block.Codes == null)
         block.Codes = new List<CodePiece>();
     if (insert)
         block.Codes.Insert(0, code);
     else
         block.Codes.Add(code);
 }
Exemple #3
0
 private bool ReadSwitch(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("switch"))
     {
         var jsswitch = new SwitchCode();
         ReqText(p, "(");
         ReqVal(p, out jsswitch.Arg);
         ReqText(p, ")");
         ReqText(p, "{");
         while (ReadCase(p, jsswitch)) ;
         if (p.ThisWord("default"))
         {
             ReqText(p, ":");
             ReadCodes(p, ref jsswitch.Default);
         }
         ReqText(p, "}");
         AddCode(ref block, jsswitch);
         return true;
     }
     return false;
 }
Exemple #4
0
 private bool ReadReturn(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("return"))
     {
         var jsret = new ReturnCode();
         ReadValue(p, out jsret.Arg);
         AddCode(ref block, jsret);
         return true;
     }
     return false;
 }
Exemple #5
0
 private bool ReadIf(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("if"))
     {
         var jsif = new IfCode();
         ReqText(p, "(");
         ReqVal(p, out jsif.Condition);
         ReqText(p, ")");
         ReadCode(p, ref jsif.Then);
         if (p.ThisWord("else"))
             ReadCode(p, ref jsif.Else);
         AddCode(ref block, jsif);
         return true;
     }
     return false;
 }
Exemple #6
0
 private bool ReadFunctionDecl(StringReader p, ref CodeBlock block)
 {
     FunctionCode funcRead;
     if (ReadFunctionBody(p, true, out funcRead))
     {
         AddCode(ref block, funcRead, true);
         return true;
     }
     return false;
 }
Exemple #7
0
 private bool ReadBreak(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("break"))
     {
         AddCode(ref block, new BreakCode());
         return true;
     }
     return false;
 }
Exemple #8
0
 private bool ReadVar(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("var"))
     {
     ANOTHER:
         var jsvar = new VarCode();
         Expect(p, ReadIdent(p, out jsvar.Name), "var name");
         if (p.ThisText("="))
             ReqVal(p, out jsvar.Value);
         AddCode(ref block, jsvar);
         if (p.ThisText(","))
             goto ANOTHER;
         return true;
     }
     return false;
 }
Exemple #9
0
 private bool ReadDo(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("do"))
     {
         var jsdo = new UntilCode();
         ReqText(p, "{");
         ReadCodes(p, ref jsdo.Code);
         ReqText(p, "}");
         Expect(p, p.ThisWord("while"), "while");
         ReqText(p, "(");
         ReqVal(p, out jsdo.Condition);
         ReqText(p, ")");
         AddCode(ref block, jsdo);
         return true;
     }
     return false;
 }
Exemple #10
0
 private bool ReadDelete(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("delete"))
     {
         ExprPiece objRef;
         ReqVal(p, out objRef);
         Expect(p, objRef is DotExpr || objRef is NamedExpr || objRef is IndexExpr, "delete expr");
         var jsdel = new DeleteCode();
         jsdel.ObjectRef = objRef;
         AddCode(ref block, jsdel);
         return true;
     }
     return false;
 }
Exemple #11
0
 private bool ReadContinue(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("continue"))
     {
         AddCode(ref block, new ContinueCode());
         return true;
     }
     return false;
 }
Exemple #12
0
 private bool ReadCodes(StringReader p, ref CodeBlock block)
 {
     var readCount = 0;
     while (ReadCode(p, ref block))
         readCount++;
     return readCount > 0;
 }
Exemple #13
0
 private bool ReadCode(StringReader p, ref CodeBlock block)
 {
     var lret = ReadBlock(p, ref block) || ReadIf(p, ref block) || ReadWhile(p, ref block) ||
         ReadDo(p, ref block) || ReadFunctionDecl(p, ref block) || ReadDelete(p, ref block) || ReadTry(p, ref block) ||
         ReadVar(p, ref block) || ReadForEach(p, ref block) || ReadFor(p, ref block) || ReadSwitch(p, ref block) ||
         ReadBreak(p, ref block) || ReadReturn(p, ref block) || ReadContinue(p, ref block) || ReadCall(p, ref block);
     if (lret)
         p.ThisText(";");
     return lret;
 }
Exemple #14
0
 private bool ReadCall(StringReader p, ref CodeBlock block)
 {
     ExprPiece value;
     var saved = p.Position;
     if (ReadValue(p, out value))
     {
         if (value is ICallable)
         {
             AddCode(ref block, new CallCode { Arg = value });
             return true;
         }
         else
             p.Position = saved;
     }
     return false;
 }
Exemple #15
0
 private bool ReadTry(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("try"))
     {
         var jstry = new TryCode();
         ReqText(p, "{");
         ReadCodes(p, ref jstry.Code);
         ReqText(p, "}");
         int catchCount = 0;
         int finallyCount = 0;
         if (p.ThisWord("catch"))
         {
             ReqText(p, "(");
             ReadIdent(p, out jstry.CatchName);
             ReqText(p, ")");
             ReqText(p, "{");
             ReadCodes(p, ref jstry.Catch);
             ReqText(p, "}");
             catchCount++;
         }
         if (p.ThisWord("finally"))
         {
             ReqText(p, "{");
             ReadCodes(p, ref jstry.Finally);
             ReqText(p, "}");
             finallyCount++;
         }
         Expect(p, catchCount > 0 || finallyCount > 0, "catch/finally");
         AddCode(ref block, jstry);
         return true;
     }
     return false;
 }
Exemple #16
0
 private bool ReadFor(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("for"))
     {
         var jsfor = new ForCode();
         ReqText(p, "(");
         ReadForInitialization(p, jsfor);
         ReqText(p, ";");
         ReadForCondition(p, jsfor);
         ReqText(p, ";");
         ReadForIncrement(p, jsfor);
         ReqText(p, ")");
         ReadCode(p, ref jsfor.Code);
         AddCode(ref block, jsfor);
         return true;
     }
     return false;
 }
Exemple #17
0
 private bool ReadForEach(StringReader p, ref CodeBlock block)
 {
     var saved = p.Position;
     if (p.ThisWord("for"))
     {
         var jsfor = new ForeachCode();
         ReqText(p, "(");
         if (p.ThisWord("var"))
             if (ReadIdent(p, out jsfor.VarName))
                 if (p.ThisWord("in"))
                 {
                     ReqVal(p, out jsfor.SetExpr);
                     ReqText(p, ")");
                     ReadCode(p, ref jsfor.Code);
                     AddCode(ref block, jsfor);
                     return true;
                 }
         p.Position = saved;
     }
     return false;
 }
Exemple #18
0
 private bool ReadWhile(StringReader p, ref CodeBlock block)
 {
     if (p.ThisWord("while"))
     {
         var jswhile = new WhileCode();
         ReqText(p, "(");
         ReqVal(p, out jswhile.Condition);
         ReqText(p, ")");
         ReadCode(p, ref jswhile.Code);
         AddCode(ref block, jswhile);
         return true;
     }
     return false;
 }
Exemple #19
0
 private bool ReadBlock(StringReader p, ref CodeBlock block)
 {
     if (p.ThisText("{"))
     {
         ReadCodes(p, ref block);
         ReqText(p, "}");
         return true;
     }
     return false;
 }