public static void QualifierWithMofV2FlavorsAndQuirksDisabledShouldThrow()
            {
                var sourceMof =
                    "[Locale(1033): ToInstance, UUID(\"{BE46D060-7A7C-11d2-BC85-00104B2CF71C}\"): ToInstance]\r\n" +
                    "class Win32_PrivilegesStatus : __ExtendedStatus\r\n" +
                    "{\r\n" +
                    "\t[read: ToSubClass, MappingStrings{\"Win32API|AccessControl|Windows NT Privileges\"}: ToSubClass] string PrivilegesNotHeld[];\r\n" +
                    "\t[read: ToSubClass, MappingStrings{\"Win32API|AccessControl|Windows NT Privileges\"}: ToSubClass] string PrivilegesRequired[];\r\n" +
                    "};";
                var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
                var tokensMof = TokenMofGenerator.ConvertToMof(tokens);
                var ex        = Assert.Throws <UnexpectedTokenException>(
                    () =>
                {
                    var astNodes = Parser.Parse(tokens);
                }
                    );

                Assert.AreEqual(
                    "Unexpected token found at Position 13, Line Number 1, Column Number 14.\r\n" +
                    "Token Type: 'ColonToken'\r\n" +
                    "Token Text: ':'",
                    ex.Message
                    );
            }
            public static void EnumerationDeclarationDeprecatedMof300IntegerBaseAndQuirksEnabledShouldThrow()
            {
                // this should throw because "uint32" is recognized as an integer type.
                // as a result, "July" (a string) is not a valid value for an integer enumElement value
                var sourceMof =
                    "enumeration MonthsEnum : uint32\r\n" +
                    "{\r\n" +
                    "\tJuly = \"July\"\r\n" +
                    "};";
                var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
                var tokensMof = TokenMofGenerator.ConvertToMof(tokens);
                var ex        = Assert.Throws <UnexpectedTokenException>(
                    () =>
                {
                    var astNodes = Parser.Parse(
                        tokens,
                        ParserQuirks.AllowDeprecatedMof300IntegerTypesAsEnumerationDeclarationsBase
                        );
                }
                    );

                Assert.AreEqual(
                    "Unexpected token found at Position 44, Line Number 3, Column Number 9.\r\n" +
                    "Token Type: 'StringLiteralToken'\r\n" +
                    "Token Text: '\"July\"'",
                    ex.Message
                    );
            }
        //[TestFixture]
        //public static class ConvertToMofMethodTestCasesWmiWinXp
        //{
        //    [Test, TestCaseSource(typeof(ConvertToMofMethodTestCasesWmiWinXp), "GetTestCases")]
        //    public static void ConvertToMofMethodTestsFromDisk(string mofFilename)
        //    {
        //        ConvertToMofTests.MofGeneratorRoundtripTest(mofFilename);
        //    }
        //    public static IEnumerable<TestCaseData> GetTestCases
        //    {
        //        get
        //        {
        //            return TestUtils.GetMofTestCase("Parsing\\WMI\\WinXp");
        //        }
        //    }
        //}

        //[TestFixture]
        //public static class ConvertToMofMethodGolfExamples
        //{
        //    //[Test, TestCaseSource(typeof(ConvertToMofMethodGolfExamples), "GetTestCases")]
        //    public static void ConvertToMofMethodTestsFromDisk(string mofFilename)
        //    {
        //        ConvertToMofTests.MofGeneratorRoundtripTest(mofFilename);
        //    }
        //    public static IEnumerable<TestCaseData> GetTestCases
        //    {
        //        get
        //        {
        //            return TestUtils.GetMofTestCase("Parsing\\DSP0221_3.0.1");
        //        }
        //    }
        //}

        private static void AssertRoundtrip(string sourceMof, ParserQuirks parserQuirks = ParserQuirks.None)
        {
            // check the lexer tokens roundtrips ok
            var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
            var tokensMof = TokenMofGenerator.ConvertToMof(tokens);

            Assert.AreEqual(sourceMof, tokensMof);
            // check the parser ast roundtrips ok
            var astNodes = Parser.Parse(tokens, parserQuirks);
            var astMof   = AstMofGenerator.ConvertToMof(astNodes);

            Assert.AreEqual(sourceMof, astMof);
        }
            public static void InvalidClassFeatureShouldThrow()
            {
                var sourceMof =
                    "class Sponsor\r\n" +
                    "{\r\n" +
                    "\t100\r\n" +
                    "};";
                var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
                var tokensMof = TokenMofGenerator.ConvertToMof(tokens);
                var ex        = Assert.Throws <UnexpectedTokenException>(
                    () => {
                    var astNodes = Parser.Parse(tokens);
                }
                    );

                Assert.AreEqual(
                    "Unexpected token found at Position 19, Line Number 3, Column Number 2.\r\n" +
                    "Token Type: 'IntegerLiteralToken'\r\n" +
                    "Token Text: '100'",
                    ex.Message
                    );
            }
            public static void EnumValueArrayWithQualifiedEnumValuesAndQuirksDisabledShouldThrow()
            {
                var sourceMof =
                    "instance of GOLF_Date\r\n" +
                    "{\r\n" +
                    "\tMonth = {MonthEnums.July};\r\n" +
                    "};";
                var tokens    = Lexing.Lexer.Lex(SourceReader.From(sourceMof));
                var tokensMof = TokenMofGenerator.ConvertToMof(tokens);
                var ex        = Assert.Throws <UnexpectedTokenException>(
                    () =>
                {
                    var astNodes = Parser.Parse(tokens);
                }
                    );

                Assert.AreEqual(
                    "Unexpected token found at Position 46, Line Number 3, Column Number 21.\r\n" +
                    "Token Type: 'DotOperatorToken'\r\n" +
                    "Token Text: '.'",
                    ex.Message
                    );
            }
