private bool TryParseComponentPosition(string s, FileRange range, out ComponentPosition componentPosition, out string remaining)
 {
     if (s.StartsWith("_Start", StringComparison.OrdinalIgnoreCase))
     {
         componentPosition = ComponentPosition.Start;
         remaining         = s.Substring("_Start".Length);
         return(true);
     }
     else if (s.StartsWith("_Middle", StringComparison.OrdinalIgnoreCase))
     {
         componentPosition = ComponentPosition.Middle;
         remaining         = s.Substring("_Middle".Length);
         return(true);
     }
     else if (s.StartsWith("_End", StringComparison.OrdinalIgnoreCase))
     {
         componentPosition = ComponentPosition.End;
         remaining         = s.Substring("_End".Length);
         return(true);
     }
     else
     {
         _logger.Log(LogLevel.Error, range, $"Invalid point '{s}' (expected a string beginning with '_Start', '_Middle' or '_End'", null);
         componentPosition = ComponentPosition.Absolute;
         remaining         = null;
         return(false);
     }
 }
Example #2
0
        public void Log(LogLevel level, FileRange position, string message, Exception innerException)
        {
            if (level >= LogLevel.Error)
            {
                HasErrors = true;
            }

            underlying.Log(level, position, message, innerException);
        }
Example #3
0
        private bool ReportUndeclared(FileRange position, ISet <string> allUndeclared)
        {
            if (allUndeclared.Any())
            {
                foreach (var undeclared in allUndeclared)
                {
                    logger.Log(LogLevel.Error, position, $"Usage of variable not present in the enclosing 'whenDefined' attribute: ${undeclared}", null);
                }
                return(false);
            }

            return(true);
        }
        protected override bool TryParseOffset(string offset, OffsetAxis axis, FileRange range, out IXmlComponentPointOffset result)
        {
            if (!offset.Contains("$"))
            {
                // Does not contain a variable
                return(base.TryParseOffset(offset, axis, range, out result));
            }

            offset = offset.Replace("(", "").Replace(")", "");

            var variableIndex = offset.IndexOf("$");
            var variableName  = offset.Substring(variableIndex + 1);

            // Check variable exists
            if (!definitionsSection.Definitions.TryGetValue(variableName, out var variableValues))
            {
                logger.Log(LogLevel.Error, range, $"Variable '{variableName}' does not exist", null);
                result = null;
                return(false);
            }

            // Check all possible values are valid
            var parsedValues = new ConditionalCollection <double>();

            foreach (var variableValue in variableValues)
            {
                if (!double.TryParse(variableValue.Value, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var parsedValue))
                {
                    logger.Log(LogLevel.Error, range, $"Value '{variableValue.Value}' for ${variableName} is not a valid decimal", null);
                    result = null;
                    return(false);
                }

                parsedValues.Add(new TypeDescription.Conditions.Conditional <double>(parsedValue, variableValue.Conditions));
            }

            result = new ComponentPointOffsetWithDefinition(offset.First() == '-', parsedValues, axis);
            return(true);
        }
Example #5
0
 public static bool Parse(this IConditionParser parser, XAttribute conditionsAttribute, ComponentDescription description, IXmlLoadLogger logger, out IConditionTreeItem value)
 {
     try
     {
         value = parser.Parse(description, conditionsAttribute.Value);
         return(true);
     }
     catch (ConditionFormatException ex)
     {
         IXmlLineInfo line     = conditionsAttribute;
         int          startCol = line.LinePosition + conditionsAttribute.Name.LocalName.Length + 2 + ex.PositionStart;
         var          position = new FileRange(line.LineNumber, startCol, line.LineNumber, startCol + ex.Length);
         logger.Log(LogLevel.Error, position, ex.Message, null);
         value = null;
         return(false);
     }
 }
Example #6
0
        public bool Load(Stream stream, IXmlLoadLogger logger, out ComponentDescription description)
        {
            description = new ComponentDescription();

            if (stream is FileStream fs)
            {
                description.Source = new ComponentDescriptionSource(fs.Name);
            }

            var errorCheckingLogger = new ErrorCheckingLogger(logger);
            var sectionRegistry     = new SectionRegistry();

            try
            {
                var root = XElement.Load(stream, LoadOptions.SetLineInfo);
                ReadHeader(root, description);

                var featureSwitcher = new FeatureSwitcher();

                // Read declaration
                var declaration = root.Element(ComponentNamespace + "declaration");
                if (declaration == null)
                {
                    logger.LogError(root, "Missing required element: 'declaration'");
                    return(false);
                }
                var declarationReader = container.Value.ResolveNamed <IXmlSectionReader>(declaration.Name.NamespaceName + ":" + declaration.Name.LocalName,
                                                                                         new TypedParameter(typeof(IXmlLoadLogger), errorCheckingLogger),
                                                                                         new TypedParameter(typeof(FeatureSwitcher), featureSwitcher));
                declarationReader.ReadSection(declaration, description);

                var descriptionInstance = description;
                var scope = container.Value.BeginLifetimeScope(configure =>
                {
                    configure.RegisterInstance(errorCheckingLogger).As <IXmlLoadLogger>();
                    configure.RegisterInstance(sectionRegistry).As <ISectionRegistry>();
                    configure.RegisterInstance(descriptionInstance);
                    configure.RegisterInstance(featureSwitcher).As <IFeatureSwitcher>();

                    foreach (var feature in features)
                    {
                        if (featureSwitcher.IsFeatureEnabled(feature.Key, out var featureSourceElement))
                        {
                            logger.Log(LogLevel.Information, featureSourceElement, $"Feature enabled: {feature.Key}");
                            feature.Value(configure);
                        }
                    }
                });

                try
                {
                    // Read remaining elements
                    foreach (var element in root.Elements().Except(new[] { declaration }))
                    {
                        var sectionReader = scope.ResolveOptionalNamed <IXmlSectionReader>(element.Name.NamespaceName + ":" + element.Name.LocalName);
                        sectionReader?.ReadSection(element, description);
                    }

                    return(!errorCheckingLogger.HasErrors);
                }
                finally
                {
                    scope.Dispose();
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, new FileRange(1, 1, 1, 2), ex.Message, ex);
                return(false);
            }
        }
