diff --git a/python/NeutrinoParser.ipynb b/python/NeutrinoParser.ipynb index 6cd2f6e..10b5744 100644 --- a/python/NeutrinoParser.ipynb +++ b/python/NeutrinoParser.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 109, + "execution_count": 380, "source": [ "# load up the test file\r\n", "data = open(\"Test.neu\").read()\r\n", @@ -11,27 +11,14 @@ "data = data.split(\"\\n\")\r\n", "for i, line in enumerate(data):\r\n", " data[i] = line.strip()\r\n", - "data = \"\".join(data)\r\n", - "\r\n", - "data" - ], - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "'@{string,float,int}#{2:\"100\"}*{&0,0:\"Hello, world! :@#*\",1:\"512.0\",&0}'" - ] - }, - "metadata": {}, - "execution_count": 109 - } + "data = \"\".join(data)" ], + "outputs": [], "metadata": {} }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 381, "source": [ "cache = {\r\n", " \"names\": [],\r\n", @@ -40,22 +27,28 @@ "}\r\n", "\r\n", "# parse a string entry according to its type and return a dict\r\n", - "def parse_entry(entry_type, entry_value):\r\n", - " # convert values to proper types as needed\r\n", - " if entry_type == \"float\":\r\n", - " converted_value = float(entry_value)\r\n", - " elif entry_type == \"int\":\r\n", - " converted_value = int(entry_value)\r\n", - " else:\r\n", - " converted_value = entry_value\r\n", + "def parse_entry(entry):\r\n", + " if type(entry[\"value\"]) == str:\r\n", + " # convert values to proper types as needed\r\n", + " if entry[\"type\"][\"name\"] == \"float\":\r\n", + " converted_value = float(entry[\"value\"])\r\n", + " elif entry[\"type\"][\"name\"] == \"int\":\r\n", + " converted_value = int(entry[\"value\"])\r\n", + " elif entry[\"type\"][\"name\"] == \"vector\":\r\n", + " converted_value = entry[\"value\"].split(\",\")\r\n", + " for i, value in enumerate(converted_value):\r\n", + " converted_value[i] = float(value)\r\n", + " else:\r\n", + " converted_value = entry[\"value\"]\r\n", "\r\n", - " # return a dictionary of the parsed entry\r\n", - " return {\"type\": entry_type, \"value\": converted_value}\r\n", + " # return a dictionary of the parsed entry\r\n", + " return {\"type\": entry[\"type\"], \"value\": converted_value}\r\n", + " else:\r\n", + " return entry\r\n", "\r\n", "# iterate through each character in the raw data and create a cache of parsed strings\r\n", "stage = \"\"\r\n", - "entry_type = \"\"\r\n", - "entry_value = \"\"\r\n", + "entry = {\"type\": \"\", \"value\": \"\"}\r\n", "consume = False\r\n", "is_literal = False\r\n", "is_reference = False\r\n", @@ -82,10 +75,13 @@ " elif char == \"}\":\r\n", " # commit entry\r\n", " if stage != \"names\":\r\n", - " cache[stage].append(parse_entry(entry_type, entry_value))\r\n", + " # parse into proper values\r\n", + " entry = parse_entry(entry)\r\n", + " cache[stage].append(entry)\r\n", + " entry = {\"type\": \"\", \"value\": \"\"}\r\n", " else:\r\n", - " cache[stage].append(entry_value)\r\n", - " entry_value = \"\"\r\n", + " cache[stage].append(entry[\"value\"])\r\n", + " entry[\"value\"] = \"\"\r\n", "\r\n", " is_reference = False\r\n", "\r\n", @@ -94,36 +90,69 @@ " elif char == \",\":\r\n", " # commit entry\r\n", " if stage != \"names\":\r\n", - " cache[stage].append(parse_entry(entry_type, entry_value))\r\n", + " # parse into proper values\r\n", + " entry = parse_entry(entry)\r\n", + " cache[stage].append(entry)\r\n", + " entry = {\"type\": \"\", \"value\": \"\"}\r\n", " else:\r\n", - " cache[stage].append(entry_value)\r\n", - " entry_value = \"\"\r\n", + " cache[stage].append(entry[\"value\"])\r\n", + " entry[\"value\"] = \"\"\r\n", " \r\n", " is_reference = False\r\n", "\r\n", " continue\r\n", " elif char == \":\":\r\n", " # handle names/types\r\n", - " entry_type = cache[\"names\"][int(entry_value, base = 16)]\r\n", - " entry_value = \"\"\r\n", + " # handle nested types\r\n", + " if \"=\" in entry[\"value\"]:\r\n", + " type_parent = cache[\"names\"][int(entry[\"value\"][0])]\r\n", + " type_children = entry[\"value\"][2:].split(\"/\")\r\n", + " for i, child in enumerate(type_children):\r\n", + " child_name = cache[\"names\"][int(child.split(\".\")[0])]\r\n", + " child_type = cache[\"names\"][int(child.split(\".\")[1])]\r\n", + " type_children[i] = {\"name\": child_name, \"type\": child_type}\r\n", + " this_type = {\r\n", + " \"name\": type_parent,\r\n", + " \"parameters\": type_children\r\n", + " }\r\n", + " entry[\"type\"] = this_type\r\n", + " else:\r\n", + " this_type = entry[\"value\"]\r\n", + " entry[\"type\"] = {\"name\": cache[\"names\"][int(this_type, base = 16)], \"parameters\": []}\r\n", + " entry[\"value\"] = \"\"\r\n", + " continue\r\n", + " elif char == \"&\":\r\n", + " is_reference = True\r\n", " continue\r\n", "\r\n", " if consume:\r\n", " # handle cache references\r\n", - " if entry_value == \"&\":\r\n", + " if is_reference:\r\n", " cached_object = cache[\"cache\"][int(char, base = 16)]\r\n", - " entry_type = cached_object[\"type\"]\r\n", - " entry_value = cached_object[\"value\"]\r\n", + " entry = cached_object\r\n", " continue\r\n", " else:\r\n", - " entry_value += char" + " entry[\"value\"] += char" + ], + "outputs": [ + { + "output_type": "error", + "ename": "AttributeError", + "evalue": "'dict' object has no attribute 'split'", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_19468/2470265224.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 57\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mstage\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;34m\"names\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 58\u001b[0m \u001b[1;31m# parse into proper values\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 59\u001b[1;33m \u001b[0mentry\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparse_entry\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mentry\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 60\u001b[0m \u001b[0mcache\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mstage\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mentry\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 61\u001b[0m \u001b[0mentry\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m\"type\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m\"\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"value\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m\"\"\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_19468/2470265224.py\u001b[0m in \u001b[0;36mparse_entry\u001b[1;34m(entry)\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[0mconverted_value\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mentry\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"type\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"name\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"object\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0mconverted_value\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mentry\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"/\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 21\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[0mconverted_value\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mentry\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"value\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAttributeError\u001b[0m: 'dict' object has no attribute 'split'" + ] + } ], - "outputs": [], "metadata": {} }, { "cell_type": "code", - "execution_count": 111, + "execution_count": null, "source": [ "graph = cache[\"objects\"]" ], @@ -132,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": null, "source": [ "# DEBUG\r\n", "import json\r\n", diff --git a/python/NeutrinoTest_Cache.json b/python/NeutrinoTest_Cache.json index 6656408..37b6b71 100644 --- a/python/NeutrinoTest_Cache.json +++ b/python/NeutrinoTest_Cache.json @@ -1,18 +1,54 @@ [ { - "type": "int", - "value": 100 + "type": { + "name": "string", + "parameters": [] + }, + "value": "Hello, world! Here's a bunch of special characters: &@#*{},,/|,{{}#@*" }, { - "type": "string", - "value": "Hello, world! Here's a bunch of special characters: @#*{},,/|,{{}#@*" - }, - { - "type": "float", + "type": { + "name": "float", + "parameters": [] + }, "value": 512.0 }, { - "type": "int", + "type": { + "name": "int", + "parameters": [] + }, "value": 100 + }, + { + "type": { + "name": "vector", + "parameters": [] + }, + "value": [ + 0.0, + 0.0, + 0.0 + ] + }, + { + "type": { + "name": "object", + "parameters": [ + { + "name": "position", + "type": "vector" + }, + { + "name": "rotation", + "type": "vector" + }, + { + "name": "scale", + "type": "vector" + } + ] + }, + "value": "0.0,0.0,0.0/0.0,0.0,0.0/0.0,0.0,0.0" } ] \ No newline at end of file diff --git a/python/Test.neu b/python/Test.neu index 688260a..cec5eb2 100644 --- a/python/Test.neu +++ b/python/Test.neu @@ -1,14 +1,21 @@ @{ string, float, - int + int, + vector, + position, + rotation, + scale, + object } #{ - 2:"100" + 2:"100", + 3:"0.0,0.0,0.0" } *{ - &0, - 0:"Hello, world! Here's a bunch of special characters: @#*{},,/|,{{}#@*", + 0:"Hello, world! Here's a bunch of special characters: &@#*{},,/|,{{}#@*", 1:"512.0", - &0 + &0, + &1, + 7=4.3/5.3/6.3:"0.0,0.0,0.0/0.0,0.0,0.0/0.0,0.0,0.0" } \ No newline at end of file