Example #1
0
        public static AstNode Parse(List <Token> lexerTokens)
        {
            // remove all comments and whitespace
            var tokens = lexerTokens.Where(lt => !(lt is CommentToken) &&
                                           !(lt is WhitespaceToken)).ToList();

            var stream  = new ParserStream(tokens);
            var program = MofSpecificationAst.Parse(stream);

            return(program);
        }
Example #2
0
        public static string ConvertToMof(MofSpecificationAst node, MofQuirks quirks = MofQuirks.None)
        {
            var source = new StringBuilder();

            for (var i = 0; i < node.Productions.Count; i++)
            {
                if (i > 0)
                {
                    source.AppendLine();
                }
                source.Append(MofGenerator.ConvertToMof(node.Productions[i], quirks));
            }
            return(source.ToString());
        }
            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);
            }
            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);
            }