summaryrefslogtreecommitdiff
path: root/test/rlhc.d/case/url1.ml-O-F0--var-backend.in
blob: 7164d05decd25314c760717d435179afcb9b0305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
host( "working/url1.rl", 1 ) @{(*
	* @@LANG: ocaml
	*)
	
	(*
	//
	// URL Parser
	// Copyright (c) 2010 J.A. Roberts Tunney
	// MIT License
	//
	// Converted to OCaml by ygrek
	//
	// To compile:
	//
	//	ragel -O url.rl -o url.ml
	//  ragel -O url_authority.rl -o url_authority.ml
	//	ocamlopt -g unix.cmxa url_authority.ml url.ml -o url
	//	./url
	//
	// To show a diagram of your state machine:
	//
	//   ragel -V -G2 -p -o url.dot url.rl
	//   dot -Tpng -o url.png url.dot
	//   chrome url.png
	//
	//   ragel -V -G2 -p -o url_authority.dot url_authority.rl
	//   dot -Tpng -o url_authority.png url_authority.dot
	//   chrome url_authority.png
	//
	// Reference:
	//
	// - http://tools.ietf.org/html/rfc3986
	//
	*)
	
	(*
	// -*-go-*-
	//
	// URL Parser
	// Copyright (c) 2010 J.A. Roberts Tunney
	// MIT License
	//
	*)
	
	
}@
array s8 _url_authority_actions( 0, 12 ) = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 11, 1, 12, 2, 1, 2, 2, 5, 7, 2, 6, 8, 2, 6, 11, 2, 9, 10, 3, 0, 9, 10, 3, 1, 6, 8, 3, 5, 7, 12, 3, 5, 11, 2, 3, 6, 8, 12, 3, 12, 5, 7, 3, 12, 6, 8, 4, 1, 6, 8, 12, 0 };

array int _url_authority_trans_keys( 0, 9 ) = { 1, 0, 0, 9, 3, 7, 3, 7, 0, 9, 3, 7, 3, 7, 0, 9, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 0, 9, 0, 9, 3, 7, 3, 7, 3, 7, 3, 7, 0, 9, 0, 9, 3, 7, 3, 7, 0, 9, 0, 9, 0, 9, 2, 5, 2, 5, 0, 9, 0, 9, 4, 5, 0, 9, 0, 9, 4, 6, 0 };

array s8 _url_authority_char_class( 0, 9 ) = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 5, 0, 1, 0, 0, 6, 7, 7, 7, 7, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 1, 9, 1, 1, 1, 7, 7, 7, 7, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 };

array s16 _url_authority_index_offsets( 0, 230 ) = { 0, 0, 10, 15, 20, 30, 35, 40, 50, 55, 60, 65, 70, 75, 80, 90, 100, 105, 110, 115, 120, 130, 140, 145, 150, 160, 170, 180, 184, 188, 198, 208, 210, 220, 230, 0 };

array s8 _url_authority_indices( 0, 61 ) = { 1, 0, 2, 0, 1, 1, 3, 0, 4, 1, 5, 1, 1, 1, 5, 6, 1, 1, 1, 6, 1, 7, 8, 7, 1, 1, 9, 7, 1, 1, 10, 1, 1, 1, 10, 11, 1, 1, 1, 11, 1, 3, 12, 3, 1, 1, 3, 3, 13, 1, 14, 1, 1, 1, 14, 15, 1, 1, 1, 15, 16, 1, 1, 1, 16, 17, 1, 1, 1, 17, 18, 1, 1, 1, 18, 19, 1, 1, 1, 19, 1, 20, 21, 20, 20, 1, 20, 20, 1, 1, 1, 22, 23, 22, 22, 1, 22, 22, 1, 24, 25, 1, 1, 1, 25, 26, 1, 1, 1, 26, 27, 1, 1, 1, 27, 28, 1, 1, 1, 28, 1, 29, 30, 29, 29, 1, 20, 29, 1, 1, 1, 31, 32, 31, 31, 1, 22, 31, 1, 33, 34, 1, 1, 1, 34, 35, 1, 1, 1, 35, 1, 36, 37, 36, 38, 39, 40, 36, 1, 1, 1, 41, 42, 43, 1, 44, 1, 41, 1, 1, 1, 45, 46, 45, 47, 39, 45, 45, 1, 1, 48, 49, 1, 44, 50, 51, 1, 52, 1, 53, 54, 53, 53, 53, 53, 53, 1, 1, 1, 19, 55, 19, 19, 19, 19, 19, 1, 1, 56, 57, 1, 7, 58, 59, 1, 52, 9, 7, 1, 1, 1, 3, 12, 3, 47, 39, 3, 3, 13, 1, 60, 57, 61, 0 };