Example #6
0
        public static List <Instance> ParseMofFileInstances(string filename)
        {
            // read the text from the mof file
            var sourceText = File.ReadAllText(filename);

            // turn the text into a stream of characters for lexing
            var reader = SourceReader.From(sourceText);

            // lex the characters into a sequence of tokens
            var tokens = Lexer.Lex(reader);

            // parse the tokens into an ast tree
            var ast = Parser.Parse(tokens);

            // scan the ast for any "instance" definitions and convert them
            var instances = ((MofSpecificationAst)ast).Productions
                            .Where(p => (p is InstanceValueDeclarationAst))
                            .Cast <InstanceValueDeclarationAst>()
                            .Select(Instance.FromAstNode)
                            .ToList();

            // return the result
            return(instances.ToList());
        }
        public static void ParseMofFileInstances(string filename)
        {
            // read the text from the mof file
            var sourceText = File.ReadAllText(filename);

            // turn the text into a stream of characters for lexing
            var reader = SourceReader.From(sourceText);

            // lex the characters into a sequence of tokens
            var tokens = Lexer.Lex(reader);

            // parse the tokens into an ast tree
            var ast = Parser.Parse(tokens);

            // scan the ast for any "instance" definitions and convert them

            /*            var instances = ((MofSpecificationAst)ast).Productions
             *                                                    .Where(p => (p is InstanceValueDeclarationAst))
             *                                                    .Cast<InstanceValueDeclarationAst>()
             *                                                    .Select(Instance.FromAstNode)
             *                                                    .ToList();*/

            var classes = ((MofSpecificationAst)ast).Productions
                          .Where(p => (p is ClassDeclarationAst))
                          .Cast <ClassDeclarationAst>()
                          .ToList();

            foreach (var cc in classes)
            {
                System.Console.WriteLine(cc.ClassName);
            }


            // return the result
            //return instances.ToList();
        }
 public static void ParsePropetyValueArrayWithNumericLiteralValues()
 {
     var tokens = Lexing.Lexer.Lex(
         SourceReader.From(
             "instance of myType as $Alias00000070\r\n" +
             "{\r\n" +
             "    MyBinaryValue = 0101010b;\r\n" +
             "    MyOctalValue = 0444444;\r\n" +
             "    MyHexValue = 0xABC123;\r\n" +
             "    MyDecimalValue = 12345;\r\n" +
             "    MyRealValue = 123.45;\r\n" +
             "};"
             )
         );
     var actualAst   = Parser.Parse(tokens);
     var expectedAst = new MofSpecificationAst(
         new ReadOnlyCollection <MofProductionAst>(
             new List <MofProductionAst>
     {
         new InstanceValueDeclarationAst(
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(0, 1, 1),
                     new SourcePosition(7, 1, 8),
                     "instance"
                     ),
                 "instance"
                 ),
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(9, 1, 10),
                     new SourcePosition(10, 1, 11),
                     "of"
                     ),
                 "of"
                 ),
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(12, 1, 13),
                     new SourcePosition(17, 1, 18),
                     "myType"
                     ),
                 "myType"
                 ),
             new IdentifierToken(
                 new SourceExtent(
                     new SourcePosition(19, 1, 20),
                     new SourcePosition(20, 1, 21),
                     "as"
                     ),
                 "as"
                 ),
             new AliasIdentifierToken(
                 new SourceExtent(
                     new SourcePosition(22, 1, 23),
                     new SourcePosition(35, 1, 36),
                     "$Alias00000070"
                     ),
                 "Alias00000070"
                 ),
             new PropertyValueListAst(
                 new ReadOnlyDictionary <string, PropertyValueAst>(
                     new Dictionary <string, PropertyValueAst> {
             { "MyBinaryValue", new IntegerValueAst(
                   new IntegerLiteralToken(
                       new SourceExtent(
                           new SourcePosition(61, 3, 21),
                           new SourcePosition(68, 3, 28),
                           "0101010b"
                           ),
                       IntegerKind.BinaryValue, 0b101010
                       )
                   ) },
            public static void ParsePropetyValueArrayWithLiteralStrings()
            {
                var tokens = Lexing.Lexer.Lex(
                    SourceReader.From(
                        "instance of myType as $Alias00000070\r\n" +
                        "{\r\n" +
                        "    ServerURLs = { \"https://URL1\", \"https://URL2\" };\r\n" +
                        "};"
                        )
                    );
                var actualAst   = Parser.Parse(tokens);
                var expectedAst = new MofSpecificationAst(
                    new ReadOnlyCollection <MofProductionAst>(
                        new List <MofProductionAst>
                {
                    new InstanceValueDeclarationAst(
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(0, 1, 1),
                                new SourcePosition(7, 1, 8),
                                "instance"
                                ),
                            "instance"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(9, 1, 10),
                                new SourcePosition(10, 1, 11),
                                "of"
                                ),
                            "of"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(12, 1, 13),
                                new SourcePosition(17, 1, 18),
                                "myType"
                                ),
                            "myType"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(19, 1, 20),
                                new SourcePosition(20, 1, 21),
                                "as"
                                ),
                            "as"
                            ),
                        new AliasIdentifierToken(
                            new SourceExtent(
                                new SourcePosition(22, 1, 23),
                                new SourcePosition(35, 1, 36),
                                "$Alias00000070"
                                ),
                            "Alias00000070"
                            ),
                        new PropertyValueListAst(
                            new ReadOnlyDictionary <string, PropertyValueAst>(
                                new Dictionary <string, PropertyValueAst> {
                        { "ServerURLs", new LiteralValueArrayAst(
                              new ReadOnlyCollection <LiteralValueAst>(
                                  new List <LiteralValueAst> {
                                new StringValueAst.Builder {
                                    StringLiteralValues = new List <StringLiteralToken> {
                                        new StringLiteralToken(
                                            new SourceExtent(
                                                new SourcePosition(60, 3, 20),
                                                new SourcePosition(73, 3, 33),
                                                "\"https://URL1\""
                                                ),
                                            "https://URL1"
                                            )
                                    },
                                    Value = "https://URL1"
                                }.Build(),
                                new StringValueAst.Builder {
                                    StringLiteralValues = new List <StringLiteralToken> {
                                        new StringLiteralToken(
                                            new SourceExtent(
                                                new SourcePosition(76, 3, 36),
                                                new SourcePosition(89, 3, 49),
                                                "\"https://URL2\""
                                                ),
                                            "https://URL2"
                                            )
                                    },
                                    Value = "https://URL2"
                                }.Build()
                            }
                                  )
                              ) }
                    }
                                )
                            ),
                        new StatementEndToken(
                            new SourceExtent(
                                new SourcePosition(96, 4, 2),
                                new SourcePosition(96, 4, 2),
                                ";"
                                )
                            )
                        )
                }
                        )
                    );
                var actualJson   = TestUtils.ConvertToJson(actualAst);
                var expectedJson = TestUtils.ConvertToJson(expectedAst);

                Assert.AreEqual(expectedJson, actualJson);
            }
