Mercurial Hosting > luan
comparison src/luan/modules/parsers/LuanToString.java @ 1791:f8f5c51f5b36
xml work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 26 Dec 2023 19:16:18 -0700 |
parents | 582384548a69 |
children |
comparison
equal
deleted
inserted
replaced
1790:a8c685a894b4 | 1791:f8f5c51f5b36 |
---|---|
77 | 77 |
78 public final Settings settingsInit = new Settings(); | 78 public final Settings settingsInit = new Settings(); |
79 private final Luan luan; | 79 private final Luan luan; |
80 private final LuanFunction fnOptions; | 80 private final LuanFunction fnOptions; |
81 private final LuanTable stack = new LuanTable(); | 81 private final LuanTable stack = new LuanTable(); |
82 private final Set<LuanTable> seen = new HashSet<LuanTable>(); | |
82 | 83 |
83 public LuanToString() { | 84 public LuanToString() { |
84 this.luan = null; | 85 this.luan = null; |
85 this.fnOptions = null; | 86 this.fnOptions = null; |
86 } | 87 } |
113 return (LuanTable)rtn; | 114 return (LuanTable)rtn; |
114 } | 115 } |
115 | 116 |
116 public String toString(Object obj) throws LuanException { | 117 public String toString(Object obj) throws LuanException { |
117 StringBuilder sb = new StringBuilder(); | 118 StringBuilder sb = new StringBuilder(); |
118 toString(obj,sb,0,settingsInit); | 119 objectToString(obj,sb,0,settingsInit); |
119 return sb.toString(); | 120 return sb.toString(); |
120 } | 121 } |
121 | 122 |
122 private void toString(Object obj,StringBuilder sb,int indented,Settings settings) throws LuanException { | 123 private void objectToString(Object obj,StringBuilder sb,int indented,Settings settings) throws LuanException { |
123 if( obj == null ) { | 124 if( obj == null ) { |
124 sb.append( "nil" ); | 125 sb.append( "nil" ); |
125 return; | 126 return; |
126 } | 127 } |
127 if( obj instanceof Boolean ) { | 128 if( obj instanceof Boolean ) { |
146 sb.append( obj ); | 147 sb.append( obj ); |
147 sb.append( '>' ); | 148 sb.append( '>' ); |
148 } | 149 } |
149 | 150 |
150 private void toString(LuanTable tbl,StringBuilder sb,int indented,Settings settings) throws LuanException { | 151 private void toString(LuanTable tbl,StringBuilder sb,int indented,Settings settings) throws LuanException { |
152 if( !seen.add(tbl) ) { | |
153 if( settings.strict ) | |
154 throw new LuanException("loop in "+tbl.rawToString()); | |
155 sb.append("<loop>"); | |
156 return; | |
157 } | |
151 if( tbl.getMetatable()!=null ) { | 158 if( tbl.getMetatable()!=null ) { |
152 if( settings.strict ) | 159 if( settings.strict ) |
153 throw new LuanException("can't handle metatables when strict"); | 160 throw new LuanException("can't handle metatables when strict"); |
154 if( luan==null ) | 161 if( luan==null ) |
155 throw new LuanException("can't handle metatables when luan isn't set"); | 162 throw new LuanException("can't handle metatables when luan isn't set"); |
205 } else | 212 } else |
206 sb.append( ", " ); | 213 sb.append( ", " ); |
207 } else { | 214 } else { |
208 indent(sb,indented+1); | 215 indent(sb,indented+1); |
209 } | 216 } |
210 toString(obj,sb,indented+1,settings); | 217 objectToString(obj,sb,indented+1,settings); |
211 } | 218 } |
212 for( Object obj : map.entrySet() ) { | 219 for( Object obj : map.entrySet() ) { |
213 Map.Entry entry = (Map.Entry)obj; | 220 Map.Entry entry = (Map.Entry)obj; |
214 if( settings.compressed ) { | 221 if( settings.compressed ) { |
215 if( first ) | 222 if( first ) |
245 Object key = entry.getKey(); | 252 Object key = entry.getKey(); |
246 if( key instanceof String && !settings.noNameKeys && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) { | 253 if( key instanceof String && !settings.noNameKeys && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) { |
247 sb.append( (String)key ); | 254 sb.append( (String)key ); |
248 } else { | 255 } else { |
249 sb.append( '[' ); | 256 sb.append( '[' ); |
250 toString( key, sb, indented, keySettings ); | 257 objectToString( key, sb, indented, keySettings ); |
251 sb.append( ']' ); | 258 sb.append( ']' ); |
252 } | 259 } |
253 sb.append( settings.compressed ? "=" : " = " ); | 260 sb.append( settings.compressed ? "=" : " = " ); |
254 stack.rawAdd(key); // push | 261 stack.rawAdd(key); // push |
255 LuanTable options = getOptions(); | 262 LuanTable options = getOptions(); |
256 if( options != null ) { | 263 if( options != null ) { |
257 settings = settings.cloneSettings(); | 264 settings = settings.cloneSettings(); |
258 settings.applyOptions(options); | 265 settings.applyOptions(options); |
259 } | 266 } |
260 toString( entry.getValue(), sb, indented, settings ); | 267 objectToString( entry.getValue(), sb, indented, settings ); |
261 stack.removeFromList(stack.rawLength()); // pop | 268 stack.removeFromList(stack.rawLength()); // pop |
262 } | 269 } |
263 | 270 |
264 private void indent(StringBuilder sb,int indented) { | 271 private void indent(StringBuilder sb,int indented) { |
265 sb.append( '\n' ); | 272 sb.append( '\n' ); |