Пример #1
0
 private void Traverse(LuaIndentState indentState, ParseTreeNode node, int level)
 {
     if (node.Token != null)
     {
         if (tokenMap.ContainsKey(node.Token.Location) == false)
         {
             tokenMap.Add(node.Token.Location, new LuaTokenInfo {
                 Token = node.Token
             });
         }
     }
     else
     {
         IAstNode astNode = node.AstNode as IAstNode;
         if (astNode != null)
         {
             astNode.Indent(indentState, tokenMap);
         }
         for (int i = 0; i < node.ChildNodes.Count; i++)
         {
             Traverse(indentState, node.ChildNodes[i], level + 1);
         }
     }
 }
Пример #2
0
        private string ExectueFormat(string text)
        {
            StringBuilder output = new StringBuilder();

            InitParser();
            var pt = Parser.Parse(text);

            if (Parser.Context.HasErrors)
            {
                throw new Exception("Cannot formet code due to syntax error");
            }

            tokenMap = new Dictionary <SourceLocation, LuaTokenInfo>();

            var indentState = new LuaIndentState(text);

            Traverse(indentState, pt.Root, 0);

            var depthTable = indentState.BuildDepthTable(pt.Root);

            InitParser();
            Parser.Scanner.VsSetSource(text, 0);

            bool          lastLineHasToken = false;
            int           lastLine         = 0;
            Token         lastToken        = null;
            Token         lastLastToken    = null;
            StringBuilder lastLineBuilder  = new StringBuilder();
            List <string> lineCache        = new List <string>();

            while (true)
            {
                int   s     = 0;
                Token token = Parser.Scanner.VsReadToken(ref s);
                if (token == null)
                {
                    break;
                }

                SourceLocation sl        = new SourceLocation(token.Location.Position, token.Location.Line - 1, token.Location.Column);
                LuaTokenInfo   tokenInfo = null;
                if (tokenMap.ContainsKey(sl))
                {
                    tokenInfo = tokenMap[sl];
                }

                LuaTokenInfo lastTokenInfo = null;
                if (lastToken != null)
                {
                    SourceLocation lastSl = new SourceLocation(lastToken.Location.Position, lastToken.Location.Line - 1, lastToken.Location.Column);

                    if (tokenMap.ContainsKey(lastSl))
                    {
                        lastTokenInfo = tokenMap[lastSl];
                    }
                }

                if (sl.Line != lastLine)
                {
                    if (!lastLineHasToken)
                    {
                        using (StringReader sr = new StringReader(lastLineBuilder.ToString()))
                        {
                            while (true)
                            {
                                string cacheLine = sr.ReadLine();
                                if (cacheLine == null)
                                {
                                    break;
                                }
                                lineCache.Add(cacheLine);
                            }
                        }
                    }
                    else
                    {
                        Write(output, depthTable, lastLine, lastLineBuilder, lineCache);

                        lineCache.Clear();
                    }

                    for (int i = lastLine + lineCache.Count + 1; i < sl.Line; i++)
                    {
                        lineCache.Add("");
                    }

                    lastLineHasToken = false;
                    lastLine         = sl.Line;
                    lastLineBuilder.Clear();
                    lastToken     = null;
                    lastLastToken = null;
                }

                if (tokenInfo != null)
                {
                    lastLineHasToken = true;
                }

                AppendSpace(lastLineBuilder, lastToken, lastTokenInfo, token);

                lastLineBuilder.Append(token.Text);
                lastLastToken = lastToken;
                lastToken     = token;
            }

            Write(output, depthTable, lastLine, lastLineBuilder, lineCache);

            return(output.ToString());
        }