Example #10
0
            public static void ParsePropetyValueArrayWithAliasIdentifier()
            {
                var tokens = Lexing.Lexer.Lex(
                    SourceReader.From(
                        "instance of myType as $Alias00000070\r\n" +
                        "{\r\n" +
                        "    Reference = {$Alias0000006E};\r\n" +
                        "};"
                        )
                    );
                var actualAst   = Parser.Parse(tokens);
                var expectedAst = new MofSpecificationAst(
                    new ReadOnlyCollection <MofProductionAst>(
                        new List <MofProductionAst>
                {
                    new InstanceValueDeclarationAst(
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(0, 1, 1),
                                new SourcePosition(7, 1, 8),
                                "instance"
                                ),
                            "instance"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(9, 1, 10),
                                new SourcePosition(10, 1, 11),
                                "of"
                                ),
                            "of"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(12, 1, 13),
                                new SourcePosition(17, 1, 18),
                                "myType"
                                ),
                            "myType"
                            ),
                        new IdentifierToken(
                            new SourceExtent(
                                new SourcePosition(19, 1, 20),
                                new SourcePosition(20, 1, 21),
                                "as"
                                ),
                            "as"
                            ),
                        new AliasIdentifierToken(
                            new SourceExtent(
                                new SourcePosition(22, 1, 23),
                                new SourcePosition(35, 1, 36),
                                "$Alias00000070"
                                ),
                            "Alias00000070"
                            ),
                        new PropertyValueListAst(
                            new ReadOnlyDictionary <string, PropertyValueAst>(
                                new Dictionary <string, PropertyValueAst> {
                        { "Reference", new ComplexValueArrayAst(
                              new ReadOnlyCollection <ComplexValueAst>(
                                  new List <ComplexValueAst> {
                                new ComplexValueAst(
                                    new AliasIdentifierToken(
                                        new SourceExtent(
                                            new SourcePosition(58, 3, 18),
                                            new SourcePosition(71, 3, 31),
                                            "$Alias0000006E"
                                            ),
                                        "Alias0000006E"
                                        )
                                    )
                            }
                                  )
                              ) }
                    }
                                )
                            ),
                        new StatementEndToken(
                            new SourceExtent(
                                new SourcePosition(77, 4, 2),
                                new SourcePosition(77, 4, 2),
                                ";"
                                )
                            )
                        )
                }
                        )
                    );
                var actualJson   = TestUtils.ConvertToJson(actualAst);
                var expectedJson = TestUtils.ConvertToJson(expectedAst);

                Assert.AreEqual(expectedJson, actualJson);
            }
        public List <SyntaxToken> Lex(string sourceText)
        {
            var reader = SourceReader.From(sourceText);

            return(this.ReadToEnd(reader).ToList());
        }