private bool ReadArrayValue(StringReader p, out ExprPiece value) { if (p.ThisText("[")) { var list = new List<ExprPiece>(); ExprPiece item; while (ReadValue(p, out item)) { list.Add(item); if (!p.ThisText(",")) break; } ReqText(p, "]"); value = new ArrayExpr { Items = list.ToArray() }; return true; } value = null; return false; }
private bool ReadValue(StringReader p, out ExprPiece value) { OperationExpr op; var mixture = new List<object>(); if (!ReadValueItem(p, mixture)) { value = null; return false; } while (ReadBinaryOp(p, out op)) { mixture.Add(op); Expect(p, ReadValueItem(p, mixture), "mix item"); } value = SplitOperands(mixture, 0, mixture.Count); return true; }
private void ReqVal(StringReader p, out ExprPiece val) { if (!ReadValue(p, out val)) throw new Exception("value expected" + "\n" + p.RestOf); }
private bool ReadStringValue(StringReader p, out ExprPiece value) { string s; if (p.AnyQuoted(out s)) { value = new LiteralConst { Literal = s }; return true; } value = null; 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 ReadParensValue(StringReader p, out ExprPiece value) { if (p.ThisText("(")) { var parens = new ParensExpr(); ReadValue(p, out parens.InnerExpr); ReqText(p, ")"); value = parens; return true; } value = null; return false; }
private bool ReadSimpleValue(StringReader p, out ExprPiece value) { if (ReadFunctionValue(p, out value) || ReadSymbolValue(p, out value) || ReadNewValue(p, out value) || ReadNamedValue(p, out value) || ReadFloatValue(p, out value) || ReadStringValue(p, out value) || ReadParensValue(p, out value) || ReadArrayValue(p, out value) || ReadObjectValue(p, out value)) { while (ReadMemberValue(p, ref value)) ; return true; } value = null; return false; }
private bool ReadObjectValue(StringReader p, out ExprPiece value) { if (p.ThisText("{")) { var obj = new ObjectExpr(); while (ReadValuePair(p, obj)) if (!p.ThisText(",")) break; ReqText(p, "}"); value = obj; return true; } value = null; return false; }
private bool ReadParamsMember(StringReader p, ref ExprPiece value) { ParamsExpr param; if (ReadParamsExpr(p, out param)) { param.FuncExpr = value; value = param; return true; } return false; }
private bool ReadNamedValue(StringReader p, out ExprPiece value) { string aux; if (ReadIdent(p, out aux)) { value = new NamedExpr { Name = aux }; 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 ReadMemberValue(StringReader p, ref ExprPiece value) { return ReadDotMember(p, ref value) || ReadIndexMember(p, ref value) || ReadParamsMember(p, ref value); }
private bool ReadIndexMember(StringReader p, ref ExprPiece value) { if (p.ThisText("[")) { var index = new IndexExpr(); index.Array = value; ReqVal(p, out index.Index); ReqText(p, "]"); value = index; return true; } return false; }
private bool ReadFunctionValue(StringReader p, out ExprPiece value) { FunctionCode funcRead; if (ReadFunctionBody(p, false, out funcRead) || ReadClosure(p, out funcRead)) { value = new ClosureExpr { Function = funcRead }; return true; } value = null; return false; }
private bool ReadFloatValue(StringReader p, out ExprPiece value) { var saved = p.Position; p.Skip(); string f; // sinal char sig; if (p.ThisCharNoSkip("+-", out sig)) f = sig.ToString(); else f = ""; bool hasint = false, hasfrac = false, hasexp = false; // int part string intpart; if (p.ThisSetNoSkip("0123456789", out intpart)) { f += intpart; hasint = true; } else f += "0"; var befpt = p.Position; // frac part if (p.ThisCharNoSkip('.')) { string fracpart; if (p.ThisSetNoSkip("0123456789", out fracpart)) { f += "." + fracpart; hasfrac = true; } if (!hasfrac) p.Position = befpt; } // exp part if (hasint || hasfrac) { var befexp = p.Position; if (p.ThisCharNoSkip("eE")) { char expsig; if (p.ThisCharNoSkip("+-", out expsig)) { string exppart; if (p.ThisSetNoSkip("0123456789", out exppart)) { f += "e" + expsig.ToString() + exppart; hasexp = true; } } if (!hasexp) p.Position = befexp; } var lit = new LiteralConst(); int ival; if (hasfrac) { var comma = 1.1.ToString()[1]; f = f.Replace('.', comma); lit.Literal = double.Parse(f); } else if (int.TryParse(f, out ival)) lit.Literal = ival; else lit.Literal = long.Parse(f); value = lit; return true; } p.Position = saved; value = null; return false; }
private bool ReadDotMember(StringReader p, ref ExprPiece value) { var saved = p.Position; if (p.ThisText(".")) { string nameRead; if (ReadIdent(p, out nameRead)) { value = new DotExpr { MemberName = nameRead, LeftArg = value }; return true; } p.Position = saved; } return false; }