array s8 _url_authority_index_defaults( 0, 53 ) = { 0, 0, 1, 1, 7, 1, 1, 3, 1, 1, 1, 1, 1, 1, 20, 22, 1, 1, 1, 1, 29, 31, 1, 1, 36, 41, 45, 1, 1, 53, 19, 1, 7, 3, 1, 0 };

array s8 _url_authority_trans_cond_spaces( -1, 0 ) = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0 };

array s8 _url_authority_cond_targs( 0, 34 ) = { 24, 0, 2, 26, 20, 3, 24, 4, 5, 7, 6, 4, 8, 14, 9, 26, 11, 28, 13, 30, 15, 16, 15, 16, 31, 17, 15, 19, 32, 21, 22, 21, 22, 34, 23, 21, 24, 2, 25, 29, 33, 4, 18, 32, 29, 26, 8, 27, 10, 28, 10, 28, 29, 30, 12, 12, 27, 29, 18, 32, 25, 7, 0 };

array s8 _url_authority_cond_actions( 0, 42 ) = { 15, 0, 3, 15, 0, 7, 9, 5, 0, 24, 7, 9, 3, 0, 7, 9, 7, 9, 0, 0, 15, 3, 5, 0, 18, 7, 9, 7, 9, 15, 3, 5, 0, 18, 7, 9, 5, 0, 18, 18, 42, 15, 3, 15, 34, 5, 0, 18, 3, 15, 0, 5, 21, 1, 1, 0, 0, 0, 0, 5, 0, 11, 0 };

array s8 _url_authority_eof_actions( 0, 58 ) = { 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 58, 38, 58, 46, 30, 27, 13, 54, 38, 13, 0 };

array s8 _url_authority_nfa_targs( 0, 0 ) = { 0, 0 };

array s8 _url_authority_nfa_offsets( 0, 0 ) = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

array s8 _url_authority_nfa_push_actions( 0, 0 ) = { 0, 0 };

array s8 _url_authority_nfa_pop_trans( 0, 0 ) = { 0, 0 };

value int url_authority_start = 1;
value int url_authority_first_final = 24;
value int url_authority_error = 0;

value int url_authority_en_main = 1;

host( "working/url1.rl", 47 ) @{
	
	(*
	// i parse strings like `alice@@pokémon.com`.
	//
	// sounds simple right?  but i also parse stuff like:
	//
	//   bob%20barker:priceisright@@[dead:beef::666]:5060;isup-oli=00
	//
	// which in actual reality is:
	//
	// - User: "bob barker"
	// - Pass: "priceisright"
	// - Host: "dead:beef::666"
	// - Port: 5060
	// - Params: "isup-oli=00"
	//
	// which was probably extracted from an absolute url that looked like:
	//
	//   sip:bob%20barker:priceisright@@[dead:beef::666]:5060;isup-oli=00/palfun.html?haha#omg
	//
	// which was probably extracted from its address form:
	//
	//   "Bob Barker" <sip:bob%20barker:priceisright@@[dead:beef::666]:5060;isup-oli=00/palfun.html?haha#omg>;tag=666
	//
	// who would have thought this could be so hard ._.
	*)
	
	type url = {
		scheme   : string; (* http, sip, file, etc. (never blank, always lowercase) *)
		user     : string; (* who is you *)
		pass     : string; (* for like, logging in *)
		host     : string; (* IP 4/6 address or hostname (mandatory) *)
		port     : int;    (* like 80 or 5060 (default 0) *)
		params   : string; (* stuff after ';' (NOT UNESCAPED, used in sip) *)
		path     : string; (* stuff starting with '/' *)
		query    : string; (* stuff after '?' (NOT UNESCAPED) *)
		fragment : string; (* stuff after '#' *)
	}
	
	let fail fmt = Printf.ksprintf failwith fmt
	
	let unhex c =
	match c with
	| '0'..'9' -> Char.code c - Char.code '0'
	| 'a'..'f' -> Char.code c - Char.code 'a' + 10
	| 'A'..'F' -> Char.code c - Char.code 'A' + 10
	| _ -> fail "unhex %C" c
	
	let parse_authority u data =
	let (cs, p, pe, eof) = (ref 0, ref 0, ref (String.length data), ref (String.length data)) in
	let mark = ref 0 in
	
	(*
	// temporary holding place for user:pass and/or host:port cuz an
	// optional term (user[:pass]) coming before a mandatory term
	// (host[:pass]) would require require backtracking and all that
	// evil nondeterministic stuff which ragel seems to hate.  (for
	// this same reason you're also allowed to use square quotes
	// around the username.)
	*)
	let (b1, b2) = (ref "", ref "") in
	
	(*
	// this buffer is so we can unescape while we roll
	var hex byte
	buf := make([]byte, len(data))
	amt := 0
	*)
	let buf = Buffer.create 10 in
	let hex = ref 0 in
	
}@
{
	cs = cast(int)url_authority_start;
}

