Beispiel #1
0
        public string Beautify(string s, BeautifierOptions opts = null)
        {
            if (opts != null)
                this.Opts = opts;

            this.BlankState();

            while (s.Length != 0 && (s[0] == ' ' || s[0] == '\t'))
            {
                this.PreindentString += s[0];
                s = s.Remove(0, 1);
            }

            this.Input = s;
            this.ParserPos = 0;
            while (true)
            {
                Tuple<string, string> token = GetNextToken();
                // print (token_text, token_type, self.flags.mode)
                string tokenText = token.Item1;
                string tokenType = token.Item2;
                if (tokenType == "TK_EOF")
                    break;

                Dictionary<string, Action<string>> handlers = new Dictionary<string, Action<string>> {
                    { "TK_START_EXPR", HandleStartExpr },
                    { "TK_END_EXPR", HandleEndExpr },
                    { "TK_START_BLOCK", HandleStartBlock },
                    { "TK_END_BLOCK", HandleEndBlock },
                    { "TK_WORD", HandleWord },
                    { "TK_SEMICOLON", HandleSemicolon },
                    { "TK_STRING", HandleString },
                    { "TK_EQUALS", HandleEquals },
                    { "TK_OPERATOR", HandleOperator },
                    { "TK_COMMA", HandleComma },
                    { "TK_BLOCK_COMMENT", HandleBlockComment },
                    { "TK_INLINE_COMMENT", HandleInlineComment },
                    { "TK_COMMENT", HandleComment },
                    { "TK_DOT", HandleDot },
                    { "TK_UNKNOWN", HandleUnknown }
                };
                handlers[tokenType](tokenText);

                this.LastLastText = this.LastText;
                this.LastType = tokenType;
                this.LastText = tokenText;
            }

            Regex regex = new Regex(@"[\n ]+$");

            string sweetCode = this.PreindentString + regex.Replace(string.Concat(this.Output), "", 1);
            return sweetCode;
        }
 public Beautifier(BeautifierOptions opts)
 {
     this.Opts = opts;
     this.BlankState();
 }
        public string Beautify(string s, BeautifierOptions opts = null)
        {
            if (opts != null)
            {
                Opts = opts;
            }

            BlankState();

            s = string.IsNullOrWhiteSpace(s) ? string.Empty : s;

            while (s.Length != 0 && (s[0] == ' ' || s[0] == '\t'))
            {
                PreindentString += s[0];
                s = s.Remove(0, 1);
            }

            Input     = s;
            ParserPos = 0;
            while (true)
            {
                var token = GetNextToken();
                // print (token_text, token_type, self.flags.mode)
                var tokenText = token.Item1;
                var tokenType = token.Item2;
                if (tokenType == "TK_EOF")
                {
                    break;
                }

                var handlers = new Dictionary <string, Action <string> >
                {
                    { "TK_START_EXPR", HandleStartExpr },
                    { "TK_END_EXPR", HandleEndExpr },
                    { "TK_START_BLOCK", HandleStartBlock },
                    { "TK_END_BLOCK", HandleEndBlock },
                    { "TK_WORD", HandleWord },
                    { "TK_SEMICOLON", HandleSemicolon },
                    { "TK_STRING", HandleString },
                    { "TK_EQUALS", HandleEquals },
                    { "TK_OPERATOR", HandleOperator },
                    { "TK_COMMA", HandleComma },
                    { "TK_BLOCK_COMMENT", HandleBlockComment },
                    { "TK_INLINE_COMMENT", HandleInlineComment },
                    { "TK_COMMENT", HandleComment },
                    { "TK_DOT", HandleDot },
                    { "TK_UNKNOWN", HandleUnknown }
                };
                handlers[tokenType](tokenText);

                LastLastText = LastText;
                LastType     = tokenType;
                LastText     = tokenText;
            }

            var regex = new Regex(@"[\n ]+$");

            var sweetCode = PreindentString + regex.Replace(string.Concat(Output), "", 1);

            return(sweetCode);
        }
 public Beautifier(BeautifierOptions opts)
 {
     Opts = opts;
     BlankState();
 }