Debian Codegraph @

llvm-project/llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py

raw
1 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5 import os
6 import shutil
7 import subprocess
8 import tempfile
9 from ipykernel.kernelbase import Kernel
10
11 __version__ = "0.0.1"
12
13
14 class TableGenKernel(Kernel):
15 """Kernel using llvm-tblgen inside jupyter.
16
17 All input is treated as TableGen unless the first non whitespace character
18 is "%" in which case it is a "magic" line.
19
20 The supported magic is:
21 * %args - to set the arguments passed to llvm-tblgen.
22 * %reset - to reset the cached code and magic state.
23
24 These are "cell magic" meaning it applies to the whole cell. Therefore
25 it must be the first line, or part of a run of magic lines starting
26 from the first line.
27
28 ```tablgen
29 %args
30 %reset
31 %args --print-records --print-detailed-records
32 class Stuff {
33 string Name;
34 }
35
36 def a_thing : Stuff {}
37 ```
38
39 """
40
41 implementation = "tablegen"
42 implementation_version = __version__
43
44 language_version = __version__
45 language = "tablegen"
46 language_info = {
47 "name": "tablegen",
48 "mimetype": "text/x-tablegen",
49 "file_extension": ".td",
50 "pygments_lexer": "text",
51 }
52
53 def __init__(self, **kwargs):
54 Kernel.__init__(self, **kwargs)
55 self._executable = None
56 # A list of (code, magic) tuples.
57 # All the previous cell's code we have run since the last reset.
58 # This emulates a persistent state like a Python interpreter would have.
59 self._previous_code = ""
60 # The most recent set of magic since the last reset.
61 self._previous_magic = {}
62
63 @property
64 def banner(self):
65 return "llvm-tblgen kernel %s" % __version__
66
67 @property
68 def executable(self):
69 """If this is the first run, search for llvm-tblgen.
70 Otherwise return the cached path to it."""
71 if self._executable is None:
72 path = os.environ.get("LLVM_TBLGEN_EXECUTABLE")
73 if path is not None and os.path.isfile(path) and os.access(path, os.X_OK):
74 self._executable = path
75 else:
76 path = shutil.which("llvm-tblgen")
77 if path is None:
78 raise OSError("llvm-tblgen not found, please see README")
79 self._executable = path
80
81 return self._executable
82
83 def get_magic(self, code):
84 """Given a block of code remove the magic lines from it.
85 Returns a tuple of newline joined code lines and a dictionary of magic.
86 Where the key is the magic name (minus the %) and the values are lists
87 of the arguments to the magic.
88
89 Currently we only look for "cell magic" which must be at the start of
90 the cell. Meaning the first line, or a set of lines beginning with %
91 that come before the first non-magic line.
92
93 >>> k.get_magic("")
94 ('', {})
95 >>> k.get_magic("Hello World.\\nHello again.")
96 ('Hello World.\\nHello again.', {})
97 >>> k.get_magic(" %foo a b c")
98 ('', {'foo': ['a', 'b', 'c']})
99 >>> k.get_magic("%foo\\n %foo a b c\\nFoo")
100 ('Foo', {'foo': ['a', 'b', 'c']})
101 >>> k.get_magic("%foo\\n%bar\\nFoo")
102 ('Foo', {'foo': [], 'bar': []})
103 >>> k.get_magic("Foo\\n%foo\\nFoo")
104 ('Foo\\n%foo\\nFoo', {})
105 >>> k.get_magic("%bar\\n\\n%foo")
106 ('\\n%foo', {'bar': []})
107 >>> k.get_magic("%foo a b\\n Foo\\n%foo c d")
108 (' Foo\\n%foo c d', {'foo': ['a', 'b']})
109 >>> k.get_magic("%foo a b\\n \\n%foo c d")
110 (' \\n%foo c d', {'foo': ['a', 'b']})
111 """
112 magic = {}
113 code_lines = []
114
115 lines = code.splitlines()
116 while lines:
117 line = lines.pop(0)
118 possible_magic = line.lstrip()
119 if possible_magic.startswith("%"):
120 magic_parts = possible_magic.split()
121 # Key has the % removed
122 magic[magic_parts[0][1:]] = magic_parts[1:]
123 else:
124 code_lines = [line, *lines]
125 break
126
127 return "\n".join(code_lines), magic
128
129 def get_code_and_args(self, new_code):
130 """Get the code that do_execute should use, taking into account
131 the code from any cached cells.
132
133 Returns the code to compile and the arguments to use to do so.
134
135 >>> k._previous_code = ""
136 >>> k._previous_magic = {}
137 >>> k.get_code_and_args("")
138 ('', [])
139 >>> k.get_code_and_args("%args 1\\nSome code")
140 ('Some code', ['1'])
141 >>> k.get_code_and_args("%args 2\\nSome more code")
142 ('Some code\\nSome more code', ['2'])
143 >>> k.get_code_and_args("%reset\\n%args 3 4\\nSome new code")
144 ('Some new code', ['3', '4'])
145 >>> k.get_code_and_args("%reset\\nSome new code")
146 ('Some new code', [])
147 """
148 new_code, new_magic = self.get_magic(new_code)
149
150 # Only a reset in the newest cell actually does a reset.
151 if new_magic.get("reset") is not None:
152 self._previous_code = new_code
153 self._previous_magic = new_magic
154 else:
155 self._previous_code += ("\n" if self._previous_code else "") + new_code
156 self._previous_magic.update(new_magic)
157
158 return self._previous_code, self._previous_magic.get("args", [])
159
160 def do_execute(
161 self, code, silent, store_history=True, user_expressions=None, allow_stdin=False
162 ):
163 """Execute user code using llvm-tblgen binary."""
164 all_code, args = self.get_code_and_args(code)
165
166 with tempfile.TemporaryFile("w+") as f:
167 f.write(all_code)
168 f.seek(0)
169 got = subprocess.run(
170 [self.executable, *args],
171 stdin=f,
172 stderr=subprocess.PIPE,
173 stdout=subprocess.PIPE,
174 universal_newlines=True,
175 )
176
177 if not silent:
178 if got.stderr:
179 self.send_response(
180 self.iopub_socket, "stream", {"name": "stderr", "text": got.stderr}
181 )
182 else:
183 self.send_response(
184 self.iopub_socket, "stream", {"name": "stdout", "text": got.stdout}
185 )
186
187 return {
188 "status": "ok",
189 "execution_count": self.execution_count,
190 "payload": [],
191 "user_expressions": {},
192 }
193
194
195 if __name__ == "__main__":
196 import doctest
197
198 doctest.testmod(extraglobs={"k": TableGenKernel()})