Exemple #1
0
        protected void EmitFunctionDeclarationStatement(BinaryOperatorExpression expression, bool isMember)
        {
            var id    = expression.LeftOperand.ToIdentifier();
            var func  = expression.RightOperand.ToFunction();
            var attrs = AphidAttributeParser.Parse <FunctionDeclarationAttributes>(id, out var unparsed);

            if ((!isMember && attrs.IsStatic) || unparsed.Length > 0)
            {
                throw new NotImplementedException();
            }

            if (attrs.IsStatic)
            {
                Append("@staticmethod\r\n");
                AppendTabs();
            }

            Append("def {0}(", id.Identifier);

            if (isMember && !attrs.IsStatic)
            {
                Append("self{0}", func.Args.Count > 0 ? ", " : "");
            }

            EmitTuple(func.Args);
            Append("):\r\n");
            Indent();
            Emit(func.Body);
            Unindent();
            AppendLine();
        }
Exemple #2
0
        public static ParserIdentifier FromIdentifierExpression(IdentifierExpression identifierExpression)
        {
            var id = AphidAttributeParser.Parse <ParserIdentifier>(identifierExpression);

            id.Name = identifierExpression.Identifier;

            return(id);
        }
Exemple #3
0
        private ParserIdentifier ParseMethodAttributes(IdentifierExpression node)
        {
            var attrs = AphidAttributeParser.Parse <ParserIdentifier>(node);

            if (attrs.Type == null)
            {
                attrs.Type = _config.BaseClass;
            }

            return(attrs);
        }
Exemple #4
0
        private ParserIdentifier ParseRuleProperty(IdentifierExpression propertyId)
        {
            var prop = AphidAttributeParser.Parse <ParserIdentifier>(propertyId);

            prop.Name = propertyId.Identifier;

            if (prop.Type == null)
            {
                prop.Type = _config.BaseClass;
            }

            return(prop);
        }
