Пример #1
0
        public static void Format(ICharStream stream, IFormatWriter writer, FormatOptions options)
        {
            var lexer  = new LuaLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new LuaParser(tokens);

            tokens.Fill();

            var comments = tokens.GetTokens().Where(t => t.Channel == LuaLexer.Hidden);
            var spaces   = tokens.GetTokens().Where(t => t.Channel == 2);

            parser.BuildParseTree = true;
            parser.TrimParseTree  = false;

            IRuleNode root = parser.chunk();

            var ctx = new FormatContext(root, comments, spaces, writer, options);

            RuleFormatter.Format(root, ctx);

            ctx.WriteComments(int.MaxValue);

            var allTokens = tokens.GetTokens();

            if (allTokens.Count > 0)
            {
                var lastToken = allTokens[allTokens.Count - 1];
                while (ctx.line <= lastToken.Line)
                {
                    ctx.WriteLineBreak();
                }
            }

            tokens.Release(0);
        }
Пример #2
0
        public static void Format(IRuleNode node, FormatContext ctx, int form = RuleType.FORM_GENERAL, RuleFormatter parent = null)
        {
            RuleType nodeRuleType = new RuleType(node.RuleContext.RuleIndex, form);

            RuleFormatter formatter = null;

            Stack <RuleFormatter> stack = null;

            if (!dict.TryGetValue(nodeRuleType, out stack))
            {
                dict.Add(nodeRuleType, stack = new Stack <RuleFormatter>());
            }

            if (stack.Count > 0)
            {
                formatter = stack.Pop();
            }
            else
            {
                Type formatterType = null;

                if (typeDict == null)
                {
                    typeDict = new Dictionary <RuleType, Type>();

                    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (var assembly in assemblies)
                    {
                        var types = assembly.GetTypes();
                        foreach (var t in types)
                        {
                            if (t.IsSubclassOf(typeof(RuleFormatter)))
                            {
                                var attributes = t.GetCustomAttributes(typeof(CustomRuleFormatterAttribute), false);
                                foreach (var attr in attributes)
                                {
                                    var ruleFormatterAttr = attr as CustomRuleFormatterAttribute;
                                    typeDict[new RuleType(ruleFormatterAttr.ruleIndex, ruleFormatterAttr.ruleForm)] = t;
                                }
                            }
                        }
                    }
                }

                if (!typeDict.TryGetValue(nodeRuleType, out formatterType))
                {
                    //foreach (var pair in typeDict)
                    //{
                    //    if (pair.Key.Test(nodeRuleType))
                    //    {
                    //        formatterType = pair.Value;
                    //        break;
                    //    }
                    //}
                    //typeDict.TryGetValue(new RuleType(nodeRuleType.ruleIndex, RuleType.FORM_GENERAL), out formatterType);

                    if (formatterType == null)
                    {
                        formatterType = typeof(RuleFormatter);
                    }

                    typeDict.Add(nodeRuleType, formatterType);
                }

                formatter = Activator.CreateInstance(formatterType) as RuleFormatter;
            }

            formatter.target   = node;
            formatter.ctx      = ctx;
            formatter.parent   = parent;
            formatter.ruleType = nodeRuleType;

            formatter.Format();
            formatter.Reset();

            stack.Push(formatter);
        }