Esempio n. 1
0
 public void AssignmentToken()
 {
     var reader = new StringReader("=");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<OperatorToken>());
     reader.Close();
 }
Esempio n. 2
0
 public void CombinedWhiteSpaceAndComments()
 {
     var reader = new StringReader("\n\t\t// ... \n // ... \n     foo");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(((IdentifierToken)lexer.NextToken()).Lexeme, Is.EqualTo("foo"));
     reader.Close();
 }
Esempio n. 3
0
 public void KeywordsAreRecognised(string @keyword)
 {
     var reader = new StringReader(@keyword);
     var lexer = new MiniJavaScanner(reader);
     IToken next = lexer.NextToken();
     Assert.That(next, Is.InstanceOf<KeywordToken>());
     Assert.That(((KeywordToken)next).Lexeme, Is.EqualTo(@keyword));
     reader.Close();
 }
Esempio n. 4
0
 public void ArithmeticOperatorsAreRecognised(string @operator)
 {
     var reader = new StringReader(@operator);
     var lexer = new MiniJavaScanner(reader);
     IToken token = lexer.NextToken();
     Assert.That(token, Is.InstanceOf<OperatorToken>());
     Assert.That(((OperatorToken)token).Lexeme, Is.EqualTo(@operator));
     reader.Close();
 }
 public Program ParseProgram(string program)
 {
     var scanner = new MiniJavaScanner(new StringReader(program));
     var errorLog = new ErrorLogger();
     var parser = new Parser(scanner, errorLog, true);
     Program ast;
     if (parser.TryParse(out ast))
     {
         return ast;
     }
     return null;
 }
Esempio n. 6
0
 /* Performs lexical and semantic analysis. Returns null if either phase fails.
  *
  * Syntax analysis is always attempted regardless of lexical errors.
  * All errors are logged in the error log.
  */
 private Program ConstructAbstractSyntaxTree()
 {
     var scanner = new MiniJavaScanner(_program);
     var parser = new Parser(scanner, _errorLog);
     Program program;
     if (parser.TryParse(out program))
     {
         return program;
     }
     else
     {
         return null;
     }
 }
        public static SemanticsChecker SetUpTypeAndReferenceChecker(string program, out IErrorReporter errorLog)
        {
            var reader = new StringReader(program);
            var scanner = new MiniJavaScanner(reader);
            var errors = new ErrorLogger();
            var parser = new Parser(scanner, errors, true);
            Program syntaxTree;
            parser.TryParse(out syntaxTree);
            reader.Close();
            Assert.That(errors.Errors, Is.Empty);

            var symbolTableBuilder = new SymbolTableBuilder(syntaxTree, errors);
            Assert.That(errors.Errors, Is.Empty);

            Assert.DoesNotThrow(() => symbolTableBuilder.BuildSymbolTable());
            errorLog = new ErrorLogger();

            return new SemanticsChecker(syntaxTree, errorLog);
        }
Esempio n. 8
0
        public void DivisionSymbolIsNotConfusedWithAComment()
        {
            var reader = new StringReader("/");
            var lexer = new MiniJavaScanner(reader);
            Assert.That(lexer.NextToken(), Is.InstanceOf<OperatorToken>());
            reader.Close();

            reader = new StringReader("// .. / ..\n /");
            lexer = new MiniJavaScanner(reader);
            Assert.That(((OperatorToken)lexer.NextToken()).Lexeme, Is.EqualTo("/"));
            reader.Close();
        }
Esempio n. 9
0
        public void CommentsAreSkipped()
        {
            var reader = new StringReader("// ... \n // ... \n foo");
            var lexer = new MiniJavaScanner(reader);
            var token = (IdentifierToken)lexer.NextToken();
            Assert.That(token.Lexeme, Is.EqualTo("foo"));
            Assert.That(token.Row, Is.EqualTo(3));
            Assert.That(token.Col, Is.EqualTo(2));
            reader.Close();

            reader = new StringReader("/* ... \n\n*/ \tfoo");
            lexer = new MiniJavaScanner(reader);
            token = (IdentifierToken)lexer.NextToken();
            Assert.That(token.Lexeme, Is.EqualTo("foo"));
            Assert.That(token.Row, Is.EqualTo(3));
            Assert.That(token.Col, Is.EqualTo(5));
            reader.Close();

            reader = new StringReader("\n\n// ...//\n// ... \n\n/* ... */ foo");
            lexer = new MiniJavaScanner(reader);
            Assert.That(((IdentifierToken)lexer.NextToken()).Lexeme, Is.EqualTo("foo"));
            reader.Close();
        }
Esempio n. 10
0
 public void Identifiers()
 {
     var reader = new StringReader("42foo");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(((IntegerLiteralToken)lexer.NextToken()).Lexeme, Is.EqualTo("42"));
     IToken next = lexer.NextToken();
     Assert.That(next, Is.InstanceOf<IdentifierToken>());
     Assert.That(((IdentifierToken)next).Lexeme, Is.EqualTo("foo"));
     lexer = new MiniJavaScanner(new StringReader("f_o12a"));
     Assert.That(((IdentifierToken)lexer.NextToken()).Lexeme, Is.EqualTo("f_o12a"));
     reader.Close();
 }
Esempio n. 11
0
        public void EndlessComment()
        {
            var reader = new StringReader("/* ... ");
            var scanner = new MiniJavaScanner(reader);
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            reader.Close();

            reader = new StringReader("/* ... /");
            scanner = new MiniJavaScanner(reader);
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            reader.Close();

            reader = new StringReader("/* ... *");
            scanner = new MiniJavaScanner(reader);
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            reader.Close();

            reader = new StringReader("/* ... /* ... /*");
            scanner = new MiniJavaScanner(reader);
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            reader.Close();
        }
