Ejemplo n.º 1
0
        public Result ResolveVarFunction(ref int index)
        {
            m_ResolvedVarStack.Clear();
            m_ValidationExpression = null;

            if (!m_Property.isCustomProperty)
            {
                string syntax;
                if (!StylePropertyCache.TryGetSyntax(m_Property.name, out syntax))
                {
                    Debug.LogAssertion($"Unknown style property {m_Property.name}");
                    return(Result.Invalid);
                }

                m_ValidationExpression = s_SyntaxParser.Parse(syntax);
            }

            int    argc;
            string varName;

            ParseVarFunction(m_Sheet, m_Handles, ref index, out argc, out varName);

            var result = ResolveVariable(varName);

            if (result != Result.Valid)
            {
                // var() fallback
                if (result == Result.NotFound && argc > 1 && !m_Property.isCustomProperty)
                {
                    var h = m_Handles[++index];
                    Debug.Assert(h.valueType == StyleValueType.FunctionSeparator, $"Unexpected value type {h.valueType} in var function");
                    if (h.valueType == StyleValueType.FunctionSeparator && index + 1 < m_Handles.Length)
                    {
                        ++index;
                        result = ResolveFallback(ref index);
                    }
                }
                else
                {
                    m_ResolvedValues.Clear();
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public bool ValidateResolvedValues()
        {
            // Cannot validate custom property before it's assigned so it's always true in this case
            if (m_Property.isCustomProperty)
            {
                return(true);
            }

            if (!StylePropertyCache.TryGetSyntax(m_Property.name, out var syntax))
            {
                Debug.LogAssertion($"Unknown style property {m_Property.name}");
                return(false);
            }

            var validationExpression = s_SyntaxParser.Parse(syntax);
            var result = m_Matcher.Match(validationExpression, m_ResolvedValues);

            return(result.success);
        }
        public static List <string> GetStyleKeywords(string binding)
        {
            if (string.IsNullOrEmpty(binding))
            {
                return(StyleFieldConstants.KLDefault);
            }

            var syntaxParser = new StyleSyntaxParser();

#if UNITY_2019_3_OR_NEWER
            var syntaxFound = StylePropertyCache.TryGetSyntax(binding, out var syntax);
#else
            var syntaxFound = StyleFieldConstants.StylePropertySyntaxCache.TryGetValue(binding, out var syntax);
#endif
            if (!syntaxFound)
            {
                return(StyleFieldConstants.KLDefault);
            }

            var expression = syntaxParser.Parse(syntax);
            if (expression == null)
            {
                return(StyleFieldConstants.KLDefault);
            }

            var hasAuto = FindKeywordInExpression(expression, StyleFieldConstants.KeywordAuto);
            var hasNone = FindKeywordInExpression(expression, StyleFieldConstants.KeywordNone);

            if (hasAuto)
            {
                return(StyleFieldConstants.KLAuto);
            }
            else if (hasNone)
            {
                return(StyleFieldConstants.KLNone);
            }

            return(StyleFieldConstants.KLDefault);
        }
Ejemplo n.º 4
0
        public StyleValidationResult ValidateProperty(string name, string value)
        {
            var result = new StyleValidationResult()
            {
                status = StyleValidationStatus.Ok
            };

            // Bypass custom styles
            if (name.StartsWith("--"))
            {
                return(result);
            }

            string syntax;

            if (!StylePropertyCache.TryGetSyntax(name, out syntax))
            {
                string closestName = StylePropertyCache.FindClosestPropertyName(name);
                result.status  = StyleValidationStatus.Error;
                result.message = $"Unknown property '{name}'";
                if (!string.IsNullOrEmpty(closestName))
                {
                    result.message = $"{result.message} (did you mean '{closestName}'?)";
                }

                return(result);
            }

            var syntaxTree = m_SyntaxParser.Parse(syntax);

            if (syntaxTree == null)
            {
                result.status  = StyleValidationStatus.Error;
                result.message = $"Invalid '{name}' property syntax '{syntax}'";
                return(result);
            }

            var matchResult = m_StyleMatcher.Match(syntaxTree, value);

            if (!matchResult.success)
            {
                result.errorValue = matchResult.errorValue;
                switch (matchResult.errorCode)
                {
                case MatchResultErrorCode.Syntax:
                    result.status = StyleValidationStatus.Error;
                    if (IsUnitMissing(syntax, value))
                    {
                        result.hint = "Property expects a unit. Did you forget to add px or %?";
                    }
                    else if (IsUnsupportedColor(syntax))
                    {
                        result.hint = $"Unsupported color '{value}'.";
                    }
                    result.message = $"Expected ({syntax}) but found '{matchResult.errorValue}'";
                    break;

                case MatchResultErrorCode.EmptyValue:
                    result.status  = StyleValidationStatus.Error;
                    result.message = $"Expected ({syntax}) but found empty value";
                    break;

                case MatchResultErrorCode.ExpectedEndOfValue:
                    result.status  = StyleValidationStatus.Warning;
                    result.message = $"Expected end of value but found '{matchResult.errorValue}'";
                    break;

                default:
                    Debug.LogAssertion($"Unexpected error code '{matchResult.errorCode}'");
                    break;
                }
            }

            return(result);
        }