1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Rook
.Compiler
19 using Castle
.Rook
.Compiler
.AST
;
20 using Castle
.Rook
.Compiler
.Services
;
21 using Castle
.Rook
.Compiler
.Services
.Passes
;
23 public delegate void PassInfoHandler(
24 object sender
, ICompilerPass pass
, CompilationUnit unit
, IErrorReport errorService
);
29 private CompilerContainer container
;
31 public event PassInfoHandler PrePassExecution
;
33 public event PassInfoHandler PostPassExecution
;
35 public Compiler() : this(new CompilerContainer())
39 public Compiler(CompilerContainer container
)
41 this.container
= container
;
44 public ICompilerOptionsSet OptionsSet
46 get { return (ICompilerOptionsSet) container[ typeof(ICompilerOptionsSet) ]; }
49 public bool Compile( String textContent
)
51 return Compile( new StringReader(textContent
) );
54 public bool Compile( FileInfo fileInfo
)
56 using(StreamReader reader
= new StreamReader(fileInfo
.FullName
))
58 return Compile( reader
);
62 public bool Compile( CompilationUnit unit
, FileInfo fileInfo
)
64 using(StreamReader reader
= new StreamReader(fileInfo
.FullName
))
66 return Compile( unit
, reader
);
70 public bool Compile( String
[] files
)
72 CompilationUnit cunit
= new CompilationUnit();
74 foreach( String file
in files
)
76 if (!Compile(cunit
, new FileInfo(file
)))
82 return RunPasses(cunit
);
85 public bool Compile( TextReader reader
)
87 CompilationUnit cunit
= new CompilationUnit();
89 if (Compile(cunit
, reader
))
91 return RunPasses(cunit
);
97 public bool Compile( CompilationUnit cunit
, TextReader reader
)
99 container
.ParserService
.Parse(cunit
, reader
);
101 return !container
.ErrorReportService
.HasErrors
;
104 private bool RunPasses(CompilationUnit cunit
)
106 if (!container
.ErrorReportService
.HasErrors
)
108 ICompilerPass builderSkeleton
=
109 container
[ typeof(CreateBuilderSkeleton
) ] as ICompilerPass
;
111 ExecutePass( builderSkeleton
, cunit
);
114 if (!container
.ErrorReportService
.HasErrors
)
116 ICompilerPass scopePass
=
117 container
[ typeof(ScopePass
) ] as ICompilerPass
;
119 ExecutePass( scopePass
, cunit
);
122 if (!container
.ErrorReportService
.HasErrors
)
124 ICompilerPass typeResPass
=
125 container
[ typeof(TypeResolutionPass
) ] as ICompilerPass
;
127 ExecutePass( typeResPass
, cunit
);
130 if (!container
.ErrorReportService
.HasErrors
)
132 ICompilerPass emission
=
133 container
[ typeof(Emission
) ] as ICompilerPass
;
135 emission
.ExecutePass(cunit
);
138 return !container
.ErrorReportService
.HasErrors
;
141 private void ExecutePass(ICompilerPass pass
, CompilationUnit unit
)
143 if (PrePassExecution
!= null)
145 PrePassExecution(this, pass
, unit
, container
.ErrorReportService
);
148 pass
.ExecutePass(unit
);
150 if (PostPassExecution
!= null)
152 PostPassExecution(this, pass
, unit
, container
.ErrorReportService
);