This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathutil.py
154 lines (116 loc) · 4.47 KB
/
util.py
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
# coding=utf-8
# Copyright 2016 Google Inc. All Rights Reserved.
#
# 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.
"""Utilities for generating Go code."""
from __future__ import unicode_literals
import codecs
import contextlib
import cStringIO
import string
import StringIO
import textwrap
_SIMPLE_CHARS = set(string.digits + string.letters + string.punctuation + " ")
_ESCAPES = {'\t': r'\t', '\r': r'\r', '\n': r'\n', '"': r'\"', '\\': r'\\'}
# This is the max length of a direct allocation tuple supported by the runtime.
# This should match the number of specializations found in tuple.go.
MAX_DIRECT_TUPLE = 6
class CompileError(Exception):
def __init__(self, node, msg):
if hasattr(node, 'lineno'):
msg = 'line {}: {}'.format(node.lineno, msg)
super(CompileError, self).__init__(msg)
class ParseError(CompileError):
pass
class ImportError(CompileError): # pylint: disable=redefined-builtin
pass
class LateFutureError(ImportError):
def __init__(self, node):
msg = 'from __future__ imports must occur at the beginning of the file'
super(LateFutureError, self).__init__(node, msg)
class Writer(object):
"""Utility class for writing blocks of Go code to a file-like object."""
def __init__(self, out=None):
self.out = codecs.getwriter('utf8')(out or cStringIO.StringIO())
self.indent_level = 0
def getvalue(self):
return self.out.getvalue().decode('utf8')
@contextlib.contextmanager
def indent_block(self, n=1):
"""A context manager that indents by n on entry and dedents on exit."""
self.indent(n)
yield
self.dedent(n)
def write(self, output):
for line in output.split('\n'):
if line:
self.out.write(''.join(('\t' * self.indent_level, line, '\n')))
def write_block(self, block_, body):
"""Outputs the boilerplate necessary for code blocks like functions.
Args:
block_: The Block object representing the code block.
body: String containing Go code making up the body of the code block.
"""
self.write('for ; πF.State() >= 0; πF.PopCheckpoint() {')
with self.indent_block():
self.write('switch πF.State() {')
self.write('case 0:')
for checkpoint in block_.checkpoints:
self.write_tmpl('case $state: goto Label$state', state=checkpoint)
self.write('default: panic("unexpected function state")')
self.write('}')
# Assume that body is aligned with goto labels.
with self.indent_block(-1):
self.write(body)
self.write('}')
def write_label(self, label):
with self.indent_block(-1):
self.write('Label{}:'.format(label))
def write_py_context(self, lineno, line):
self.write_tmpl('// line $lineno: $line', lineno=lineno, line=line)
def write_tmpl(self, tmpl, **kwargs):
self.write(string.Template(tmpl).substitute(kwargs))
def write_checked_call2(self, result, call, *args, **kwargs):
return self.write_tmpl(textwrap.dedent("""\
if $result, πE = $call; πE != nil {
\tcontinue
}"""), result=result.name, call=call.format(*args, **kwargs))
def write_checked_call1(self, call, *args, **kwargs):
return self.write_tmpl(textwrap.dedent("""\
if πE = $call; πE != nil {
\tcontinue
}"""), call=call.format(*args, **kwargs))
def write_temp_decls(self, block_):
all_temps = block_.free_temps | block_.used_temps
for temp in sorted(all_temps, key=lambda t: t.name):
self.write('var {0} {1}\n_ = {0}'.format(temp.name, temp.type_))
def indent(self, n=1):
self.indent_level += n
def dedent(self, n=1):
self.indent_level -= n
def go_str(value):
"""Returns value as a valid Go string literal."""
io = StringIO.StringIO()
io.write('"')
for c in value:
if c in _ESCAPES:
io.write(_ESCAPES[c])
elif c in _SIMPLE_CHARS:
io.write(c)
else:
io.write(r'\x{:02x}'.format(ord(c)))
io.write('"')
return io.getvalue()
def adjust_local_name(name):
"""Returns a Go identifier for the given Python variable name."""
return 'µ' + name