Skip to content

Commit 25aaaba

Browse files
committed
Work on TDL
1 parent bfe0230 commit 25aaaba

File tree

13 files changed

+457
-191
lines changed

13 files changed

+457
-191
lines changed

CSharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{EDCC3B85-0BAD-11DB-BC1A-00112FDE8B61}") = "Nitra.Runtime", "Nitra\Nitr
1111
EndProject
1212
Project("{EDCC3B85-0BAD-11DB-BC1A-00112FDE8B61}") = "DotNetLang", "Nitra\DotNetLang\DotNetLang.nproj", "{D3C87D75-E119-4D30-A8BE-DC28014BB653}"
1313
EndProject
14+
Project("{EDCC3B85-0BAD-11DB-BC1A-00112FDE8B61}") = "Tdl", "Grammars\DSLs\TDL\Tdl.nproj", "{82C32D2D-8E24-4975-B606-B2C7BE54C340}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{D3C87D75-E119-4D30-A8BE-DC28014BB653}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{D3C87D75-E119-4D30-A8BE-DC28014BB653}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{D3C87D75-E119-4D30-A8BE-DC28014BB653}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{82C32D2D-8E24-4975-B606-B2C7BE54C340}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{82C32D2D-8E24-4975-B606-B2C7BE54C340}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{82C32D2D-8E24-4975-B606-B2C7BE54C340}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{82C32D2D-8E24-4975-B606-B2C7BE54C340}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

Grammars/CSharp/CSharp.Grammar/CSharp/AST/AstUtils2.n

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ namespace DotNet
6868
when (!returnType.Equals(m.ReturnType))
6969
sameReturnType = false;
7070
}
71+
72+
| _ => ()
7173
}
7274
if (methodsCount == memberCount)
7375
{

Grammars/CSharp/CSharp.Grammar/CSharp/AST/CompilationUnit/CompilationUnit.n

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ namespace CSharp
189189
}
190190
}
191191

