Exemple #1
0
        /// <inheritdoc />
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString(nameof(Location), Location.ToFormatString());
            if (FormatterName != null)
            {
                writer.WriteAttributeString(nameof(FormatterName), FormatterName);
            }

            if (PathParts.Any())
            {
                var pathStr = string.Join(",", PathParts.Select(f =>
                {
                    if (f.Key != null)
                    {
                        return("{" + f.Value + ";" + f.Key + "}");
                    }
                    return("{" + f.Value + "}");
                }));
                writer.WriteAttributeString(nameof(PathParts), pathStr);
            }
            foreach (var expressionArgument in Formats)
            {
                writer.WriteStartElement("Format");
                expressionArgument.WriteXml(writer);
                writer.WriteEndElement();                //</Format>
            }
        }
Exemple #2
0
        /// <inheritdoc />
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString(nameof(Location), Location.ToFormatString());
            if (EndsWithDelimiter)
            {
                writer.WriteAttributeString(nameof(EndsWithDelimiter), bool.TrueString);
            }

            if (PathParts.Any())
            {
                writer.WriteStartElement("Path");
                foreach (var pathPart in PathParts.ToArray())
                {
                    writer.WriteElementString(pathPart.Value.ToString(), pathPart.Key);
                }
                writer.WriteEndElement();                //</Path>
            }
            if (FormatterName != null)
            {
                writer.WriteStartElement("Format");
                writer.WriteAttributeString(nameof(FormatterName), FormatterName);
                foreach (var expressionArgument in Formats)
                {
                    writer.WriteStartElement("Argument");
                    expressionArgument.WriteXml(writer);
                    writer.WriteEndElement();            //</Argument>
                }
                writer.WriteEndElement();                //</Format>
            }
        }
Exemple #3
0
        private bool ComputeCurrentPart(TokenzierContext context, int index)
        {
            var checkPathPart = CheckPathPart();

            if (checkPathPart != -1)
            {
                context.Errors.Add(
                    new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                               .AddWindow(new CharacterSnippedLocation(1, checkPathPart, CurrentPart)),
                                               CurrentPart));

                return(false);
            }

            if (CurrentPart == "null")
            {
                if (PathParts.Any())
                {
                    context.Errors.Add(
                        new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                                   .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                                   CurrentPart,
                                                   "An null must be at the start of an expression"));

                    return(false);
                }

                PathParts.Add(new KeyValuePair <string, PathType>(null, PathType.Null));
            }
            else if (CurrentPart == "true" || CurrentPart == "false")
            {
                if (PathParts.Any())
                {
                    context.Errors.Add(
                        new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                                   .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                                   CurrentPart,
                                                   "An boolean must be at the start of an expression"));

                    return(false);
                }

                PathParts.Add(new KeyValuePair <string, PathType>(CurrentPart, PathType.Boolean));
            }
            else
            {
                PathParts.Add(new KeyValuePair <string, PathType>(CurrentPart, PathType.DataPath));
            }

            return(true);
        }
Exemple #4
0
        /// <inheritdoc />
        public async ContextObjectPromise GetValue(ContextObject contextObject, ScopeData scopeData)
        {
            if (!PathParts.Any() && Formats.Count == 1 && FormatterName == "")
            {
                //indicates the usage of brackets
                return(await Formats[0].GetValue(contextObject, scopeData));
            }

            var contextForPath = contextObject.GetContextForPath(PathParts, scopeData, this);

            if (!Formats.Any() && FormatterName == null)
            {
                return(contextForPath);
            }

            if (contextForPath == contextObject)
            {
                contextForPath = contextObject.CloneForEdit();
            }

            var arguments    = new FormatterArgumentType[Formats.Count];
            var naturalValue = contextObject.FindNextNaturalContextObject();

            for (var index = 0; index < Formats.Count; index++)
            {
                var formatterArgument = Formats[index];
                var value             = await formatterArgument.MorestachioExpression.GetValue(naturalValue, scopeData);

                arguments[index] = new FormatterArgumentType(index, formatterArgument.Name, value?.Value);
            }
            //contextForPath.Value = await contextForPath.Format(FormatterName, argList, scopeData);

            if (Cache == null)
            {
                Cache = contextForPath.PrepareFormatterCall(
                    contextForPath.Value?.GetType() ?? typeof(object),
                    FormatterName,
                    arguments,
                    scopeData);
            }

            if (Cache != null /* && !Equals(Cache.Value, default(FormatterCache))*/)
            {
                contextForPath.Value = await scopeData.ParserOptions.Formatters.Execute(Cache, contextForPath.Value, scopeData.ParserOptions, arguments);

                contextForPath.MakeSyntetic();
            }
            return(contextForPath);
        }
