public override XmlSchemaObject VisitASTAlias(ASTAlias astAlias) { var _modifier = astAlias.Type.First().Value; var _type = astAlias.Type.Last().Value; if (_modifier == "List") { var list = Mapper.MapList(astAlias); Schema.Items.Add(list); return(list); } var result = _type switch { "String" => Mapper.MapString(astAlias), "Number" => Mapper.MapNumber(astAlias), "Boolean" => Mapper.MapBoolean(astAlias), "Date" => Mapper.MapDate(astAlias), "Time" => Mapper.MapDate(astAlias), "DateTime" => Mapper.MapDate(astAlias), _ => Mapper.MapString(astAlias), }; Schema.Items.Add(result); ExtractElement(astAlias); return(result); }
public void RestrictionCommentsInAlias() { var code = @" alias Name = String @ Might need to be 1 & min 2 @ Will change to 30 later on & max 23 ; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; foreach (ASTRestriction restriction in alias.Restrictions) { Assert.Single(restriction.Annotations); Assert.Equal(TokenType.Number, restriction.Token.TokenType); } }
public void RestrictionCommentsInAlias() { var code = @" alias Name = String @ Might need to be 1 & min 2 @ Will change to 30 later on & max 23 ; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; alias.Restrictions.ForEach(restriction => { Assert.Single(restriction.Annotations); }); }
private void ResolveImports() { this.Imports = new List <IASTNode>(); var imports = Generator.AST.FindAll(n => n is ASTImport).ToList(); imports.ForEach(node => { ASTImport import = (ASTImport)node; var ast = this.Project.GetAstForModule(import.Name); if (!import.Imports.Any()) { var copies = ast .FindAll(a => a is ASTType || a is ASTAlias || a is ASTData || a is ASTChoice) .Select(a => { return(a switch { ASTType t => t.Clone() as IASTNode, ASTAlias t => t.Clone() as IASTNode, ASTData t => t.Clone() as IASTNode, ASTChoice t => t.Clone() as IASTNode, _ => throw new Exception("Can only serialize real AST nodes.") }); }) .ToList(); this.Imports.AddRange(copies); }
private JSchema MapASTAlias(ASTAlias astAlias) { var _mod = astAlias.Type.First().Value; var _type = astAlias.Type.Last().Value; var result = MapDefinition(_mod, _type); result.Title = astAlias.Name; return(result); }
private dynamic GenerateAlias(ASTAlias alias) { var fakerDirective = alias.Directives.FirstOrDefault(f => f.Key == "faker")?.Value.ToLower(); var(_mod, _type) = (alias.Types.First().Value, alias.Types.Last().Value); return((_mod, _type) switch { ("Maybe", _) => GenerateBaseValue(_type, alias, fakerDirective), ("List", _) => GenerateList(_type, alias, fakerDirective), (_, _) => GenerateBaseValue(_type, alias, fakerDirective) });
/// <summary> /// Creates and loads a type alias (e.g. CHARACTERGUID, ITEMGUID, etc.) from an AST node. /// </summary> private bool LoadAliasFromAST(ASTAlias astAlias) { var type = new ValueType { Name = astAlias.TypeName, TypeId = astAlias.TypeId, IntrinsicTypeId = (Value.Type)astAlias.AliasId }; return(Context.RegisterType(type)); }
public override IEnumerable <Descriptor> VisitASTAlias(ASTAlias astAlias) { yield return(new Descriptor(astAlias.Name) { Module = ModuleName, Name = astAlias.Name, Description = MapAnnotations(astAlias.Annotations), Type = MapTypes(astAlias.Types), DescriptorType = DescriptorType.Alias.ToString("g") }); }
private JSchema?MapASTAlias(ASTAlias astAlias) { var _mod = astAlias.Types.First().Value; var _type = astAlias.Types.Last().Value; var result = MapDefinition(_mod, _type, astAlias); if (result != null) { result.Title = astAlias.Name; } return(result); }
public override string VisitASTAlias(ASTAlias astAlias) { var template = $@" class {astAlias.Name} {{ {string.Join("\n", astAlias.Type.Select(t => t.Value).ToList())} }} "; this.Parts.Add(template); return(template); }
public override string VisitASTAlias(ASTAlias astAlias) { string template = $@" class {astAlias.Name} {{ <<Interface>> {string.Join(" ", astAlias.Type.Select(t => t.Value).ToList())} }} "; this.Parts.Add(template); return(template); }
public JSchema?Create(ASTAlias astAlias, ASTGenerator generator) { var schema = new JSchema(); if (schema != null) { schema.SchemaVersion = new Uri("http://json-schema.org/draft-07/schema#"); schema.Title = astAlias.Name; schema.Description = JsonMapper.Annotate(astAlias.Annotations); var _mod = astAlias.Types.First().Value; var _type = astAlias.Types.Last().Value; if (_mod == "List") { schema.Type = JSchemaType.Array; if (Parser.BaseTypes.Contains(_mod)) { schema.Items.Add(JsonMapper.MapBasicType(_type, astAlias, this.CarConfig)); return(schema); } else { var typeDef = generator.Find <ASTType>(_type); if (typeDef != null) { var typeSchema = new ASTTypeToJSchema(this.CarConfig).Create(typeDef, generator.AST); schema.Items.Add(typeSchema); } } } else if (Parser.BaseTypes.Contains(_type)) { var mappedType = JsonMapper.MapBasicType(_type, astAlias, this.CarConfig); schema.Properties.Clear(); schema.Type = mappedType.Type; } else { schema.Type = JSchemaType.Object; var typeDef = generator.Find <ASTType>(_type); if (typeDef != null) { var typeSchema = new ASTTypeToJSchema(this.CarConfig).Create(typeDef, generator.AST); schema.Items.Add(typeSchema); } } } return(schema); }
internal static IASTNode?Find(IEnumerable <IASTNode> nodes, string name) { return(nodes.FirstOrDefault(n => { return n switch { ASTType node => node.Name == name, ASTAlias node => node.Name == name, ASTData node => node.Name == name, ASTChoice node => node.Name == name, _ => false }; })); }
private JSchema MapAstNode(string name) { var refNode = JsonMapper.Find(Nodes, name); var result = refNode switch { ASTType n => MapASTType(n), ASTAlias n => MapASTAlias(n), ASTChoice n => MapASTChoice(n), ASTData n => MapASTData(n), _ => throw new NotImplementedException("Not implemented.") }; AddReference(name, result); return(result); }
public void ListAlias() { var code = @" alias Name = List String; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; Assert.Equal(Helpers.ToTypeDefinition(new[] { "List", "String" }), alias.Type); }
private void ResolveAlias(ASTAlias alias, List <IASTNode> nodes, List <ASTError> errors) { // here we'll resolve generic aliasses var _mod = alias.Type.First().Value; if (_mod == "List" || _mod == "Maybe" || alias.Type.Count() == 1) { // non generic type... // we do not allow generic Lists or Maybes like: // // alias ConcreteFoo = List Foo String // nodes.Add(alias); } else { var clone = (FindNode(_mod) as ASTType)?.Clone(); if (clone is null) { errors.Add(new ASTError("Cannot resolve generic type", null)); } else if (clone.Parameters.Count() != alias.Type.Count() - 1) { errors.Add(new ASTError("Not resolving all generic parameters.", null)); } else { clone.Name = alias.Name; clone.Fields.ToList().ForEach(field => { field.Type = field.Type.Select(t => { if (t.IsGeneric) { var index = clone.Parameters.ToList().IndexOf(t.Value) + 1; return(alias.Type.ElementAt(index)); } else { return(t); } }).ToList(); }); nodes.Add(clone); } } }
public void AliasAnnotations() { var code = @" @ This alias represents a name alias Name = String; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; Assert.Empty(alias.Restrictions); Assert.Single(alias.Annotations.ToList()); Assert.Equal("This alias represents a name", alias.Annotations.First().Value); }
public T Visit(IASTNode node) { return(node switch { ASTType n => VisitASTType(n), ASTTypeField n => VisitASTTypeField(n), ASTTypeDefinition n => VisitASTTypeDefinition(n), ASTRestriction n => VisitASTRestriction(n), ASTAlias n => VisitASTAlias(n), ASTData n => VisitASTData(n), ASTAnnotation n => VisitASTAnnotation(n), ASTDirective n => VisitASTDirective(n), ASTChoice n => VisitASTChoice(n), ASTOption n => VisitASTOption(n), ASTChapter n => VisitASTChapter(n), ASTParagraph n => VisitASTParagraph(n), _ => VisitDefault(node), });
public void MaybeAlias() { var code = @" @ A name might be null. We will represent this as a @ Maybe type. alias Name = Maybe String; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; Assert.Empty(alias.Restrictions); Assert.Equal(2, alias.Annotations.Count()); Assert.Equal(Helpers.ToTypeDefinition(new[] { "Maybe", "String" }), alias.Types); }
public override string VisitASTAlias(ASTAlias astAlias) { var typeDescription = $@"From module: <a href=""/index.html?path=preview&module={astAlias.Module}"" target='_parent'>{astAlias.Module}</a> {string.Join(" ", astAlias.Annotations.Select(a => a.Value).ToList()).Trim()}"; var _mod = astAlias.Types.First().Value; var _type = astAlias.Types.Last().Value; var restrictions = String.Join(Environment.NewLine, astAlias.Restrictions.Select(r => $"{r.Key} {r.Value}")); return($@" <div class=""table-container""> <table> <thead> <tr> <th colspan=""5"">{astAlias.Name}</th> </tr> <tr class=""description""> <th colspan=""5"">{typeDescription}</th> </tr> <tr> <th>Name</th> <th>Type</th> <th>Required</th> <th>Restrictions</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>{astAlias.Name}</td> <td>{string.Join(" ", astAlias.Types.Select(t => t.Value).ToList())}</td> <td>{_mod != "Maybe"}</td> <td>{restrictions}</td> <td> From module: <a href=""/index.html?path=preview&module={astAlias.Module}"" target='_parent'>{astAlias.Module}</a> <br /> {string.Join(" ", astAlias.Annotations.Select(a => a.Value).ToList())} </td> </tr> <tbody> </table> </div> "); }
public void RestrictionsTest() { var code = @" alias Name = String & min 2 & max 34 ; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; Assert.Equal(2, alias.Restrictions.Count()); }
public void StringRestrictions() { var code = @" alias Name = String & default ""Other thing"" ; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; Assert.Single(alias.Restrictions); ASTRestriction restriction = alias.Restrictions.First(); Assert.Equal(TokenType.String, restriction.Token.TokenType); }
public override string VisitASTAlias(ASTAlias astAlias) { List <string> parts = new List <string>(); parts.AddRange(astAlias.Annotations.Select(Visit)); parts.AddRange(astAlias.Directives.Select(Visit)); string typeDef = string.Join(" ", astAlias.Type.Select(Visit)); if (astAlias.Restrictions.Count() == 0) { parts.Add($@"alias {astAlias.Name} = {typeDef}"); } else { string restrictions = string.Join("\n", astAlias.Restrictions.Select(Visit)); parts.Add($"alias {astAlias.Name} = {typeDef}\n{restrictions}"); } return(string.Join("\n", parts.ToArray())); }
public void PatternRestrictions() { var code = @" alias Name = String & pattern /[A-Z][a-z]{1,30}/ ; "; var tokens = new Lexer().Lex(code); var parseTree = new Parser(tokens).Parse().ToList(); Assert.NotNull(parseTree); Assert.Single(parseTree); ASTAlias alias = parseTree[0] as ASTAlias; Assert.Single(alias.Restrictions); ASTRestriction restriction = alias.Restrictions.First(); Assert.Equal(TokenType.Pattern, restriction.Token.TokenType); }
public static List <IASTNode> ResolveImports(ASTGenerator generator) { var imports = new List <IASTNode>(); var _imports = generator.AST.FindAll(n => n is ASTImport).ToList(); _imports.ForEach(node => { var import = (ASTImport)node; var ast = ProjectContext.Instance?.GetAstForModule(import.ModuleName); if (!import.Imports.Any()) { var copies = ast? .FindAll(a => a is ASTType || a is ASTAlias || a is ASTData || a is ASTChoice || a is ASTView) .Select(a => { return(a switch { ASTType t => t.Clone() as IASTNode, ASTAlias t => t.Clone() as IASTNode, ASTData t => t.Clone() as IASTNode, ASTChoice t => t.Clone() as IASTNode, ASTView t => t.Clone() as IASTNode, _ => null }); }) .ToList() .Where(n => n != null) .OfType <IASTNode>() .ToList() ?? Enumerable.Empty <IASTNode>(); // For some reason the compiler cannot find that I filter out all of the // null's from the list and so I will only have a list of reference types // of type IASTNode... #pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. imports.AddRange(copies); #pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. }
private JSchema?MapAstNode(string name) { if (NodeNames.Contains(name)) { return((JSchema)References.GetValue(name)); } var refNode = JsonMapper.Find(Nodes, name); var result = refNode switch { ASTType n => MapASTType(n), ASTAlias n => MapASTAlias(n), ASTChoice n => MapASTChoice(n), ASTData n => MapASTData(n), _ => new JSchema() }; if (result != null) { AddReference(name, result); } return(result); } }
public IEnumerable <IASTNode> Parse() { var annotations = new List <ASTAnnotation>(); var directives = new List <ASTDirective>(); while (HasNext() && Current.TokenType != TokenType.EndOfFile) { if (Current.TokenType == TokenType.KW_Type) { var(errors, t) = ASTType.Parse(this, annotations, directives); Errors.AddRange(errors); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); yield return(t); } else if (Current.TokenType == TokenType.KW_Alias) { var(errors, alias) = ASTAlias.Parse(this, annotations, directives); Errors.AddRange(errors); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); yield return(alias); } else if (Current.TokenType == TokenType.KW_Choice) { var(errors, result) = ASTChoice.Parse(this); Errors.AddRange(errors); yield return(result); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); } else if (Current.TokenType == TokenType.KW_Data) { var(errors, data) = ASTData.Parse(this, annotations, directives); Errors.AddRange(errors); yield return(data); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); } else if (Current.TokenType == TokenType.KW_View) { var(errors, data) = ASTView.Parse(this, annotations, directives); Errors.AddRange(errors); yield return(data); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); } else if (Current.TokenType == TokenType.KW_Open) { var(errors, data) = ASTImport.Parse(this); Errors.AddRange(errors); yield return(data); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); } else if (Current.TokenType == TokenType.KW_Flow) { var(errors, data) = ASTFlow.Parse(this); Errors.AddRange(errors); yield return(data); annotations = new List <ASTAnnotation>(); directives = new List <ASTDirective>(); } else if (Current.TokenType == TokenType.Annotation) { annotations = ASTAnnotation.Parse(this).ToList(); } else if (Current.TokenType == TokenType.Directive) { var(errors, dirs) = ASTDirective.Parse(this); Errors.AddRange(errors.ToList()); directives = dirs.ToList(); } else if (Current.TokenType == TokenType.Chapter) { yield return(new ASTChapter(Current.Value)); Next(); } else if (Current.TokenType == TokenType.Paragraph) { yield return(new ASTParagraph(Current.Value)); Next(); } else { Next(); } } yield break; }
public override string VisitASTAlias(ASTAlias astAlias) { return(""); }
public override string VisitASTAlias(ASTAlias astAlias) { var t = (astAlias.Types.First().Value, astAlias.Types.Last().Value); return(Intercept($@"using {astAlias.Name} = {CSharpHelpers.ToCSharpType(t)};")); }
public override T VisitASTAlias(ASTAlias astAlias) => d;