Mercurial Hosting > luan
annotate src/luan/modules/parsers/LuanToString.java @ 1562:b89212fd04b5
remove table.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 Nov 2020 16:50:59 -0700 |
parents | 83249ce59d13 |
children | 8fbcc4747091 |
rev | line source |
---|---|
1189 | 1 package luan.modules.parsers; |
720 | 2 |
3 import java.util.List; | |
4 import java.util.Map; | |
1552
46d4baaad64d
add long_string_keys
Franklin Schmidt <fschmidt@gmail.com>
parents:
1550
diff
changeset
|
5 import java.util.Set; |
1553 | 6 import java.util.HashSet; |
1552
46d4baaad64d
add long_string_keys
Franklin Schmidt <fschmidt@gmail.com>
parents:
1550
diff
changeset
|
7 import java.util.Collections; |
1189 | 8 import luan.Luan; |
9 import luan.LuanTable; | |
10 import luan.LuanException; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
11 import luan.LuanRuntimeException; |
720 | 12 |
13 | |
1189 | 14 public final class LuanToString { |
1553 | 15 public static class Settings implements Cloneable { |
16 public boolean strict = false; | |
17 public boolean numberTypes = false; | |
18 public boolean compressed = false; | |
19 public boolean longStrings = false; | |
20 public boolean inline = false; | |
21 | |
1562 | 22 void applyOptions(Luan luan,LuanTable options) throws LuanException { |
1553 | 23 Boolean b; |
1562 | 24 b = (Boolean)options.get(luan,"strict"); |
1553 | 25 if( b != null ) |
26 strict = b; | |
1562 | 27 b = (Boolean)options.get(luan,"number_types"); |
1553 | 28 if( b != null ) |
29 numberTypes = b; | |
1562 | 30 b = (Boolean)options.get(luan,"compressed"); |
1553 | 31 if( b != null ) |
32 compressed = b; | |
1562 | 33 b = (Boolean)options.get(luan,"long_strings"); |
1553 | 34 if( b != null ) |
35 longStrings = b; | |
1562 | 36 b = (Boolean)options.get(luan,"inline"); |
1553 | 37 if( b != null ) |
38 inline = b; | |
39 } | |
40 | |
41 public Settings cloneSettings() { | |
42 try { | |
43 return (Settings)clone(); | |
44 } catch(CloneNotSupportedException e) { | |
45 throw new RuntimeException(e); | |
46 } | |
47 } | |
48 } | |
49 private static final Settings keySettings = new Settings(); | |
50 private static final Set<String> settingsKeys = new HashSet<String>(); | |
51 static { | |
52 Collections.addAll(settingsKeys,"strict","number_types","compressed","long_strings","inline"); | |
53 } | |
54 | |
55 private static void checkOptions(LuanTable options) throws LuanException { | |
56 for( Map.Entry entry : options.iterable() ) { | |
57 if( !settingsKeys.contains(entry.getKey()) ) | |
58 throw new LuanException("invalid option: "+entry.getKey()); | |
59 if( !(entry.getValue() instanceof Boolean) ) | |
60 throw new LuanException("options values must be boolean"); | |
61 } | |
62 } | |
63 | |
64 public final Settings settingsInit = new Settings(); | |
1562 | 65 private final Luan luan; |
1553 | 66 private final LuanTable subOptions; |
67 | |
1562 | 68 public LuanToString(Luan luan,LuanTable options,LuanTable subOptions) throws LuanException { |
69 this.luan = luan; | |
1553 | 70 this.subOptions = subOptions; |
71 if( options != null ) { | |
72 checkOptions(options); | |
1562 | 73 settingsInit.applyOptions(luan,options); |
1553 | 74 } |
75 if( subOptions != null ) { | |
76 for( Map.Entry entry : subOptions.iterable() ) { | |
77 /* | |
78 if( !(entry.getKey() instanceof String) ) | |
79 throw new LuanException("sub_options keys must be strings"); | |
80 */ | |
81 if( !(entry.getValue() instanceof LuanTable) ) | |
82 throw new LuanException("sub_options keys must be tables"); | |
83 LuanTable tbl = (LuanTable)entry.getValue(); | |
84 checkOptions(tbl); | |
85 } | |
86 } | |
87 } | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1409
diff
changeset
|
88 |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
89 public String toString(Object obj) throws LuanException { |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
90 StringBuilder sb = new StringBuilder(); |
1553 | 91 toString(obj,sb,0,settingsInit); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
92 return sb.toString(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
93 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
94 |
1553 | 95 private void toString(Object obj,StringBuilder sb,int indented,Settings settings) throws LuanException { |
1189 | 96 if( obj == null ) { |
97 sb.append( "nil" ); | |
98 return; | |
99 } | |
100 if( obj instanceof Boolean ) { | |
101 sb.append( obj ); | |
102 return; | |
103 } | |
104 if( obj instanceof Number ) { | |
1553 | 105 toString((Number)obj,sb,settings); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
106 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
107 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
108 if( obj instanceof String ) { |
1553 | 109 toString((String)obj,sb,settings); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
110 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
111 } |
1189 | 112 if( obj instanceof LuanTable ) { |
1553 | 113 toString((LuanTable)obj,sb,indented,settings); |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
114 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
115 } |
1553 | 116 if( settings.strict ) |
1409 | 117 throw new LuanException("can't handle type "+Luan.type(obj)); |
1189 | 118 sb.append( '<' ); |
119 sb.append( obj ); | |
120 sb.append( '>' ); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
121 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
122 |
1553 | 123 private void toString(LuanTable tbl,StringBuilder sb,int indented,Settings settings) throws LuanException { |
1189 | 124 List list = tbl.asList(); |
125 Map map = tbl.rawMap(); | |
126 sb.append( '{' ); | |
1518
f989778ca9e1
stringify, json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1485
diff
changeset
|
127 boolean first = true; |
f989778ca9e1
stringify, json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1485
diff
changeset
|
128 for( Object obj : list ) { |
1553 | 129 if( settings.compressed ) { |
130 if( first ) | |
131 first = false; | |
132 else | |
133 sb.append( ',' ); | |
134 } else if( settings.inline ) { | |
135 if( first ) { | |
136 first = false; | |
137 sb.append( ' ' ); | |
138 } else | |
139 sb.append( ", " ); | |
140 } else { | |
1419
59fd2e8b1b9d
stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1409
diff
changeset
|
141 indent(sb,indented+1); |
1553 | 142 } |
143 toString(obj,sb,indented+1,settings); | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
144 } |
1189 | 145 for( Object obj : map.entrySet() ) { |
146 Map.Entry entry = (Map.Entry)obj; | |
1553 | 147 if( settings.compressed ) { |
148 if( first ) | |
149 first = false; | |
150 else | |
151 sb.append( ',' ); | |
152 } else if( settings.inline ) { | |
153 if( first ) { | |
154 first = false; | |
155 sb.append( ' ' ); | |
156 } else | |
157 sb.append( ", " ); | |
158 } else { | |
1518
f989778ca9e1
stringify, json_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1485
diff
changeset
|
159 indent(sb,indented+1); |
1553 | 160 } |
161 toString(entry,sb,indented+1,settings); | |
1189 | 162 } |
1553 | 163 if( !list.isEmpty() || !map.isEmpty() ) { |
164 if( settings.compressed ) { | |
165 } else if( settings.inline ) { | |
166 sb.append( ' ' ); | |
167 } else { | |
168 indent(sb,indented); | |
169 } | |
170 } | |
1189 | 171 sb.append( '}' ); |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
172 return; |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
173 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
174 |
1553 | 175 private void toString(Map.Entry entry,StringBuilder sb,int indented,Settings settings) throws LuanException { |
1189 | 176 Object key = entry.getKey(); |
177 if( key instanceof String && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) { | |
178 sb.append( (String)key ); | |
179 } else { | |
180 sb.append( '[' ); | |
1553 | 181 toString( key, sb, indented, keySettings ); |
1189 | 182 sb.append( ']' ); |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
183 } |
1553 | 184 sb.append( settings.compressed ? "=" : " = " ); |
1554 | 185 if( subOptions != null ) { |
1562 | 186 LuanTable options = (LuanTable)subOptions.get(luan,key); |
1554 | 187 if( options != null ) { |
188 settings = settings.cloneSettings(); | |
1562 | 189 settings.applyOptions(luan,options); |
1554 | 190 } |
1553 | 191 } |
192 toString( entry.getValue(), sb, indented, settings ); | |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
193 } |
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
194 |
1189 | 195 private void indent(StringBuilder sb,int indented) { |
196 sb.append( '\n' ); | |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
197 for( int i=0; i<indented; i++ ) { |
1189 | 198 sb.append( '\t' ); |
1117
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
199 } |
9a1aa6fc0b4e
remove json_pretty_string and make json_string somewhat pretty
Franklin Schmidt <fschmidt@gmail.com>
parents:
1116
diff
changeset
|
200 } |
1115
809d74db1415
add json_pretty_string
Franklin Schmidt <fschmidt@gmail.com>
parents:
1113
diff
changeset
|
201 |
1553 | 202 private void toString(Number n,StringBuilder sb,Settings settings) throws LuanException { |
203 if( settings.numberTypes ) { | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
204 sb.append( n.getClass().getSimpleName().toLowerCase() ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
205 sb.append( '(' ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
206 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
207 sb.append( Luan.toString(n) ); |
1553 | 208 if( settings.numberTypes ) |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
209 sb.append( ')' ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
210 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
211 |
1562 | 212 public static void addNumberTypes(Luan luan,LuanTable env) { |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
213 try { |
1562 | 214 LuanTable module = (LuanTable)luan.require("luan:Number.luan"); |
215 env.put( luan, "double", module.fn(luan,"double") ); | |
216 env.put( luan, "float", module.fn(luan,"float") ); | |
217 env.put( luan, "integer", module.fn(luan,"integer") ); | |
218 env.put( luan, "long", module.fn(luan,"long") ); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
219 } catch(LuanException e) { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
220 throw new LuanRuntimeException(e); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
221 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1190
diff
changeset
|
222 } |
1550 | 223 |
1553 | 224 private void toString(String s,StringBuilder sb,Settings settings) { |
225 if( settings.longStrings ) { | |
1552
46d4baaad64d
add long_string_keys
Franklin Schmidt <fschmidt@gmail.com>
parents:
1550
diff
changeset
|
226 StringBuilder start = new StringBuilder("[["); |
46d4baaad64d
add long_string_keys
Franklin Schmidt <fschmidt@gmail.com>
parents:
1550
diff
changeset
|
227 if( s.indexOf('\n') != -1 ) |
46d4baaad64d
add long_string_keys
Franklin Schmidt <fschmidt@gmail.com>
parents:
1550
diff
changeset
|
228 start.append('\n'); |
1550 | 229 StringBuilder end = new StringBuilder("]]"); |
230 while( s.contains(end) ) { | |
231 start.insert(1,'='); | |
232 end.insert(1,'='); | |
233 } | |
234 sb.append(start); | |
235 sb.append(s); | |
236 sb.append(end); | |
237 return; | |
238 } | |
239 sb.append( '"' ); | |
240 sb.append( Luan.stringEncode(s) ); | |
241 sb.append( '"' ); | |
242 } | |
243 | |
720 | 244 } |