Exemple #5
0
        public bool Add(char c, TokenzierContext context, int index)
        {
            if (!Tokenizer.IsExpressionChar(c))
            {
                context.Errors.Add(
                    new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                               .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                               CurrentPart));
                return(false);
            }

            if (PathParts.Any(f => f.Value == PathType.Null))
            {
                context.Errors.Add(
                    new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                               .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                               CurrentPart,
                                               "Nothing can follow on a null"));
            }

            LastCharWasDelimiter = c == '.';

            if (c == '/')
            {
                if (CurrentPart == "..")
                {
                    if (PathParts.Any() && PathParts.Any(e => e.Value != PathType.ParentSelector))
                    {
                        context.Errors.Add(
                            new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                                       .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                                       CurrentPart,
                                                       "An Parent selector '..\\' can only follow on another parent selector like and never on an root or an data selector"));

                        return(false);
                    }
                    PathParts.Add(new KeyValuePair <string, PathType>(null, PathType.ParentSelector));
                    CurrentPart = "";
                    return(true);
                }
                context.Errors.Add(
                    new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                               .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                               CurrentPart,
                                               "Unexpected '/'. Expected ether the start of an expression or an './'"));
                return(false);
            }

            if (c == '~')
            {
                if (CurrentPart != string.Empty || PathParts.Any())
                {
                    context.Errors.Add(
                        new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                                   .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                                   CurrentPart,
                                                   "An root selector '~' must be at the start of an expression"));

                    return(false);
                }

                PathParts.Add(new KeyValuePair <string, PathType>(null, PathType.RootSelector));
                CurrentPart = "";
                return(true);
            }

            if (c == '?')
            {
                PathParts.Add(new KeyValuePair <string, PathType>(null, PathType.ObjectSelector));
                CurrentPart = "";
                return(true);
            }

            if (c != '.' && CurrentPart == "." && PathParts.Count == 0)
            {
                //in this case somebody wrote .data
                //so ignore the dot
                CurrentPart = "";
            }

            if (CurrentPart != "" && CurrentPart != "." && c == '.')
            {
                if (CurrentPartIsNumber)
                {
                    if (CurrentPart.Contains("."))
                    {
                        PathParts.Add(new KeyValuePair <string, PathType>(CurrentPart, PathType.Number));
                        CurrentPart = "";
                    }
                    else
                    {
                        CurrentPart += c;
                    }

                    return(true);
                }

                if (!ComputeCurrentPart(context, index))
                {
                    return(false);
                }

                CurrentPart = "";
            }
            else
            {
                if (CurrentPart == string.Empty && char.IsDigit(c))
                {
                    CurrentPartIsNumber = true;
                    if (PathParts.Any())
                    {
                        context.Errors.Add(
                            new InvalidPathSyntaxError(context.CurrentLocation.Offset(index)
                                                       .AddWindow(new CharacterSnippedLocation(1, index, CurrentPart)),
                                                       CurrentPart,
                                                       "A number expression must be at the start of the expression and cannot follow on anything else"));

                        return(false);
                    }
                }

                CurrentPart += c;
            }

            return(true);
        }