Esempio n. 12
0
 public void InputConsistingOfWhitespaceOnly()
 {
     var reader = new StringReader("\n   \t\t\v\r\n  ");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<EndOfFile>());
     reader.Close();
 }
Esempio n. 13
0
 public void SimpleTypes(string @type)
 {
     var reader = new StringReader(@type);
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<MiniJavaTypeToken>());
     reader.Close();
 }
Esempio n. 14
0
 public void ThrowsAnExceptionIfInputExhausted()
 {
     var reader = new StringReader("");
     var scanner = new MiniJavaScanner(reader);
     Assert.That(scanner.NextToken(), Is.InstanceOf<EndOfFile>());
     Assert.Throws<OutOfInput>(() => scanner.NextToken());
     reader.Close();
 }
        private SymbolTableBuilder.ExitCode BuildSymbolTableFor(string program, out GlobalScope symbolTable)
        {
            var reader = new StringReader(program);
            var scanner = new MiniJavaScanner(reader);
            _errors = new ErrorLogger();
            var parser = new Parser(scanner, _errors, true);
            Program syntaxTree;
            parser.TryParse(out syntaxTree);
            reader.Close();
            Assert.That(_errors.Errors, Is.Empty);

            var symbolTableBuilder = new SymbolTableBuilder(syntaxTree, _errors);
            Assert.That(_errors.Errors, Is.Empty);

            var success = symbolTableBuilder.BuildSymbolTable();
            symbolTable = syntaxTree.Scope as GlobalScope;
            return success;
        }
Esempio n. 16
0
        public void ShouldBeInvalid()
        {
            var reader = new StringReader("$");
            var scanner = new MiniJavaScanner(reader);
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            reader.Close();

            reader = new StringReader("&|");
            scanner = new MiniJavaScanner(reader);
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            Assert.That(scanner.NextToken(), Is.InstanceOf<ErrorToken>());
            reader.Close();
        }
Esempio n. 17
0
 public void CommentsMayBeNested()
 {
     var reader = new StringReader("\n\n// ...//\n// ... \n\n/* ... /* ... */ ... */ foo");
     var lexer = new MiniJavaScanner(reader);
     Assert.That((lexer.NextToken()).Lexeme, Is.EqualTo("foo"));
     reader.Close();
 }
Esempio n. 18
0
 public void ReportsRowsAndColumnsRightForTokens()
 {
     string program = "class Factorial {\n" +
                      "\t public static void main () {\n";
     var reader = new StringReader(program);
     var scanner = new MiniJavaScanner(reader);
     var token = scanner.NextToken();
     Assert.AreEqual(token.Row, 1);
     Assert.AreEqual(token.Col, 1);
     token = scanner.NextToken();
     Assert.AreEqual(token.Row, 1);
     Assert.AreEqual(token.Col, 7);
     token = scanner.NextToken();
     Assert.AreEqual(token.Row, 1);
     Assert.AreEqual(token.Col, 17);
     token = scanner.NextToken();
     Assert.AreEqual(token.Row, 2);
     Assert.AreEqual(token.Col, 3);
     reader.Close();
 }
Esempio n. 19
0
 public void ParameterSeparator()
 {
     var reader = new StringReader(",");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     reader.Close();
 }
Esempio n. 20
0
 public void MethodInvocation()
 {
     var reader = new StringReader("foo.bar()");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<IdentifierToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<IdentifierToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<EndOfFile>());
     reader.Close();
 }
Esempio n. 21
0
 public void CurlyBraces()
 {
     var reader = new StringReader("{}");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     reader.Close();
 }
Esempio n. 22
0
 public void WhiteSpaceIsSkipped()
 {
     var reader = new StringReader("\n\t\v\n  foo");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(((IdentifierToken)lexer.NextToken()).Lexeme, Is.EqualTo("foo"));
     reader.Close();
 }
 public void SetUpParser(string program)
 {
     var scanner = new MiniJavaScanner(new StringReader(program));
     _errorLog = new ErrorLogger();
     _parser = new Parser(scanner, _errorLog);
 }
Esempio n. 24
0
 public void ArrayType(string @type)
 {
     var reader = new StringReader(@type + "[]");
     var lexer = new MiniJavaScanner(reader);
     Assert.That(lexer.NextToken(), Is.InstanceOf<MiniJavaTypeToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     Assert.That(lexer.NextToken(), Is.InstanceOf<PunctuationToken>());
     reader.Close();
 }
Esempio n. 25
0
        public void TestEndOfFile()
        {
            var reader = new StringReader("");
            var lexer = new MiniJavaScanner(reader);
            Assert.That(lexer.NextToken(), Is.InstanceOf<EndOfFile>());
            reader.Close();

            reader = new StringReader("123");
            lexer = new MiniJavaScanner(reader);
            lexer.NextToken();
            Assert.That(lexer.NextToken(), Is.InstanceOf<EndOfFile>());
            Assert.Throws<OutOfInput>(() => lexer.NextToken());
            reader.Close();
        }
Esempio n. 26
0
        public void IntegerConstants()
        {
            var reader = new StringReader("123");
            var lexer = new MiniJavaScanner(reader);
            Assert.That(((IntegerLiteralToken)lexer.NextToken()).Lexeme, Is.EqualTo("123"));
            reader.Close();

            reader = new StringReader("1 23");
            lexer = new MiniJavaScanner(reader);
            var token = (IntegerLiteralToken)lexer.NextToken();
            Assert.That(token.Lexeme, Is.EqualTo("1"));
            Assert.That(token.Row, Is.EqualTo(1));
            Assert.That(token.Col, Is.EqualTo(1));
            Assert.That(((IntegerLiteralToken)lexer.NextToken()).Lexeme, Is.EqualTo("23"));
            reader.Close();
        }