{
	uint _trans = 0;
	uint _have = 0;
	uint _cont = 1;
	index s8 _acts;
	uint _nacts;
	index int _keys;
	index s8 _inds;
	while ( _cont == 1 ) {
		
		if ( cs == 0 )
		_cont = 0;
		_have = 0;
		if ( p == pe ) {
			if ( p == eof )
			{
				if ( _have == 0 ) {
					index s8 __acts;
					uint __nacts;
					__acts = offset( _url_authority_actions, _url_authority_eof_actions[cs] );
					__nacts = cast(uint)deref( _url_authority_actions, __acts );
					__acts += 1;
					while ( __nacts > 0 ) {
						switch ( deref( _url_authority_actions, __acts ) ) {
							case 0 {
								host( "working/url1.rl", 119 ) ${ mark := !p                             }$
							}
							case 1 {
								host( "working/url1.rl", 120 ) ${ Buffer.reset buf                       }$
							}
							case 5 {
								host( "working/url1.rl", 125 ) ${ b1 := Buffer.contents buf; Buffer.clear buf }$
							}
							case 6 {
								host( "working/url1.rl", 126 ) ${ b2 := Buffer.contents buf; Buffer.clear buf }$
							}
							case 7 {
								host( "working/url1.rl", 127 ) ${ u := { !u with host = !b1 }; Buffer.clear buf }$
							}
							case 8 {
								host( "working/url1.rl", 129 ) ${
									if !b2 <> "" then
									begin
									u := { !u with port = int_of_string !b2 };
									if !u.port > 65535 then fail "bad url authority: %S" data
									end
								}$
							}
							case 9 {
								host( "working/url1.rl", 137 ) ${
									u := { !u with params = String.sub data !mark (!p - !mark) }
								}$
							}
							case 10 {
								host( "working/url1.rl", 141 ) ${
									u := { !u with params = String.sub data !mark (!p - !mark) }
									(*       return nil *)
								}$
							}
							case 12 {
								host( "working/url1.rl", 151 ) ${
									u := { !u with host = !b1 };
									if !u.host = "" then
									u := { !u with host = Buffer.contents buf }
									else
									begin
									if Buffer.length buf > 0 then b2 := Buffer.contents buf;
									if !b2 <> "" then
									begin
									u := { !u with port = int_of_string !b2 };
									if !u.port > 65535 then fail "bad url authority: %S" data
									end
									end
									(* 			return nil *)
								}$
							}
						}
						__nacts -= 1;
						__acts += 1;
					}
				}
			}
			
			if ( _have == 0 )
			_cont = 0;
		}
		if ( _cont == 1 ) {
			if ( _have == 0 ) {
				_keys = offset( _url_authority_trans_keys, (cs<<1) );
				_inds = offset( _url_authority_indices, _url_authority_index_offsets[cs] );
				
				if ( ( deref( data, p )) <= 127 && ( deref( data, p )) >= 0 )
				{
					int _ic = cast(int)_url_authority_char_class[cast(int)( deref( data, p )) - 0];
					if ( _ic <= cast(int)deref( _url_authority_trans_keys, _keys+1 ) && _ic >= cast(int)deref( _url_authority_trans_keys, _keys ) )
					_trans = cast(uint)deref( _url_authority_indices, _inds + cast(int)( _ic - cast(int)deref( _url_authority_trans_keys, _keys ) )  ); 
					else
					_trans = cast(uint)_url_authority_index_defaults[cs];
				}
				else {
					_trans = cast(uint)_url_authority_index_defaults[cs];
				}
				
			}
			if ( _cont == 1 ) {
				cs = cast(int)_url_authority_cond_targs[_trans];
				
				if ( _url_authority_cond_actions[_trans] != 0 ) {
					_acts = offset( _url_authority_actions, _url_authority_cond_actions[_trans] );
					_nacts = cast(uint)deref( _url_authority_actions, _acts );
					_acts += 1;
					while ( _nacts > 0 )
					{
						switch ( deref( _url_authority_actions, _acts ) )
						{
							case 0 {
								host( "working/url1.rl", 119 ) ${ mark := !p                             }$
							}
							case 1 {
								host( "working/url1.rl", 120 ) ${ Buffer.reset buf                       }$
							}
							case 2 {
								host( "working/url1.rl", 121 ) ${ Buffer.add_char buf data.[p.contents]                }$
							}
							case 3 {
								host( "working/url1.rl", 123 ) ${ hex := unhex data.[p.contents] * 16                   }$
							}
							case 4 {
								host( "working/url1.rl", 124 ) ${ Buffer.add_char buf (Char.chr (!hex + unhex data.[p.contents])) }$
							}
							case 5 {
								host( "working/url1.rl", 125 ) ${ b1 := Buffer.contents buf; Buffer.clear buf }$
							}
							case 6 {
								host( "working/url1.rl", 126 ) ${ b2 := Buffer.contents buf; Buffer.clear buf }$
							}
							case 7 {
								host( "working/url1.rl", 127 ) ${ u := { !u with host = !b1 }; Buffer.clear buf }$
							}
							case 8 {
								host( "working/url1.rl", 129 ) ${
									if !b2 <> "" then
									begin
									u := { !u with port = int_of_string !b2 };
									if !u.port > 65535 then fail "bad url authority: %S" data
									end
								}$
							}
							case 11 {
								host( "working/url1.rl", 146 ) ${
									u := { !u with user = !b1; pass = !b2 };
									b2 := ""
								}$
							}
						}
						_nacts -= 1;
						_acts += 1;
					}
				}
				
				if ( cs == 0 )
				_cont = 0;
				if ( _cont == 1 )
				p += 1;
				
			}}
	}
}
host( "working/url1.rl", 207 ) @{
	
	(*
	// if cs >= url_authority_first_final {
		// 	return nil
		// }
	*)
	
	(*
	// fmt.Println("error state", cs)
	// fmt.Println(string(data))
	// for i := 0; i < p; i++ {
		// 	fmt.Print(" ")
		// }
	// fmt.Println("^")
	// fmt.Println(url)
	*)
	;;
	
	
	let dummy = {
		scheme = ""; user = ""; pass = ""; host = ""; port = 0; 
		params = ""; path = ""; query = ""; fragment = ""; }
	
	let show u =
	Printf.sprintf "%s :// %s : %s @@ %s : %d ;%s %s ?%s #%s" u.scheme u.user u.pass u.host u.port
	u.params u.path u.query u.fragment
	
	
}@
array s8 _url_actions( 0, 10 ) = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 2, 0, 9, 2, 1, 2, 2, 1, 3, 2, 1, 10, 3, 7, 1, 2, 0 };

