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; }
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; }
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; }
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; }
private bool ReadSymbolValue(StringReader p, out ExprPiece value) { int wordRead; if (p.ThisWord(new[] { "null", "true", "false" }, out wordRead)) switch (wordRead) { case 0: value = new LiteralConst { Literal = null }; return true; case 1: value = new LiteralConst { Literal = true }; return true; case 2: value = new LiteralConst { Literal = false }; return true; } value = null; return false; }
private bool ReadNewValue(StringReader p, out ExprPiece value) { if (p.ThisWord("new")) { ExprPiece creator; Expect(p, ReadNamedValue(p, out creator) || ReadFloatValue(p, out creator) || ReadStringValue(p, out creator) || ReadParensValue(p, out creator) || ReadArrayValue(p, out creator) || ReadObjectValue(p, out creator), "new arg"); while (ReadMemberValue(p, ref creator)) if (creator is ParamsExpr) break; value = new NewExpr { Creator = creator }; return true; } value = null; return false; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
private bool ReadContinue(StringReader p, ref CodeBlock block) { if (p.ThisWord("continue")) { AddCode(ref block, new ContinueCode()); return true; } return false; }
private bool ReadCase(StringReader p, SwitchCode jsswitch) { if (p.ThisWord("case")) { var jscase = new SwitchCaseCode(); ReqVal(p, out jscase.Test); ReqText(p, ":"); ReadCodes(p, ref jscase.Code); (jsswitch.Cases ?? (jsswitch.Cases = new List<SwitchCaseCode>())).Add(jscase); return true; } return false; }
private bool ReadBreak(StringReader p, ref CodeBlock block) { if (p.ThisWord("break")) { AddCode(ref block, new BreakCode()); return true; } return false; }