Exemplo n.º 1
0
        public string Generate(List <AphidExpression> nodes, string code)
        {
            ParseDirectives(nodes);
            var lexer = GenerateLexer(nodes, code);

            ParseRuleStructs(nodes);
            nodes = new PlusEqualMutator().MutateRecursively(nodes);
            var rules = GetRules(nodes);

            _ruleNames = GetRuleNames(nodes).ToArray();
            var mutator = new TypeInferenceMutator(_config);

            foreach (var r in rules)
            {
                nodes.Remove(r);
            }

            do
            {
                rules = rules
                        .Select(x => new BinaryOperatorExpression(
                                    x.LeftOperand,
                                    x.Operator,
                                    new FunctionExpression(
                                        x.RightOperand.ToFunction().Args,
                                        mutator.MutateRecursively(x.RightOperand.ToFunction().Body))))
                        .ToArray();
            }while (mutator.HasMutated);

            nodes.AddRange(rules);
            var declMutator = new DeclarativeStatementMutator(_tokenTypes, _ruleNames);

            nodes = declMutator.Mutate(nodes);
            nodes = AddIndexTracking(nodes);
            var ruleTypeBuilder = new RuleTypeBuilder(_config, _ruleTypes.Select(x => x.Value));
            var typeClasses     = ruleTypeBuilder.CreateRuleTypeClasses();
            var enumBuilder     = new EnumBuilder(_config.BaseClass, _ruleTypes.Select(x => x.Key));
            var enumDecl        = enumBuilder.CreateEnum();

            var ns = new CodeNamespace(
                string.Join(".", _config.Namespace.Concat(new[] { ParserName.Parser })));

            ns.Imports.Add(new CodeNamespaceImport(
                               string.Join(".", _config.Namespace.Concat(new[] { ParserName.Lexer }))));

            ns.Imports.Add(new CodeNamespaceImport("System.Linq"));
            ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            ns.Types.Add(enumDecl);
            ns.Types.AddRange(typeClasses);

            var parserType = new CodeTypeDeclaration(_config.ParserClass)
            {
                IsPartial = true
            };

            parserType.Members.Add(GenerateContextField());
            parserType.Members.AddRange(nodes.Select(Generate).Where(x => x != null).ToArray());
            ns.Types.Add(parserType);

            var str = CSharpHelper.GenerateCode(ns);

            return(str + "\r\n\r\n" + lexer);
        }
Exemplo n.º 2
0
        public string Generate(List<AphidExpression> nodes)
        {
            ParseDirectives(nodes);
            ParseRuleStructs(nodes);
            nodes = new PlusEqualMutator().MutateRecursively(nodes);
            var rules = GetRules(nodes);
            _ruleNames = GetRuleNames(nodes).ToArray();
            var mutator = new TypeInferenceMutator(_config);

            do
            {
                foreach (var r in rules)
                {
                    var func = r.RightOperand.ToFunction();
                    func.Body = mutator.MutateRecursively(func.Body);
                }
            }
            while (mutator.HasMutated);

            var declMutator = new DeclarativeStatementMutator(_tokenTypes, _ruleNames);
            nodes = declMutator.Mutate(nodes);

            //rules = GetRules(nodes);
            //var callIdMutator = new CallIdentifierMutator(_ruleNames);
            ////nodes = callIdMutator.Mutate(nodes);
            //foreach (var r in rules)
            //{
            //    var func = r.RightOperand.ToFunction();
            //    func.Body = callIdMutator.Mutate(func.Body);
            //}

            var typeClasses = CreateRuleTypeClasses();

            var ns = new CodeNamespace(
                string.Join(".", _config.Namespace.Concat(new[] { "Parser" })));

            ns.Imports.Add(new System.CodeDom.CodeNamespaceImport(
                string.Join(".", _config.Namespace.Concat(new[] { "Lexer" }))));
            //ns.Types.AddRange(typeClasses);

            var parserType = new CodeTypeDeclaration(_config.ParserClass)
            {
                IsPartial = true,
            };

            parserType.Members.AddRange(nodes.Select(Generate).Where(x => x != null).ToArray());
            ns.Types.Add(parserType);

            var str = CSharpHelper.GenerateCode(ns);

            return str;
        }