Пример #1
0
        public void TestTokenize()
        {
            CollectionAssert.AreEqual(ExprTree.Tokenize("x").ToArray(), new[] { "x" });

            CollectionAssert.AreEqual(ExprTree.Tokenize("hello world").ToArray(), new[] { "hello", "world" });

            CollectionAssert.AreEqual(ExprTree.Tokenize(" hello  world ").ToArray(), new[] { "hello", "world" });

            CollectionAssert.AreEqual(ExprTree.Tokenize("{a list }").ToArray(), new[] { "{", "a", "list", "}" });

            CollectionAssert.AreEqual(ExprTree.Tokenize("{a \n{nested}\n list}").ToArray(),
                                      new[] { "{", "a", "{", "nested", "}", "list", "}" });

            CollectionAssert.AreEqual(ExprTree.Tokenize("[other (types of) brackets]").ToArray(),
                                      new[] { "[", "other", "(", "types", "of", ")", "brackets", "]" });

            CollectionAssert.AreEqual(ExprTree.Tokenize("{}").ToArray(), new[] { "{", "}" });

            CollectionAssert.AreEqual(ExprTree.Tokenize("").ToArray(), new string[] {});

            CollectionAssert.AreEqual(ExprTree.Tokenize(
                                          @"{a; this is a comment
                    {nested} ; another comment}
                   list}").ToArray(),
                                      new[] { "{", "a", "{", "nested", "}", "list", "}" }
                                      );
        }
 /// Returns a TruId, or throws an error if the TruExpr isn't a valid TruId
 public static new TruId Parse(ExprTree expr)
 {
     if (expr is ExprLiteral exprLit)
     {
         foreach (string keyword in ReservedWords)
         {
             if (exprLit.val == keyword)
             {
                 throw new TruSyntaxError($"Can't use \"{exprLit.val}\" as an identifier.");
             }
         }
         return(new TruId(exprLit.val));
     }
     else
     {
         throw new TruSyntaxError($"Can't use an expression as an identifier.");
     }
 }
Пример #3
0
        public void TestExprTreeParseAll()
        {
            CollectionAssert.AreEqual(ExprTree.ParseAll("x"), new[] { new ExprLiteral("x") });

            CollectionAssert.AreEqual(ExprTree.ParseAll("hello world"),
                                      new[] { new ExprLiteral("hello"), new ExprLiteral("world") });


            CollectionAssert.AreEqual(ExprTree.ParseAll("{a list} x"),
                                      new ExprTree[] {
                new ExprList(new ExprTree[] {
                    new ExprLiteral("a"),
                    new ExprLiteral("list")
                }),
                new ExprLiteral("x")
            });

            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("missing)"));
        }
Пример #4
0
        public void TestExprTreeParse()
        {
            Assert.AreEqual(ExprTree.Parse("x"), new ExprLiteral("x"));

            Assert.AreEqual(ExprTree.Parse("{a  list }"),
                            new ExprList(new ExprTree[] { new ExprLiteral("a"), new ExprLiteral("list") }));

            Assert.AreEqual(ExprTree.Parse("{a {nested} list}"),
                            new ExprList(new ExprTree[] {
                new ExprLiteral("a"),
                new ExprList(new ExprTree[] {
                    new ExprLiteral("nested")
                }),
                new ExprLiteral("list")
            }));

            Assert.AreEqual(ExprTree.Parse("{}"), new ExprList());

            Assert.AreEqual(ExprTree.Parse("[other (types of) brackets]"),
                            new ExprList(new ExprTree[] {
                new ExprLiteral("other"),
                new ExprList(new ExprTree[] {
                    new ExprLiteral("types"),
                    new ExprLiteral("of")
                }),
                new ExprLiteral("brackets")
            }));


            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("hello world"));
            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse(""));
            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("}"));
            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("{"));
            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("[mismatched}"));
            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("(missing"));
            Assert.ThrowsException <TruSyntaxError>(() => ExprTree.Parse("missing)"));
        }
 public static new TruExpr Parse(string code)
 {
     return(TruId.Parse(ExprTree.Parse(code)));
 }
Пример #6
0
 public Connection <MessageType> AddChild <MessageType>(AbstractBlock destination, Filter <MessageType> filter)
 {
     return(ExprTree.BlockAddChild(this, destination, filter));
 }
