Example #1
0
    public void Parse_DotAccessExpression()
    {
        ASTNode root = ExpressionParser.Parse("someProperty.someValue");

        Assert.IsInstanceOf <MemberAccessExpressionNode>(root);
        MemberAccessExpressionNode node = (MemberAccessExpressionNode)root;

        Assert.AreEqual(1, node.parts.Count);
        DotAccessNode dotNode = node.parts[0] as DotAccessNode;

        Assert.AreEqual("someValue", dotNode.propertyName);
        Assert.AreEqual("someProperty", node.identifier);
    }
Example #2
0
        private StyleConstant Resolve(StyleCompileContext context, StyleConstant constant)
        {
            // shortcut return for constants that have been resolved already
            for (int index = 0; index < context.constants.Count; index++)
            {
                StyleConstant c = context.constants[index];
                if (c.name == constant.name)
                {
                    return(c);
                }
            }

            StyleConstant referencedConstant;

            if (constant.constReferenceNode.children.Count > 0)
            {
                if (context.importedStyleConstants.ContainsKey(constant.constReferenceNode.identifier))
                {
                    DotAccessNode importedConstant = (DotAccessNode)constant.constReferenceNode.children[0];

                    referencedConstant = context.importedStyleConstants[constant.constReferenceNode.identifier]
                                         .Find(importedConstant.propertyName, s_FindStyleConstant);

                    if (referencedConstant.name == null)
                    {
                        throw new CompileException(importedConstant, "Could not find referenced property in imported scope.");
                    }
                }
                else
                {
                    throw new CompileException(constant.constReferenceNode, "Constants cannot reference members of other constants.");
                }
            }
            else
            {
                referencedConstant = ResolveReference(context, constant.constReferenceNode);
            }

            StyleConstant styleConstant = new StyleConstant {
                name     = constant.name,
                value    = referencedConstant.value,
                exported = constant.exported
            };

            context.constants.Add(styleConstant);
            return(styleConstant);
        }
        /// <summary>
        /// Returns a referenced StyleASTNode if the passed in node is a ReferenceNode.
        /// Only called once all references have been resolved in the constants list.
        /// </summary>
        /// <param name="node">A node that can be a ReferenceNode or something else.</param>
        /// <returns>The referenced node or the node itself if it's a regular one.</returns>
        /// <exception cref="CompileException">thrown in case a reference cannot be resolved.</exception>
        public StyleASTNode GetValueForReference(StyleASTNode node)
        {
            if (node is ConstReferenceNode referenceNode)
            {
                for (int index = 0; index < constants.Count; index++)
                {
                    StyleConstant c = constants[index];
                    if (c.name == referenceNode.identifier)
                    {
                        return(c.value);
                    }
                }

                if (referenceNode.children.Count > 0)
                {
                    if (importedStyleConstants.ContainsKey(referenceNode.identifier))
                    {
                        DotAccessNode importedConstant = (DotAccessNode)referenceNode.children[0];

                        StyleConstant importedStyleConstant = importedStyleConstants[referenceNode.identifier]
                                                              .Find(importedConstant.propertyName, s_FindStyleConstant);

                        if (importedStyleConstant.name == null)
                        {
                            throw new CompileException(importedConstant, $"Could not find referenced property '{importedConstant.propertyName}' in imported scope '{referenceNode.identifier}'.");
                        }

                        return(importedStyleConstant.value);
                    }

                    throw new CompileException(referenceNode, "Constants cannot reference members of other constants.");
                }


                throw new CompileException(referenceNode, $"Couldn't resolve reference {referenceNode}. Known references are: {PrintConstants()}");
            }

            return(node);
        }