/// <summary> /// space_delimited_expressions: expression (WS expression)* important? ; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> /// <returns></returns> private IEnumerable<INode> SpaceDelimitedExpressions(LessPegNode node, ElementBlock elementBlock) { yield return Expression(node.Child, elementBlock); node = node.Next; //Tail while (node != null) { switch (node.Type()) { case EnLess.expression: yield return Expression(node.Child, elementBlock); break; case EnLess.important: yield return new Keyword("!important"); break; } node = node.Next; } }
/// <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> /// 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> /// 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> /// expression: '(' s expressions s ')' / entity ; /// </summary> /// <param name="node"></param> /// <param name="elementBlock"></param> /// <returns></returns> private INode Expression(LessPegNode node, ElementBlock elementBlock) { switch (node.Type()) { case EnLess.expressions: return new Expression(Expressions(node, elementBlock), elementBlock); case EnLess.entity: var entity = Entity(node.Child, elementBlock); entity.Parent = elementBlock; return entity; default: throw new ParsingException("Expression should either be child expressions or an entity"); } }
/// <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)); } }