Mercurial Hosting > luan
comparison core/src/luan/modules/Io.luan @ 591:790d5de23042
add "strict" param to Io.repr();
add Lucene.index.ensure_open();
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 30 Aug 2015 11:37:39 -0600 |
parents | 97c8ae330efe |
children | 35dde32c02ab |
comparison
equal
deleted
inserted
replaced
590:f050c30952c0 | 591:790d5de23042 |
---|---|
70 end | 70 end |
71 | 71 |
72 | 72 |
73 -- repr | 73 -- repr |
74 | 74 |
75 local function do_repr(obj,done) | 75 local function do_repr(obj,strict,done) |
76 local tp = type(obj) | 76 local tp = type(obj) |
77 if tp == "table" then | 77 if tp == "table" then |
78 if done[obj] == true then | 78 if done[obj] == true then |
79 strict and error "circular reference" | |
79 %><circular reference><% | 80 %><circular reference><% |
80 return | 81 return |
81 end | 82 end |
82 done[obj] = true | 83 done[obj] = true |
83 %>{<% | 84 %>{<% |
84 local is_first = true | 85 local is_first = true |
85 local in_list = {} | 86 local in_list = {} |
86 for key, value in ipairs(obj) do | 87 for key, value in ipairs(obj) do |
87 if is_first then is_first = false else %>, <% end | 88 if is_first then is_first = false else %>, <% end |
88 do_repr(value,done) | 89 do_repr(value,strict,done) |
89 in_list[key] = true | 90 in_list[key] = true |
90 end | 91 end |
91 for key, value in pairs(obj) do | 92 for key, value in pairs(obj) do |
92 if in_list[key] ~= true then | 93 if in_list[key] ~= true then |
93 if is_first then is_first = false else %>, <% end | 94 if is_first then is_first = false else %>, <% end |
94 if type(key) == "string" and matches(key,"[a-zA-Z_][a-zA-Z_0-9]*") ~= nil then | 95 if type(key) == "string" and matches(key,"[a-zA-Z_][a-zA-Z_0-9]*") ~= nil then |
95 %><%=key%><% | 96 %><%=key%><% |
96 elseif type(key) == "table" then | 97 elseif type(key) == "table" then |
97 %>[<<%=key%>>]<% | 98 %>[<<%=key%>>]<% |
98 else | 99 else |
99 %>[<%do_repr(key,done)%>]<% | 100 %>[<%do_repr(key,strict,done)%>]<% |
100 end | 101 end |
101 %>=<% do_repr(value,done) | 102 %>=<% do_repr(value,strict,done) |
102 end | 103 end |
103 end | 104 end |
104 %>}<% | 105 %>}<% |
105 elseif tp == "string" then | 106 elseif tp == "string" then |
106 %>"<%=encode(obj)%>"<% | 107 %>"<%=encode(obj)%>"<% |
107 elseif tp == "nil" or tp == "boolean" or tp == "number" then | 108 elseif tp == "nil" or tp == "boolean" or tp == "number" then |
108 %><%=obj%><% | 109 %><%=obj%><% |
109 else | 110 else |
111 strict and error("can't repr type '"..tp.."' of "..obj) | |
110 %><<%=obj%>><% | 112 %><<%=obj%>><% |
111 end | 113 end |
112 end | 114 end |
113 | 115 |
114 function M.repr(obj) | 116 function M.repr(obj,strict) |
115 return M.output_of(do_repr,obj,{}) | 117 return M.output_of(do_repr,obj,strict or false,{}) |
116 end | 118 end |
117 | 119 |
118 | 120 |
119 -- useful for SimplyHTML responsiveness | 121 -- useful for SimplyHTML responsiveness |
120 | 122 |