Permalink
Browse files

Current compiler sources

  • Loading branch information...
1 parent e699dd0 commit 59c1c3065d16aacc7216f9503738d98d1108877f @nikhilk committed Aug 5, 2012
Showing 409 changed files with 55,974 additions and 7 deletions.
View
@@ -40,10 +40,10 @@
<Reference Include="System" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities" />
- <Reference Include="ScriptSharp">
- <HintPath>..\..\..\bin\ref\ScriptSharp.dll</HintPath>
- <Private>True</Private>
- </Reference>
+ <ProjectReference Include="..\Compiler\Compiler.csproj">
+ <Project>{9f14036f-673f-418e-b817-7e2289d7f3f6}</Project>
+ <Name>Compiler</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\FileInputStreamSource.cs">
@@ -38,9 +38,10 @@
<Reference Include="System.XML" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities" />
- <Reference Include="ScriptSharp">
- <HintPath>..\..\..\bin\ref\ScriptSharp.dll</HintPath>
- </Reference>
+ <ProjectReference Include="..\Compiler\Compiler.csproj">
+ <Project>{9f14036f-673f-418e-b817-7e2289d7f3f6}</Project>
+ <Name>Compiler</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -0,0 +1,30 @@
+// AttributeBlockNode.cs
+// Script#/Core/ScriptSharp
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Diagnostics;
+
+namespace ScriptSharp.CodeModel {
+
+ internal sealed class AttributeBlockNode : ParseNode {
+
+ private AttributeTargets _location;
+ private ParseNodeList _attributes;
+
+ public AttributeBlockNode(Token token,
+ AttributeTargets location,
+ ParseNodeList attributes)
+ : base(ParseNodeType.AttributeBlock, token) {
+ _location = location;
+ _attributes = GetParentedNodeList(attributes);
+ }
+
+ public ParseNodeList Attributes {
+ get {
+ return _attributes;
+ }
+ }
+ }
+}
@@ -0,0 +1,67 @@
+// AttributeNode.cs
+// Script#/Core/ScriptSharp
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Diagnostics;
+
+namespace ScriptSharp.CodeModel {
+
+ internal sealed class AttributeNode : ParseNode {
+
+ private NameNode _typeName;
+ private ParseNode _arguments;
+
+ public AttributeNode(NameNode typeName, ParseNode arguments)
+ : base(ParseNodeType.Attribute, typeName.token) {
+ _typeName = (NameNode)GetParentedNode(typeName);
+ _arguments = GetParentedNode(arguments);
+ }
+
+ public ParseNodeList Arguments {
+ get {
+ if (_arguments != null) {
+ return ((ExpressionListNode)_arguments).Expressions;
+ }
+ return null;
+ }
+ }
+
+ public string TypeName {
+ get {
+ return _typeName.Name;
+ }
+ }
+
+ public static AttributeNode FindAttribute(ParseNodeList attributeNodes, string attributeName) {
+ if ((attributeNodes == null) || (attributeNodes.Count == 0)) {
+ return null;
+ }
+
+ foreach (AttributeNode attrNode in attributeNodes) {
+ if (attrNode.TypeName.Equals(attributeName, StringComparison.Ordinal)) {
+ return attrNode;
+ }
+ }
+
+ return null;
+ }
+
+ public static ParseNodeList GetAttributeList(ParseNodeList attributeBlocks) {
+ if ((attributeBlocks == null) || (attributeBlocks.Count == 0)) {
+ return attributeBlocks;
+ }
+
+ ParseNodeList attributes = new ParseNodeList();
+ foreach (AttributeBlockNode attributeBlock in attributeBlocks) {
+ ParseNodeList localAttributes = attributeBlock.Attributes;
+ if (localAttributes.Count != 0) {
+ attributes.Append(localAttributes);
+ }
+ }
+
+ return attributes;
+ }
+ }
+}
@@ -0,0 +1,33 @@
+// AttributeTargets.cs
+// Script#/Core/ScriptSharp
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+
+namespace ScriptSharp.CodeModel {
+
+ /// <summary>
+ /// Locations which an attribute section can target.
+ /// </summary>
+ internal enum AttributeTargets {
+
+ Assembly,
+
+ Field,
+
+ Event,
+
+ Method,
+
+ Module,
+
+ Param,
+
+ Property,
+
+ Return,
+
+ Type
+ }
+}
@@ -0,0 +1,101 @@
+// CodeModelBuilder.cs
+// Script#/Core/ScriptSharp
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using ScriptSharp;
+using ScriptSharp.Parser;
+
+namespace ScriptSharp.CodeModel {
+
+ internal sealed class CodeModelBuilder {
+
+ private CompilerOptions _options;
+ private IErrorHandler _errorHandler;
+
+ private bool _hasErrors;
+
+ public CodeModelBuilder(CompilerOptions options, IErrorHandler errorHandler) {
+ _options = options;
+ _errorHandler = errorHandler;
+ }
+
+ public CompilationUnitNode BuildCodeModel(IStreamSource source) {
+ _hasErrors = false;
+
+ string filePath = source.FullName;
+#if DEBUG
+ if (_options.InternalTestMode) {
+ // This ensures in file paths are just file names in test output.
+ filePath = Path.GetFileName(filePath);
+ }
+#endif // DEBUG
+ char[] buffer = GetBuffer(source);
+ if (buffer == null) {
+ _errorHandler.ReportError("Unable to read from file " + filePath, filePath);
+ return null;
+ }
+
+ IDictionary definesTable = new Hashtable();
+ if ((_options.Defines != null) && (_options.Defines.Count != 0)) {
+ foreach (string s in _options.Defines) {
+ definesTable[s] = null;
+ }
+ }
+
+ NameTable nameTable = new NameTable();
+ LineMap lineMap = new LineMap(filePath);
+
+ FileLexer lexer = new FileLexer(nameTable, filePath);
+ lexer.OnError += new FileErrorEventHandler(OnError);
+ Token[] tokens = lexer.Lex(buffer, definesTable, lineMap, /* includeComments */ false);
+
+ if (_hasErrors == false) {
+ FileParser parser = new FileParser(nameTable, filePath);
+ parser.OnError += new FileErrorEventHandler(OnError);
+
+ CompilationUnitNode compilationUnit = parser.Parse(tokens, lineMap);
+ foreach (ParseNode node in compilationUnit.Members) {
+ NamespaceNode namespaceNode = node as NamespaceNode;
+ if (namespaceNode != null) {
+ namespaceNode.IncludeCompilationUnitUsingClauses();
+ }
+ }
+
+ if (_hasErrors == false) {
+ return compilationUnit;
+ }
+ }
+
+ return null;
+ }
+
+ private char[] GetBuffer(IStreamSource source) {
+ char[] buffer = null;
+
+ Stream stream = source.GetStream();
+ if (stream != null) {
+ StreamReader reader = new StreamReader(stream);
+ string text = reader.ReadToEnd();
+
+ buffer = text.ToCharArray();
+ source.CloseStream(stream);
+ }
+ return buffer;
+ }
+
+ private void OnError(object sender, FileErrorEventArgs e) {
+ _hasErrors = true;
+
+ string location = e.Position.ToString();
+ string message = String.Format(e.Error.Message, e.Args);
+
+ _errorHandler.ReportError(message, location);
+ }
+ }
+}
@@ -0,0 +1,117 @@
+// CodeModelProcessor.cs
+// Script#/Core/ScriptSharp
+// This source code is subject to terms and conditions of the Apache License, Version 2.0.
+//
+
+using System;
+using System.Diagnostics;
+using System.Reflection;
+
+namespace ScriptSharp.CodeModel {
+
+ internal sealed class CodeModelProcessor {
+
+ private IParseNodeHandler _nodeHandler;
+ private object _context;
+ private bool _notifyChildren;
+
+ public CodeModelProcessor(IParseNodeHandler nodeHandler, object context) {
+ _nodeHandler = nodeHandler;
+ _context = context;
+ _notifyChildren = nodeHandler.RequiresChildrenGrouping;
+ }
+
+ private void EndChildren() {
+ if (_notifyChildren) {
+ _nodeHandler.EndChildren();
+ }
+ }
+
+ public void Process(ParseNode node) {
+ Visit(node);
+ }
+
+ private bool ProcessNode(ParseNode node) {
+ return _nodeHandler.HandleNode(node, _context);
+ }
+
+ private void StartChildren(string identifier) {
+ if (_notifyChildren) {
+ _nodeHandler.StartChildren(identifier);
+ }
+ }
+
+ private void Visit(ParseNode node) {
+ bool recurse = ProcessNode(node);
+
+ if (recurse) {
+ StartChildren(String.Empty);
+
+ Type nodeType = node.GetType();
+ foreach (PropertyInfo propertyInfo in nodeType.GetProperties()) {
+ string propertyName = propertyInfo.Name;
+ if (propertyName.Equals("NodeType")) {
+ continue;
+ }
+ if (propertyName.Equals("Parent")) {
+ continue;
+ }
+ if (propertyName.Equals("Token")) {
+ continue;
+ }
+
+ Visit(node, propertyInfo);
+ }
+
+ EndChildren();
+ }
+ }
+
+ private void Visit(ParseNode node, PropertyInfo propertyInfo) {
+ string name = propertyInfo.Name;
+ object value = propertyInfo.GetValue(node, null);
+
+ string text = name + " (" + propertyInfo.PropertyType.Name + ")";
+
+ if (value != null) {
+ if (value is ParseNodeList) {
+ ParseNodeList nodeList = (ParseNodeList)value;
+
+ if (nodeList.Count == 0) {
+ text += " : Empty";
+ }
+ else {
+ text += " : " + nodeList.Count.ToString();
+ }
+
+ StartChildren(text);
+ foreach (ParseNode nodeItem in nodeList) {
+ Visit(nodeItem);
+ }
+ EndChildren();
+ }
+ else if (value is ParseNode) {
+ StartChildren(text);
+ Visit((ParseNode)value);
+ EndChildren();
+ }
+ else {
+ if (value is string) {
+ text += " : \"" + (string)value + "\"";
+ }
+ else {
+ text += " : " + value.ToString();
+ }
+
+ StartChildren(text);
+ EndChildren();
+ }
+ }
+ else {
+ text += " : null";
+ StartChildren(text);
+ EndChildren();
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 59c1c30

Please sign in to comment.