diff --git a/llvm/utils/TableGen/jupyter/LLVM_TableGen.ipynb b/llvm/utils/TableGen/jupyter/LLVM_TableGen.ipynb --- a/llvm/utils/TableGen/jupyter/LLVM_TableGen.ipynb +++ b/llvm/utils/TableGen/jupyter/LLVM_TableGen.ipynb @@ -316,6 +316,50 @@ "%args\n", "class Thing {}" ] + }, + { + "cell_type": "markdown", + "id": "9e04969a", + "metadata": {}, + "source": [ + "The `%json` magic can be used to show the records as JSON. This is equivalent to `%args --dump-json` with the added benefit that we format the output for you." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "21d3283b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"!instanceof\": {\n", + " \"Root\": [\n", + " \"thing\"\n", + " ]\n", + " },\n", + " \"!tablegen_json_version\": 1,\n", + " \"thing\": {\n", + " \"!anonymous\": false,\n", + " \"!fields\": [],\n", + " \"!name\": \"thing\",\n", + " \"!superclasses\": [\n", + " \"Root\"\n", + " ]\n", + " }\n", + "}" + ] + } + ], + "source": [ + "%reset\n", + "%json\n", + "class Root {}\n", + "def thing : Root {}" + ] } ], "metadata": { diff --git a/llvm/utils/TableGen/jupyter/LLVM_TableGen.md b/llvm/utils/TableGen/jupyter/LLVM_TableGen.md --- a/llvm/utils/TableGen/jupyter/LLVM_TableGen.md +++ b/llvm/utils/TableGen/jupyter/LLVM_TableGen.md @@ -150,3 +150,30 @@ class Thing {} ^ + +The `%json` magic can be used to show the records as JSON. This is equivalent to `%args --dump-json` with the added benefit that we format the output for you. + + +```tablegen +%reset +%json +class Root {} +def thing : Root {} +``` + + { + "!instanceof": { + "Root": [ + "thing" + ] + }, + "!tablegen_json_version": 1, + "thing": { + "!anonymous": false, + "!fields": [], + "!name": "thing", + "!superclasses": [ + "Root" + ] + } + } diff --git a/llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py b/llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py --- a/llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py +++ b/llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py @@ -2,6 +2,7 @@ # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +import json import os import shutil import subprocess @@ -20,6 +21,7 @@ The supported magic is: * %args - to set the arguments passed to llvm-tblgen. * %reset - to reset the cached code and magic state. + * %json - to emit records as JSON. These are "cell magic" meaning it applies to the whole cell. Therefore it must be the first line, or part of a run of magic lines starting @@ -126,24 +128,24 @@ return "\n".join(code_lines), magic - def get_code_and_args(self, new_code): - """Get the code that do_execute should use, taking into account - the code from any cached cells. - - Returns the code to compile and the arguments to use to do so. + def get_code_and_magic(self, new_code): + """Get the code that do_execute should use, and the magic + to run it with. >>> k._previous_code = "" >>> k._previous_magic = {} - >>> k.get_code_and_args("") - ('', []) - >>> k.get_code_and_args("%args 1\\nSome code") - ('Some code', ['1']) - >>> k.get_code_and_args("%args 2\\nSome more code") - ('Some code\\nSome more code', ['2']) - >>> k.get_code_and_args("%reset\\n%args 3 4\\nSome new code") - ('Some new code', ['3', '4']) - >>> k.get_code_and_args("%reset\\nSome new code") - ('Some new code', []) + >>> k.get_code_and_magic("") + ('', {}) + >>> k.get_code_and_magic("%args 1\\nSome code") + ('Some code', {'args': ['1']}) + >>> k.get_code_and_magic("%args 2\\nSome more code") + ('Some code\\nSome more code', {'args': ['2']}) + >>> k.get_code_and_magic("") + ('Some code\\nSome more code\\n', {'args': ['2']}) + >>> k.get_code_and_magic("%reset\\n%args 3 4\\nSome new code") + ('Some new code', {'args': ['3', '4']}) + >>> k.get_code_and_magic("%reset\\nSome new code") + ('Some new code', {}) """ new_code, new_magic = self.get_magic(new_code) @@ -155,13 +157,17 @@ self._previous_code += ("\n" if self._previous_code else "") + new_code self._previous_magic.update(new_magic) - return self._previous_code, self._previous_magic.get("args", []) + return self._previous_code, self._previous_magic def do_execute( self, code, silent, store_history=True, user_expressions=None, allow_stdin=False ): """Execute user code using llvm-tblgen binary.""" - all_code, args = self.get_code_and_args(code) + all_code, magic = self.get_code_and_magic(code) + + args = magic.get("args", []) + if magic.get("json") is not None: + args.append("--dump-json") with tempfile.TemporaryFile("w+") as f: f.write(all_code) @@ -180,8 +186,15 @@ self.iopub_socket, "stream", {"name": "stderr", "text": got.stderr} ) else: + out = got.stdout + if magic.get("json") is not None: + j = json.loads(out) + out = json.dumps(j, indent=4) + self.send_response( - self.iopub_socket, "stream", {"name": "stdout", "text": got.stdout} + self.iopub_socket, + "stream", + {"name": "stdout", "text": out}, ) return {