public CodeGenerator(SyntaxNode rootNode)
 {
     foreach (StatementNode statement in rootNode.Nodes.OfType<StatementNode>())
     {
         processStatement(statement);
     }
 }
        private void command(SyntaxNode parentNode)
        {
            if (current is Keyword && current.Text == "namespace")
            {
                read(); // namespace
                if (!(current is Identifier))
                    throw new SyntaxException();

                CommandNode commandNode =
                    new CommandNode(CommandType.Namespace, current.Text).AddTo(parentNode) as CommandNode;

                read(); // keyword
            }
            else if (current is Keyword && current.Text == "using")
            {
                read(); // using
                if (!(current is Identifier))
                    throw new SyntaxException();

                CommandNode commandNode =
                    new CommandNode(CommandType.Using, current.Text).AddTo(parentNode) as CommandNode;

                read(); // identifier
            }
            else if (current is Keyword && current.Text == "generate")
            {
                CommandNode commandNode =
                    new CommandNode(CommandType.Generate, current.Text).AddTo(parentNode) as CommandNode;

                read(); // using
                generateParameters(commandNode);
            }
            else
                throw new SyntaxException();
        }
Example #3
0
        public SyntaxNode AddTo(SyntaxNode node)
        {
            if (node != null)
            {
                node.Nodes.Add(this);
                this.Parent = node;
            }

            return this;
        }
Example #4
0
        public SyntaxNode ReplaceWith(SyntaxNode node)
        {
            node.Parent = Parent;

            if (Parent != null)
            {
                for (int i = 0; i < Parent.Nodes.Count; i++)
                {
                    if (Parent.Nodes[i] == this)
                    {
                        Parent.Nodes.RemoveAt(i);
                        Parent.Nodes.Insert(i, node);
                        return node;
                    }
                }
            }

            return node;
        }
        private void typedIdentifier(SyntaxNode parentNode)
        {
            if (current is Identifier)
            {
                var propertyNode = new PropertyNode(current.Text).AddTo(parentNode) as PropertyNode;
                read(); // identifier
                modRequired(propertyNode);
                modForeignKey(propertyNode);
            }
            else if (current is Character && current.Text == "[")
            {
                read(); // [
                if (!(current is Identifier))
                    throw new SyntaxException();

                PropertyNode propertyName =
                    new PropertyNode(current.Text, true).AddTo(parentNode) as PropertyNode;

                read(); // identifier
                if (!(current is Character) || current.Text != "]")
                    throw new SyntaxException();

                read(); // ]
                modRequired(propertyName);
                modForeignKey(propertyName);
            }
            else
                throw new SyntaxException();
        }
        private void statementList(SyntaxNode parentNode)
        {
            if (current is EndOfCode)
                return;

            if (current is Identifier || (current is Character && "@[$#&!".Contains(current.Text)))
            {
                statementLine(parentNode);
                statementList(parentNode);
            }
            else
            {
                var message = String.Format("Unexpected element: " + current.Text);
                throw new SyntaxException(message, current.Line);
            }
        }
        private void statementLine(SyntaxNode parentNode)
        {
            if (current is Identifier || (current is Character && "@[$#&!".Contains(current.Text)))
            {
                statement(new StatementNode().AddTo(parentNode));

                if (current is NewLine)
                    read(); // newline
                else
                    throw new SyntaxException();
            }
            else
                throw new SyntaxException();
        }
        private void statement(SyntaxNode parentNode)
        {
            if (current is Character && current.Text == "@")
            {
                read(); // @
                command(parentNode);
            }
            else if (current is Identifier)
            {
                IdentifierNode identifierNode =
                    new IdentifierNode(current.Text).AddTo(parentNode) as IdentifierNode; // can be an entity or a property

                read(); // identifier
                untypedIdentifier(identifierNode);
            }
            else if (current is Character && "$#&!".Contains(current.Text))
            {
                new PropTypeNode(current.Text.ToPropertyType()).AddTo(parentNode);
                propType();
                typedIdentifier(parentNode);
            }
            else if (current is Character && current.Text == "[")
            {
                read(); // [

                if (!(current is Identifier))
                    throw new SyntaxException();

                PropertyNode propertyName =
                    new PropertyNode(current.Text, true).AddTo(parentNode) as PropertyNode;

                read(); // identifier
                if (!(current is Character) || current.Text != "]")
                    throw new SyntaxException();

                read(); // ]
                modRequired(propertyName);
                modForeignKey(propertyName);
            }
            else
                throw new SyntaxException();
        }
 private void print(SyntaxNode node, int level = 0)
 {
     var tabs = String.Concat(Enumerable.Range(0, level).Select(x => "    "));
     Console.WriteLine("{0}{1}", tabs, node);
     foreach (SyntaxNode childNode in node.Nodes)
     {
         print(childNode, level + 1);
     }
 }