luasocket - Read lua interface -


in lua, there way read interface file extract name/methods/args?

i have .idl file this:

interface {     name = myinterface,     methods = {         testing = {             resulttype = "double",             args = {{direction = "in",             type = "double"},         }     } } 

this equal code bellow (easier read):

interface myinterface {   double testing (in double a); }; 

i can read file, load string , parse gmatch example extract information, there easy mode parse info?

at end want (a table example) interface name, methods, result types , args. know interface i`m working.

lua has several facilities interpret chunks of code. namely, dofile, loadfile , loadstring. luckily, input file almost valid lua code (assuming braces matched). thing problematic interface {.

all of above functions create function object file's or string's contents code. dofile executes function, while others return function, can invoke whenever like. therefore, if you're free change files, replace interface in first line return. can do:

local interface = dofile("input.idl") 

and interface nice table, have specified in file. if cannot change files liking, have load file string, perform string manipulation (specifically, replace first interface return) , use loadstring instead:

io.input("input.idl") local input = io.read("*all") input = string.gsub(input, "^interface", "return") -- ^ marks beginning of string local f = loadstring(input) local interface = f() 

in both cases get:

> require"pl.pretty".dump(interface) {   name = "myinterface",   methods = {     testing = {       args = {         {           type = "double",           direction = "in"         }       },       resulttype = "double"     }   } }      > print(interface.methods.testing.args[1].type) double 

edit:

i realised, in example input myinterface not enclosed in " , therefore not proper string. mistake in input file or files like? in latter case, need change well. lua not going complain if it's name doesn't know, won't field in case.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -