示例#1
0
        public void GenerateSignatureXML()
        {
            using (FileStream fs = File.OpenRead(".\\TestInput\\SystemSignatures.txt"))
            {
                ApexLexer   scanner = new ApexLexer(fs);
                ApexParser  parser  = new ApexParser(scanner);
                ParseResult result  = parser.ParseApex();

                if (result.Errors.Length > 0)
                {
                    foreach (LanguageError err in result.Errors)
                    {
                        Console.WriteLine(err.ToString());
                    }
                    Assert.Fail("Errors in text.");
                }
                else
                {
                    PropertyInfo nameInfo = typeof(Symbol).GetProperty("Name");
                    FieldInfo    fi       = typeof(TypedSymbol).GetField("_type", BindingFlags.NonPublic | BindingFlags.Instance);
                    PropertyInfo pi       = typeof(SymbolTable).GetProperty("TableType");

                    List <SymbolTable> symbols = new List <SymbolTable>();
                    foreach (SymbolTable symbol in result.Symbols.InnerClasses)
                    {
                        if (symbol.Name == "trigger")
                        {
                            fi.SetValue(symbol, "System.Trigger");
                            nameInfo.SetValue(symbol, "Trigger");
                        }
                        else
                        {
                            fi.SetValue(symbol, String.Format("System.{0}", symbol.Name));
                        }

                        if (symbol.InnerClasses.Length > 0 &&
                            symbol.Fields.Length == 0 &&
                            symbol.Constructors.Length == 0 &&
                            symbol.Properties.Length == 0 &&
                            symbol.Methods.Length == 0 &&
                            symbol.Interfaces.Length == 0 &&
                            String.IsNullOrWhiteSpace(symbol.Extends))
                        {
                            pi.SetValue(symbol, SymbolTableType.Namespace);
                        }
                        symbols.Add(symbol);
                    }

                    XmlSerializer ser = new XmlSerializer(typeof(SymbolTable[]));
                    StringBuilder sb  = new StringBuilder();
                    using (StringWriter sw = new StringWriter(sb))
                    {
                        ser.Serialize(sw, symbols.ToArray());
                        Console.Write(sb.ToString());
                    }
                }
            }
        }
示例#2
0
        public void ApexLexer_Scan()
        {
            foreach (string file in Directory.GetFiles(".\\TestInput", "lexerTest*.txt"))
            {
                Console.WriteLine("File: " + file);

                using (FileStream fs = File.OpenRead(file))
                {
                    ApexLexer scanner = new ApexLexer(fs);
                    int       token   = 0;
                    while ((token = scanner.yylex()) != (int)Tokens.EOF)
                    {
                        Console.WriteLine((Tokens)token);
                    }
                }

                Console.WriteLine();
            }
        }
