vile / scheme / transcoder.v
154 lines · 134 sloc · 3.95 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Codec and transcoder choices are immutable scalars. Copying their Value
4// handles exports every option without retaining a VM, source or allocator.
5pub enum CodecKind as u8 {
6 latin1
7 utf8
8 utf16
9}
10
11pub enum EolStyle as u8 {
12 lf
13 cr
14 crlf
15 nel
16 crnel
17 ls
18 none
19}
20
21pub enum ErrorHandlingMode as u8 {
22 ignore
23 raise
24 replace
25}
26
27pub fn codec_value(kind CodecKind) Value {
28 return Value{ tag: .codec, bits: ValueBits{ whole: i64(kind) } }
29}
30
31pub fn transcoder_value(codec CodecKind, eol EolStyle, mode ErrorHandlingMode) Value {
32 return Value{ tag: .transcoder, bits: ValueBits{ whole: i64(codec) | (i64(eol) << 2) | (i64(mode) << 5) } }
33}
34
35pub fn (value Value) as_codec() !CodecKind {
36 if value.tag != .codec || value.whole() < 0 || value.whole() > 2 {
37 return failure('expected a codec')
38 }
39 return unsafe { CodecKind(value.whole()) }
40}
41
42pub fn (value Value) as_transcoder() !(CodecKind, EolStyle, ErrorHandlingMode) {
43 if value.tag != .transcoder { return failure('expected a transcoder') }
44 word := value.whole()
45 codec, eol, mode := word & 3, (word >> 2) & 7, (word >> 5) & 3
46 if word < 0 || word > 127 || codec > 2 || eol > 6 || mode > 2 {
47 return failure('invalid transcoder options')
48 }
49 return unsafe { CodecKind(codec) }, unsafe { EolStyle(eol) }, unsafe { ErrorHandlingMode(mode) }
50}
51
52fn native_eol() EolStyle {
53 $if windows {
54 return .crlf
55 } $else {
56 return .lf
57 }
58}
59
60fn codec_name(kind CodecKind) string {
61 return match kind {
62 .latin1 { 'latin1' }
63 .utf8 { 'utf8' }
64 .utf16 { 'utf16' }
65 }
66}
67
68fn eol_name(style EolStyle) string {
69 return match style {
70 .lf { 'lf' }
71 .cr { 'cr' }
72 .crlf { 'crlf' }
73 .nel { 'nel' }
74 .crnel { 'crnel' }
75 .ls { 'ls' }
76 .none { 'none' }
77 }
78}
79
80fn error_mode_name(mode ErrorHandlingMode) string {
81 return match mode {
82 .ignore { 'ignore' }
83 .raise { 'raise' }
84 .replace { 'replace' }
85 }
86}
87
88fn parse_eol(value Value) !EolStyle {
89 if value.tag != .symbol { return failure('expected an end-of-line style symbol') }
90 return parse_eol_name(value.text())
91}
92
93fn parse_eol_name(name string) !EolStyle {
94 return match name {
95 'lf' { EolStyle.lf }
96 'cr' { .cr }
97 'crlf' { .crlf }
98 'nel' { .nel }
99 'crnel' { .crnel }
100 'ls' { .ls }
101 'none' { .none }
102 else { return failure('unsupported end-of-line style') }
103 }
104}
105
106fn parse_error_mode(value Value) !ErrorHandlingMode {
107 if value.tag != .symbol { return failure('expected an error-handling mode symbol') }
108 return parse_error_mode_name(value.text())
109}
110
111fn parse_error_mode_name(name string) !ErrorHandlingMode {
112 return match name {
113 'ignore' { ErrorHandlingMode.ignore }
114 'raise' { .raise }
115 'replace' { .replace }
116 else { return failure('unsupported error-handling mode') }
117 }
118}
119
120// The names and symbol headers are process literals. Static headers keep a cold
121// accessor from publishing a shorter caller arena's allocations into the VM.
122const transcoder_eol_names = [Text{ value: 'lf' }, Text{ value: 'cr' }, Text{ value: 'crlf' },
123 Text{ value: 'nel' }, Text{ value: 'crnel' }, Text{ value: 'ls' }, Text{ value: 'none' }]!
124
125const transcoder_mode_names = [Text{ value: 'ignore' }, Text{ value: 'raise' },
126 Text{ value: 'replace' }]!
127
128const transcoder_symbol_table = TranscoderSymbols{
129 eol: [payload_value(.symbol, &transcoder_eol_names[0]),
130 payload_value(.symbol, &transcoder_eol_names[1]),
131 payload_value(.symbol, &transcoder_eol_names[2]),
132 payload_value(.symbol, &transcoder_eol_names[3]),
133 payload_value(.symbol, &transcoder_eol_names[4]),
134 payload_value(.symbol, &transcoder_eol_names[5]),
135 payload_value(.symbol, &transcoder_eol_names[6])]!
136 mode: [payload_value(.symbol, &transcoder_mode_names[0]),
137 payload_value(.symbol, &transcoder_mode_names[1]),
138 payload_value(.symbol, &transcoder_mode_names[2])]!
139}
140
141@[heap]
142struct TranscoderSymbols {
143mut:
144 eol [7]Value
145 mode [3]Value
146}
147
148fn (mut vm VM) transcoder_symbols() &TranscoderSymbols {
149 mut state := vm.port_state()
150 if isnil(state.transcoder_symbols) {
151 state.transcoder_symbols = &transcoder_symbol_table
152 }
153 return state.transcoder_symbols
154}
155