OLD | NEW |
(Empty) | |
| 1 let {Logger} = require( "logger" ); |
| 2 let {Encoding} = require( "encoding" ); |
| 3 let {YAML, YamlParseException} = require( "yaml" ); |
| 4 |
| 5 var what = "YAML_input"; |
| 6 var output_box; |
| 7 function sandbox_start() |
| 8 { |
| 9 output_box = document.getElementById( "sandbox_output" ); |
| 10 switch ( what ) |
| 11 { |
| 12 case "Encoding": |
| 13 exercise_Encoding(); |
| 14 break; |
| 15 case "YAML_input": |
| 16 exercise_YAML_input(); |
| 17 break; |
| 18 } |
| 19 } |
| 20 |
| 21 var write = function( s ) |
| 22 { |
| 23 output_box.value += s; |
| 24 }; |
| 25 |
| 26 |
| 27 var logger = new Logger( "sandbox" ); |
| 28 var log = logger.make_log(); |
| 29 |
| 30 function wrapped_parse( s ) |
| 31 { |
| 32 try |
| 33 { |
| 34 return YAML.parse( s ); |
| 35 } |
| 36 catch ( e if e instanceof YamlParseException ) |
| 37 { |
| 38 write( "YamlParseException: " + e.message + "\n" ); |
| 39 } |
| 40 catch ( e ) |
| 41 { |
| 42 write( "Exception: " + e.toString() + "\nstack = " + e.stack + "\n" ); |
| 43 } |
| 44 return null; |
| 45 } |
| 46 |
| 47 //==============================================================================
=========== |
| 48 // exercise_YAML_input |
| 49 //==============================================================================
=========== |
| 50 function exercise_YAML_input() |
| 51 { |
| 52 log( "Start YAML input" ); |
| 53 |
| 54 //--------------------------------------------------------------------------
--------------- |
| 55 write( "---\n" ); |
| 56 write( "# 0\n" ); |
| 57 var s = "" |
| 58 + "name: foo\n" |
| 59 + "site:\n" |
| 60 + " - yahoo.com\n" |
| 61 + " - ksl.com\n" |
| 62 + ""; |
| 63 var x = wrapped_parse( s ); |
| 64 if ( x ) |
| 65 { |
| 66 write( JSON.stringify( x ) ); |
| 67 write( "\n" ); |
| 68 } |
| 69 //--------------------------------------------------------------------------
--------------- |
| 70 write( "---\n" ); |
| 71 write( "# 1\n" ); |
| 72 s = "" |
| 73 + "name: foo\n" |
| 74 + "site:\n" |
| 75 + " - yahoo.com\n" |
| 76 + " error no array element mark\n" |
| 77 + " - ksl.com\n" |
| 78 + ""; |
| 79 x = wrapped_parse( s ); |
| 80 if ( x ) |
| 81 { |
| 82 write( JSON.stringify( x ) ); |
| 83 write( "\n" ); |
| 84 } |
| 85 } |
| 86 |
| 87 //==============================================================================
=========== |
| 88 // exercise_Encoding |
| 89 //==============================================================================
=========== |
| 90 function exercise_Encoding() |
| 91 { |
| 92 log( "Start" ); |
| 93 |
| 94 let formatter = new Encoding.YAML( write ); |
| 95 var y = new Encoding.Format_stream( formatter ); |
| 96 y.logger.suppress( true ); |
| 97 |
| 98 //--------------------------------------------------------------------------
--------------- |
| 99 write( "---\n" ); |
| 100 write( "# 0\n" ); |
| 101 y.write( "# Output generated with YAML_stream" ); |
| 102 |
| 103 //--------------------------------------------------------------------------
--------------- |
| 104 write( "---\n" ); |
| 105 write( "# 1\n" ); |
| 106 Cu.reportError( "1." ); |
| 107 var g = y.write( "Test string" ); |
| 108 if ( g ) |
| 109 { |
| 110 log( "1. error: immediate array spec returned non-null generator" ); |
| 111 } |
| 112 |
| 113 //--------------------------------------------------------------------------
--------------- |
| 114 write( "---\n" ); |
| 115 write( "# 2\n" ); |
| 116 try |
| 117 { |
| 118 Cu.reportError( "2." ); |
| 119 var test_array = [ "item 1", "item 2", [ "item 3.1", "item 3.2" ], { a:
"item 4a", b: "item 4b" } ]; |
| 120 y.write( test_array, Encoding.array() ); |
| 121 } |
| 122 catch ( e ) |
| 123 { |
| 124 Cu.reportError( "error in 2: " + e.toString() + "\n" + e.stack ); |
| 125 throw e; |
| 126 } |
| 127 |
| 128 //--------------------------------------------------------------------------
--------------- |
| 129 write( "---\n" ); |
| 130 write( "# 3\n" ); |
| 131 try |
| 132 { |
| 133 Cu.reportError( "3." ); |
| 134 /* |
| 135 * An array of deferred arrays. |
| 136 */ |
| 137 y.write( [ "3" ], Encoding.array_stream() ); |
| 138 y.sequence_start(); |
| 139 for ( let i = 0 ; i < test_array.length ; ++i ) |
| 140 { |
| 141 try |
| 142 { |
| 143 y.sequence_send( test_array[i] ); |
| 144 } |
| 145 catch ( e ) |
| 146 { |
| 147 if ( e !== StopIteration ) throw e; |
| 148 log( "3. StopIteration at " + i ); |
| 149 break; |
| 150 } |
| 151 } |
| 152 y.sequence_stop(); |
| 153 } |
| 154 catch ( e ) |
| 155 { |
| 156 Cu.reportError( "error in 3: " + e.toString() + "\n" + e.stack ); |
| 157 throw e; |
| 158 } |
| 159 |
| 160 //--------------------------------------------------------------------------
--------------- |
| 161 write( "---\n" ); |
| 162 write( "# 4\n" ); |
| 163 Cu.reportError( "4." ); |
| 164 |
| 165 y.write( [ "4" ], Encoding.array_stream( Encoding.array_stream() ) ); |
| 166 y.sequence_start(); |
| 167 for ( let i = 0 ; i < 3 ; ++i ) |
| 168 { |
| 169 y.sequence_start(); |
| 170 let n = [1, 0, 2][ i ]; |
| 171 for ( let j = 0 ; j < n ; ++j ) |
| 172 { |
| 173 let s = "item " + ( i + 1 ) + "." + j; |
| 174 //log( "4. " + s ); |
| 175 try |
| 176 { |
| 177 y.sequence_send( s ); |
| 178 } |
| 179 catch ( e ) |
| 180 { |
| 181 if ( e !== StopIteration ) throw e; |
| 182 log( "4. StopIteration at " + i + "," + j ); |
| 183 throw e; |
| 184 } |
| 185 } |
| 186 y.sequence_stop(); |
| 187 } |
| 188 y.sequence_stop(); |
| 189 |
| 190 //--------------------------------------------------------------------------
--------------- |
| 191 write( "---\n" ); |
| 192 write( "# 5\n" ); |
| 193 Cu.reportError( "5." ); |
| 194 |
| 195 var object_5 = { |
| 196 time_start: "now", |
| 197 instruction: { |
| 198 "this": "this", |
| 199 that: "that", |
| 200 the_other_thing: null |
| 201 } |
| 202 }; |
| 203 var object_5_encoding = Encoding.as_object( [ |
| 204 // prelude |
| 205 Encoding.immediate_fields( ["time_start", "instruction"] ), |
| 206 // observation |
| 207 Encoding.field( "observation", Encoding.array_stream() ), |
| 208 // postlude |
| 209 Encoding.immediate_fields( ["time_finish", "termination"] ) |
| 210 ] ); |
| 211 |
| 212 y.write( object_5, object_5_encoding ); |
| 213 y.sequence_start(); |
| 214 for ( let i = 0 ; i < 3 ; ++i ) |
| 215 { |
| 216 y.sequence_send( { site: "site " + i, square: i * i } ); |
| 217 } |
| 218 object_5.time_finish = "later"; |
| 219 object_5.termination = "success"; |
| 220 y.sequence_stop(); |
| 221 |
| 222 //--------------------------------------------------------------------------
--------------- |
| 223 write( "---\n" ); |
| 224 if ( !y.may_write() ) |
| 225 { |
| 226 var failure_message = "FAILURE: object streamer ended in an incomplete s
tate."; |
| 227 write( failure_message ); |
| 228 Cu.reportError( failure_message ); |
| 229 } |
| 230 } |
OLD | NEW |