forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonCodeDomCodeGen.cs
More file actions
97 lines (77 loc) · 3.3 KB
/
PythonCodeDomCodeGen.cs
File metadata and controls
97 lines (77 loc) · 3.3 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.CodeDom;
using System.Collections.Generic;
using Microsoft.Scripting.Runtime;
#if FEATURE_CODEDOM
namespace IronPython.Hosting {
internal class PythonCodeDomCodeGen : CodeDomCodeGen {
// Tracks the indents found in user provided snippets
private Stack<int> _indents = new Stack<int>(new int[] { 0 });
// Tracks the indent level that is required by generated
// nesting levels (that the user can't see)
private int _generatedIndent = 0;
protected override void WriteExpressionStatement(CodeExpressionStatement s) {
Writer.Write(new string(' ', _generatedIndent + _indents.Peek()));
WriteExpression(s.Expression);
Writer.Write("\n");
}
protected override void WriteSnippetExpression(CodeSnippetExpression e) {
Writer.Write(IndentSnippet(e.Value));
}
protected override void WriteSnippetStatement(CodeSnippetStatement s) {
string snippet = s.Value;
// Finally, append the snippet. Make sure that it is indented properly if
// it has nested newlines
Writer.Write(IndentSnippetStatement(snippet));
Writer.Write('\n');
// See if the snippet changes our indent level
string lastLine = snippet.Substring(snippet.LastIndexOf('\n') + 1);
// If the last line is only whitespace, then we have a new indent level
if (string.IsNullOrEmpty(lastLine.Trim('\t', ' '))) {
lastLine = lastLine.Replace("\t", " ");
int indentLen = lastLine.Length;
if (indentLen > _indents.Peek()) {
_indents.Push(indentLen);
}
else {
while (indentLen < _indents.Peek()) {
_indents.Pop();
}
}
}
}
protected override void WriteFunctionDefinition(CodeMemberMethod func) {
Writer.Write("def ");
Writer.Write(func.Name);
Writer.Write("(");
for (int i = 0; i < func.Parameters.Count; ++i) {
if (i != 0) {
Writer.Write(",");
}
Writer.Write(func.Parameters[i].Name);
}
Writer.Write("):\n");
int baseIndent = _indents.Peek();
_generatedIndent += 4;
foreach (CodeStatement stmt in func.Statements) {
WriteStatement(stmt);
}
_generatedIndent -= 4;
while (_indents.Peek() > baseIndent) {
_indents.Pop();
}
}
protected override string QuoteString(string val) {
return "'''" + val.Replace(@"\", @"\\").Replace("'''", @"\'''") + "'''";
}
private string IndentSnippet(string block) {
return block.Replace("\n", "\n" + new string(' ', _generatedIndent));
}
private string IndentSnippetStatement(string block) {
return new string(' ', _generatedIndent) + IndentSnippet(block);
}
}
}
#endif