private static void AddMethod(ClassSyntaxNode classNode, ScriptClassMethod method)
        {
            var methodNode = new MethodSyntaxNode(method.Name, method.SourceCode);

            classNode.AddSubNode(methodNode);
            classNode.AddSubNode(new EmptyLineSyntaxNode());
        }
        private static void AddProperty(ClassSyntaxNode classNode, ScriptClassProperty property)
        {
            var propertyNode = new PropertySyntaxNode(property.Name, property.ValueType, property.GetterSourceCode, property.SetterSourceCode);

            classNode.AddSubNode(propertyNode);
            classNode.AddSubNode(new EmptyLineSyntaxNode());
        }
Beispiel #3
0
        // Parsing a class starts with the class declaration, and then will loop through until it
        // finds a constructor, method or field token. This is why the syntax has these 3 constructs
        // start with those keywords. It turns these methods from `TryParse` to just parse.
        // Properties would be a cool thing to add at some point.
        public static ClassSyntaxNode ParseClass(ref ReadOnlySpan <IToken> tokens, ISyntaxNode parent)
        {
            // Need 3 more tokens: name {}
            if (tokens.Length < 3)
            {
                throw new InvalidTokenException("Not enough tokens");
            }

            if (!(tokens[0] is IdentifierToken typeToken))
            {
                throw new InvalidTokenException("First token must be a type token");
            }

            if (!(tokens[1] is LeftBraceToken))
            {
                throw new InvalidTokenException("Need a left brace as the first token");
            }

            var classSyntaxNode = new ClassSyntaxNode(parent, typeToken.Name);

            tokens = tokens.Slice(2);

            while (!tokens.IsEmpty)
            {
                var curToken = tokens[0];
                tokens = tokens.Slice(1);

                switch (curToken)
                {
                // A top level left brace is a failure
                case LeftBraceToken _:
                    throw new InvalidTokenException("Too many left braces");

                case RightBraceToken _:
                    // The other parse functions will handle their braces, so if we get here
                    // we are done with the class.
                    return(classSyntaxNode);

                // Below here just parses the 3 things that can be in a class.
                case ConstructorToken _:
                    classSyntaxNode.Constructors.Add(ParseConstructor(ref tokens, parent));
                    break;

                case MethodToken _:
                    classSyntaxNode.Methods.Add(ParseMethod(ref tokens, parent));
                    break;

                case FieldToken _:
                    classSyntaxNode.Fields.Add(ParseField(ref tokens, parent));
                    break;

                default:
                    throw new InvalidTokenException($"Unexpected token {curToken}");
                }
            }

            throw new InvalidTokenException("Out of tokens?");
        }
        private void AddClassBlock(NameSpaceSyntaxNode nameSpaceNode)
        {
            var classNode = new ClassSyntaxNode(_className, _template.ImplementsInterfaces);

            nameSpaceNode.AddSubNode(classNode);
            AddMethods(classNode);
            AddProperties(classNode);
            AddRawContents(classNode);
        }
 public void Visit(ClassSyntaxNode classSyntax)
 {
     AppendLine($"public class {classSyntax.ClassName}{GetImplementingInterfaces(classSyntax.InheritsFrom)}");
     AppendLine("{");
     _indentLevel++;
     VisitSubItems(classSyntax);
     _indentLevel--;
     AppendLine("}");
 }
Beispiel #6
0
        public static ClassSyntaxNode ParseClass(ref ReadOnlySpan <IToken> tokens, ISyntaxNode parent)
        {
            // Need 3 more tokens: name {}
            if (tokens.Length < 3)
            {
                throw new InvalidTokenException("Not enough tokens");
            }

            if (!(tokens[0] is IdentifierToken typeToken))
            {
                throw new InvalidTokenException("First token must be a type token");
            }

            if (!(tokens[1] is LeftBraceToken))
            {
                throw new InvalidTokenException("Need a left brace as the first token");
            }

            var classSyntaxNode = new ClassSyntaxNode(parent, typeToken.Name);

            tokens = tokens.Slice(2);

            while (!tokens.IsEmpty)
            {
                var curToken = tokens[0];
                tokens = tokens.Slice(1);

                switch (curToken)
                {
                case LeftBraceToken _:
                    throw new InvalidTokenException("Too many left braces");

                case RightBraceToken _:
                    return(classSyntaxNode);

                case ConstructorToken _:
                    classSyntaxNode.Constructors.Add(ParseConstructor(ref tokens, parent));
                    break;

                case MethodToken _:
                    classSyntaxNode.Methods.Add(ParseMethod(ref tokens, parent));
                    break;

                case FieldToken _:
                    classSyntaxNode.Fields.Add(ParseField(ref tokens, parent));
                    break;

                default:
                    throw new InvalidTokenException($"Unexpected token {curToken}");
                }
            }

            throw new InvalidTokenException("Out of tokens?");
        }
 private void AddMethods(ClassSyntaxNode classNode)
 {
     _template.Members.OfType <ScriptClassMethod>().ForEach(method => AddMethod(classNode, method));
 }
 private void AddProperties(ClassSyntaxNode classNode)
 {
     _template.Members.OfType <ScriptClassProperty>().ForEach(property => AddProperty(classNode, property));
 }
        private static void AddRawContent(ClassSyntaxNode classNode, ScriptClassRawContent rawContent)
        {
            var rawContentNode = new RawContentSyntaxNode(rawContent.RawContent);

            classNode.AddSubNode(rawContentNode);
        }
 private void AddRawContents(ClassSyntaxNode classNode)
 {
     _template.Members.OfType <ScriptClassRawContent>().ForEach(rawContent => AddRawContent(classNode, rawContent));
 }