Пример #7
0
        public void TestMatch()
        {
            Assert.IsTrue(ExprTree.Match("ANY", ExprTree.Parse("words")));

            Assert.IsTrue(ExprTree.Match("ANY", ExprTree.Parse("{a list}")));

            Assert.IsTrue(ExprTree.Match("ANY", ExprTree.Parse("ANY")));

            Assert.IsFalse(ExprTree.Match("{ANY}", ExprTree.Parse("word")));


            Assert.IsTrue(ExprTree.Match("LITERAL", ExprTree.Parse("words")));

            Assert.IsTrue(ExprTree.Match("LITERAL", ExprTree.Parse("LITERAL")));

            Assert.IsFalse(ExprTree.Match("LITERAL", ExprTree.Parse("{a list}")));


            Assert.IsTrue(ExprTree.Match("{a list}", ExprTree.Parse("{a  list}")));

            Assert.IsTrue(ExprTree.Match("{}", ExprTree.Parse("{}")));

            Assert.IsFalse(ExprTree.Match("{a list}", ExprTree.Parse("{a different list}")));


            Assert.IsTrue(ExprTree.Match("{append ANY ANY}", ExprTree.Parse("{append 'word' {reverse 'palindrome'}}")));

            Assert.IsFalse(ExprTree.Match("{append ANY LITERAL}", ExprTree.Parse("{append 'word' {reverse 'palindrome'}}")));

            Assert.IsFalse(ExprTree.Match("{append ANY ANY}", ExprTree.Parse("{+ 'word' {reverse 'palindrome'}}")));

            Assert.IsFalse(ExprTree.Match("{append ANY ANY}", ExprTree.Parse("{append 'word'}")));

            Assert.IsFalse(ExprTree.Match("{append ANY ANY}", ExprTree.Parse("{append 'word' 'word1' 'word2'}")));

            Assert.IsFalse(ExprTree.Match("{append {LITERAL ANY} ANY}", ExprTree.Parse("{append 'word' 'word1'}")));

            Assert.IsTrue(ExprTree.Match("{append {LITERAL ANY end} ANY}", ExprTree.Parse("{append {reverse 'palindrome' end} 'word1'}")));

            Assert.IsFalse(ExprTree.Match("{append {LITERAL ANY end} ANY}", ExprTree.Parse("{append {reverse 'palindrome' start} 'word1'}")));

            Assert.IsTrue(ExprTree.Match("LIST", ExprTree.Parse("{a list}")));

            Assert.IsTrue(ExprTree.Match("{a ...}", ExprTree.Parse("{}")));
            Assert.IsTrue(ExprTree.Match("{a ...}", ExprTree.Parse("{a}")));
            Assert.IsTrue(ExprTree.Match("{[LITERAL LITERAL] ...}", ExprTree.Parse("{[a b]}")));
            Assert.IsTrue(ExprTree.Match("{a ...}", ExprTree.Parse("{a a a}")));
            Assert.IsFalse(ExprTree.Match("{a b ...}", ExprTree.Parse("{a a b b}")));
            Assert.IsTrue(ExprTree.Match("{a b ... b}", ExprTree.Parse("{a b}")));
            Assert.IsFalse(ExprTree.Match("{a b ... b}", ExprTree.Parse("{a b c}")));
            Assert.IsFalse(ExprTree.Match("{a b ... b c}", ExprTree.Parse("{a b b}")));
            Assert.IsFalse(ExprTree.Match("{a b ... b}", ExprTree.Parse("{a}")));
            Assert.IsTrue(ExprTree.Match("{a b b ...}", ExprTree.Parse("{a b}")));
            Assert.IsFalse(ExprTree.Match("{b b ...}", ExprTree.Parse("{a b}")));
            Assert.IsTrue(ExprTree.Match("{b b ...}", ExprTree.Parse("{b}")));
            Assert.IsFalse(ExprTree.Match("{b b b ...}", ExprTree.Parse("{b}")));
            Assert.IsFalse(ExprTree.Match("{a b b ... c}", ExprTree.Parse("{a b b}")));
            Assert.IsFalse(ExprTree.Match("{a b b ...}", ExprTree.Parse("{a}")));
            Assert.IsTrue(ExprTree.Match("{b ... c ...}", ExprTree.Parse("{b b b c c c}")));
            Assert.IsFalse(ExprTree.Match("{b ... c ...}", ExprTree.Parse("{b b b c c c d}")));
        }