/// <summary> /// variable: '@' [-_a-zA-Z0-9]+; /// </summary> /// <param name="node"></param> /// <returns></returns> private INode Variable(LessPegNode node) { return new Variable(node.GetAsString(Src)); }
/// <summary> /// number: '-'? [0-9]* '.' [0-9]+ / '-'? [0-9]+; /// </summary> /// <param name="node"></param> /// <returns></returns> private INode Number(LessPegNode node) { var val = float.Parse(node.GetAsString(Src), NumberFormatInfo.InvariantInfo); var unit = ""; node = node.Next; if (node != null && node.Type() == EnLess.unit) unit = node.GetAsString(Src); return new Number(unit, val); }
/// <summary> /// operation_expressions: expression (operator expression)+; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> /// <returns></returns> private IEnumerable<INode> OperationExpressions(LessPegNode node, ElementBlock elementBlock) { yield return Expression(node.Child, elementBlock); node = node.Next; //Tail while (node != null) { switch (node.Type()) { case EnLess.@operator: yield return new Operator(node.GetAsString(Src), elementBlock); break; case EnLess.expression: yield return Expression(node.Child, elementBlock); break; case EnLess.comment: node.ToString(); break; } node = node.Next; } }
/// <summary> /// keyword: [-a-zA-Z]+ !ns; /// </summary> /// <param name="node"></param> /// <returns></returns> private INode Keyword(LessPegNode node) { return new Keyword(node.GetAsString(Src)); }
/// <summary> /// expressions: operation_expressions / space_delimited_expressions / [-a-zA-Z0-9_%*/.&=:,#+? \[\]()]+ ; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> /// <returns></returns> private IEnumerable<INode> Expressions(LessPegNode node, ElementBlock elementBlock) { // Expression switch (node.Type()) { case EnLess.operation_expressions: return OperationExpressions(node.Child, elementBlock).ToList(); case EnLess.space_delimited_expressions: return SpaceDelimitedExpressions(node.Child, elementBlock).ToList(); default: if (node.Child == null) //CatchAll return new List<INode> { new Anonymous(node.GetAsString(Src)) }; return Expressions(node.Child, elementBlock); } }
/// <summary> /// entity : function / fonts / accessor / keyword / variable / literal ; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> /// <returns></returns> private INode Entity(LessPegNode node, ElementBlock elementBlock) { switch (node.Type()) { case EnLess.literal: return Entity(node.Child, elementBlock); case EnLess.number: return Number(node); case EnLess.color: return Color(node); case EnLess.variable: return Variable(node); case EnLess.accessor: return Accessor(node.Child, elementBlock); case EnLess.fonts: return Fonts(node); case EnLess.keyword: return Keyword(node); case EnLess.function: return Function(node, elementBlock); case EnLess.cursors: return Cursors(node); case EnLess.@string: return new String(node.GetAsString(Src)); default: return new Anonymous(node.GetAsString(Src)); } }
/// <summary> /// declaration: standard_declaration / catchall_declaration ; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> private void Declaration(LessPegNode node, ElementBlock elementBlock) { var name = node.GetAsString(Src).Replace(" ", ""); var nextNode = node.Next; if(nextNode == null){ // TODO: emit warning: empty declaration // return; } if (nextNode.Type() == EnLess.comment) nextNode = nextNode.Next; var values = Expressions(nextNode, elementBlock); var property = name.StartsWith("@") ? new Variable(name, values) : new Property(name, values); elementBlock.Add(property); }
/// <summary> /// accessor: accessor_name '[' accessor_key ']'; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> /// <returns></returns> private INode Accessor(LessPegNode node, ElementBlock elementBlock) { var ident = node.GetAsString(Src); var key = node.Next.GetAsString(Src).Replace("'", ""); var el = elementBlock.NearestAs<ElementBlock>(ident); if (el != null) { var prop = el.GetAs<Property>(key); if (prop != null) return prop.Value; } return new Anonymous(""); }