/// <summary> /// Generates a slice literal of the specified type name and optional values. /// </summary> /// <param name="typeName">The fully qualified slice type name.</param> /// <param name="values">Optional slice values. Pass null to omit values.</param> /// <returns>The root node in this slice literal AST.</returns> public static Node Generate(string typeName, IEnumerable <Node> values) { if (string.IsNullOrWhiteSpace(typeName)) { throw new ArgumentException(nameof(typeName)); } if (values != null && !values.Any()) { throw new ArgumentException("pass null for no slice elements"); } var openBracket = new OpenDelimiter(BinaryDelimiterType.Bracket); var closeBracket = openBracket.AddClosingDelimiter(); var id = new Identifier(typeName); closeBracket.AddChild(id); var openBrace = new OpenDelimiter(BinaryDelimiterType.Brace); if (values != null) { var seq = DelimitedSequence.Generate(UnaryDelimiterType.Comma, values.ToList()); openBrace.AddChild(seq); } openBrace.AddClosingDelimiter(); id.AddChild(openBrace); return(openBracket); }
/// <summary> /// Generates a struct definition with the specified name and fields. /// If the struct is to contain no fields pass null for the fields parameter. /// </summary> /// <param name="name">The name of the struct to be created.</param> /// <param name="fields">Optional list of fields. Pass null if the struct has no fields.</param> /// <returns>Root node for this struct definition's AST.</returns> public static Node Generate(string name, IReadOnlyList <StructFieldDef> fields) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException(nameof(name)); } if (fields != null && fields.Count == 0) { throw new ArgumentException("pass null for no struct fields"); } var typeDef = new TypeDef(); typeDef.AddChild(new Identifier(name)); var structDef = new StructDef(); var defStart = new OpenDelimiter(BinaryDelimiterType.Brace); if (fields != null) { foreach (var field in fields) { var id = new Identifier(field.Name); id.AddChild(new TypeName(field.TypeName)); if (field.Tag != null) { id.AddChild(new Tag(field.Tag)); } defStart.AddChild(id); } } defStart.AddClosingDelimiter(); structDef.AddChild(defStart); typeDef.AddChild(structDef); return(typeDef); }
/// <summary> /// Generates a function call for the specified values. /// Example: package.Func(one, &two, three) /// </summary> /// <param name="name">The fully qualified name of the function to call.</param> /// <param name="parameters">The optional list of parameters. Pass null if there are no parameters.</param> /// <returns>The root node for this function call AST.</returns> public static Node Generate(string name, IReadOnlyList <FuncCallParam> parameters) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException(nameof(name)); } if (parameters != null && parameters.Count == 0) { throw new ArgumentException("pass null if there are no arguments"); } var funcCall = new Identifier(name); var paramsStart = new OpenDelimiter(BinaryDelimiterType.Paren); funcCall.AddChild(paramsStart); if (parameters != null) { var seq = new Node[parameters.Count]; // build a comma separated list of params for (int i = 0; i < parameters.Count; ++i) { var param = parameters[i]; Node p = null; if (param.Pass == TypeModifier.ByReference) { p = UnaryOpSequence.Generate(UnaryOperatorType.Ampersand, param.Param); } else if (param.Pass == TypeModifier.ByValue) { p = param.Param; } seq[i] = p; } paramsStart.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, seq)); } paramsStart.AddClosingDelimiter(); return(funcCall); }
/// <summary> /// Generates a struct literal of the specified type name and optional field values. /// </summary> /// <param name="structTypeName">The fully qualified struct type name.</param> /// <param name="values">Optional field values. Pass null to omit field initialization.</param> /// <returns>The root node in this struct literal AST.</returns> public static Node Generate(string structTypeName, IEnumerable <StructField> values) { if (string.IsNullOrWhiteSpace(structTypeName)) { throw new ArgumentException(nameof(structTypeName)); } if (values != null && !values.Any()) { throw new ArgumentException("pass null to omit struct field initializers"); } var id = new Identifier(structTypeName); var openBrace = new OpenDelimiter(BinaryDelimiterType.Brace); if (values != null) { var initList = new List <Node>(); foreach (var val in values) { var root = DelimitedSequence.Generate(UnaryDelimiterType.Colon, new[] { new Identifier(val.FieldName), val.Value }); initList.Add(root); } var seqRoot = DelimitedSequence.Generate(UnaryDelimiterType.Comma, initList, true); openBrace.AddChild(seqRoot); } openBrace.AddClosingDelimiter(); id.AddChild(openBrace); return(id); }