示例#3
0
        public void ApexParser_ParseApex()
        {
            foreach (string file in Directory.GetFiles(".\\TestInput", "apexTest*.txt"))
            {
                Console.WriteLine("File: " + file);

                using (FileStream fs = File.OpenRead(file))
                {
                    ApexLexer   scanner = new ApexLexer(fs);
                    ApexParser  parser  = new ApexParser(scanner);
                    ParseResult result  = parser.ParseApex();

                    foreach (LanguageError err in result.Errors)
                    {
                        Console.WriteLine(err.ToString());
                    }

                    Assert.AreEqual(0, result.Errors.Length, "Errors occured parsing: " + file);
                }

                Console.WriteLine();
            }
        }
        public static void Test()
        {
            var methodTestData = LexerTestData.GetMethods();

            foreach (var lexerTestElement in methodTestData)
            {
                var apexTokens     = ApexLexer.GetApexTokens(lexerTestElement.ApexLine);
                var apexTokensTest = lexerTestElement.TokenList;

                using (List <Token> .Enumerator apexTokEnumerator = apexTokens.GetEnumerator())
                {
                    using (List <TokenType> .Enumerator apexTestTokenEnumerator = apexTokensTest.GetEnumerator())
                    {
                        Console.WriteLine(lexerTestElement.ApexLine);
                        Console.WriteLine();
                        while (apexTestTokenEnumerator.MoveNext() && apexTokEnumerator.MoveNext())
                        {
                            Console.WriteLine(apexTestTokenEnumerator.Current + "  " + apexTokEnumerator.Current.TokenType);
                        }
                        Console.WriteLine();
                    }
                }
            }
        }
        public static List <string> FormatApexCodeNoIndent(string apexCode)
        {
            List <string> apexCodeList = new List <string>();

            var apexTokens               = ApexLexer.GetApexTokens(apexCode);
            var multiLineCommentLevel    = 0;
            var bracketNestingLevel      = 0;
            var lastValidCommentPosition = 0;
            var lastTokenType            = TokenType.Empty;

            string apexLine = string.Empty;

            foreach (var apexToken in apexTokens)
            {
                // Reposition the comment at the start of the last statement
                if (apexToken.TokenType == TokenType.CommentLine && multiLineCommentLevel == 0)
                {
                    apexCodeList.Insert(lastValidCommentPosition, apexToken.Content.Trim());
                    apexLine = apexLine.TrimEnd();
                    lastValidCommentPosition++;
                }

                // Replace the original newlines with a single space unless it's a multi-line comment
                else if (apexToken.TokenType == TokenType.Return)
                {
                    if (multiLineCommentLevel == 0 && lastTokenType != TokenType.CommentEnd)
                    {
                        if (lastTokenType != TokenType.Space)
                        {
                            apexLine += " ";
                        }

                        lastTokenType = TokenType.Space;
                        continue;
                    }

                    apexCodeList.Add(apexLine.Trim());
                    apexLine = string.Empty;
                }

                // Ignore duplicate spaces unless it's a multi-line comment
                else if (apexToken.TokenType == TokenType.Space && multiLineCommentLevel == 0)
                {
                    // Replace multiple spaces with a single one
                    if (lastTokenType != TokenType.Space)
                    {
                        apexLine += " ";
                    }

                    lastTokenType = apexToken.TokenType;
                    continue;
                }

                // If we have a ';' then next line should be new
                else if (apexToken.TokenType == TokenType.StatementTerminator && bracketNestingLevel == 0)
                {
                    apexLine = apexLine + apexToken.Content;

                    if (lastTokenType != TokenType.KwGetSet)
                    {
                        apexCodeList.Add(apexLine.Trim());
                        apexLine = string.Empty;
                    }
                }

                // '{' and "}" should be on its own line
                else if (apexToken.TokenType == TokenType.OpenCurlyBrackets || apexToken.TokenType == TokenType.CloseCurlyBrackets)
                {
                    apexCodeList.Add(apexLine.Trim());
                    apexCodeList.Add(apexToken.Content);
                    apexLine = string.Empty;
                }
                else
                {
                    apexLine = apexLine + apexToken.Content;
                }

                // Save the last statement starting position
                if (ValidCommentPositions.Contains(apexToken.TokenType))
                {
                    lastValidCommentPosition = apexCodeList.Count;
                }

                // Compute the multi-line comment nesting level
                else if (apexToken.TokenType == TokenType.CommentStart)
                {
                    multiLineCommentLevel++;
                }
                else if (apexToken.TokenType == TokenType.CommentEnd)
                {
                    multiLineCommentLevel--;
                }

                // Compute the bracket nesting level
                else if (apexToken.TokenType == TokenType.OpenBrackets)
                {
                    bracketNestingLevel++;
                }
                else if (apexToken.TokenType == TokenType.CloseBrackets)
                {
                    bracketNestingLevel--;
                }

                lastTokenType = apexToken.TokenType;
            }

            // Make sure that the last line isn't lost if it's not empty
            if (!string.IsNullOrWhiteSpace(apexLine))
            {
                apexCodeList.Add(apexLine.Trim());
            }

            // Ignore empty lines and collapse empty getters/setters
            var newApexCodeList = new List <string>();

            for (var index = 0; index < apexCodeList.Count; index++)
            {
                var apexCodeLine = apexCodeList[index];
                if (apexCodeLine == "{" &&
                    index < apexCodeList.Count - 2 &&
                    apexCodeList[index + 2] == "}" &&
                    IsEmptyGetterOrSetter(apexCodeList[index + 1]))
                {
                    apexCodeLine = $"{{ {apexCodeList[index + 1]} }}";
                    index       += 2;

                    if (newApexCodeList.Count > 0)
                    {
                        newApexCodeList[newApexCodeList.Count - 1] += " " + apexCodeLine;
                        continue;
                    }
                }

                if (apexCodeLine.Length != 0)
                {
                    newApexCodeList.Add(apexCodeLine);
                }
            }

            return(newApexCodeList);
        }