示例#1
0
        internal static List <Tuple <string, TypeReference, object> > ParseDatatableRow(string content)
        {
            ErrorCollector errors = new ErrorCollector(null);
            ITokenSource   lexer  = new Grammar.StepBroLexer(new AntlrInputStream(content));
            ITokenStream   tokens = new CommonTokenStream(lexer);
            var            parser = new SBP(tokens);

            parser.AddErrorListener(errors);
            parser.BuildParseTree = true;
            StepBroListener listener = new StepBroListener(errors);
            var             context  = parser.datatableRow();

            ParseTreeWalker walker = new ParseTreeWalker();

            walker.Walk(listener, context);

            if (errors.ErrorCount > 0)
            {
                throw new Exception("PARSING ERRORS");
            }

            return(listener.GetLastDatatableRow());
        }
示例#2
0
        internal static StepBroTypeScanListener.FileContent TypeScanFile(string content)
        {
            ITokenSource lexer  = new Grammar.StepBroLexer(new AntlrInputStream(content));
            ITokenStream tokens = new CommonTokenStream(lexer);
            var          parser = new SBP(tokens);
            var          errors = new ErrorCollector(null);

            parser.AddErrorListener(errors);
            parser.BuildParseTree = true;
            var listener = new StepBroTypeScanListener("Angus");     // Some random default namespace
            var context  = parser.compilationUnit();

            var walker = new ParseTreeWalker();

            walker.Walk(listener, context);

            if (errors.ErrorCount > 0)
            {
                throw new Exception("PARSING ERRORS");
            }

            return(listener.GetContent());
        }
示例#3
0
        internal static Data.PropertyBlock ParsePropertyBlock(string content)
        {
            ErrorCollector errors = new ErrorCollector(null);
            ITokenSource   lexer  = new Grammar.StepBroLexer(new AntlrInputStream(content));
            ITokenStream   tokens = new CommonTokenStream(lexer);
            var            parser = new SBP(tokens);

            parser.AddErrorListener(errors);
            parser.BuildParseTree = true;
            StepBroListener listener = new StepBroListener(errors);
            var             context  = parser.elementPropertyblock();

            ParseTreeWalker walker = new ParseTreeWalker();

            walker.Walk(listener, context);

            if (errors.ErrorCount > 0)
            {
                throw new Exception("PARSING ERRORS");
            }

            return(listener.PopPropertyBlockData());
        }
