comparison src/luan/modules/parsers/LuanToString.java @ 1552:46d4baaad64d

add long_string_keys
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Oct 2020 20:26:49 -0600
parents 0dc3be25ad20
children 26c51acf00f3
comparison
equal deleted inserted replaced
1551:9cc4cee39b8b 1552:46d4baaad64d
1 package luan.modules.parsers; 1 package luan.modules.parsers;
2 2
3 import java.util.List; 3 import java.util.List;
4 import java.util.Map; 4 import java.util.Map;
5 import java.util.Set;
6 import java.util.Collections;
5 import luan.Luan; 7 import luan.Luan;
6 import luan.LuanTable; 8 import luan.LuanTable;
7 import luan.LuanException; 9 import luan.LuanException;
8 import luan.LuanRuntimeException; 10 import luan.LuanRuntimeException;
9 11
11 public final class LuanToString { 13 public final class LuanToString {
12 public boolean strict = false; 14 public boolean strict = false;
13 public boolean numberTypes = false; 15 public boolean numberTypes = false;
14 public boolean compressed = false; 16 public boolean compressed = false;
15 public boolean useLongStrings = false; 17 public boolean useLongStrings = false;
18 public Set longStringKeys = Collections.emptySet();
16 19
17 public String toString(Object obj) throws LuanException { 20 public String toString(Object obj) throws LuanException {
18 StringBuilder sb = new StringBuilder(); 21 StringBuilder sb = new StringBuilder();
19 toString(obj,sb,0); 22 toString(obj,sb,0,useLongStrings);
20 return sb.toString(); 23 return sb.toString();
21 } 24 }
22 25
23 private void toString(Object obj,StringBuilder sb,int indented) throws LuanException { 26 private void toString(Object obj,StringBuilder sb,int indented,boolean longStrings) throws LuanException {
24 if( obj == null ) { 27 if( obj == null ) {
25 sb.append( "nil" ); 28 sb.append( "nil" );
26 return; 29 return;
27 } 30 }
28 if( obj instanceof Boolean ) { 31 if( obj instanceof Boolean ) {
32 if( obj instanceof Number ) { 35 if( obj instanceof Number ) {
33 toString((Number)obj,sb); 36 toString((Number)obj,sb);
34 return; 37 return;
35 } 38 }
36 if( obj instanceof String ) { 39 if( obj instanceof String ) {
37 toString((String)obj,sb); 40 toString((String)obj,sb,longStrings);
38 return; 41 return;
39 } 42 }
40 if( obj instanceof LuanTable ) { 43 if( obj instanceof LuanTable ) {
41 toString((LuanTable)obj,sb,indented); 44 toString((LuanTable)obj,sb,indented,longStrings);
42 return; 45 return;
43 } 46 }
44 if( strict ) 47 if( strict )
45 throw new LuanException("can't handle type "+Luan.type(obj)); 48 throw new LuanException("can't handle type "+Luan.type(obj));
46 sb.append( '<' ); 49 sb.append( '<' );
47 sb.append( obj ); 50 sb.append( obj );
48 sb.append( '>' ); 51 sb.append( '>' );
49 } 52 }
50 53
51 private void toString(LuanTable tbl,StringBuilder sb,int indented) throws LuanException { 54 private void toString(LuanTable tbl,StringBuilder sb,int indented,boolean longStrings) throws LuanException {
52 List list = tbl.asList(); 55 List list = tbl.asList();
53 Map map = tbl.rawMap(); 56 Map map = tbl.rawMap();
54 sb.append( '{' ); 57 sb.append( '{' );
55 boolean first = true; 58 boolean first = true;
56 for( Object obj : list ) { 59 for( Object obj : list ) {
58 indent(sb,indented+1); 61 indent(sb,indented+1);
59 else if( first ) 62 else if( first )
60 first = false; 63 first = false;
61 else 64 else
62 sb.append( ',' ); 65 sb.append( ',' );
63 toString(obj,sb,indented+1); 66 toString(obj,sb,indented+1,longStrings);
64 } 67 }
65 for( Object obj : map.entrySet() ) { 68 for( Object obj : map.entrySet() ) {
66 Map.Entry entry = (Map.Entry)obj; 69 Map.Entry entry = (Map.Entry)obj;
67 if( !compressed ) 70 if( !compressed )
68 indent(sb,indented+1); 71 indent(sb,indented+1);
82 Object key = entry.getKey(); 85 Object key = entry.getKey();
83 if( key instanceof String && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) { 86 if( key instanceof String && ((String)key).matches("[a-zA-Z_][a-zA-Z_0-9]*") ) {
84 sb.append( (String)key ); 87 sb.append( (String)key );
85 } else { 88 } else {
86 sb.append( '[' ); 89 sb.append( '[' );
87 toString( key, sb, indented ); 90 toString( key, sb, indented, false );
88 sb.append( ']' ); 91 sb.append( ']' );
89 } 92 }
90 sb.append( compressed ? "=" : " = " ); 93 sb.append( compressed ? "=" : " = " );
91 toString( entry.getValue(), sb, indented ); 94 toString( entry.getValue(), sb, indented, longStringKeys.contains(key) );
92 } 95 }
93 96
94 private void indent(StringBuilder sb,int indented) { 97 private void indent(StringBuilder sb,int indented) {
95 sb.append( '\n' ); 98 sb.append( '\n' );
96 for( int i=0; i<indented; i++ ) { 99 for( int i=0; i<indented; i++ ) {
118 } catch(LuanException e) { 121 } catch(LuanException e) {
119 throw new LuanRuntimeException(e); 122 throw new LuanRuntimeException(e);
120 } 123 }
121 } 124 }
122 125
123 private void toString(String s,StringBuilder sb) { 126 private void toString(String s,StringBuilder sb,boolean longStrings) {
124 if( useLongStrings && s.indexOf('\n') != -1 ) { 127 if( longStrings ) {
125 StringBuilder start = new StringBuilder("[[\n"); 128 StringBuilder start = new StringBuilder("[[");
129 if( s.indexOf('\n') != -1 )
130 start.append('\n');
126 StringBuilder end = new StringBuilder("]]"); 131 StringBuilder end = new StringBuilder("]]");
127 while( s.contains(end) ) { 132 while( s.contains(end) ) {
128 start.insert(1,'='); 133 start.insert(1,'=');
129 end.insert(1,'='); 134 end.insert(1,'=');
130 } 135 }