Esempio n. 1
0
        public void is_a_string_token()
        {
            StringTokenizer st = new StringTokenizer("\"\"coucou\"a string\" 1454 toto");
            string          s;

            // we must have ""
            Assert.That(st.IsString(out s));
            Assert.That(s, Is.EqualTo(""));
            // we must have coucou
            Assert.That(st.MatchIdentifier("coucou"));
            // we must have ""
            Assert.That(st.IsString(out s) && s == "a string");

            Assert.That(st.IsString(out s), Is.False);
            Assert.That(st.IsIdentifier(out s), Is.False);
            Assert.That(st.Match(TokenType.OpenCurly), Is.False);
            Assert.That(st.MatchIdentifier("b"), Is.False);
            int i;

            Assert.That(st.IsInteger(out i) && i == 1454);

            Assert.That(st.IsString(out s), Is.False);
            Assert.That(st.IsInteger(out i), Is.False);
            Assert.That(st.Match(TokenType.SemiColon), Is.False);
            Assert.That(st.MatchIdentifier("YOYO"), Is.False);

            Assert.That(st.MatchIdentifier("toto"));
            Assert.That(st.Match(TokenType.EndOfInput));
        }
        public void simple_tokens()
        {
            StringTokenizer t = new StringTokenizer("2 + 96");

            t.Match(TokenType.None).Should().BeTrue();
            t.MatchInteger(out var i).Should().BeTrue(); i.Should().Be(2);
            t.Match(TokenType.Plus).Should().BeTrue();
            t.MatchInteger(out i).Should().BeTrue(); i.Should().Be(96);
        }
Esempio n. 3
0
        Node ParseCondExpression(StringTokenizer t)
        {
            var condition = ParseExpression(t);

            if (t.Match(TokenType.QuestionMark))
            {
                var then = ParseCondExpression(t);
                if (!t.Match(TokenType.Colon))
                {
                    return(new ErrorNode("Expected : of ternary operator."));
                }
                var @else = ParseCondExpression(t);
                condition = new IfNode(condition, then, @else);
            }
            return(condition);
        }
Esempio n. 4
0
 Node ParseFactor(StringTokenizer t)
 {
     if (t.Match(TokenType.Minus))
     {
         return(new UnaryNode(TokenType.Minus, ParsePositiveFactor(t)));
     }
     return(ParsePositiveFactor(t));
 }
Esempio n. 5
0
 Node ParsePositiveFactor(StringTokenizer t)
 {
     if (t.MatchDouble(out var f))
     {
         return(new ConstantNode(f));
     }
     if (t.MatchIdentifier(out var id))
     {
         return(new IdentifierNode(id));
     }
     if (t.Match(TokenType.OpenPar))
     {
         Node expr = ParseCondExpression(t);
         if (!t.Match(TokenType.ClosePar))
         {
             return(new ErrorNode("Expected )."));
         }
         return(expr);
     }
     return(new ErrorNode("Expected ( or number."));
 }