public StyleValueImporter(AssetImportContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            m_Context   = context;
            m_AssetPath = context.assetPath;
            m_Parser    = new Parser();
            m_Builder   = new StyleSheetBuilder();
            m_Errors    = new StyleSheetImportErrors();
            m_Validator = new StyleValidator();
        }
        protected virtual void OnImportError(StyleSheetImportErrors errors)
        {
            if (m_Context == null)
            {
                return;
            }

            foreach (var e in errors)
            {
                if (e.isWarning)
                {
                    m_Context.LogImportWarning(e.ToString(), e.assetPath, e.line);
                }
                else
                {
                    m_Context.LogImportError(e.ToString(), e.assetPath, e.line);
                }
            }
        }
Example #3
0
        internal static void VisitValue(StyleSheetImportErrors errors, StyleSheetBuilder ssb, Term term)
        {
            PrimitiveTerm   primitiveTerm   = term as PrimitiveTerm;
            HtmlColor       htmlColor       = term as HtmlColor;
            GenericFunction genericFunction = term as GenericFunction;
            TermList        termList        = term as TermList;

            if (term == Term.Inherit)
            {
                ssb.AddValue(StyleValueKeyword.Inherit);
            }
            else if (primitiveTerm != null)
            {
                string   text          = term.ToString();
                UnitType primitiveType = primitiveTerm.PrimitiveType;
                switch (primitiveType)
                {
                case UnitType.String:
                {
                    string value = text.Trim(new char[]
                        {
                            '\'',
                            '"'
                        });
                    ssb.AddValue(value, StyleValueType.String);
                    goto IL_F9;
                }

                case UnitType.Uri:
IL_63:
                    if (primitiveType != UnitType.Number && primitiveType != UnitType.Pixel)
                    {
                        errors.AddSemanticError(StyleSheetImportErrorCode.UnsupportedUnit, primitiveTerm.ToString());
                        return;
                    }
                    ssb.AddValue(primitiveTerm.GetFloatValue(UnitType.Pixel).Value);
                    goto IL_F9;

                case UnitType.Ident:
                {
                    StyleValueKeyword keyword;
                    if (StyleSheetImporterImpl.TryParseKeyword(text, out keyword))
                    {
                        ssb.AddValue(keyword);
                    }
                    else
                    {
                        ssb.AddValue(text, StyleValueType.Enum);
                    }
                    goto IL_F9;
                }
                }
                goto IL_63;
                IL_F9 :;
            }
            else if (htmlColor != null)
            {
                Color value2 = new Color((float)htmlColor.R / 255f, (float)htmlColor.G / 255f, (float)htmlColor.B / 255f, (float)htmlColor.A / 255f);
                ssb.AddValue(value2);
            }
            else if (genericFunction != null)
            {
                primitiveTerm = (genericFunction.Arguments.FirstOrDefault <Term>() as PrimitiveTerm);
                if (genericFunction.Name == "resource" && primitiveTerm != null)
                {
                    string value3 = primitiveTerm.Value as string;
                    ssb.AddValue(value3, StyleValueType.ResourcePath);
                }
                else
                {
                    errors.AddSemanticError(StyleSheetImportErrorCode.UnsupportedFunction, genericFunction.Name);
                }
            }
            else if (termList != null)
            {
                foreach (Term current in termList)
                {
                    StyleSheetImporterImpl.VisitValue(errors, ssb, current);
                }
            }
            else
            {
                errors.AddInternalError(term.GetType().Name);
            }
        }
Example #4
0
 public StyleSheetImporterImpl()
 {
     this.m_Parser  = new Parser();
     this.m_Builder = new StyleSheetBuilder();
     this.m_Errors  = new StyleSheetImportErrors();
 }
Example #5
0
        internal static void VisitValue(StyleSheetImportErrors errors, StyleSheetBuilder ssb, Term term)
        {
            var primitiveTerm = term as PrimitiveTerm;
            var colorTerm     = term as HtmlColor;
            var funcTerm      = term as GenericFunction;
            var termList      = term as TermList;

            if (term == PrimitiveTerm.Inherit)
            {
                ssb.AddValue(StyleValueKeyword.Inherit);
            }
            else if (primitiveTerm != null)
            {
                string rawStr = term.ToString();

                switch (primitiveTerm.PrimitiveType)
                {
                case UnitType.Pixel:
                case UnitType.Number:
                    float?floatValue = primitiveTerm.GetFloatValue(UnitType.Pixel);
                    ssb.AddValue(floatValue.Value);
                    break;

                case UnitType.Ident:
                    StyleValueKeyword keyword;
                    if (TryParseKeyword(rawStr, out keyword))
                    {
                        ssb.AddValue(keyword);
                    }
                    else
                    {
                        ssb.AddValue(rawStr, StyleValueType.Enum);
                    }
                    break;

                case UnitType.String:
                    string unquotedStr = rawStr.Trim('\'', '\"');
                    ssb.AddValue(unquotedStr, StyleValueType.String);
                    break;

                default:
                    errors.AddSemanticError(StyleSheetImportErrorCode.UnsupportedUnit, primitiveTerm.ToString());
                    return;
                }
            }
            else if (colorTerm != null)
            {
                var color = new Color((float)colorTerm.R / 255.0f, (float)colorTerm.G / 255.0f, (float)colorTerm.B / 255.0f, (float)colorTerm.A / 255.0f);
                ssb.AddValue(color);
            }
            else if (funcTerm != null)
            {
                primitiveTerm = funcTerm.Arguments.FirstOrDefault() as PrimitiveTerm;
                if (funcTerm.Name == k_ResourcePathFunctionName && primitiveTerm != null)
                {
                    string path = primitiveTerm.Value as string;
                    ssb.AddValue(path, StyleValueType.ResourcePath);
                }
                else
                {
                    errors.AddSemanticError(StyleSheetImportErrorCode.UnsupportedFunction, funcTerm.Name);
                }
            }
            else if (termList != null)
            {
                foreach (Term childTerm in termList)
                {
                    VisitValue(errors, ssb, childTerm);
                }
                return;
            }
            else
            {
                errors.AddInternalError(term.GetType().Name);
            }
        }