Пример #1
0
        private bool CheckSimpleStatement()
        {
            if (this.LookAheadFirst.TokenType != TokenType.OpenBraces)
            {
                return(false);
            }
            this.DiscardToken(TokenType.OpenBraces);
            bool flag = false;

            while (!flag)
            {
                flag = !this.DoExpressionRules();
            }
            if (this.IsArithmeticExpression(this.LookAheadFirst.TokenType))
            {
                TextExpression token = (TextExpression) new SimpleExpression(this.LookAheadFirst);
                this.DiscardToken();
                this.DiscardToken(TokenType.CloseBraces);
                this.PushToken(token);
            }
            else
            {
                this.DiscardToken(TokenType.CloseBraces);
            }
            return(true);
        }
Пример #2
0
        private bool CheckFieldStatement()
        {
            if (this.LookAheadFirst.TokenType != TokenType.FieldStarter)
            {
                return(false);
            }
            this.DiscardToken(TokenType.FieldStarter);
            bool flag = false;

            while (!flag)
            {
                flag = !this.DoExpressionRules();
            }
            if (this.LookAheadFirst.TokenType != TokenType.Identifier)
            {
                return(false);
            }
            TextExpression lookAheadFirst = this.LookAheadFirst;

            this.DiscardToken(TokenType.Identifier);
            this.DiscardToken(TokenType.CloseBraces);
            TextExpression textExpression = this.GetRootExpressions() ?? (TextExpression) new SimpleToken(TokenType.Text, "");

            this.DiscardToken(TokenType.FieldFinalizer);
            TextExpression part2 = textExpression;

            this.PushToken((TextExpression) new FieldExpression(lookAheadFirst, part2));
            return(true);
        }
Пример #3
0
        private bool CollapseStatements()
        {
            if (!this.IsRootExpression(this.LookAheadFirst.TokenType) || this.LookAheadFirst.TokenType == TokenType.MultiStatement)
            {
                return(false);
            }
            List <TextExpression> textExpressionList = new List <TextExpression>();
            TextExpression        lookAheadFirst1    = this.LookAheadFirst;

            this.DiscardToken();
            textExpressionList.Add(lookAheadFirst1);
            bool flag = false;

            while (!flag)
            {
                do
                {
                    ;
                }while (this.RunRootGrammarRulesExceptCollapse());
                if (this.IsRootExpression(this.LookAheadFirst.TokenType))
                {
                    TextExpression lookAheadFirst2 = this.LookAheadFirst;
                    this.DiscardToken();
                    textExpressionList.Add(lookAheadFirst2);
                }
                else
                {
                    flag = true;
                }
            }
            this.PushToken((TextExpression) new MultiStatement((IEnumerable <TextExpression>)textExpressionList));
            return(true);
        }
Пример #4
0
    public static void CompileExpressions(DynamicActivity dynamicActivity, params Assembly[] references)
    {
        // See https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions
        string activityName      = dynamicActivity.Name;
        string activityType      = activityName.Split('.').Last() + "_CompiledExpressionRoot";
        string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
        TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
        {
            Activity               = dynamicActivity,
            Language               = "C#",
            ActivityName           = activityType,
            ActivityNamespace      = activityNamespace,
            RootNamespace          = null,
            GenerateAsPartialClass = false,
            AlwaysGenerateSource   = true,
            ForImplementation      = true
        };

        // add assembly references
        TextExpression.SetReferencesForImplementation(dynamicActivity, references.Select(a => (AssemblyReference)a).ToList());
        // Compile the C# expression.
        var results = new TextExpressionCompiler(settings).Compile();

        if (results.HasErrors)
        {
            throw new Exception("Compilation failed.");
        }
        // attach compilation result to live activity
        var compiledExpression = (ICompiledExpressionRoot)Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity });

        CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpression);
    }
