This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

[Patterns] Parsing This String into a table

Started by KingofGamesYami, 29 April 2015 - 03:46 PM
KingofGamesYami #1
Posted 29 April 2015 - 05:46 PM
Input

local str = [==[ [
  {
    "name": "Assets",
    "path": "src/Assets",
    "sha": "7bd258067a344963aac739e6c22314ead2fbe107",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Assets?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Assets",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/7bd258067a344963aac739e6c22314ead2fbe107",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Assets?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/7bd258067a344963aac739e6c22314ead2fbe107",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Assets"
    }
  },
  {
    "name": "Client",
    "path": "src/Client",
    "sha": "229ca28c46aa554954792433e09c44ee63b7f38b",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Client?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Client",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/229ca28c46aa554954792433e09c44ee63b7f38b",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Client?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/229ca28c46aa554954792433e09c44ee63b7f38b",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Client"
    }
  },
  {
    "name": "Logs",
    "path": "src/Logs",
    "sha": "b94794cbbc8d99443d87ade5d9e67d8b99e15211",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Logs?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Logs",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/b94794cbbc8d99443d87ade5d9e67d8b99e15211",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Logs?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/b94794cbbc8d99443d87ade5d9e67d8b99e15211",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Logs"
    }
  },
  {
    "name": "Pocket",
    "path": "src/Pocket",
    "sha": "d8765e5aeba1ff1cb024e7fdad4cd70506a8786d",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Pocket?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Pocket",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/d8765e5aeba1ff1cb024e7fdad4cd70506a8786d",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Pocket?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/d8765e5aeba1ff1cb024e7fdad4cd70506a8786d",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Pocket"
    }
  },
  {
    "name": "Server",
    "path": "src/Server",
    "sha": "7ed8803b6fc7bc8b24c0b354e8184192cb495c2d",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Server?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Server",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/7ed8803b6fc7bc8b24c0b354e8184192cb495c2d",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Server?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/7ed8803b6fc7bc8b24c0b354e8184192cb495c2d",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Server"
    }
  },
  {
    "name": "Shared",
    "path": "src/Shared",
    "sha": "16cfc7b0230cfb8bc69fb146e234095ff9a43fef",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Shared?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Shared",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/16cfc7b0230cfb8bc69fb146e234095ff9a43fef",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Shared?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/16cfc7b0230cfb8bc69fb146e234095ff9a43fef",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Shared"
    }
  },
  {
    "name": "TODO.md",
    "path": "src/TODO.md",
    "sha": "a72bf605ea4e7847d5b09998a97dd6a281a6c577",
    "size": 1832,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/TODO.md?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/blob/master/src/TODO.md",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/blobs/a72bf605ea4e7847d5b09998a97dd6a281a6c577",
    "download_url": "https://raw.githubusercontent.com/lupus590/CC-Hive/master/src/TODO.md",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/TODO.md?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/blobs/a72bf605ea4e7847d5b09998a97dd6a281a6c577",
      "html": "https://github.com/lupus590/CC-Hive/blob/master/src/TODO.md"
    }
  },
  {
    "name": "Turtle",
    "path": "src/Turtle",
    "sha": "7d91f62c57196b6608867f85a0597932cf361cdd",
    "size": 0,
    "url": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Turtle?ref=master",
    "html_url": "https://github.com/lupus590/CC-Hive/tree/master/src/Turtle",
    "git_url": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/7d91f62c57196b6608867f85a0597932cf361cdd",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/lupus590/CC-Hive/contents/src/Turtle?ref=master",
      "git": "https://api.github.com/repos/lupus590/CC-Hive/git/trees/7d91f62c57196b6608867f85a0597932cf361cdd",
      "html": "https://github.com/lupus590/CC-Hive/tree/master/src/Turtle"
    }
  }
]
]==]

Parser

local r

function r( str )
	if not str then
		error( "NO STRING", 2 )
	end
	local tAll = {}
	for tbl in str:gmatch( "%b{}" ) do
		local t = {}
		--gotta get rid of the _links
		local s, e = string.find( tbl, "\"_links\": %b{}" )
		if s then
			t[ "_links" ] = r( str:match( "\"_links\": (%b{})" ) )
			tbl = str:gsub( "_links: (%b{})", "" )
		end
		for k, v in tbl:gmatch( "(%^\"+): \"(%^\"+)" ) do
			t[ k ] = v
		end
		tAll[ #tAll + 1 ] = t
	end
	return tAll
end

local files = r( str )

Output

{
  {
    _links = {
       {},
    }
   },
   ...
}

I think my intent here is clear enough, I want to take the input and turn it into a table, (but not a blank table like the output currently).
Lignum #2
Posted 29 April 2015 - 06:15 PM
Take a look at this.
Creator #3
Posted 29 April 2015 - 08:37 PM
You releasing an OS?

That would be mindblowing
KingofGamesYami #4
Posted 29 April 2015 - 08:48 PM
You releasing an OS?

That would be mindblowing

What? I don't intend to make an os, not for a while anyway… I am helping with Hive, but Hive isn't really an OS..
Creator #5
Posted 29 April 2015 - 09:08 PM
Oh, *disapointement*