array int _url_trans_keys( 0, 12 ) = { 1, 0, 9, 12, 4, 12, 0, 8, 6, 11, 6, 11, 6, 11, 6, 11, 6, 11, 6, 11, 6, 11, 6, 11, 5, 5, 0, 8, 0, 3, 0, 3, 0, 8, 0, 3, 0, 3, 0, 8, 0 };

array s8 _url_char_class( 0, 12 ) = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 1, 3, 1, 1, 1, 1, 1, 4, 1, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 1, 0, 1, 0, 8, 1, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1, 1, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 1, 1, 1, 1, 0, 0 };

array s8 _url_index_offsets( 0, 105 ) = { 0, 0, 4, 13, 22, 28, 34, 40, 46, 52, 58, 64, 70, 71, 80, 84, 88, 97, 101, 105, 0 };

array s8 _url_indices( 0, 37 ) = { 0, 0, 2, 2, 3, 1, 3, 4, 1, 5, 5, 3, 3, 1, 6, 1, 7, 6, 8, 6, 6, 1, 9, 1, 1, 9, 1, 9, 10, 1, 1, 10, 1, 10, 11, 1, 1, 11, 1, 11, 12, 1, 1, 12, 1, 12, 13, 1, 1, 13, 1, 13, 14, 1, 1, 14, 1, 14, 15, 1, 1, 15, 1, 15, 16, 1, 1, 16, 1, 16, 17, 1, 12, 18, 19, 12, 20, 12, 12, 21, 1, 22, 1, 23, 1, 24, 1, 25, 1, 26, 27, 28, 26, 26, 26, 26, 29, 1, 30, 31, 32, 1, 16, 33, 34, 1, 6, 35, 7, 6, 36, 6, 6, 37, 0 };

