示例#1
0
        public void GetArithmeticOperators()
        {
            Lexer lexer = new Lexer("+-*/");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("+", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("-", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("*", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("/", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#2
0
        public void GetAtom()
        {
            Lexer lexer = new Lexer("ok");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Atom, token.Type);
            Assert.AreEqual("ok", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#3
0
        public void GetAtomAndSkipComment()
        {
            Lexer lexer = new Lexer("ok % this is a comment");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Atom, token.Type);
            Assert.AreEqual("ok", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#4
0
        public void GetAtomWithAt()
        {
            Lexer lexer = new Lexer("joe@somehost");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Atom, token.Type);
            Assert.AreEqual("joe@somehost", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#5
0
文件: Program.cs 项目: ajlopez/AjErl
        public static void Main(string[] args)
        {
            Console.WriteLine("AjErl alfa 0.0.1");

            Lexer lexer = new Lexer(Console.In);
            Parser parser = new Parser(lexer);
            Machine machine = new Machine();

            while (true)
                try
                {
                    ProcessExpression(parser, machine.RootContext);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    Console.Error.WriteLine(ex.StackTrace);
                }
        }
示例#6
0
        public void GetIntegerWithSpaces()
        {
            Lexer lexer = new Lexer("  123   ");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Integer, token.Type);
            Assert.AreEqual("123", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#7
0
        public void GetStrictEqualAsOperator()
        {
            Lexer lexer = new Lexer("=:=");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("=:=", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#8
0
        public void GetNumeralBracketAsSeparator()
        {
            Lexer lexer = new Lexer("#{");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual("#{", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#9
0
        public void UnexpectedSign()
        {
            Lexer lexer = new Lexer("?");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("Unexpected '?'", ex.Message);
            }
        }
示例#10
0
        public void GetVerticalBarAsSeparator()
        {
            Lexer lexer = new Lexer("|");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual("|", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#11
0
        public void UnclosedString()
        {
            Lexer lexer = new Lexer("\"foo");

            try
            {
                lexer.NextToken();
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ParserException));
                Assert.AreEqual("unclosed string", ex.Message);
            }
        }
示例#12
0
        public void GetUnderscoreNameAsVariable()
        {
            Lexer lexer = new Lexer("_ok");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Variable, token.Type);
            Assert.AreEqual("_ok", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#13
0
        public void GetVariable()
        {
            Lexer lexer = new Lexer("X");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Variable, token.Type);
            Assert.AreEqual("X", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#14
0
文件: Parser.cs 项目: ajlopez/AjErl
 public Parser(Lexer lexer)
 {
     this.lexer = lexer;
 }
示例#15
0
        public void GetParenthesisAsSeparators()
        {
            Lexer lexer = new Lexer("()");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual("(", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual(")", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#16
0
        public void GetSimpleMatch()
        {
            Lexer lexer = new Lexer("X=ok.");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Variable, token.Type);
            Assert.AreEqual("X", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("=", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Atom, token.Type);
            Assert.AreEqual("ok", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual(".", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#17
0
        public void GetSemiColonAsSeparator()
        {
            Lexer lexer = new Lexer(";");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual(";", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#18
0
        public void GetRightArrowAsOperator()
        {
            Lexer lexer = new Lexer("->");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Operator, token.Type);
            Assert.AreEqual("->", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#19
0
        public void GetReal()
        {
            Lexer lexer = new Lexer("123.45");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Real, token.Type);
            Assert.AreEqual("123.45", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#20
0
        public void GetPointAsOperator()
        {
            Lexer lexer = new Lexer(".");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual(".", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#21
0
        public void GetString()
        {
            Lexer lexer = new Lexer("\"foo\"");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.String, token.Type);
            Assert.AreEqual("foo", token.Value);

            Assert.IsNull(lexer.NextToken());
        }
示例#22
0
        public void GetBracketsAsSeparators()
        {
            Lexer lexer = new Lexer("[]");

            Token token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual("[", token.Value);

            token = lexer.NextToken();

            Assert.IsNotNull(token);
            Assert.AreEqual(TokenType.Separator, token.Type);
            Assert.AreEqual("]", token.Value);

            Assert.IsNull(lexer.NextToken());
        }