Repository navigation
Expand file tree
/
Copy pathLuaSyntaxNodeTransfor.Query.cs
More file actions
154 lines (136 loc) · 7.17 KB
/
Copy pathLuaSyntaxNodeTransfor.Query.cs
File metadata and controls
154 lines (136 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2016 YANG Huan ([email protected]).
Copyright 2016 Redmoon Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using CSharpLua.LuaAst;
namespace CSharpLua {
public sealed partial class LuaSyntaxNodeTransfor {
public override LuaSyntaxNode VisitAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax node) {
var name = (LuaIdentifierNameSyntax)node.NameEquals.Accept(this);
var expression = (LuaExpressionSyntax)node.Expression.Accept(this);
return new LuaKeyValueTableItemSyntax(new LuaTableLiteralKeySyntax(name), expression);
}
public override LuaSyntaxNode VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax node) {
LuaTableInitializerExpression table = new LuaTableInitializerExpression();
foreach (var initializer in node.Initializers) {
var item = (LuaKeyValueTableItemSyntax)initializer.Accept(this);
table.Items.Add(item);
}
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.AnonymousType, table);
}
public override LuaSyntaxNode VisitQueryExpression(QueryExpressionSyntax node) {
CurCompilationUnit.ImportLinq();
LuaIdentifierNameSyntax rangeVariable = new LuaIdentifierNameSyntax(node.FromClause.Identifier.ValueText);
CheckVariableDeclaratorName(ref rangeVariable, node);
var fromClauseExpression = (LuaExpressionSyntax)node.FromClause.Expression.Accept(this);
return BuildQueryBody(node.Body, fromClauseExpression, rangeVariable);
}
public override LuaSyntaxNode VisitFromClause(FromClauseSyntax node) {
throw new InvalidOperationException();
}
public override LuaSyntaxNode VisitWhereClause(WhereClauseSyntax node) {
throw new InvalidOperationException();
}
public override LuaSyntaxNode VisitQueryBody(QueryBodySyntax node) {
throw new InvalidOperationException();
}
public override LuaSyntaxNode VisitSelectClause(SelectClauseSyntax node) {
throw new InvalidOperationException();
}
private LuaExpressionSyntax BuildQueryWhere(LuaExpressionSyntax collection, WhereClauseSyntax node, LuaIdentifierNameSyntax rangeVariable) {
var condition = (LuaExpressionSyntax)node.Condition.Accept(this);
var whereFunction = new LuaFunctionExpressionSyntax();
whereFunction.AddParameter(rangeVariable);
whereFunction.AddStatement(new LuaReturnStatementSyntax(condition));
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.LinqWhere, collection, whereFunction);
}
private LuaExpressionSyntax BuildOrdering(LuaIdentifierNameSyntax methodName, LuaExpressionSyntax collection, OrderingSyntax node, LuaIdentifierNameSyntax rangeVariable) {
var type = semanticModel_.GetTypeInfo(node.Expression).Type;
var typeName = GetTypeName(type);
var expression = (LuaExpressionSyntax)node.Expression.Accept(this);
var keySelector = new LuaFunctionExpressionSyntax();
keySelector.AddParameter(rangeVariable);
keySelector.AddStatement(new LuaReturnStatementSyntax(expression));
return new LuaInvocationExpressionSyntax(methodName, collection, keySelector, LuaIdentifierNameSyntax.Nil, typeName);
}
private LuaExpressionSyntax BuildQueryOrderBy(LuaExpressionSyntax collection, OrderByClauseSyntax node, LuaIdentifierNameSyntax rangeVariable) {
foreach (var ordering in node.Orderings) {
bool isDescending = ordering.AscendingOrDescendingKeyword.IsKind(SyntaxKind.DescendingKeyword);
if (ordering == node.Orderings.First()) {
var methodName = isDescending ? LuaIdentifierNameSyntax.LinqOrderByDescending : LuaIdentifierNameSyntax.LinqOrderBy;
collection = BuildOrdering(methodName, collection, ordering, rangeVariable);
}
else {
var methodName = isDescending ? LuaIdentifierNameSyntax.LinqThenByDescending : LuaIdentifierNameSyntax.LinqThenBy;
collection = BuildOrdering(methodName, collection, ordering, rangeVariable);
}
}
return collection;
}
private LuaExpressionSyntax BuildQuerySelect(LuaExpressionSyntax collection, SelectClauseSyntax node, LuaIdentifierNameSyntax rangeVariable) {
var expression = (LuaExpressionSyntax)node.Expression.Accept(this);
if (node.Expression.IsKind(SyntaxKind.IdentifierName)) {
var identifierName = expression as LuaIdentifierNameSyntax;
if (identifierName != null && identifierName.ValueText == rangeVariable.ValueText) {
return collection;
}
}
var type = semanticModel_.GetTypeInfo(node.Expression).Type;
var typeExpression = GetTypeName(type);
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.LinqSelect, collection, expression, typeExpression);
}
private LuaExpressionSyntax BuildGroupClause(LuaExpressionSyntax collection, GroupClauseSyntax node, LuaIdentifierNameSyntax rangeVariable) {
var type = semanticModel_.GetTypeInfo(node.ByExpression).Type;
var typeName = GetTypeName(type);
var byExpression = (LuaExpressionSyntax)node.ByExpression.Accept(this);
var keySelector = new LuaFunctionExpressionSyntax();
keySelector.AddParameter(rangeVariable);
keySelector.AddStatement(new LuaReturnStatementSyntax(byExpression));
return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.LinqGroupBy, collection, keySelector, typeName);
}
public LuaSyntaxNode BuildQueryBody(QueryBodySyntax node, LuaExpressionSyntax fromClauseExpression, LuaIdentifierNameSyntax rangeVariable) {
LuaExpressionSyntax collection = fromClauseExpression;
foreach (var clause in node.Clauses) {
switch (clause.Kind()) {
case SyntaxKind.WhereClause: {
collection = BuildQueryWhere(collection, (WhereClauseSyntax)clause, rangeVariable);
break;
}
case SyntaxKind.OrderByClause: {
collection = BuildQueryOrderBy(collection, (OrderByClauseSyntax)clause, rangeVariable);
break;
}
default: {
throw new NotSupportedException();
}
}
}
if (node.SelectOrGroup.IsKind(SyntaxKind.SelectClause)) {
var selectClause = (SelectClauseSyntax)node.SelectOrGroup;
collection = BuildQuerySelect(collection, selectClause, rangeVariable);
}
else {
var groupClause = (GroupClauseSyntax)node.SelectOrGroup;
collection = BuildGroupClause(collection, groupClause, rangeVariable);
}
return collection;
}
}
}