Exemple #5
0
        protected void EmitIdentifierStatement(IdentifierExpression expression)
        {
            var attrs = AphidAttributeParser.Parse <IdentifierStatementAttributes>(expression, out var unparsed);

            if (unparsed.Length > 0)
            {
                throw new NotImplementedException();
            }
            else if (attrs.IsThrow)
            {
                Append("raise {0}", expression.Identifier);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemple #6
0
        protected void EmitObjectStatement(ObjectExpression statement)
        {
            if (statement.Identifier == null)
            {
                throw new NotImplementedException();
            }

            var attrs = AphidAttributeParser.Parse <ClassDeclarationAttributes>(statement.Identifier, out var otherAttrs);

            if (attrs.IsClass)
            {
                EmitClassStatement(statement, attrs, otherAttrs);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            if (!IsStatement || expression.Type != AphidExpressionType.IdentifierExpression)
            {
                return(null);
            }

            var id         = expression.ToIdentifier();
            var attributes = AphidAttributeParser.Parse <DeclarativeStatementAttributes>(id);
            var nameId     = new IdentifierExpression(attributes.Name);

            if (_tokenTypes.Contains(id.Identifier))
            {
                hasChanged = true;

                var match = new CallExpression(
                    new IdentifierExpression("Match"),
                    new IdentifierExpression(id.Identifier));

                return(attributes.Name == null ?
                       new List <AphidExpression> {
                    match
                } :
                       new List <AphidExpression>
                {
                    new BinaryOperatorExpression(
                        nameId,
                        AphidTokenType.AssignmentOperator,
                        new IdentifierExpression("TokenType")),
                    match
                });
            }
            else if (_parseFunctions.Contains(id.Identifier))
            {
                hasChanged = true;
                var call = new CallExpression(new IdentifierExpression(id.Identifier));

                return(attributes.Name == null ?
                       new List <AphidExpression> {
                    call
                } :
                       new List <AphidExpression>
                {
                    new BinaryOperatorExpression(
                        nameId,
                        AphidTokenType.AssignmentOperator,
                        call)
                });
            }
            else if (id.Identifier == "NextToken")
            {
                hasChanged = true;

                return(new List <AphidExpression> {
                    new CallExpression(new IdentifierExpression(id.Identifier))
                });
            }

            return(null);
        }
Exemple #8
0
        protected void EmitClassMember(
            ObjectExpression classDecl,
            BinaryOperatorExpression member)
        {
            if (member.LeftOperand.Type != AphidExpressionType.IdentifierExpression)
            {
                throw new InvalidOperationException("Expected identifier expression on lhs of class member.");
            }

            var id    = member.LeftOperand.ToIdentifier();
            var attrs = AphidAttributeParser.Parse <MemberDeclarationAttributes>(id);

            if ((attrs.IsPrivate && attrs.IsProtected) ||
                (attrs.IsPrivate && attrs.IsPublic) ||
                (attrs.IsProtected && attrs.IsPrivate) ||
                (attrs.IsProtected && attrs.IsPublic) ||
                (attrs.IsPublic && attrs.IsPrivate) ||
                (attrs.IsPublic && attrs.IsProtected))
            {
                throw new InvalidOperationException("Member must be private, protected, or public.");
            }

            if (!attrs.IsPrivate && !attrs.IsProtected && !attrs.IsPublic)
            {
                attrs.IsPrivate = true;
            }

            if (attrs.IsPrivate && attrs.IsAbstract)
            {
                throw new InvalidOperationException("Member cannot be both private and abstract.");
            }

            var access =
                attrs.IsPrivate ? "private " :
                attrs.IsProtected ? "protected " :
                "public ";

            Append(GetTabs() + access);

            if (attrs.IsAbstract)
            {
                Append("abstract ");
            }

            if (member.RightOperand.Type == AphidExpressionType.FunctionExpression)
            {
                attrs.IsFunction = true;
            }

            if (attrs.IsFunction)
            {
                Append("function ");
            }
            else
            {
                Append("$");
            }

            Append(id.Identifier);

            var hasInitializer =
                member.RightOperand.Type != AphidExpressionType.IdentifierExpression ||
                id.Identifier != member.RightOperand.ToIdentifier().Identifier;

            if (!attrs.IsFunction)
            {
                if (hasInitializer)
                {
                    Append(" = ");
                    Emit(member.RightOperand);
                }
            }
            else
            {
                if (hasInitializer)
                {
                    if (member.RightOperand.Type != AphidExpressionType.FunctionExpression)
                    {
                        throw new InvalidOperationException();
                    }

                    var func = member.RightOperand.ToFunction();
                    Append("(");
                    EmitTuple(func.Args);
                    Append(") {\r\n");
                    Indent();
                    Emit(func.Body);
                    Unindent();
                    Append(GetTabs() + "}\r\n\r\n");
                }
            }

            if (!attrs.IsFunction || !hasInitializer)
            {
                Append(";\r\n");
            }
        }
Exemple #9
0
        protected void EmitClassDeclaration(ObjectExpression classDecl)
        {
            var attrs = AphidAttributeParser.Parse <ObjectStatementAttributes>(
                classDecl.Identifier,
                out var unparsed);

            if (attrs.IsInterface && attrs.IsAbstract)
            {
                throw new InvalidOperationException("Interfaces cannot be abstract.");
            }
            else if ((attrs.IsInterface && attrs.IsClass) ||
                     (!attrs.IsInterface && !attrs.IsClass))
            {
                throw new InvalidOperationException("Type must be interface or class.");
            }

            if (attrs.IsAbstract)
            {
                Append("abstract ");
            }

            Append(attrs.IsClass ? "class " : "interface ");
            Append(classDecl.Identifier.Identifier);
            bool first = true, extend = true;

            foreach (var attr in unparsed)
            {
                if (extend)
                {
                    if (attr == "impl")
                    {
                        extend = false;
                        first  = true;
                    }
                    else
                    {
                        if (first)
                        {
                            Append(" extends ");
                            first = false;
                        }
                        else
                        {
                            Append(", ");
                        }

                        Append(attr);
                    }
                }
                else
                {
                    if (first)
                    {
                        Append(" implements ");
                        first = false;
                    }
                    else
                    {
                        Append(", ");
                    }

                    Append(attr);
                }
            }


            Append(" {\r\n");
            Indent();

            foreach (var member in classDecl.Pairs)
            {
                EmitClassMember(classDecl, member);
            }

            Unindent();
            Append(GetTabs() + "}");
        }