public void testScanner2() {
			var source = @"
class C {
}
";
			var codeErrorManager = new CodeErrorManager();
			var preprocessor = new Preprocessor(codeErrorManager, source.toCharArray());
			var scanner = new PreprocessedTextScanner(codeErrorManager, preprocessor.preprocess());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Keyword, scanner.nextLexicalUnit());
			RestorePoint rp = scanner.createRestorePoint();
			Assert.assertEquals(LexicalUnit.Whitespace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Identifier, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Whitespace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.OpenBrace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			scanner.restore(rp);
			Assert.assertEquals(LexicalUnit.Keyword, scanner.nextLexicalUnit());
			Assert.assertEquals("class", new String(scanner.Text, scanner.StartPosition, scanner.EndPosition - scanner.StartPosition));
			Assert.assertEquals(LexicalUnit.Whitespace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Identifier, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Whitespace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.OpenBrace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.CloseBrace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.EndOfStream, scanner.nextLexicalUnit());
		}
		public void testScanner() {
			var source = @"
class C {
}
";
			var codeErrorManager = new CodeErrorManager();
			var preprocessor = new Preprocessor(codeErrorManager, source.toCharArray());
			var scanner = new PreprocessedTextScanner(codeErrorManager, preprocessor.preprocess());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Keyword, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Whitespace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Identifier, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.Whitespace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.OpenBrace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.CloseBrace, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.NewLine, scanner.nextLexicalUnit());
			Assert.assertEquals(LexicalUnit.EndOfStream, scanner.nextLexicalUnit());
		}
Exemplo n.º 3
0
        public CompilerResults compileFromFiles(CompilerParameters parameters, File[] files) {
            var results = new CompilerResults();
            this.context = new CompilerContext(parameters, results);
            this.statementValidator = new StatementValidator(this.context);
            this.expressionValidator = new ExpressionValidator(this.context);
            this.statementValidator.ExpressionValidator = this.expressionValidator;
            this.expressionValidator.StatementValidator = this.statementValidator;
            this.reachabilityChecker = new ReachabilityChecker(context);
            this.assignmentChecker = new AssignmentChecker(context);
            this.bytecodeGenerator = new BytecodeGenerator(context);
            bool tragicError = false;
			
            var buffer = new char[4096];
            var sb = new StringBuilder();
            var parser = new Parser();
            
            foreach (var file in files) {
                sb.setLength(0);
                InputStreamReader reader = null;
                try {
                    reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
                    int read;
                    while ((read = reader.read(buffer)) != -1) {
                        sb.append(buffer, 0, read);
                    }
                    
                    var text = new char[sb.length()];
                    sb.getChars(0, sizeof(text), text, 0);
                    if (sizeof(text) > 0) {
                        if (text[sizeof(text) - 1] == '\u001a') {
                            text[sizeof(text) - 1] = ' ';
                        }
                    }
                    var preprocessor = new Preprocessor(results.codeErrorManager, text);
                    preprocessor.Filename = file.getAbsolutePath();
					preprocessor.Symbols.addAll(parameters.Symbols);
                    
                    var scanner = new PreprocessedTextScanner(results.codeErrorManager, preprocessor.preprocess());
                    scanner.Filename = file.getAbsolutePath();
                    var compilationUnit = parser.parseCompilationUnit(scanner);
                    
                    if (compilationUnit != null) {
                        compilationUnit.Symbols = preprocessor.Symbols;
                        context.CompilationUnits.add(compilationUnit);
                    }
                } catch (CodeErrorException) {
				} catch (Exception e) {
					e.printStackTrace();
					tragicError = true;
					break;
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException) {
                        }
                    }
                }
            }
            if (!tragicError) {
				if (!context.HasErrors) {
					if (parameters.ProgressTracker != null) {
						parameters.ProgressTracker.compilationStageFinished(CompilationStage.Parsing);
					}
					doCompile();
				}
			}
            this.context = null;
            this.statementValidator = null;
            this.expressionValidator = null;
            this.reachabilityChecker = null;
            this.assignmentChecker = null;
            this.queryTranslator = null;
            this.documentationBuilder = null;
			
			if (parameters.ProgressTracker != null) {
				parameters.ProgressTracker.compilationFinished();
			}
            return results;
        }
		private void parse(IFile file, char[] text, CodeErrorManager errorManager, Map<String, CompilationUnitNode> compilationUnits) {
			var filename = parameters.AllFiles.getProjectRelativeName(file);
			
			var preprocessor = new Preprocessor(errorManager, text);
			preprocessor.setFilename(filename);
			preprocessor.Symbols.addAll(parameters.PreprocessorSymbols);
			var preprocessedText = preprocessor.preprocess();
			
			var parser = new Parser();
			var scanner = new PreprocessedTextScanner(errorManager, preprocessedText);
			scanner.setFilename(filename);
			scanner.setTabWidth(Environment.getTabWidth());
			
			var compilationUnit = parser.parseCompilationUnit(scanner);
			if (compilationUnit != null) {
				compilationUnit.setSymbols(preprocessor.Symbols);
				compilationUnits[filename] = compilationUnit;
			}
		}