array s8 _url_index_defaults( 0, 30 ) = { 0, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 22, 24, 26, 30, 16, 6, 0 };

array s8 _url_trans_cond_spaces( -1, 0 ) = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0 };

array s8 _url_cond_targs( 0, 19 ) = { 2, 0, 2, 2, 3, 2, 13, 6, 12, 5, 15, 7, 13, 9, 16, 11, 18, 19, 14, 6, 16, 17, 15, 4, 15, 4, 16, 14, 8, 17, 18, 14, 10, 14, 10, 14, 16, 17, 0 };

array s8 _url_cond_actions( 0, 35 ) = { 29, 0, 26, 5, 13, 7, 1, 1, 0, 9, 11, 0, 0, 9, 11, 0, 0, 0, 15, 0, 35, 15, 26, 3, 5, 0, 5, 17, 0, 17, 1, 23, 1, 19, 0, 0, 26, 0, 0 };

array s8 _url_eof_actions( 0, 32 ) = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 32, 21, 17, 23, 19, 0, 0 };

array s8 _url_nfa_targs( 0, 0 ) = { 0, 0 };

array s8 _url_nfa_offsets( 0, 0 ) = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

array s8 _url_nfa_push_actions( 0, 0 ) = { 0, 0 };

array s8 _url_nfa_pop_trans( 0, 0 ) = { 0, 0 };

value int url_start = 1;
value int url_first_final = 13;
value int url_error = 0;

value int url_en_main = 1;

host( "working/url1.rl", 237 ) @{
	
	(*
	// i parse absolute urls and don't suck at it.  i'll parse just about
	// any type of url you can think of and give you a human-friendly data
	// structure.
	//
	// this routine takes no more than a few microseconds, is reentrant,
	// performs in a predictable manner (for security/soft-realtime,)
	// doesn't modify your `data` buffer, and under no circumstances will
	// it panic (i hope!)
	*)
	let url_parse data =
	let (cs, p, pe, eof) = (ref 0, ref 0, ref (String.length data), ref (String.length data)) in
	let mark = ref 0 in
	let u = ref dummy in
	
	(*
	// this buffer is so we can unescape while we roll
	*)
	let buf = Buffer.create 16 in
	let hex = ref 0 in
	
}@
{
	cs = cast(int)url_start;
}