192-
class CSharpProjectEvalPropertiesHost : ProjectEvalPropertiesHost
192+
public class CSharpProjectEvalPropertiesHost : ProjectEvalPropertiesHost
193193
{
194194
private _rootNamespace : NamespaceSymbol;
195195

Grammars/DSLs/TDL/Ast.nitra

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1-
namespace Tdl
2-
{
3-
}
1+
using DotNet;
2+
using DotNet.AstUtils;
3+
4+
using Nitra;
5+
using Nitra.Declarations;
6+
7+
namespace Tdl
8+
{
9+
declaration Module : NamespaceBody
10+
{
11+
stage 0:
12+
in RootNamespace : NamespaceSymbol;
13+
14+
Members.DeclaredIn = RootNamespace;
15+
EnteredScope = Scope;
16+
}
17+
18+
abstract declaration Platform : NamespaceMember
19+
{
20+
| Def {}
21+
| Set { Platforms : Reference*; }
22+
}
23+
24+
//abstract ast ModuleMember : BindableAst, ScopedAst
25+
//{
26+
// in DeclaredIn : ModuleSymbol;
27+
//
28+
//stage 1:
29+
// inout Opening : Scope;
30+
// in CurrentScope : Scope;
31+
// in UsingAliasesScope : TableScope;
32+
//
33+
// ContainingTable = DeclaredIn.MemberTable;
34+
//}
35+
}

Grammars/DSLs/TDL/Grammar.nitra

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1-
namespace Tdl
2-
{
3-
syntax module Grammar2
4-
{
5-
using Nitra.Core;
6-
using CSharp.CsIdentifiers;
7-
using CSharp.Literals;
8-
using CSharp.TokenNames;
9-
10-
alias QualifiedName = QualifiedIdentifier;
11-
12-
[StartRule]
13-
syntax CompilationUnit = NamespaceMemberDeclaration*;
14-
15-
syntax StringLiteral
16-
{
17-
| RegularStringLiteral;
18-
| VerbatimStringLiteral;
19-
}
20-
21-
syntax NamespaceMemberDeclaration
22-
{
23-
| Alias = "using" sm Name sm "=" sm QualifiedName ";" nl;
24-
| Open = "using" sm QualifiedName ";" nl;
25-
| Platform = "platform" sm Name ";" nl;
26-
| PlatformSet = "platform" sm Name sm "=" Platforms=(Reference; "," sm)+ ";" nl;
27-
| Product = "product" sm Name nl ProductBody;
28-
}
29-
30-
syntax ProductBody = outline_begin_before "{" inl ProductPropertyDeclaration* d "}" nl outline_end_before;
31-
32-
syntax ProductPropertyDeclaration
33-
{
34-
| AutotestBinariesExcludedFilesMasks = Name sm "=" sm StringLiteral ";" nl;
35-
}
36-
}
37-
}
1+
namespace Tdl
2+
{
3+
syntax module Grammar
4+
{
5+
using Nitra.Core;
6+
using CSharp.CsIdentifiers;
7+
using CSharp.Literals;
8+
using CSharp.TokenNames;
9+
10+
[StartRule]
11+
syntax Module = MemberDeclaration*;
12+
13+
syntax StringLiteral
14+
{
15+
| RegularStringLiteral;
16+
| VerbatimStringLiteral;
17+
}
18+
19+
syntax MemberDeclaration
20+
{
21+
| Alias = "using" sm Name sm "=" sm QualifiedIdentifier ";" nl;
22+
| Open = "using" sm QualifiedIdentifier ";" nl;
23+
| Platform = "platform" sm Name ";" nl;
24+
| PlatformSet = "platform" sm Name sm "=" sm Platforms=(Reference; "," sm)+ ";" nl;
25+
//| Product = "product" sm Name nl Body;
26+
//| ProductSet = "product" sm Name sm "=" sm Platforms=(Reference; "," sm)+ ";" nl;
27+
//| DeploymentScript = "deployment" sm Name sm "script" ScriptPath=StringLiteral sm nl Body;
28+
}
29+
30+
syntax Body = outline_begin_before "{" inl DefDeclaration* d "}" nl outline_end_before;
31+
32+
syntax DefDeclaration = Name sm "=" sm DefExpr ";" nl;
33+
34+
syntax DefExpr
35+
{
36+
| StringLiteral
37+
| "true"
38+
| "false"
39+
| RealLiteral;
40+
| DecimalIntegerLiteral = Lit=DecimalIntegerLiteral
41+
| HexadecimalIntegerLiteral = Lit=HexadecimalIntegerLiteral
42+
}
43+
}
44+
}

Grammars/DSLs/TDL/Language.nitra

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
authors = Vlad Chistyakov;
1010
copyright = NitraTeam;
1111

12-
syntax module Tdl.Grammar2 start rule CompilationUnit;
12+
syntax module Tdl.Grammar start rule Module;
1313
}

Grammars/DSLs/TDL/Mapping.nitra

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using DotNet;
2+
using Nitra.Declarations;
3+
4+
namespace Tdl
5+
{
6+
map syntax Grammar.Module -> Module
7+
{
8+
Name = Name(Location.FromStart(), Location.Source.File.Name);
9+
MemberDeclarations -> Members;
10+
}
11+
12+
map syntax Grammar.MemberDeclaration -> NamespaceMember
13+
{
14+
| Open -> UsingOpenDirective { QualifiedIdentifier -> NamespaceOrTypeName; }
15+
| Alias -> UsingAliasDirective { Name -> Name; QualifiedIdentifier -> NamespaceOrTypeName; }
16+
| Platform -> Platform.Def { Name -> Name; }
17+
| PlatformSet -> Platform.Set { Name -> Name; Platforms.Item1 -> Platforms; }
18+
}
19+
}

