private bool CreateListFromString(string operation) { tokensTree.Clear(); for (int i = 0; i < operation.Length; i++) { string character = operation[i].ToString(CultureInfo.CurrentCulture); if (TokenDictionaryTree.ContainsKey(character)) { TokenTree n = new TokenTree(TokenDictionaryTree[character]); if (tokensTree.Count > 0) { TokenTree previousToken = tokensTree[tokensTree.Count - 1]; if (previousToken.TypeToken == TypeTokenTree.Number && n.TypeToken == TypeTokenTree.Number) { previousToken.ExpressionToken += n.ExpressionToken; previousToken.ValToken = double.Parse(previousToken.ExpressionToken, CultureInfo.CurrentCulture); } else { tokensTree.Add(n); } } else { tokensTree.Add(n); } } else { return(false); } } return(true); }
public void single_FunctionCall___ResultExpression_only() { var tokenTree = new TokenTree { FunctionCalls = new[] { new FunctionCall { FunctionName = nameof(EmptyFunction) } } }; var functionExpression = new FunctionExpression <EmptyFunction, Domain.FunctionsDomain.Void>(); var expressionGenerator = Substitute.For <IExpressionGenerator>(); expressionGenerator.Generate(tokenTree.FunctionCalls[0]) .Returns(functionExpression); var result = Act(tokenTree, expressionGenerator); result.Should().NotBeNull(); result.SideExpressions.Should().BeEmpty(); result.ResultExpression.Should().BeSameAs(functionExpression); }
private void BuildTreeRecursiv(TokenTree subHead, List <TokenTree> leftPart, List <TokenTree> rightPart) { if (leftPart != null && leftPart.Count > 0) { int indexLeft = IndexOfWorstOp(leftPart); TokenTree leftTokenTree = leftPart[indexLeft]; subHead.SonLeft = leftTokenTree; if (leftPart.Count > indexLeft) { var subTokensRight = leftPart.GetRange(indexLeft + 1, leftPart.Count - indexLeft - 1); var subTokensLeft = leftPart.GetRange(0, indexLeft); BuildTreeRecursiv(subHead.SonLeft, subTokensLeft, subTokensRight); } } if (rightPart != null && rightPart.Count > 0) { int indexRight = IndexOfWorstOp(rightPart); TokenTree rightTokenTree = rightPart[indexRight]; subHead.SonRight = rightTokenTree; if (rightPart.Count > indexRight) { var subTokensRight = rightPart.GetRange(indexRight + 1, rightPart.Count - indexRight - 1); var subTokensLeft = rightPart.GetRange(0, indexRight); BuildTreeRecursiv(subHead.SonRight, subTokensLeft, subTokensRight); } } }
private bool IsOperationSyntaxCorrect(TokenTree currentToken) { bool ret = true; if (currentToken.SonRight == null && currentToken.SonLeft == null) { if (currentToken.TypeToken != TypeTokenTree.Number) { return(false); } } else if (currentToken.SonRight == null && currentToken.SonLeft != null) { if (currentToken.AdvancedTypeToken != AdvancedTypeTokenTree.OpUnaireRight) { return(false); } ret = IsOperationSyntaxCorrect(currentToken.SonLeft); } else if (currentToken.SonRight != null && currentToken.SonLeft == null) { if (currentToken.AdvancedTypeToken != AdvancedTypeTokenTree.OpUnaireLeft) { return(false); } ret = IsOperationSyntaxCorrect(currentToken.SonRight); } else { ret = IsOperationSyntaxCorrect(currentToken.SonLeft) && IsOperationSyntaxCorrect(currentToken.SonRight); } return(ret); }
public void BugFixes() { Expr("(a + b).b<c>()", F.Call(F.Of(F.Dot(F.InParens(F.Call(S.Add, a, b)), b), c))); Stmt("@`+`(a, b)(c, 1);", F.Call(F.Call(S.Add, a, b), c, one)); // was: "c+1" // was "partial #var(Foo, a);" which would be parsed as a method declaration Stmt("([#partial]\n#var(Foo, a));", F.InParens(Attr(@partial, F.Vars(Foo, a)))); Stmt("public partial alt class BinaryTree<T>\n{\n}", F.Attr(F.Public, partialWA, WordAttr("#alt"), F.Call(S.Class, F.Of(F.Id("BinaryTree"), T), F.List(), F.Braces()))); Stmt("partial Foo.T x\n{\n get;\n}", Attr(partialWA, F.Property(F.Dot(Foo, T), x, F.Braces(get)))); Stmt("IFRange<char> ICloneable<IFRange<char>>.Clone()\n{\n return Clone();\n}", F.Fn(F.Of(_("IFRange"), F.Char), F.Dot(F.Of(_("ICloneable"), F.Of(_("IFRange"), F.Char)), _("Clone")), F.List(), F.Braces(F.Call(S.Return, F.Call("Clone"))))); Stmt("Foo<a> IDictionary<a,b>.Keys\n{\n}", F.Property(F.Of(Foo, a), F.Dot(F.Of(_("IDictionary"), a, b), _("Keys")), F.Braces())); Stmt("T IDictionary<Symbol,T>.this[Symbol x]\n{\n get;\n set;\n}", F.Property(T, F.Dot(F.Of(_("IDictionary"), _("Symbol"), T), F.@this), F.List(F.Var(_("Symbol"), x)), F.Braces(get, set))); Stmt("Func<T,T> x = delegate(T a) {\n return a;\n};", F.Var(F.Of(_("Func"), T, T), x, F.Call(S.Lambda, F.List(F.Var(T, a)), F.Braces(F.Call(S.Return, a))).SetBaseStyle(NodeStyle.OldStyle))); Stmt("public static rule EmailAddress Parse(T x)\n{\n}", F.Attr(F.Public, _(S.Static), WordAttr("rule"), F.Fn(_("EmailAddress"), _("Parse"), F.List(F.Var(T, x)), F.Braces()))); // Currently we're not trying to treat this as a keyword Stmt("dynamic Foo();", F.Fn(_("dynamic"), Foo, F.List())); Stmt("dynamic x;", F.Var(_("dynamic"), x)); Token[] token = new[] { new Token((int)TokenType.Literal, 0, 0, 0, 'a') }; var tree = new TokenTree(F.File, (ICollection<Token>)token); Stmt("LLLPG(lexer) {\n public rule a @{ 'a' };\n}", F.Call(_("LLLPG"), _("lexer"), F.Braces( Attr(F.Public, F.Property(_("rule"), a, F.Literal(tree))))).SetBaseStyle(NodeStyle.Special)); }
private double ComputeRecursif(TokenTree subHead) { double retValue = 0; TokenTree leftfSonToken = subHead.SonLeft; TokenTree rightSonToken = subHead.SonRight; string expressionToken = subHead.ExpressionToken; if (leftfSonToken == null && rightSonToken == null) { retValue = subHead.ValToken; } else if (leftfSonToken == null) { retValue += ComputeOperationHelper(expressionToken, 0, ComputeRecursif(rightSonToken)); } else if (rightSonToken == null) { retValue += ComputeOperationHelper(expressionToken, ComputeRecursif(leftfSonToken), 0); } else { retValue += ComputeOperationHelper(expressionToken, ComputeRecursif(leftfSonToken), ComputeRecursif(rightSonToken)); } return(retValue); }
public void BugFixes() { Expr("(a + b).b<c>()", F.Call(F.Of(F.Dot(F.InParens(F.Call(S.Add, a, b)), b), c))); Stmt("@`+`(a, b)(c, 1);", F.Call(F.Call(S.Add, a, b), c, one)); // was: "c+1" // was "partial #var(Foo, a);" which would be parsed as a method declaration Stmt("([#partial]\n#var(Foo, a));", F.InParens(Attr(@partial, F.Vars(Foo, a)))); Stmt("public partial alt class BinaryTree<T>\n{\n}", F.Attr(F.Public, partialWA, WordAttr("#alt"), F.Call(S.Class, F.Of(F.Id("BinaryTree"), T), F.List(), F.Braces()))); Stmt("partial Foo.T x\n{\n get;\n}", Attr(partialWA, F.Property(F.Dot(Foo, T), x, F.Braces(get)))); Stmt("IFRange<char> ICloneable<IFRange<char>>.Clone()\n{\n return Clone();\n}", F.Fn(F.Of(_("IFRange"), F.Char), F.Dot(F.Of(_("ICloneable"), F.Of(_("IFRange"), F.Char)), _("Clone")), F.List(), F.Braces(F.Call(S.Return, F.Call("Clone"))))); Stmt("Foo<a> IDictionary<a,b>.Keys\n{\n}", F.Property(F.Of(Foo, a), F.Dot(F.Of(_("IDictionary"), a, b), _("Keys")), F.Braces())); Stmt("T IDictionary<Symbol,T>.this[Symbol x]\n{\n get;\n set;\n}", F.Property(T, F.Dot(F.Of(_("IDictionary"), _("Symbol"), T), F.@this), F.List(F.Var(_("Symbol"), x)), F.Braces(get, set))); Stmt("Func<T,T> x = delegate(T a) {\n return a;\n};", F.Var(F.Of(_("Func"), T, T), x, F.Call(S.Lambda, F.List(F.Var(T, a)), F.Braces(F.Call(S.Return, a))).SetBaseStyle(NodeStyle.OldStyle))); Stmt("public static rule EmailAddress Parse(T x)\n{\n}", F.Attr(F.Public, _(S.Static), WordAttr("rule"), F.Fn(_("EmailAddress"), _("Parse"), F.List(F.Var(T, x)), F.Braces()))); // Currently we're not trying to treat this as a keyword Stmt("dynamic Foo();", F.Fn(_("dynamic"), Foo, F.List())); Stmt("dynamic x;", F.Var(_("dynamic"), x)); Token[] token = new[] { new Token((int)TokenType.Literal, 0, 0, 0, 'a') }; var tree = new TokenTree(F.File, (ICollection <Token>)token); Stmt("LLLPG(lexer) {\n public rule a @{ 'a' };\n}", F.Call(_("LLLPG"), _("lexer"), F.Braces( Attr(F.Public, F.Property(_("rule"), a, F.Literal(tree))))).SetBaseStyle(NodeStyle.Special)); }
public SyntaxTree Build(TokenTree tokenTree) { TokenListParser parser = new TokenListParser(parseProperties); SyntaxNode root = parser.parse(tokenTree.RootTokens); return(new SyntaxTree(root)); }
private void BuildTree() { int indexWorstOp = IndexOfWorstOp(tokensTree); List <TokenTree> subTokensLeft = tokensTree.GetRange(0, indexWorstOp); List <TokenTree> subTokensRight = tokensTree.GetRange(indexWorstOp + 1, tokensTree.Count - indexWorstOp - 1); head = tokensTree[indexWorstOp]; BuildTreeRecursiv(head, subTokensLeft, subTokensRight); }
public TokenTree(TokenTree token) { ValToken = token.ValToken; TypeToken = token.TypeToken; AdvancedTypeToken = token.AdvancedTypeToken; Priority = token.Priority; ExpressionToken = token.ExpressionToken; SonLeft = null; SonRight = null; }
public TokenTree(double valueToken, TypeTokenTree typeToken, AdvancedTypeTokenTree advancedTypeToken, int priority, string expressionToken) { ValToken = valueToken; TypeToken = typeToken; AdvancedTypeToken = advancedTypeToken; Priority = priority; ExpressionToken = expressionToken; SonLeft = null; SonRight = null; }
private static void ReplaceAt(ref bool modified, ref TokenTree tokens, int i, Token newToken) { // Do not modify the token tree directly since it's stored in a // (supposed-to-be-immutable) LNode if (!modified) { modified = true; tokens = tokens.Clone(true); } tokens[i] = newToken; }
public void BugFixes() { LNode stmt; Expr("(a + b).b<c>()", F.Call(F.Dot(F.InParens(F.Call(S.Add, a, b)), F.Of(b, c)))); Stmt("@`'+`(a, b)(c, 1);", F.Call(F.Call(S.Add, a, b), c, one)); // was: "c+1" // once printed as "partial #var(Foo, a);" which would be parsed as a method declaration Stmt("partial Foo a;", Attr(@partialWA, F.Vars(Foo, a))); Stmt("public partial alt class BinaryTree<T> { }", F.Attr(F.Public, partialWA, WordAttr("#alt"), F.Call(S.Class, F.Of(F.Id("BinaryTree"), T), F.AltList(), F.Braces()))); Stmt("partial Foo.T x { get; }", Attr(partialWA, F.Property(F.Dot(Foo, T), x, BracesOnOneLine(get)))); Stmt("IFRange<char> ICloneable<IFRange<char>>.Clone() {\n return Clone();\n}", F.Fn(F.Of(_("IFRange"), F.Char), F.Dot(F.Of(_("ICloneable"), F.Of(_("IFRange"), F.Char)), _("Clone")), F.AltList(), F.Braces(F.Call(S.Return, F.Call("Clone"))))); Stmt("Foo<a> IDictionary<a, b>.Keys { }", F.Property(F.Of(Foo, a), F.Dot(F.Of(_("IDictionary"), a, b), _("Keys")), F.Braces())); Stmt("T IDictionary<Symbol, T>.this[Symbol x] { get; set; }", F.Property(T, F.Dot(F.Of(_("IDictionary"), _("Symbol"), T), F.@this), F.AltList(F.Var(_("Symbol"), x)), BracesOnOneLine(get, set))); Stmt("Func<T, T> x = delegate(T a) {\n return a;\n};", F.Var(F.Of(_("Func"), T, T), x, F.Call(S.Lambda, F.AltList(F.Var(T, a)), F.Braces(F.Call(S.Return, a))).SetBaseStyle(NodeStyle.OldStyle))); Stmt("public static rule EmailAddress Parse(T x) { }", F.Attr(F.Public, _(S.Static), WordAttr("rule"), F.Fn(_("EmailAddress"), _("Parse"), F.AltList(F.Var(T, x)), F.Braces()))); // Currently we're not trying to treat this as a keyword Stmt("dynamic Foo();", F.Fn(_("dynamic"), Foo, F.AltList())); Stmt("dynamic x;", F.Var(_("dynamic"), x)); Token[] token = new[] { new Token((int)TokenType.Literal, 0, 0, 0, 'a') }; var tree = new TokenTree(F.File, (ICollection <Token>)token); var lexer = F.Call(_("LLLPG"), _("lexer"), F.Braces( Attr(F.Public, F.Property(_("rule"), a, F.Literal(tree))))).SetBaseStyle(NodeStyle.Special); Stmt("LLLPG (lexer) {\n public rule a @{ 'a' };\n}", lexer, Mode.ParserTest); Stmt("LLLPG (lexer) {\n public rule a => @{ 'a' };\n}", lexer); // 2016-04 bug: ForEachStmt failed to call Up() before returning Stmt("{\n foreach (var x in Foo) { }\n Foo();\n}", F.Braces(F.Call(S.ForEach, F.Vars(F.Missing, x), Foo, F.Braces()), F.Call(Foo))); // 2016-10 bug: `property:` was applied to both attributes stmt = F.Attr(a, F.Call(S.NamedArg, F.Id("property"), b), F.Private, F.Property(F.String, Foo, BracesOnOneLine(get, set))); Stmt("[a, property: b] private string Foo { get; set; }", stmt); Stmt("[a] [property: b] public string Foo { get; set; }", stmt.WithAttrChanged(2, @public), Mode.ParserTest); Stmt("a = (var b = x);", F.Call(S.Assign, a, F.InParens(F.Var(F.Missing, b, x)))); // 2017-01 bug: operator>> and operator<< wouldn't parse // (because there is no dedicated token for >> or <<) stmt = Attr(F.Id(S.Static), F.Fn(F.Int32, Attr(trivia_operator, _(S.Shl)), F.AltList(F.Var(Foo, x), F.Var(F.Int32, a)), F.Braces(F.Call(S.Return, F.Call(S.Shl, x, a))))); Stmt("static int operator<<(Foo x, int a) {\n return x << a;\n}", stmt); stmt = Attr(F.Id(S.Static), F.Fn(F.Int32, Attr(trivia_operator, _(S.Shr)), F.AltList(F.Var(Foo, x), F.Var(F.Int32, a)), F.Braces(F.Call(S.Return, F.Call(S.Shr, x, a))))); Stmt("static int operator>>(Foo x, int a) {\n return x >> a;\n}", stmt); // https://github.com/qwertie/ecsharp/issues/90 Expr("({\n Foo;\n})", F.InParens(F.Braces(Foo))); Stmt("({\n stuff;\n});", F.InParens(F.Braces(_("stuff")))); }
/// <summary> /// Evaluate the function. /// </summary> /// <param name="parameters">The tokens that make up the function parameter list.</param> /// <param name="substitutions">The tokens that can be used for substitutions.</param> /// <param name="isFinal">Whether a result needs to be returned.</param> /// <returns></returns> public override IToken Perform(IToken parameters, TokenTreeList substitutions, bool isFinal) { ListToken listToken = parameters as ListToken; if (listToken == null) throw new Exception($"Next token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; IToken iterandKey = lastList[count - 2]; IToken iterandIndex = null; ListToken iterandList = iterandKey as ListToken; if (iterandList != null) { if (iterandList.Tokens.Count != 2) throw new Exception($"Can only have 1 or 2 iterators for '{ID}'"); iterandIndex = iterandList.Tokens[1]; iterandKey = iterandList.Tokens[0]; } IToken method = lastList[count - 1]; ListToken tokens = new ListToken(); for (int i = 0; i < count - 2; i++) { IToken token = lastList[i]; ListToken list = token as ListToken; if (list == null) { list = new ListToken(); list.Tokens.Add(token); } int index = 0; foreach (IToken item in list.Tokens) { TokenTree tree = new TokenTree(); tree.Children.Add(new TokenTree(iterandKey.Text, item)); if (iterandIndex != null) tree.Children.Add(new TokenTree(iterandIndex.Text, new IntToken(index))); IToken toCall = method.SubstituteParameters(tree); IToken parsed = toCall.Evaluate(substitutions, isFinal); if (parsed is ExpressionToken) { if (!isFinal) return UnParsed(listToken); } else if (!(parsed is NullToken)) { tokens.Add(parsed); } ++index; } } return tokens; }
void SyntaxParsingGlobal(ref int i, ref int skip, List <Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable <TokenTree> trees, string filename, Dictionary <string, Func <dynamic[], ScriptRunner, dynamic> > system_command, IEnumerable <Func <string, Expression> > literal_checker) { if (vec.Length < 3) { throw new Exception(token.GetData().ExceptionMessage("global argument is too few")); } var name = vec[1].GetToken(); if (name == null) { throw new Exception(token.GetData().ExceptionMessage("invalid value name")); } code.Add(new Global(name, ExpressionParse(new Tree(vec.Skip(2).ToArray(), vec[2].GetData()), literal_checker))); }
TokenTree TokenTree() { TT la1; TokenTree got_TokenTree = default(TokenTree); TokenTree result = default(TokenTree); result = new TokenTree(SourceFile); // Line 231: nongreedy((TT.Indent|TT.LBrace|TT.LBrack|TT.LParen|TT.SpaceLParen) TokenTree (TT.Dedent|TT.RBrace|TT.RBrack|TT.RParen) / ~(EOF))* for (;;) { switch ((TT)LA0) { case EOF: case TT.Dedent: case TT.RBrace: case TT.RBrack: case TT.RParen: goto stop; case TT.Indent: case TT.LBrace: case TT.LBrack: case TT.LParen: case TT.SpaceLParen: { la1 = (TT)LA(1); if (la1 != (TT)EOF) { var open = MatchAny(); got_TokenTree = TokenTree(); // line 233 result.Add(open.WithValue(got_TokenTree)); result.Add(Match((int)TT.Dedent, (int)TT.RBrace, (int)TT.RBrack, (int)TT.RParen)); } else { result.Add(MatchAny()); } } break; default: result.Add(MatchAny()); break; } } stop :; return(result); }
void SyntaxParsingIf(ref int i, ref int skip, List <Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable <TokenTree> trees, string filename, Dictionary <string, Func <dynamic[], ScriptRunner, dynamic> > system_command, IEnumerable <Func <string, Expression> > literal_checker) { if (vec.Length == 1) { throw new Exception(token.GetData().ExceptionMessage("if parameter is too few")); } var syns = new List <Tuple <Expression, List <Syntax> > >(); while (true) { var v = SyntaxParse(trees.Skip(i + skip), filename, system_command, literal_checker); var expr = ExpressionParse(new Tree(trees.ElementAt(i + skip - 1).GetTree().Skip(1).ToArray(), vec[1].GetData()), literal_checker); if (v.Item3 == ReturnFlag.Elif) { syns.Add(Tuple.Create(expr, v.Item1)); skip += v.Item2; if (trees.ElementAt(i + skip - 1).GetTree().Length == 1) { throw new Exception(token.GetData().ExceptionMessage("elif parameter is too few")); } } else if (v.Item3 == ReturnFlag.Else) { syns.Add(Tuple.Create(expr, v.Item1)); skip += v.Item2; var u = SyntaxParse(trees.Skip(i + skip), filename, system_command, literal_checker); if (u.Item3 != ReturnFlag.Endif) { throw new Exception(token.GetData().ExceptionMessage("endif corresponding to the if can not be found")); } code.Add(new If(token.GetData(), syns, u.Item1)); skip += u.Item2; break; } else if (v.Item3 == ReturnFlag.Endif) { syns.Add(Tuple.Create(expr, v.Item1)); code.Add(new If(token.GetData(), syns, new List <Syntax>())); skip += v.Item2; break; } else { throw new Exception(token.GetData().ExceptionMessage("endif corresponding to the if can not be found")); } } }
void SyntaxParsingWhile(ref int i, ref int skip, List <Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable <TokenTree> trees, string filename, Dictionary <string, Func <dynamic[], ScriptRunner, dynamic> > system_command, IEnumerable <Func <string, Expression> > literal_checker) { if (vec.Length == 1) { throw new Exception(token.GetData().ExceptionMessage("while parameter is too few")); } var expr = ExpressionParse(new Tree(vec.Skip(1).ToArray(), vec[1].GetData()), literal_checker); var c = SyntaxParse(trees.Skip(i), filename, system_command, literal_checker); if (c.Item3 != ReturnFlag.Loop) { throw new Exception(token.GetData().ExceptionMessage("endif corresponding to the if can not be found")); } code.Add(new While(token.GetData(), c.Item1, expr)); skip += c.Item2; }
public List <TokenTree> Tokenize(string text) { var initiallySplit = SplitIntoTokens(text); var totalList = new List <TokenTree>(); TokenTree tempListPtr = null; var nestCounter = 0; var nestStack = new Stack <TokenTree>(); foreach (var token in initiallySplit) { if (token == "(") { if (nestCounter == 0) { totalList.Add(new TokenNode()); tempListPtr = totalList.Last(); nestStack.Push(tempListPtr); } else if (nestCounter >= 1) { ((TokenNode)tempListPtr).children.Add(new TokenNode()); nestStack.Push(tempListPtr); tempListPtr = ((TokenNode)tempListPtr).children.Last(); } nestCounter++; } else if (token == ")") { nestCounter--; tempListPtr = nestStack.Pop(); } else if (nestCounter != 0) { ((TokenNode)tempListPtr).children.Add(new Token(token)); } else { totalList.Add(new Token(token)); } } return(totalList); }
/// <summary>Replaces Ids with Ids or Literals in token trees.</summary> private static bool ReplaceInTokenTree(ref TokenTree tokens, InternalList <Triplet <Symbol, LNode, int> > Replacements) { TokenTree children; bool modified = false; for (int i = 0; i < tokens.Count; i++) { Token token = tokens[i]; Symbol id = token.Value as Symbol; if (id != null) { for (int r = 0; r < Replacements.Count; r++) { var repl = Replacements[r]; if (id == repl.A) { if (repl.B.IsId) { ReplaceAt(ref modified, ref tokens, i, token.WithValue(repl.B.Name)); } else if (repl.B.IsLiteral) { ReplaceAt(ref modified, ref tokens, i, new Token( (int)TokenKind.Literal, token.StartIndex, token.Length, token.Style, repl.B.Value)); } if (!repl.B.IsCall) { Replacements.InternalArray[r].C++; // prevent 'never used' warning } } } } else if ((children = token.Children) != null) { if (ReplaceInTokenTree(ref children, Replacements)) { ReplaceAt(ref modified, ref tokens, i, token.WithValue(children)); } } } return(modified); }
public override IToken SubstituteParameters(IToken first, IToken last, TokenTree parameters) { if (parameters == null) throw new Exception($"Operation {Text} must have parameters if final."); if (first != null) throw new Exception($"Operation {Text} is unary."); if (last == null) throw new Exception($"Operation {Text} needs a variable."); IToken evaluated = last.SubstituteParameters(parameters); if (evaluated is ExpressionToken) return new ExpressionToken(null, this, evaluated); ListToken listToken = evaluated as ListToken; if (listToken != null && listToken.Tokens.Exists(x => x is ExpressionToken)) return new ExpressionToken(null, this, evaluated); string text = evaluated.Text; TokenTree found = parameters.FindFirst(text); return found?.Value ?? new ExpressionToken(null, new SubstitutionOperator(), evaluated); }
public IBatchExpression Generate(TokenTree tokenTree) { var functionExpressions = tokenTree.FunctionCalls .Select(x => _expressionGeneratorFactory.Value.Generate(x)) .ToList(); var sideExpressions = functionExpressions.Take(functionExpressions.Count - 1); var resultExpression = functionExpressions.Last(); try { return((IBatchExpression)_createBatchExpressionMethod.Value .MakeGenericMethod(resultExpression.ResultType) .Invoke(this, new object[] { sideExpressions, resultExpression })); } catch (TargetInvocationException exception) { throw exception.InnerException; } }
private int IndexOfWorstOp(List <TokenTree> subTokensTree) { int retIndexWorstOp = 0; for (int j = 0; j < subTokensTree.Count; j++) { TokenTree currentToken = subTokensTree[j]; if (currentToken.TypeToken != TypeTokenTree.Number) { if (currentToken.AdvancedTypeToken == AdvancedTypeTokenTree.OpUnaireRight && subTokensTree[retIndexWorstOp].AdvancedTypeToken == AdvancedTypeTokenTree.OpUnaireRight) { retIndexWorstOp = j; } if (currentToken.Priority < subTokensTree[retIndexWorstOp].Priority) { retIndexWorstOp = j; } } } return(retIndexWorstOp); }
public void BugFixes() { LNode stmt; Expr("(a + b).b<c>()", F.Call(F.Dot(F.InParens(F.Call(S.Add, a, b)), F.Of(b, c)))); Stmt("@`'+`(a, b)(c, 1);", F.Call(F.Call(S.Add, a, b), c, one)); // was: "c+1" // once printed as "partial #var(Foo, a);" which would be parsed as a method declaration Stmt("partial Foo a;", Attr(@partialWA, F.Vars(Foo, a))); Stmt("public partial alt class BinaryTree<T> { }", F.Attr(F.Public, partialWA, WordAttr("#alt"), F.Call(S.Class, F.Of(F.Id("BinaryTree"), T), F.List(), F.Braces()))); Stmt("partial Foo.T x { get; }", Attr(partialWA, F.Property(F.Dot(Foo, T), x, F.Braces(get)))); Stmt("IFRange<char> ICloneable<IFRange<char>>.Clone() {\n return Clone();\n}", F.Fn(F.Of(_("IFRange"), F.Char), F.Dot(F.Of(_("ICloneable"), F.Of(_("IFRange"), F.Char)), _("Clone")), F.List(), F.Braces(F.Call(S.Return, F.Call("Clone"))))); Stmt("Foo<a> IDictionary<a, b>.Keys { }", F.Property(F.Of(Foo, a), F.Dot(F.Of(_("IDictionary"), a, b), _("Keys")), F.Braces())); Stmt("T IDictionary<Symbol, T>.this[Symbol x] { get; set; }", F.Property(T, F.Dot(F.Of(_("IDictionary"), _("Symbol"), T), F.@this), F.List(F.Var(_("Symbol"), x)), F.Braces(get, set))); Stmt("Func<T, T> x = delegate(T a) {\n return a;\n};", F.Var(F.Of(_("Func"), T, T), x, F.Call(S.Lambda, F.List(F.Var(T, a)), F.Braces(F.Call(S.Return, a))).SetBaseStyle(NodeStyle.OldStyle))); Stmt("public static rule EmailAddress Parse(T x) { }", F.Attr(F.Public, _(S.Static), WordAttr("rule"), F.Fn(_("EmailAddress"), _("Parse"), F.List(F.Var(T, x)), F.Braces()))); // Currently we're not trying to treat this as a keyword Stmt("dynamic Foo();", F.Fn(_("dynamic"), Foo, F.List())); Stmt("dynamic x;", F.Var(_("dynamic"), x)); Token[] token = new[] { new Token((int)TokenType.Literal, 0, 0, 0, 'a') }; var tree = new TokenTree(F.File, (ICollection<Token>)token); var lexer = F.Call(_("LLLPG"), _("lexer"), F.Braces( Attr(F.Public, F.Property(_("rule"), a, F.Literal(tree))))).SetBaseStyle(NodeStyle.Special); Stmt("LLLPG (lexer) {\n public rule a @{ 'a' };\n}", lexer, Mode.ParserTest); Stmt("LLLPG (lexer) {\n public rule a => @{ 'a' };\n}", lexer); // 2016-04 bug: ForEachStmt failed to call Up() before returning Stmt("{\n foreach (var x in Foo) { }\n Foo();\n}", F.Braces(F.Call(S.ForEach, F.Vars(F.Missing, x), Foo, F.Braces()), F.Call(Foo))); // 2016-10 bug: `property:` was applied to both attributes stmt = F.Attr(a, F.Call(S.NamedArg, F.Id("property"), b), F.Private, F.Property(F.String, Foo, F.Braces(get, set))); Stmt("[a, property: b] private string Foo { get; set; }", stmt); Stmt("[a] [property: b] public string Foo { get; set; }", stmt.WithAttrChanged(2, @public), Mode.ParserTest); Stmt("a = (var b = x);", F.Call(S.Assign, a, F.InParens(F.Var(F.Missing, b, x)))); }
static void Main(string[] args) { String docPath = @"..\..\proba - Copy.docx"; WordprocessingDocument doc = WordprocessingDocument.Open(docPath, false); Body docBody = doc.MainDocumentPart.Document.Body; ParseProperties parseProperties = buildProperties(); TokenTreeBuilder tokenTreeBuilder = new TokenTreeBuilder(parseProperties); SyntaxTreeBuilder syntaxTreeBuilder = new SyntaxTreeBuilder(parseProperties); List <OfficeMath> mathExpressions = new List <OfficeMath>(docBody.Descendants <OfficeMath>()); foreach (var expression in mathExpressions) { if (String.IsNullOrWhiteSpace(expression.InnerText)) { continue; } Console.WriteLine("OMath paragraph found!"); Console.WriteLine(System.Xml.Linq.XDocument.Parse(expression.OuterXml).ToString()); TokenTree tokenTree = tokenTreeBuilder.build(expression); SyntaxTree syntaxTree = syntaxTreeBuilder.Build(tokenTree); Console.WriteLine("\nSyntax tree built!"); Console.WriteLine("Postfix notation: "); Console.WriteLine(syntaxTree.toPostfixNotation()); Console.WriteLine("Infix notation: "); Console.WriteLine(syntaxTree.toInfixNotation()); Console.WriteLine("\n====================================================================\n"); } Console.Read(); }
public override IToken Perform(IToken parameterList, TokenTreeList parameters, bool isFinal) { ListToken listToken = parameterList as ListToken; if (listToken == null) throw new Exception($"Last token must be list for '{ID}'"); List<IToken> lastList = listToken.Tokens; int count = lastList.Count; if (count < 2) throw new Exception($"Must have at least 2 values for '{ID}': {listToken}"); IToken method = lastList[0]; if (isFinal) { TokenTree tree = new TokenTree(); for (int i = 1; i < lastList.Count; ++i) { IToken parameter = lastList[i]; tree.Children.Add(new TokenTree(i.ToString(), parameter)); } method = method.SubstituteParameters(tree); } IToken parsed = method.Evaluate(parameters, isFinal); if (parsed is ExpressionToken) { if (!isFinal) return UnParsed(listToken); } else { return parsed; } return new NullToken(); }
// lecture de l'ensemble des situations d'une personne d'une IP donnée et actualise les views // attachées // C'est incomplet et il faut lire les pe_even ainsi que steven qui doit être = 02 (= mutation passée) // la relation entre pe_situ et pe_even est se fait via pe_even_id private void read_pers_assu_elsi(String nperso) { dg_elsi_simple.ItemsSource = null; dg_elsi_multiple.ItemsSource = null; dicts.Clear(); var assus_a = uc_select_connection.session.CreateCriteria <pe_assu>().Add(Restrictions.Eq("nperso", nperso)) .Add(Restrictions.Eq("no_ip", noIp)) .List <pe_assu>() .Select(a => a.pe_assu_id). ToArray <string>(); List <pe_elsi> elsis = uc_select_connection.session.CreateCriteria <pe_elsi>().Add(Restrictions.In("pe_assu_id", assus_a)) .AddOrder(Order.Desc("dtmutx")) .AddOrder(Order.Desc("nositu")) .List <pe_elsi>().ToList(); if (elsis.Count > 0) { List <TokenTree> tokenTrees = new List <TokenTree>(); foreach (pe_elsi elsi in elsis) { var elsiTree = new TokenTree(); elsiTree.Tokens = parser.parse(new StringBuilder(elsi.liste_elsi), grammar).ToList(); elsiTree.build(); dicts.Add(elsiTree.asDictionary()); tokenTrees.Add(elsiTree); } // arbitrairement la première mutation de la liste dg_elsi_simple.ItemsSource = CollectionViewSource.GetDefaultView(tokenTrees.First().Tokens); dg_elsi_multiple.ItemsSource = null; dg_elsi_multiple.Visibility = Visibility.Hidden; dg_elsi_multiple.ItemsSource = CollectionViewSource.GetDefaultView(extension_method.to_data_table(dicts)); } }
public void multiple_FunctionCalls___SideExpressions_and_ResultExpression() { var functionExpressions = new List <Tuple <FunctionCall, IFunctionExpression> > { new Tuple <FunctionCall, IFunctionExpression>(new FunctionCall { FunctionName = nameof(EmptyFunction) }, new FunctionExpression <EmptyFunction, Domain.FunctionsDomain.Void>()), new Tuple <FunctionCall, IFunctionExpression>(new FunctionCall { FunctionName = nameof(EmptyFunction) }, new FunctionExpression <EmptyFunction, Domain.FunctionsDomain.Void>()), new Tuple <FunctionCall, IFunctionExpression>(new FunctionCall { FunctionName = nameof(EmptyFunction) }, new FunctionExpression <EmptyFunction, Domain.FunctionsDomain.Void>()) }; var tokenTree = new TokenTree { FunctionCalls = functionExpressions.Select(x => x.Item1).ToList() }; var expressionGenerator = Substitute.For <IExpressionGenerator>(); foreach (var expression in functionExpressions) { expressionGenerator.Generate(expression.Item1).Returns(expression.Item2); } var result = Act(tokenTree, expressionGenerator); result.Should().NotBeNull(); result.SideExpressions.ShouldAllBeEquivalentTo(functionExpressions.Take(2).Select(x => x.Item2).ToList()); result.ResultExpression.Should().BeSameAs(functionExpressions.Last().Item2); }
void SyntaxParsingDef(ref int i, ref int skip, List <Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable <TokenTree> trees, string filename, Dictionary <string, Func <dynamic[], ScriptRunner, dynamic> > system_command, IEnumerable <Func <string, Expression> > literal_checker) { if (vec.Length == 1) { throw new Exception(token.GetData().ExceptionMessage("def parameter is too few")); } var funcname = vec[1].GetToken(); if (funcname == null) { throw new Exception(token.GetData().ExceptionMessage("invalid function name")); } List <string> names = new List <string>(); var index = code.Count; foreach (var t in vec.Skip(2)) { var str = t.GetToken(); if (str == null) { throw new Exception(t.GetData().ExceptionMessage("invalid function parameter")); } names.Add(str); } Func <dynamic[], CodeData, dynamic> func = (dy, data) => { Dictionary <string, dynamic> local = new Dictionary <string, dynamic>(); for (int u = 0; u < names.Count; ++u) { local[names[u]] = dy[u]; } return(this.Run(code.Skip(index), local)); }; function.Add(funcname, Tuple.Create(vec.Length - 2, func)); }
void SyntaxParseFor(ref int i, ref int skip, List <Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable <TokenTree> trees, string filename, Dictionary <string, Func <dynamic[], ScriptRunner, dynamic> > system_command, IEnumerable <Func <string, Expression> > literal_checker) { if (vec.Length < 4) { throw new Exception(token.GetData().ExceptionMessage("for parameter is too few")); } var valname = vec[1].GetToken(); if (valname == null) { throw new Exception(token.GetData().ExceptionMessage("invalid value name")); } var first = ExpressionParse(vec[2], literal_checker); var last = ExpressionParse(vec[3], literal_checker); var step = vec.Length > 4 ? ExpressionParse(new Tree(vec.Skip(4).ToArray(), vec[4].GetData()), literal_checker) : new Literal(1); var c = SyntaxParse(trees.Skip(i), filename, system_command, literal_checker); if (c.Item3 != ReturnFlag.Next) { throw new Exception(token.GetData().ExceptionMessage("next corresponding to the for can not be found")); } code.Add(new For(token.GetData(), c.Item1, valname, first, last, step)); skip += c.Item2; }
public new static VList<LNode> ParseTokenTree(TokenTree tokens, IMessageSink sink) { return Parse(tokens, tokens.File, sink); }
public new static LNodeList ParseTokenTree(TokenTree tokens, IMessageSink sink) { return(Parse(tokens, tokens.File, sink)); }
// An Particle is: // - an (expression) in parenthesis or a tuple // - a literal or simple identifier // - simple calls are also handled here, as a space optimization // - a token literal @{ ... } // - a prefix operator followed by an Expr // - a { block } in braces // - a [ list ] in square brackets LNode Particle() { TT la0; Token c = default(Token); Token o = default(Token); LNode result = default(LNode); TokenTree tree = default(TokenTree); // Line 192: ( TT.Id | TT.Literal | TT.At (TT.LBrack TokenTree TT.RBrack | TT.LBrace TokenTree TT.RBrace) | TT.LBrace StmtList TT.RBrace | TT.LBrack ExprList TT.RBrack | (TT.LParen|TT.SpaceLParen) ExprList TT.RParen ) switch ((TT)LA0) { case TT.Id: { var id = MatchAny(); // line 193 result = F.Id(id).SetStyle(id.Style); } break; case TT.Literal: { var lit = MatchAny(); // line 195 result = F.Literal(lit).SetStyle(lit.Style); } break; case TT.At: { o = MatchAny(); // Line 198: (TT.LBrack TokenTree TT.RBrack | TT.LBrace TokenTree TT.RBrace) la0 = (TT)LA0; if (la0 == TT.LBrack) { Skip(); tree = TokenTree(); c = Match((int)TT.RBrack); } else { Match((int)TT.LBrace); tree = TokenTree(); c = Match((int)TT.RBrace); } // line 200 result = F.Literal(tree, o.StartIndex, c.EndIndex); } break; case TT.LBrace: { o = MatchAny(); var list = StmtList(); c = Match((int)TT.RBrace); // line 203 result = F.Braces(list, o.StartIndex, c.EndIndex).SetStyle(NodeStyle.StatementBlock); } break; case TT.LBrack: { o = MatchAny(); var list = ExprList(); c = Match((int)TT.RBrack); // line 205 result = F.Call(S.Array, list, o.StartIndex, c.EndIndex, o.StartIndex, o.EndIndex, NodeStyle.Expression); } break; case TT.LParen: case TT.SpaceLParen: { // line 207 var endMarker = default(TT); o = MatchAny(); // line 208 var hasAttrList = ((TT)LA0) == TT.LBrack || ((TT)LA0) == TT.At; var list = ExprList(ref endMarker); c = Match((int)TT.RParen); // line 211 if ((endMarker == TT.Semicolon || list.Count != 1)) { result = F.Call(S.Tuple, list, o.StartIndex, c.EndIndex, o.StartIndex, o.EndIndex, NodeStyle.Expression); if ((endMarker == TT.Comma)) { var msg = "Tuples require ';' as a separator."; if ((o.Type() == TT.SpaceLParen)) { msg += " If a function call was intended, remove the space(s) before '('."; } ErrorSink.Error(list[0].Range.End, msg); } } else { result = hasAttrList ? list[0] : F.InParens(list[0], o.StartIndex, c.EndIndex); } } break; default: { // line 225 Error(0, "Expected a particle (id, literal, {braces} or (parens))."); result = MissingExpr(LT0); } break; } return(result); }
void SyntaxParsingIf(ref int i, ref int skip, List<Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable<TokenTree> trees, string filename, Dictionary<string, Func<dynamic[], ScriptRunner, dynamic>> system_command, IEnumerable<Func<string,Expression>> literal_checker) { if (vec.Length == 1) throw new Exception(token.GetData().ExceptionMessage("if parameter is too few")); var syns = new List<Tuple<Expression, List<Syntax>>>(); while (true) { var v = SyntaxParse(trees.Skip(i + skip), filename, system_command, literal_checker); var expr = ExpressionParse(new Tree(trees.ElementAt(i + skip - 1).GetTree().Skip(1).ToArray(), vec[1].GetData()), literal_checker); if (v.Item3 == ReturnFlag.Elif) { syns.Add(Tuple.Create(expr, v.Item1)); skip += v.Item2; if (trees.ElementAt(i + skip - 1).GetTree().Length == 1) { throw new Exception(token.GetData().ExceptionMessage("elif parameter is too few")); } } else if (v.Item3 == ReturnFlag.Else) { syns.Add(Tuple.Create(expr, v.Item1)); skip += v.Item2; var u = SyntaxParse(trees.Skip(i + skip), filename, system_command, literal_checker); if (u.Item3 != ReturnFlag.Endif) { throw new Exception(token.GetData().ExceptionMessage("endif corresponding to the if can not be found")); } code.Add(new If(token.GetData(), syns, u.Item1)); skip += u.Item2; break; } else if (v.Item3 == ReturnFlag.Endif) { syns.Add(Tuple.Create(expr, v.Item1)); code.Add(new If(token.GetData(), syns, new List<Syntax>())); skip += v.Item2; break; } else { throw new Exception(token.GetData().ExceptionMessage("endif corresponding to the if can not be found")); } } }
void SyntaxParsingLet(ref int i, ref int skip, List<Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable<TokenTree> trees, string filename, Dictionary<string, Func<dynamic[], ScriptRunner, dynamic>> system_command, IEnumerable<Func<string,Expression>> literal_checker) { if (vec.Length < 3) throw new Exception(token.GetData().ExceptionMessage("let argument is too few")); var name = vec[1].GetToken(); if (name == null) throw new Exception(token.GetData().ExceptionMessage("invalid value name")); code.Add(new Let(name, ExpressionParse(new Tree(vec.Skip(2).ToArray(), vec[2].GetData()), literal_checker))); }
public virtual IToken SubstituteParameters(IToken firstToken, IToken lastToken, TokenTree parameters) { IToken first = firstToken?.SubstituteParameters(parameters); IToken last = lastToken?.SubstituteParameters(parameters); return new ExpressionToken(first, this, last); }
void SyntaxParsingWhile(ref int i, ref int skip, List<Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable<TokenTree> trees, string filename, Dictionary<string, Func<dynamic[], ScriptRunner, dynamic>> system_command, IEnumerable<Func<string,Expression>> literal_checker) { if (vec.Length == 1) throw new Exception(token.GetData().ExceptionMessage("while parameter is too few")); var expr = ExpressionParse(new Tree(vec.Skip(1).ToArray(), vec[1].GetData()), literal_checker); var c = SyntaxParse(trees.Skip(i), filename, system_command, literal_checker); if (c.Item3 != ReturnFlag.Loop) { throw new Exception(token.GetData().ExceptionMessage("endif corresponding to the if can not be found")); } code.Add(new While(token.GetData(), c.Item1, expr)); skip += c.Item2; }
Expression ExpressionParse( TokenTree tree, IEnumerable<Func<string,Expression>> literal_checker) { var val = tree.GetToken(); var vec = tree.GetTree(); if (val != null) { foreach (var func in literal_checker) { var expr = func(val); if (expr != null) return expr; } Tuple<int, Func<dynamic[], CodeData, dynamic>> set; if (function.TryGetValue(val, out set) && set.Item1 == 0) return new Function(set.Item2, new Expression[0], tree.GetData()); return new Value(val, tree.GetData()); } else { if (vec.Length == 0) throw new Exception(tree.GetData().ExceptionMessage("null expression is invalid")); var name = vec[0].GetToken(); if (name == null) throw new Exception(tree.GetData().ExceptionMessage("invalid expression")); if (vec.Length == 1) return ExpressionParse(vec[0], literal_checker); Tuple<int, Func<dynamic[], CodeData, dynamic>> set; if (!function.TryGetValue(name, out set)) throw new Exception(tree.GetData().ExceptionMessage(string.Format("function {0} was not found", name))); if (set.Item1 >= vec.Length) throw new Exception(tree.GetData().ExceptionMessage(string.Format("function {0} requires {1} argument, but provided was {2}", name, set.Item1, vec.Length - 1))); if (set.Item1 != -1) { var exprs = new Expression[set.Item1]; for (int i = 1; i < set.Item1; ++i) { exprs[i - 1] = ExpressionParse(vec[i], literal_checker); } exprs[set.Item1 - 1] = ExpressionParse(new Tree(vec.Skip(set.Item1).ToArray(), vec[set.Item1].GetData()), literal_checker); return new Function(set.Item2, exprs, tree.GetData()); } else { return new Function(set.Item2, (from e in vec.Skip(1) select ExpressionParse(e, literal_checker)).ToArray(), tree.GetData()); } } }
public Tree(TokenTree[] tree, CodeData data) { this.tree = tree; this.data = data; this.last = tree.Last().GetLast(); }
public static LNode ParseTokenTree(TokenTree tokens, IMessageSink sink, LNode basis) { return(StageOneParser.Parse(tokens, tokens.File, sink)); }
public Tokenizer() { tree = new TokenTree(); }
TokenTree TokenTree() { TT la1; TokenTree got_TokenTree = default(TokenTree); TokenTree result = default(TokenTree); result = new TokenTree(SourceFile); // Line 233: nongreedy((TT.LBrace|TT.LBrack|TT.LParen|TT.SpaceLParen) TokenTree (TT.RBrace|TT.RBrack|TT.RParen) / ~(EOF))* for (;;) { switch ((TT) LA0) { case EOF: case TT.RBrace: case TT.RBrack: case TT.RParen: goto stop; case TT.LBrace: case TT.LBrack: case TT.LParen: case TT.SpaceLParen: { la1 = (TT) LA(1); if (la1 != (TT) EOF) { var open = MatchAny(); got_TokenTree = TokenTree(); // line 235 result.Add(open.WithValue(got_TokenTree)); result.Add(Match((int) TT.RBrace, (int) TT.RBrack, (int) TT.RParen)); } else result.Add(MatchAny()); } break; default: result.Add(MatchAny()); break; } } stop:; return result; }
/// <summary>Replaces Ids with Ids or Literals in token trees.</summary> private static bool ReplaceInTokenTree(ref TokenTree tokens, InternalList<Triplet<Symbol, LNode, int>> Replacements) { TokenTree children; bool modified = false; for (int i = 0; i < tokens.Count; i++) { Token token = tokens[i]; Symbol id = token.Value as Symbol; if (id != null) { for (int r = 0; r < Replacements.Count; r++) { var repl = Replacements[r]; if (id == repl.A) { if (repl.B.IsId) { ReplaceAt(ref modified, ref tokens, i, token.WithValue(repl.B.Name)); } else if (repl.B.IsLiteral) { ReplaceAt(ref modified, ref tokens, i, new Token( (int)TokenKind.Literal, token.StartIndex, token.Length, token.Style, repl.B.Value)); } if (!repl.B.IsCall) Replacements.InternalArray[r].C++; // prevent 'never used' warning } } } else if ((children = token.Children) != null) { if (ReplaceInTokenTree(ref children, Replacements)) ReplaceAt(ref modified, ref tokens, i, token.WithValue(children)); } } return modified; }
void SyntaxParsingDef(ref int i, ref int skip, List<Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable<TokenTree> trees, string filename, Dictionary<string, Func<dynamic[], ScriptRunner, dynamic>> system_command, IEnumerable<Func<string,Expression>> literal_checker) { if (vec.Length == 1) throw new Exception(token.GetData().ExceptionMessage("def parameter is too few")); var funcname = vec[1].GetToken(); if (funcname == null) throw new Exception(token.GetData().ExceptionMessage("invalid function name")); List<string> names = new List<string>(); var index = code.Count; foreach (var t in vec.Skip(2)) { var str = t.GetToken(); if (str == null) throw new Exception(t.GetData().ExceptionMessage("invalid function parameter")); names.Add(str); } Func<dynamic[], CodeData, dynamic> func = (dy, data) => { Dictionary<string, dynamic> local = new Dictionary<string, dynamic>(); for (int u = 0; u < names.Count; ++u) { local[names[u]] = dy[u]; } return this.Run(code.Skip(index), local); }; function.Add(funcname, Tuple.Create(vec.Length - 2, func)); }
private static TokenTree Setup() { TokenTree tree = new TokenTree(); tree.AddToken("select", false, true, "select"); tree.AddToken("contains", false, true, new string[] { "value", "textsearch", "contains" }); tree.AddToken("freetext", false, true, new string[] { "value", "textsearch", "freetext" }); tree.AddToken("as", false, true, "as"); tree.AddToken("top", false, true, "top"); tree.AddToken("distinct", false, true, "distinct"); tree.AddToken("percent", false, true, "percent"); tree.AddToken("with ties", false, true, "with ties"); // do not localize tree.AddToken(",", false, false, "comma"); tree.AddToken("from", false, true, "from"); tree.AddToken("where", false, true, "where"); tree.AddToken("order by", false, true, "order by"); // do not localize tree.AddToken("min", false, true, new string[] { "function", "value", "min" }); tree.AddToken("max", false, true, new string[] { "function", "value", "max" }); tree.AddToken("avg", false, true, new string[] { "function", "value", "avg" }); tree.AddToken("sum", false, true, new string[] { "function", "value", "sum" }); tree.AddToken("soundex", false, true, new string[] { "function", "value", "soundex" }); tree.AddToken("count", false, true, new string[] { "function", "value", "count" }); tree.AddToken("isnull", false, true, new string[] { "function", "value", "isnull" }); tree.AddToken("in", false, true, new string[] { "compare", "in" }); tree.AddToken("like", false, true, new string[] { "compare", "like" }); tree.AddToken(">", false, false, new string[] { "compare", ">" }); tree.AddToken("<", false, false, new string[] { "compare", "<" }); tree.AddToken("<=", false, false, new string[] { "compare", "<=" }); tree.AddToken(">=", false, false, new string[] { "compare", ">=" }); tree.AddToken("=<", false, false, new string[] { "compare", "<=" }); tree.AddToken("=>", false, false, new string[] { "compare", ">=" }); tree.AddToken("!=", false, false, new string[] { "compare", "!=" }); tree.AddToken("<>", false, false, new string[] { "compare", "!=" }); tree.AddToken("=", false, false, new string[] { "compare", "=" }); tree.AddToken("is", false, true, new string[] { "compare", "=" }); tree.AddToken("(", false, false, new string[] { "(", "value" }); tree.AddToken(")", false, false, ")"); tree.AddToken("[", false, false, "["); tree.AddToken("]", false, false, "]"); tree.AddToken("+", false, false, new string[] { "math", "add", "sign" }); tree.AddToken("-", false, false, new string[] { "math", "minus", "sign" }); tree.AddToken("/", false, false, new string[] { "math", "div" }); tree.AddToken("^", false, false, new string[] { "math", "xor" }); tree.AddToken("%", false, false, new string[] { "math", "mod" }); tree.AddToken("*", false, false, new string[] { "identifier", "property path", "value", "math", "mul" }); // do not localize tree.AddToken("¤", false, true, new string[] { "identifier", "property path", "value" }); // do not localize tree.AddToken("and", false, true, new string[] { "boolop", "and" }); tree.AddToken("or", false, true, new string[] { "boolop", "or" }); tree.AddToken("&&", false, false, new string[] { "boolop", "and" }); tree.AddToken("||", false, false, new string[] { "boolop", "or" }); tree.AddToken("not", false, true, new string[] { "math", "not" }); tree.AddToken("!", false, false, new string[] { "math", "not" }); tree.AddToken("asc", false, true, "direction"); tree.AddToken("desc", false, true, "direction"); tree.AddToken("true", false, true, new string[] { "value", "boolean", "true" }); tree.AddToken("false", false, true, new string[] { "value", "boolean", "false" }); tree.AddToken("between", false, true, new string[] { "between" }); tree.AddToken("null", false, true, new string[] { "value", "null" }); tree.AddToken("?", false, false, new string[] { "value", "?", "parameter" }); tree.AddExpression(false, true, new RangePatternMatcher('\"', '\"', '\"'), new string[] { "value", "string", "string \"" }); // do not localize tree.AddExpression(false, true, new RangePatternMatcher('\'', '\'', '\''), new string[] { "value", "string", "string '" }); // do not localize tree.AddExpression(false, true, new RangePatternMatcher('#'), new string[] { "value", "date" }); tree.AddExpression(false, true, new RangePatternMatcher('{', '}'), new string[] { "value", "guid" }); tree.AddExpression(false, true, new DecPatternMatcher(), new string[] { "value", "decimal" }); // tree.AddExpression(false, true, new RegexPatternMatcher(@"(\@?[a-zA-Z]{1}(\w*)\.?)*"), new string[] {"property path","value"}); // do not localize //tree.AddExpression(false, true, new RegexPatternMatcher(@"(\@?[a-zA-Z]{1}(\w*))"),new string[] {"identifier","property path","value"}); // do not localize //tree.AddExpression(false, true, new RegexPatternMatcher(@"(\@?[a-zA-Z]{1}(\w*)\.{1})*(([a-zA-Z]{1}(\w*))|\*|\¤)"),new string[] {"identifier","property path","value"}); // do not localize tree.AddExpression(false, true, new PropertyPathPatterhMatcher(), new string[] { "identifier", "property path", "value" }); // do not localize return(tree); }
void SyntaxParseFor(ref int i, ref int skip, List<Syntax> code, TokenTree token, TokenTree[] vec, IEnumerable<TokenTree> trees, string filename, Dictionary<string, Func<dynamic[], ScriptRunner, dynamic>> system_command, IEnumerable<Func<string,Expression>> literal_checker) { if (vec.Length < 4) throw new Exception(token.GetData().ExceptionMessage("for parameter is too few")); var valname = vec[1].GetToken(); if (valname == null) throw new Exception(token.GetData().ExceptionMessage("invalid value name")); var first = ExpressionParse(vec[2], literal_checker); var last = ExpressionParse(vec[3], literal_checker); var step = vec.Length > 4 ? ExpressionParse(new Tree(vec.Skip(4).ToArray(), vec[4].GetData()), literal_checker) : new Literal(1); var c = SyntaxParse(trees.Skip(i), filename, system_command, literal_checker); if (c.Item3 != ReturnFlag.Next) { throw new Exception(token.GetData().ExceptionMessage("next corresponding to the for can not be found")); } code.Add(new For(token.GetData(), c.Item1, valname, first, last, step)); skip += c.Item2; }