Пример #5
0
        private Object evalAssignExpression(Object context, AssignExpression expression)
        {
            // 取得父对象和键
            TextExpression       text          = expression.Left;
            Object               parentContext = null;
            IdentifierExpression key           = null;

            if (text is PathExpression path)
            {
                parentContext = Eval(context, path.SubPath(0, path.Length - 1));
                if (path.SubPath(path.Length - 1).Path[0]
                    is IdentifierExpression identifier)
                {
                    key = identifier;
                }
                else
                {
                    throw new System.Exception("You can only assign it to a key.");
                }
            }
            else if (text is IdentifierExpression identifier)
            {
                parentContext = context;
                key           = identifier;
            }
            // 取得右值
            Object value = Eval(context, expression.Right);

            parentContext[key.Literal] = value;
            return(value);
        }
Пример #6
0
 private void UpdateLookAheads()
 {
     this._lookaheadFirst = this._symbolSequence.Count != 0 ? this._symbolSequence.Peek() : (TextExpression)SimpleToken.SequenceTerminator;
     if (this._symbolSequence.Count < 2)
     {
         this._lookaheadSecond = (TextExpression)SimpleToken.SequenceTerminator;
     }
     else
     {
         TextExpression textExpression = this._symbolSequence.Pop();
         this._lookaheadSecond = this._symbolSequence.Peek();
         this._symbolSequence.Push(textExpression);
     }
     if (this._symbolSequence.Count < 3)
     {
         this._lookaheadThird = (TextExpression)SimpleToken.SequenceTerminator;
     }
     else
     {
         TextExpression textExpression1 = this._symbolSequence.Pop();
         TextExpression textExpression2 = this._symbolSequence.Pop();
         this._lookaheadThird = this._symbolSequence.Peek();
         this._symbolSequence.Push(textExpression2);
         this._symbolSequence.Push(textExpression1);
     }
 }
Пример #7
0
        private bool ConsumeComparisonExpression()
        {
            TokenType tokenType1 = this.LookAheadFirst.TokenType;
            TokenType tokenType2 = this.LookAheadSecond.TokenType;

            if (!this.IsArithmeticExpression(tokenType1) || !this.IsComparisonOperator(tokenType2))
            {
                return(false);
            }
            TextExpression lookAheadFirst1 = this.LookAheadFirst;

            this.DiscardToken();
            ComparisonOperation comparisonOp = this.GetComparisonOp(tokenType2);

            this.DiscardToken();
            do
            {
                ;
            }while (this.DoExpressionRules());
            if (!this.IsArithmeticExpression(this.LookAheadFirst.TokenType))
            {
                return(false);
            }
            TextExpression lookAheadFirst2 = this.LookAheadFirst;

            this.DiscardToken();
            this.PushToken((TextExpression) new ComparisonExpression(comparisonOp, lookAheadFirst1, lookAheadFirst2));
            return(true);
        }
Пример #8
0
        private bool ConsumeInnerAritmeticExpression()
        {
            TokenType tokenType1 = this.LookAheadFirst.TokenType;
            TokenType tokenType2 = this.LookAheadSecond.TokenType;
            int       tokenType3 = (int)this.LookAheadThird.TokenType;

            if (!this.IsArithmeticExpression(tokenType1) || tokenType2 != TokenType.Multiply && tokenType2 != TokenType.Divide)
            {
                return(false);
            }
            TextExpression lookAheadFirst1 = this.LookAheadFirst;

            this.DiscardToken();
            ArithmeticOperation op = this.ConsumeAritmeticOperation();

            if (!this.IsArithmeticExpression(this.LookAheadFirst.TokenType))
            {
                while (this.DoExpressionRules())
                {
                    ;
                }
            }
            TextExpression lookAheadFirst2 = this.LookAheadFirst;

            this.DiscardToken();
            this.PushToken((TextExpression) new ArithmeticExpression(op, lookAheadFirst1, lookAheadFirst2));
            return(true);
        }
Пример #9
0
 internal static Text Text(TextExpression expression)
 {
     return(new Text()
     {
         QuoteChar = expression.QuoteChar,
         SourceText = expression.SourceText,
     }.Apply(n => n.TypeHint = expression.TryType().Null(t => TypeRef.Serialize(t))));
 }
Пример #10
0
        /// <summary>
        /// Parse an TextExpression
        /// </summary>
        /// <returns>Parsed TextExpression</returns>
        public TextExpression ParseTextExpression()
        {
            TextExpression textExpression = new TextExpression();

            CurrentToken = TokenStream.NextToken();
            textExpression.SetText(CurrentToken.GetValue().ToString());

            return(textExpression);
        }