Grammars/DSLs/TDL/ProjectSupport.n

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
using Nitra;
2+
using Nitra.BackEnd.Cci;
3+
using Nitra.Declarations;
4+
using Nitra.Internal;
5+
using Nitra.ProjectSystem;
6+
7+
using DotNet;
8+
9+
using Nemerle;
10+
using Nemerle.Collections;
11+
using Nemerle.Imperative;
12+
using Nemerle.Text;
13+
using Nemerle.Utility;
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Collections.Immutable;
18+
using System.Linq;
19+
20+
using System.Threading;
21+
using ND = Nitra.Declarations;
22+
23+
[assembly: ProjectSupport("TdlLang", typeof(Tdl.ProjectSupport))]
24+
25+
namespace Tdl
26+
{
27+
public partial class ProjectSupport : IProjectSupport
28+
{
29+
static NoLocation : Location = Location(SourceSnapshot.Default.File, NSpan(0));
30+
static NoFile : ProjectSystem.File = SourceSnapshot.Default.File;
31+
static NoSpan : NSpan = NSpan(0);
32+
33+
public RefreshReferences(cancellationToken : CancellationToken, project : Project) : object
34+
{
35+
TypeUnifier.InitInstance();
36+
37+
def bindNs(scope : Scope, name : string) : Ref[NamespaceSymbol]
38+
{
39+
//assert2(false);
40+
def res = Reference(Location.Default, name).Bind.[NamespaceSymbol](scope);
41+
assert2(!res.IsUnresolved);
42+
assert2(!res.IsAmbiguous);
43+
assert2(res.Symbols.Length == 1);
44+
res
45+
}
46+
def bindType(scope : Scope, name : string) : Ref[TopGenericTypeSymbol]
47+
{
48+
//assert2(false);
49+
def res = Reference(Location.Default, name).Bind.[TopGenericTypeSymbol](scope);
50+
assert2(!res.IsUnresolved);
51+
assert2(!res.IsAmbiguous);
52+
assert2(res.Symbols.Length == 1);
53+
res
54+
}
55+
def defineAlias(context : DependentPropertyEvalContext, systemNs : NamespaceSymbol, declaredIn : NamespaceSymbol, name : string, aliasName : string) : void
56+
{
57+
def symbolRef = bindType(systemNs.Scope, name);
58+
unless (symbolRef.IsSymbolEvaluated)
59+
return;
60+
61+
def symbol = symbolRef.Symbol;
62+
def decl = symbol.FirstDeclarationOrDefault :> IExternalTopTypeDeclaration;
63+
def aliasDecl = ExternalTopTypeDeclaration.[TypeAliasSymbol](ND.Name(Location.Default, aliasName), decl.Type);
64+
def alias = aliasDecl.DefineSymbol(declaredIn.MemberTable);
65+
alias.Replacement = symbolRef;
66+
alias.TypeParametersCount = 0;
67+
alias.TypeParameters = ImmutableArray.Empty;
68+
alias.DeclaredIn = declaredIn;
69+
alias.EvalProperties(context);
70+
}
71+
72+
def backEnd = CciBackEnd();
73+
backEnd.CancellationToken = cancellationToken;
74+
def context = DependentPropertyEvalContext(project);
75+
def rootNamespace = backEnd.LoadExternalSymbols(project.Libs, project.ProjectDir, project.CompilerMessages, context);
76+
def systemNsRef = bindNs(rootNamespace.Scope, "System");
77+
when (systemNsRef.IsSymbolEvaluated)
78+
{
79+
def systemNs = systemNsRef.Symbol;
80+
81+
defineAlias(context, systemNs, rootNamespace, "Object", "object");
82+
defineAlias(context, systemNs, rootNamespace, "Void", "void");
83+
defineAlias(context, systemNs, rootNamespace, "String", "string");
84+
defineAlias(context, systemNs, rootNamespace, "Boolean", "bool");
85+
defineAlias(context, systemNs, rootNamespace, "Byte", "byte");
86+
defineAlias(context, systemNs, rootNamespace, "SByte", "sbyte");
87+
defineAlias(context, systemNs, rootNamespace, "Int16", "short");
88+
defineAlias(context, systemNs, rootNamespace, "UInt16", "ushort");
89+
defineAlias(context, systemNs, rootNamespace, "Int32", "int");
90+
defineAlias(context, systemNs, rootNamespace, "UInt32", "uint");
91+
defineAlias(context, systemNs, rootNamespace, "Int64", "long");
92+
defineAlias(context, systemNs, rootNamespace, "UInt64", "ulong");
93+
defineAlias(context, systemNs, rootNamespace, "Single", "float");
94+
defineAlias(context, systemNs, rootNamespace, "Double", "double");
95+
defineAlias(context, systemNs, rootNamespace, "Decimal", "decimal");
96+
defineAlias(context, systemNs, rootNamespace, "Char", "char");
97+
}
98+
99+
(backEnd : IDotNetBackEnd, context, rootNamespace) }
100+
101+
public RefreshProject(cancellationToken : CancellationToken, files : ImmutableArray[FileEvalPropertiesData], data : object) : void
102+
{
103+
def (backEnd, context, rootNamespace) = (data :> IDotNetBackEnd * DependentPropertyEvalContext * NamespaceSymbol);
104+
context.CancellationToken = cancellationToken;
105+
backEnd.CancellationToken = cancellationToken;
106+
107+
def removeParsedSymbols(tableScope : TableScope)
108+
{
109+
tableScope.Undefine(_.IsParsed);
110+
foreach (symbols in tableScope.Symbols)
111+
foreach (symbol is NamespaceSymbol in symbols)
112+
removeParsedSymbols(symbol.MemberTable);
113+
}
114+
removeParsedSymbols(rootNamespace.MemberTable);
115+
116+
when (cancellationToken.IsCancellationRequested)
117+
return;
118+
119+
def evalHost = CSharp.CSharpProjectEvalPropertiesHost(files, rootNamespace);
120+
evalHost.EvalProperties(context, "SymbolHierarchy", 0);
121+
evalHost.EvalProperties(context, "Scopes", 1);
122+
evalHost.EvalProperties(context, "Type bodies binding", 2);
123+
}
124+
125+
public GetSymbolById(data : object, symbolId : int) : ValueOption[DeclarationSymbol]
126+
{
127+
def (_, _, rootNamespace) = DecomposeData(data);
128+
// TODO: cache symbols an use it cache to find symbol by id
129+
def findSymbol(symbols : Seq[DeclarationSymbol]) : ValueOption[DeclarationSymbol]
130+
{
131+
foreach (symbol in symbols)
132+
{
133+
when (symbol.Id == symbolId)
134+
return ValueOption.Some(symbol);
135+
136+
match (symbol)
137+
{
138+
| t is GenericContainerTypeSymbol =>
139+
def result = findSymbol(t.NestedTypes);
140+
when (result.IsSome)
141+
return result;
142+
143+
| NamespaceSymbol as ns =>
144+
foreach (symbols in ns.MemberTable.Symbols)
145+
{
146+
def result = findSymbol(symbols);
147+
when (result.IsSome)
148+
return result;
149+
}
150+
151+
| _ => ()
152+
}
153+
}
154+
155+
ValueOption.None()
156+
}
157+
158+
foreach (symbols in rootNamespace.MemberTable.Symbols)
159+
{
160+
def result = findSymbol(symbols);
161+
when (result.IsSome)
162+
return result;
163+
}
164+
165+
ValueOption.None()
166+
}
167+
168+
public DeconstructType(symbol : DeclarationSymbol, type : out TypeSymbol, typeArgs : out ImmutableArray[TypeSymbol]) : bool
169+
{
170+
match (symbol)
171+
{
172+
| s is TopConstructedTypeSymbol => type = s.TypeInfo; typeArgs = s.Args; true
173+
| s is NestedConstructedTypeSymbol => type = s.TypeInfo; typeArgs = s.Args; true
174+
| _ => type = null; typeArgs = ImmutableArray.Empty; false
175+
}
176+
}
177+
178+
public VisitGlobalSymbols(data : object, callback : Predicate[DeclarationSymbol]) : void
179+
{
180+
def (_, _, rootNamespace) = DecomposeData(data);
181+
_ = rootNamespace.VisitChildrenAndSelf(SymbolUtils.GetNestedSymbol, callback);
182+
}
183+
184+
public Postprocessing(cancellationToken : System.Threading.CancellationToken, project : Nitra.ProjectSystem.Project, asts : System.Collections.Immutable.ImmutableArray[(Nitra.Declarations.IAst * bool)], data : object) : void
185+
{
186+
IgnoreParams();
187+
}
188+
189+
public DecomposeData(data : object) : IDotNetBackEnd * DependentPropertyEvalContext * NamespaceSymbol
190+
{
191+
data :> (IDotNetBackEnd * DependentPropertyEvalContext * NamespaceSymbol)
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)