Example #7
0
 public static void LogError(this IXmlLoadLogger logger, XAttribute attribute, string message)
 {
     logger.Log(LogLevel.Error, attribute, message);
 }
Example #8
0
 public static void LogWarning(this IXmlLoadLogger logger, XAttribute attribute, string message)
 {
     logger.Log(LogLevel.Warning, attribute, message);
 }
Example #9
0
 public static void LogError(this IXmlLoadLogger logger, XElement element, string message)
 {
     logger.Log(LogLevel.Error, element, message);
 }
Example #10
0
 public static void LogWarning(this IXmlLoadLogger logger, XElement element, string message)
 {
     logger.Log(LogLevel.Warning, element, message);
 }
Example #11
0
 public static void Log(this IXmlLoadLogger logger, LogLevel level, XAttribute attribute, string message)
 {
     logger.Log(level, attribute.GetFileRange(), message, null);
 }
Example #12
0
 public static void Log(this IXmlLoadLogger logger, LogLevel level, XElement element, string message)
 {
     logger.Log(level, element.GetFileRange(), message, null);
 }
        public bool TryParse(string x, string y, IXmlLineInfo xLine, IXmlLineInfo yLine, out ComponentPointTemplate componentPointTemplate)
        {
            var errors = new StringWriter();

            try
            {
                var lexer           = new ComponentPointLexer(new AntlrInputStream(new StringReader(x)), errors, errors);
                var parser          = new ComponentPointParser(new CommonTokenStream(lexer), errors, errors);
                var ctx             = parser.r();
                var xPosition       = (ComponentPosition)Enum.Parse(typeof(ComponentPosition), ctx.position().GetText(), true);
                var xModifiers      = new List <IModifierToken>();
                var currentModifier = ctx.modifiers();
                while (currentModifier.modifier() != null)
                {
                    var  modifier = currentModifier.modifier();
                    bool positive = modifier.plusminus().GetText() == "+";

                    if (modifier.CONSTANT() != null)
                    {
                        xModifiers.Add(new ConstantModifierToken
                        {
                            Offset = (positive ? 1 : -1) * double.Parse(modifier.CONSTANT().GetText())
                        });
                    }
                    else
                    {
                        xModifiers.Add(new DefinitionModifierToken
                        {
                            Negated = !positive,
                            Name    = modifier.variable().VARIABLENAME().GetText()
                        });
                    }

                    currentModifier = currentModifier.modifiers();
                }

                lexer  = new ComponentPointLexer(new AntlrInputStream(new StringReader(y)), errors, errors);
                parser = new ComponentPointParser(new CommonTokenStream(lexer), errors, errors);
                ctx    = parser.r();
                var yPosition  = (ComponentPosition)Enum.Parse(typeof(ComponentPosition), ctx.position().GetText(), true);
                var yModifiers = new List <IModifierToken>();
                currentModifier = ctx.modifiers();
                while (currentModifier.modifier() != null)
                {
                    var  modifier = currentModifier.modifier();
                    bool positive = modifier.plusminus().GetText() == "+";

                    if (modifier.CONSTANT() != null)
                    {
                        yModifiers.Add(new ConstantModifierToken
                        {
                            Offset = (positive ? 1 : -1) * double.Parse(modifier.CONSTANT().GetText())
                        });
                    }
                    else
                    {
                        yModifiers.Add(new DefinitionModifierToken
                        {
                            Negated = !positive,
                            Name    = modifier.variable().VARIABLENAME().GetText()
                        });
                    }

                    currentModifier = currentModifier.modifiers();
                }

                componentPointTemplate = new ComponentPointTemplate(xPosition, yPosition, xModifiers, yModifiers);
                return(true);
            }
            catch
            {
                componentPointTemplate = null;
                return(false);
            }
            finally
            {
                foreach (var message in errors.ToString().Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var position = new FileRange(xLine.LineNumber, xLine.LinePosition, xLine.LineNumber, xLine.LinePosition + x.Length);
                    logger.Log(LogLevel.Error, position, message, null);
                }
            }
        }