Пример #11
0
        public void ParseTextExpressionTest()
        {
            //Create parser and parse tokens
            ExpressionParser expressionParser = new ExpressionParser(Init("text"));
            TextExpression   expression       = expressionParser.ParseTextExpression();

            //Test variable identifier
            Assert.AreEqual("text", expression.GetText());
        }
    public void EmptyInput()
    {
        // Given
        var expression = new TextExpression("foo");

        // When
        var result = expression.Transform("");

        // Then
        Assert.Null(result);
    }
    public void PartialMatch()
    {
        // Given
        var expression = new TextExpression("foo");

        // When
        var result = expression.Transform("foobar");

        // Then
        Assert.Null(result);
    }
    public void Match()
    {
        // Given
        var expression = new TextExpression("foo");

        // When
        var result = expression.Transform("foo");

        // Then
        Assert.Equal("foo", result);
    }
Пример #15
0
        public void SetNamespaces(object target)
        {
            var dev2ActivitiesAssembly = typeof(WorkflowHelper).Assembly;
            var dev2CommonAssembly     = typeof(GlobalConstants).Assembly;
            var dev2DataAssembly       = typeof(Dev2DataListDecisionHandler).Assembly;

            var namespaces = new Dictionary <string, Assembly>
            {
                { "Dev2.Common", dev2CommonAssembly },
                { "Dev2.Data.Decisions.Operations", dev2DataAssembly },
                { "Dev2.Data.SystemTemplates.Models", dev2DataAssembly },
                { "Dev2.DataList.Contract", dev2DataAssembly },
                { "Dev2.DataList.Contract.Binary_Objects", dev2DataAssembly },
                { "Unlimited.Applications.BusinessDesignStudio.Activities", dev2ActivitiesAssembly }
            };

            #region Set C# assembly references

            // http://stackoverflow.com/questions/16086612/wf-4-5-using-c-sharp-expressions-with-external-class-references
            // http://blogs.msdn.com/b/tilovell/archive/2012/05/25/wf4-5-using-csharpvalue-lt-t-gt-and-csharpreference-lt-t-gt-in-net-4-5-compiling-expressions-and-changes-in-visual-studio-generated-xaml.aspx

            TextExpression.SetReferencesForImplementation(target, namespaces.Values.Distinct().Select(a => new AssemblyReference {
                Assembly = a
            }).ToArray());

            var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");

            AttachablePropertyServices.SetProperty(target, impl, namespaces.Keys.ToList());

            #endregion

            #region Set VB assembly references

//            var vbSettings = VisualBasic.GetSettings(target) ?? new VisualBasicSettings();
//            vbSettings.ImportReferences.Clear();
//
//            foreach(var ns in namespaces.Keys)
//            {
//                try
//                {
//                    vbSettings.ImportReferences.Add(new VisualBasicImportReference { Assembly = namespaces[ns].GetName().Name, Import = ns });
//                }
//                catch(Exception e)
//                {
//                    Dev2Logger.Error(e.Message,e);
//                }
//            }
//
//            VisualBasic.SetSettings(target, vbSettings);

            #endregion
        }
Пример #16
0
 internal static void SetTextExpressionNamespaces(object root, IList <string> namespaces, IList <AssemblyReference> references)
 {
     if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
     {
         TextExpression.SetNamespacesForImplementation(root, namespaces);
         TextExpression.SetReferencesForImplementation(root, references);
     }
     else
     {
         TextExpression.SetNamespaces(root, namespaces);
         TextExpression.SetReferences(root, references);
     }
 }
Пример #17
0
 internal static IList <string> GetTextExpressionNamespaces(object root, out IList <AssemblyReference> references)
 {
     if (NamespaceHelper.ShouldUsePropertiesForImplementation(root))
     {
         references = TextExpression.GetReferencesForImplementation(root);
         return(TextExpression.GetNamespacesForImplementation(root));
     }
     else
     {
         references = TextExpression.GetReferences(root);
         return(TextExpression.GetNamespaces(root));
     }
 }