示例#4
0
        internal VariableContainerAction CreateVariableContainerObjectInitAction(
            Type objectType, PropertyBlock properties, ErrorCollector errors, IToken startToken)
        {
            VariableContainerAction action = null;

            var resolver = new DefaultContractResolver();
            var contract = resolver.ResolveContract(objectType) as JsonObjectContract;

            var dataSetters     = new List <Expression>();
            var objectReference = Expression.Variable(objectType);

            foreach (var entry in properties)
            {
                if (entry.BlockEntryType == PropertyBlockEntryType.Value)
                {
                    var valueEntry = entry as PropertyBlockValue;
                    if (String.IsNullOrEmpty(valueEntry.SpecifiedTypeName))
                    {
                        var objectProperty = contract.Properties.FirstOrDefault(p => String.Equals(entry.Name, p.PropertyName, StringComparison.InvariantCulture));
                        if (objectProperty != null)
                        {
                            entry.Tag = "Property";
                            bool dataError = false;
                            System.Diagnostics.Debug.WriteLine($"Property type: {objectProperty.PropertyType.Name}");
                            Expression valueExpression = null;
                            object     value           = valueEntry.Value;
                            if (value != null)
                            {
                                if (objectProperty.PropertyType.IsEnum)
                                {
                                    if (value is Identifier)
                                    {
                                        value = ((Identifier)value).Name;
                                    }
                                    if (value is string)
                                    {
                                        if (((string)value).StartsWith(objectProperty.PropertyType.Name + ".", StringComparison.InvariantCulture))
                                        {
                                            value = ((string)value).Substring(objectProperty.PropertyType.Name.Length + 1);
                                        }
                                        try
                                        {
                                            value = Enum.Parse(objectProperty.PropertyType, (string)value);
                                        }
                                        catch
                                        {
                                            dataError = true;
                                            errors.SymanticError(startToken.Line, startToken.Column, false, $"Unknown value for property \"{entry.Name}\".");
                                        }
                                    }
                                    else if (value is long)
                                    {
                                    }
                                    else
                                    {
                                        dataError = true;
                                        errors.SymanticError(startToken.Line, startToken.Column, false, $"Unsupported value for property \"{entry.Name}\".");
                                    }
                                }
                                else if (objectProperty.PropertyType == typeof(string))
                                {
                                    if (value is Identifier)
                                    {
                                        value = ((Identifier)value).Name;
                                    }
                                }
                                else if (!objectProperty.PropertyType.IsPrimitive && objectProperty.PropertyType != typeof(TimeSpan) && objectProperty.PropertyType != typeof(DateTime))
                                {
                                    if (value is Identifier)
                                    {
                                        value = ((Identifier)value).Name;
                                        var resolved = this.ResolveIdentifierForGetOperation(
                                            (string)value,
                                            false,
                                            new TypeReference(objectProperty.PropertyType));
                                        if (resolved != null)
                                        {
                                            if (objectProperty.PropertyType.IsAssignableFrom(resolved.DataType.Type))
                                            {
                                                valueExpression = resolved.ExpressionCode;
                                            }
                                            else
                                            {
                                                dataError = true;
                                                errors.SymanticError(startToken.Line, startToken.Column, false, $"Unresolved value for property \"{entry.Name}\": '{(string)value}'.");
                                            }
                                        }
                                        else
                                        {
                                            dataError = true;
                                            errors.SymanticError(startToken.Line, startToken.Column, false, $"Unresolved value for property \"{entry.Name}\": '{(string)value}'.");
                                        }
                                    }
                                    else
                                    {
                                        dataError = true;
                                        errors.SymanticError(startToken.Line, startToken.Column, false, $"Unsupported value type for property \"{entry.Name}\": '{(string)value.GetType().Name}'.");
                                    }
                                }
                            }
                            if (!dataError)
                            {
                                var vt = (valueExpression != null) ? valueExpression.Type : value?.GetType();
                                if (vt != null && !objectProperty.PropertyType.IsAssignableFrom(vt))
                                {
                                    errors.IncompatibleDataType(startToken.Line, startToken.Column, vt.Name, objectProperty.PropertyType.Name);
                                }
                                else
                                {
                                    if (valueExpression == null)
                                    {
                                        valueExpression = Expression.Constant(value, objectProperty.PropertyType);
                                    }
                                    dataSetters.Add(Expression.Assign(
                                                        Expression.Property(objectReference, objectProperty.UnderlyingName),
                                                        valueExpression));
                                }
                            }
                        }
                        else
                        {
                            errors.SymanticError(startToken.Line, startToken.Column, false, $"The object has no property named \"{entry.Name}\".");
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    // Not handled yet; just let it fall through.
                }
            }
            if (dataSetters.Count > 0)
            {
                try
                {
                    LabelTarget returnLabel           = Expression.Label(typeof(bool));
                    var         parameterContainer    = Expression.Parameter(typeof(IValueContainerOwnerAccess), "container");
                    var         parameterLogger       = Expression.Parameter(typeof(ILogger), "logger");
                    var         getContainerReference = Expression.Property(
                        parameterContainer,
                        typeof(IValueContainerOwnerAccess).GetProperty(nameof(IValueContainerOwnerAccess.Container)));
                    var getObjectReference = Expression.Call(
                        getContainerReference,
                        typeof(IValueContainer).GetMethod(nameof(IValueContainer.GetValue), new Type[] { typeof(ILogger) }),
                        parameterLogger);
                    var objectReferenceAssignment = Expression.Assign(
                        objectReference,
                        Expression.Convert(getObjectReference, objectType));
                    var expressions = new List <Expression>(dataSetters);
                    expressions.Insert(0, objectReferenceAssignment);

                    if (objectType.GetInterface(nameof(ISettableFromPropertyBlock)) != null)
                    {
                        var setterHelper = typeof(ExecutionHelperMethods).GetMethod(
                            nameof(ExecutionHelperMethods.SetupObjectWithPropertyBlock));

                        var propertyBlockSetter = Expression.Call(setterHelper, parameterLogger, parameterContainer);
                        expressions.Add(propertyBlockSetter);
                    }

                    expressions.Add(Expression.Label(returnLabel, Expression.Constant(true)));

                    var lambdaExpr = Expression.Lambda(
                        typeof(VariableContainerAction),
                        Expression.Block(new ParameterExpression[] { objectReference }, expressions),
                        parameterContainer,
                        parameterLogger);
                    var @delegate = lambdaExpr.Compile();
                    action = (VariableContainerAction)@delegate;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(action);
        }
示例#5
0
 public StepBroListener(ErrorCollector errorCollector)
 {
     m_errors       = errorCollector;
     m_addonManager = null;
     m_file         = null;
 }