{
	uint _trans = 0;
	uint _have = 0;
	uint _cont = 1;
	index s8 _acts;
	uint _nacts;
	index int _keys;
	index s8 _inds;
	while ( _cont == 1 ) {
		
		if ( cs == 0 )
		_cont = 0;
		_have = 0;
		if ( p == pe ) {
			if ( p == eof )
			{
				if ( _have == 0 ) {
					index s8 __acts;
					uint __nacts;
					__acts = offset( _url_actions, _url_eof_actions[cs] );
					__nacts = cast(uint)deref( _url_actions, __acts );
					__acts += 1;
					while ( __nacts > 0 ) {
						switch ( deref( _url_actions, __acts ) ) {
							case 0 {
								host( "working/url1.rl", 260 ) ${ mark := !p                             }$
							}
							case 1 {
								host( "working/url1.rl", 261 ) ${ Buffer.reset buf                       }$
							}
							case 7 {
								host( "working/url1.rl", 267 ) ${ parse_authority u (String.sub data !mark (!p - !mark)) }$
							}
							case 8 {
								host( "working/url1.rl", 268 ) ${ u := { !u with path = Buffer.contents buf } }$
							}
							case 9 {
								host( "working/url1.rl", 269 ) ${ u := { !u with query = String.sub data !mark (!p - !mark) } }$
							}
							case 10 {
								host( "working/url1.rl", 270 ) ${ u := { !u with fragment = Buffer.contents buf } }$
							}
						}
						__nacts -= 1;
						__acts += 1;
					}
				}
			}
			
			if ( _have == 0 )
			_cont = 0;
		}
		if ( _cont == 1 ) {
			if ( _have == 0 ) {
				_keys = offset( _url_trans_keys, (cs<<1) );
				_inds = offset( _url_indices, _url_index_offsets[cs] );
				
				if ( ( deref( data, p )) <= 127 && ( deref( data, p )) >= 0 )
				{
					int _ic = cast(int)_url_char_class[cast(int)( deref( data, p )) - 0];
					if ( _ic <= cast(int)deref( _url_trans_keys, _keys+1 ) && _ic >= cast(int)deref( _url_trans_keys, _keys ) )
					_trans = cast(uint)deref( _url_indices, _inds + cast(int)( _ic - cast(int)deref( _url_trans_keys, _keys ) )  ); 
					else
					_trans = cast(uint)_url_index_defaults[cs];
				}
				else {
					_trans = cast(uint)_url_index_defaults[cs];
				}
				
			}
			if ( _cont == 1 ) {
				cs = cast(int)_url_cond_targs[_trans];
				
				if ( _url_cond_actions[_trans] != 0 ) {
					_acts = offset( _url_actions, _url_cond_actions[_trans] );
					_nacts = cast(uint)deref( _url_actions, _acts );
					_acts += 1;
					while ( _nacts > 0 )
					{
						switch ( deref( _url_actions, _acts ) )
						{
							case 0 {
								host( "working/url1.rl", 260 ) ${ mark := !p                             }$
							}
							case 1 {
								host( "working/url1.rl", 261 ) ${ Buffer.reset buf                       }$
							}
							case 2 {
								host( "working/url1.rl", 262 ) ${ Buffer.add_char buf data.[p.contents]                 }$
							}
							case 3 {
								host( "working/url1.rl", 263 ) ${ Buffer.add_char buf (Char.lowercase data.[p.contents])}$
							}
							case 4 {
								host( "working/url1.rl", 264 ) ${ hex := unhex data.[p.contents] * 16                   }$
							}
							case 5 {
								host( "working/url1.rl", 265 ) ${ Buffer.add_char buf (Char.chr (!hex + unhex data.[p.contents])) }$
							}
							case 6 {
								host( "working/url1.rl", 266 ) ${ u := { !u with scheme = Buffer.contents buf } }$
							}
							case 7 {
								host( "working/url1.rl", 267 ) ${ parse_authority u (String.sub data !mark (!p - !mark)) }$
							}
							case 8 {
								host( "working/url1.rl", 268 ) ${ u := { !u with path = Buffer.contents buf } }$
							}
							case 9 {
								host( "working/url1.rl", 269 ) ${ u := { !u with query = String.sub data !mark (!p - !mark) } }$
							}
						}
						_nacts -= 1;
						_acts += 1;
					}
				}
				
				if ( cs == 0 )
				_cont = 0;
				if ( _cont == 1 )
				p += 1;
				
			}}
	}
}
host( "working/url1.rl", 307 ) @{
	
	if !cs < url_first_final then
	if !p = !pe then
	fail "unexpected eof: %s" data
	else
	fail "error in url at pos %d (%c): %s" !p data.[!p] data
	else
	!u
	
	(* ////////////////////////////////////////////////////////////////////// *)
	
	let tests = [
	"http://user:pass@@example.com:80;hello/lol.php?fun#omg",
	{
		scheme = "http";
		user = "user";
		pass = "pass";
		host = "example.com";
		port = 80;
		params = "hello";
		path = "/lol.php";
		query = "fun";
		fragment = "omg";
	};
	
	"a:b",
	{ dummy with
		scheme = "a";
		host = "b";
	};
	
	"GoPHeR://@@example.com@@:;/?#",
	{ dummy with
		scheme = "gopher";
		host = "@@example.com@@";
		path = "/";
	};
	
	"ldap://[2001:db8::7]/c=GB?objectClass/?one",
	{ dummy with
		scheme = "ldap";
		host = "2001:db8::7";
		path = "/c=GB";
		query = "objectClass/?one";
	};
	
	"http://user@@example.com",
	{ dummy with
		scheme = "http";
		user = "user";
		host = "example.com";
	};
	
	"http://品研发和研发管@@☃.com:65000;%20",
	{ dummy with
		scheme = "http";
		user = "品研发和研发管";
		host = "☃.com";
		port = 65000;
		params = "%20";
	};
	
	"https://example.com:80",
	{ dummy with
		scheme = "https";
		host = "example.com";
		port = 80;
	};
	
	"file:///etc/passwd",
	{ dummy with
		scheme = "file";
		path = "/etc/passwd";
	};
	
	"file:///c:/WINDOWS/clock.avi",
	{ dummy with
		scheme = "file";
		path = "/c:/WINDOWS/clock.avi"; (* <-- is this kosher? *)
	};
	
	"file://hostname/path/to/the%20file.txt",
	{ dummy with
		scheme = "file";
		host = "hostname";
		path = "/path/to/the file.txt";
	};
	
	"sip:example.com",
	{ dummy with
		scheme = "sip";
		host = "example.com";
	};
	
	"sip:example.com:5060",
	{ dummy with
		scheme = "sip";
		host = "example.com";
		port = 5060;
	};
	
	"mailto:ditto@@pokémon.com",
	{ dummy with
		scheme = "mailto";
		user = "ditto";
		host = "pokémon.com";
	};
	
	"sip:[dead:beef::666]:5060",
	{ dummy with
		scheme = "sip";
		host = "dead:beef::666";
		port = 5060;
	};
	
	"tel:+12126660420",
	{ dummy with
		scheme = "tel";
		host = "+12126660420";
	};
	
	"sip:bob%20barker:priceisright@@[dead:beef::666]:5060;isup-oli=00/palfun.html?haha#omg",
	{
		scheme = "sip";
		user = "bob barker";
		pass = "priceisright";
		host = "dead:beef::666";
		port = 5060;
		params = "isup-oli=00";
		path = "/palfun.html";
		query = "haha";
		fragment = "omg";
	};
	
	"http://www.google.com/search?%68l=en&safe=off&q=omfg&aq=f&aqi=g2g-s1g1g-s1g5&aql=&oq=&gs_rfai=",
	{ dummy with
		scheme = "http";
		host = "www.google.com";
		path = "/search";
		query = "%68l=en&safe=off&q=omfg&aq=f&aqi=g2g-s1g1g-s1g5&aql=&oq=&gs_rfai=";
	};
	]
	
	(*
	func (test *urlTest) compare(url *URL) (passed bool) {
		if url.Scheme != test.url.Scheme {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) scheme: %#v != %#v\n",
			string(test.s), url.Scheme, test.url.Scheme)
			passed = true
		}
		if url.User != test.url.User {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) user: %#v != %#v\n",
			string(test.s), url.User, test.url.User)
			passed = true
		}
		if url.Pass != test.url.Pass {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) pass: %#v != %#v\n",
			string(test.s), url.Pass, test.url.Pass)
			passed = true
		}
		if url.Host != test.url.Host {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) host: %#v != %#v\n",
			string(test.s), url.Host, test.url.Host)
			passed = true
		}
		if url.Port != test.url.Port {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) port: %#v != %#v\n",
			string(test.s), url.Port, test.url.Port)
			passed = true
		}
		if url.Port != test.url.Port {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) port: %#v != %#v\n",
			string(test.s), url.Port, test.url.Port)
			passed = true
		}
		if url.Params != test.url.Params {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) params: %#v != %#v\n",
			string(test.s), url.Params, test.url.Params)
			passed = true
		}
		if url.Path != test.url.Path {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) path: %#v != %#v\n",
			string(test.s), url.Path, test.url.Path)
			passed = true
		}
		if url.Query != test.url.Query {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) query: %#v != %#v\n",
			string(test.s), url.Query, test.url.Query)
			passed = true
		}
		if url.Fragment != test.url.Fragment {
			fmt.Fprintf(os.Stderr, "FAIL url(%#v) fragment: %#v != %#v\n",
			string(test.s), url.Fragment, test.url.Fragment)
			passed = true
		}
		return !passed
	}
	*)
	
	let bench () = 
	let rounds = 0 in
	let urls = [
	"a:a";
	"http://google.com/";
	"sip:jtunney@@lobstertech.com";
	"http://user:pass@@example.com:80;hello/lol.php?fun#omg";
	"file:///etc/passwd";
	] in
	List.iter (fun url ->
	for i = 1 to rounds do
	ignore (url_parse url)
	done;
	Printf.printf "BENCH parse %S \n%!" url
	) urls
	
	let test () =
	List.iter (fun (s,res) ->
	let url = url_parse s in
	if url <> res then
	fail "got %S for %S" (show url) (*show res*) s
	) tests
	
	let () =
	test ();
	bench ();
	exit 0
	
}@