Пример #18
0
 private static object?Evaluate(Expression expression, IDictionary <string, object> properties)
 {
     return(expression switch
     {
         TextExpression text => text.Value,
         ConstantExpression constant => constant.Value,
         PipeExpression pipe => PipeValue(pipe, properties),
         TemplateExpression part => EvaluateTemplate(part, properties),
         PropertyExpression property => GetProperty(property, properties),
         FunctionCallExpression function => CallFunction(function, properties),
         MultiTemplateExpression => throw new NotSupportedException("Multi-template expressions are top-level expressions only."),
         _ => throw new NotImplementedException(string.Format(Strings.EvaluationNotImplemented, expression)),
     });
Пример #19
0
        private bool ConsumeMarkerOccuranceExpression()
        {
            if (this.LookAheadFirst.TokenType != TokenType.Identifier || this.LookAheadSecond.TokenType != TokenType.MarkerOccuranceIdentifier)
            {
                return(false);
            }
            VariableExpression lookAheadFirst  = this.LookAheadFirst as VariableExpression;
            TextExpression     lookAheadSecond = this.LookAheadSecond;

            this.DiscardToken();
            this.DiscardToken();
            this.PushToken((TextExpression) new MarkerOccuranceTextExpression(lookAheadSecond.RawValue.Substring(2), lookAheadFirst));
            return(true);
        }
Пример #20
0
        private IExpression ParseInterpolation()
        {
            var nextToken = PeekToken();

            if (nextToken.GetType() != typeof(OpenBraceToken))
            {
                return(new TextExpression("$"));
            }
            ConsumeToken();
            nextToken = PeekToken();
            if (nextToken.GetType() != typeof(TextDataToken))
            {
                throw new InvalidOperationException("Function ID expected.");
            }
            var idToken = ConsumeToken();

            nextToken = PeekToken();
            if (nextToken.GetType() == typeof(CloseBraceToken))
            {
                ConsumeToken();
                return(new InterpolateExpression(idToken.Text));
            }
            if (nextToken.GetType() != typeof(ColonToken))
            {
                throw new InvalidOperationException("Function parameter list or closing brace expected.");
            }
            ConsumeToken();
            var expression = new InterpolateExpression(idToken.Text);

            while (true)
            {
                var paramExpression = ParseInside(false, typeof(CloseBraceToken), typeof(CommaToken));
                nextToken = PeekToken();

                if (paramExpression == null)
                {
                    paramExpression = new TextExpression("");
                }

                expression.Parameters.Add(paramExpression);
                if (nextToken.GetType() == typeof(CloseBraceToken))
                {
                    break;
                }
                ConsumeToken();
            }
            ConsumeToken();
            return(expression);
        }
Пример #21
0
        public void ParseKeyValuePairTest()
        {
            //Create parser and parse tokens
            ExpressionParser expressionParser = new ExpressionParser(Init("token1:\"value1\""));
            KeyValuePair     keyValuePair     = expressionParser.ParseKeyValuePair();

            //Check key
            Assert.AreEqual("token1", keyValuePair.GetKey());

            //Check value
            Assert.AreEqual(typeof(TextExpression), keyValuePair.GetValue().GetType());
            TextExpression textExpression = (TextExpression)keyValuePair.GetValue();

            Assert.AreEqual("value1", textExpression.GetText());
        }
Пример #22
0
        private bool CheckConditionalStatement()
        {
            if (this.LookAheadFirst.TokenType != TokenType.ConditionStarter)
            {
                return(false);
            }
            bool flag = false;
            List <TextExpression> conditionExpressions = new List <TextExpression>();
            List <TextExpression> resultExpressions2   = new List <TextExpression>();

            while (!flag)
            {
                TokenType tokenType = this.LookAheadFirst.TokenType;
                if (this.LookAheadFirst.TokenType == TokenType.ConditionStarter || this.LookAheadFirst.TokenType == TokenType.ConditionFollowUp)
                {
                    this.DiscardToken();
                    do
                    {
                        ;
                    }while (this.DoExpressionRules());
                    if (!this.IsArithmeticExpression(this.LookAheadFirst.TokenType))
                    {
                        return(false);
                    }
                    conditionExpressions.Add(this.LookAheadFirst);
                    this.DiscardToken();
                    this.DiscardToken(TokenType.CloseBraces);
                }
                else
                {
                    if (tokenType != TokenType.ConditionSeperator && tokenType != TokenType.Seperator)
                    {
                        return(false);
                    }
                    this.DiscardToken();
                    flag = true;
                }
                TextExpression textExpression = this.GetRootExpressions() ?? (TextExpression) new SimpleToken(TokenType.Text, "");
                resultExpressions2.Add(textExpression);
            }
            do
            {
                ;
            }while (!flag);
            this.DiscardToken(TokenType.ConditionFinalizer);
            this.PushToken((TextExpression) new ConditionExpression(conditionExpressions, resultExpressions2));
            return(true);
        }
Пример #23
0
        private static DynamicActivity GetDynamicActivity(ActivityBuilder activityDefinition)
        {
            var result = new DynamicActivity
            {
                Name = activityDefinition.Name
            };

            foreach (var property in activityDefinition.Properties)
            {
                result.Properties.Add(property);
            }
            foreach (var attrib in activityDefinition.Attributes)
            {
                result.Attributes.Add(attrib);
            }
            foreach (var constraint in activityDefinition.Constraints)
            {
                result.Constraints.Add(constraint);
            }
            result.Implementation = () => activityDefinition.Implementation;

            var vbsettings = VisualBasic.GetSettings(activityDefinition);

            if (vbsettings != null)
            {
                VisualBasic.SetSettings(result, vbsettings);
            }

            var namespacesForImplementation = TextExpression.GetNamespacesForImplementation(activityDefinition);

            if (namespacesForImplementation.Count > 0)
            {
                TextExpression.SetNamespacesForImplementation(result, namespacesForImplementation);
            }

            var referencesForImplementation = TextExpression.GetReferencesForImplementation(activityDefinition);

            if (referencesForImplementation.Count > 0)
            {
                TextExpression.SetReferencesForImplementation(result, referencesForImplementation);
            }

            return(result);
        }
Пример #24
0
        static DynamicActivity GetDynamicActivity(ActivityBuilder activityDefinition)
        {
            DynamicActivity result = new DynamicActivity
            {
                Name = activityDefinition.Name
            };

            foreach (DynamicActivityProperty property in activityDefinition.Properties)
            {
                result.Properties.Add(property);
            }
            foreach (Attribute attrib in activityDefinition.Attributes)
            {
                result.Attributes.Add(attrib);
            }
            foreach (Constraint constraint in activityDefinition.Constraints)
            {
                result.Constraints.Add(constraint);
            }
            result.Implementation = () => activityDefinition.Implementation;

            VisualBasicSettings vbsettings = VisualBasic.GetSettings(activityDefinition);

            if (vbsettings != null)
            {
                VisualBasic.SetSettings(result, vbsettings);
            }

            IList <string> namespacesForImplementation = TextExpression.GetNamespacesForImplementation(activityDefinition);

            if (namespacesForImplementation.Count > 0)
            {
                TextExpression.SetNamespacesForImplementation(result, namespacesForImplementation);
            }

            IList <AssemblyReference> referencesForImplementation = TextExpression.GetReferencesForImplementation(activityDefinition);

            if (referencesForImplementation.Count > 0)
            {
                TextExpression.SetReferencesForImplementation(result, referencesForImplementation);
            }

            return(result);
        }
Пример #25
0
        private bool CheckSelectionStatement()
        {
            if (this.LookAheadFirst.TokenType != TokenType.SelectionStarter)
            {
                return(false);
            }
            this.DiscardToken(TokenType.SelectionStarter);
            do
            {
                ;
            }while (this.DoExpressionRules());
            if (!this.IsArithmeticExpression(this.LookAheadFirst.TokenType))
            {
                return(false);
            }
            TextExpression lookAheadFirst = this.LookAheadFirst;

            this.DiscardToken();
            this.DiscardToken(TokenType.CloseBraces);
            bool flag = false;
            List <TextExpression> selectionExpressions = new List <TextExpression>();

            do
            {
                TextExpression textExpression = this.GetRootExpressions() ?? (TextExpression) new SimpleToken(TokenType.Text, "");
                selectionExpressions.Add(textExpression);
                switch (this.LookAheadFirst.TokenType)
                {
                case TokenType.SelectionSeperator:
                    this.DiscardToken();
                    break;

                case TokenType.SelectionFinalizer:
                    flag = true;
                    this.DiscardToken();
                    break;

                default:
                    return(false);
                }
            }while (!flag);
            this.PushToken((TextExpression) new SelectionExpression(lookAheadFirst, selectionExpressions));
            return(true);
        }
Пример #26
0
        public static TextExpression Create(
            AphidExpressionContext context_aphidExpressionContext,
            string text_s,
            int value_i,
            int value_i1
            )
        {
            TextExpression textExpression
                = new TextExpression(context_aphidExpressionContext, text_s);

            ((AphidExpression)textExpression).Index  = value_i;
            ((AphidExpression)textExpression).Length = value_i1;
            return(textExpression);

            // TODO: Edit factory method of TextExpression
            // This method should be able to configure the object in all possible ways.
            // Add as many parameters as needed,
            // and assign their values to each field by using the API.
        }
Пример #27
0
 private void GetRootExpressionsImp(List <TextExpression> expList)
 {
     while (true)
     {
         do
         {
             ;
         }while (this.RunRootGrammarRulesExceptCollapse());
         if (this.IsRootExpression(this.LookAheadFirst.TokenType))
         {
             TextExpression lookAheadFirst = this.LookAheadFirst;
             this.DiscardToken();
             expList.Add(lookAheadFirst);
         }
         else
         {
             break;
         }
     }
 }
        public static IEnumerable <string> EnumerateImports(EditingContext context, ModelItem modelItem)
        {
            IEnumerable <string> enumerable = context?.Services.GetService(typeof(HashSet <string>)) as HashSet <string>;
            IEnumerable <string> first      = enumerable ?? Enumerable.Empty <string>();
            object obj = modelItem?.Root?.GetCurrentValue();
            object enumerable2;

            if (obj == null)
            {
                enumerable2 = Enumerable.Empty <string>();
            }
            else
            {
                enumerable  = TextExpression.GetNamespacesForImplementation(obj);
                enumerable2 = enumerable;
            }
            IEnumerable <string> second = (IEnumerable <string>)enumerable2;

            return(first.Union(second).Distinct());
        }
Пример #29
0
        /// <summary>
        /// Visit FunctionDefinition to check declarations
        /// </summary>
        /// <param name="functionDefinition">FunctionDefinition to check</param>
        public override void Visit(FunctionDefinition functionDefinition)
        {
            CreateChildSymbolTable();

            //Check Formals
            foreach (Formal formal in functionDefinition.GetFormals())
            {
                //Add variable, but with nullvalue, because value is undefined in definition
                TextExpression textExpression = new TextExpression();
                SymbolTable.AddVariableDefinition(formal.GetIdentifier(), textExpression);
            }

            //Check Statements of function
            foreach (Statement statement in functionDefinition.GetStatements())
            {
                statement.AcceptVisitor(this);
            }

            MoveToParentSymbolTable();
        }
        static void Main()
        {
            var errorCodeWorkflow = new DynamicActivity
            {
                Name       = "MyScenario.MyDynamicActivity3",
                Properties =
                {
                    new DynamicActivityProperty
                    {
                        Name = "Address",
                        Type = typeof(InArgument <MailAddress>),
                    },
                },
                Implementation = () => new WriteLine
                {
                    Text = new CSharpValue <String>
                    {
                        ExpressionText = "\"MyDynamicActivity \" + Address.DisplayName"
                    }
                }
            };

            var impl       = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation");
            var namespaces = new List <string> {
                typeof(MailAddress).Namespace
            };

            TextExpression.SetReferencesForImplementation(errorCodeWorkflow, new AssemblyReference {
                Assembly = typeof(MailAddress).Assembly
            });
            AttachablePropertyServices.SetProperty(errorCodeWorkflow, impl, namespaces);

            CompileExpressions(errorCodeWorkflow);
            WorkflowInvoker.Invoke(errorCodeWorkflow, new Dictionary <String, Object> {
                { "Address", new MailAddress {
                      DisplayName = "TestDisplayName"
                  } }
            });
        }