| 1 | // Copyright (c) 2021 Lars Pontoppidan. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module ast |
| 5 | |
| 6 | import toml.token |
| 7 | import strconv |
| 8 | |
| 9 | // Key is a sumtype representing all types of keys that |
| 10 | // can be found in a TOML document. |
| 11 | pub type Key = Bare | Bool | Null | Number | Quoted |
| 12 | |
| 13 | // str returns the string representation of the key. This is implemented |
| 14 | // by all the variants of Key. |
| 15 | pub fn (k Key) str() string { |
| 16 | return k.text.clone() |
| 17 | } |
| 18 | |
| 19 | // Value is a sumtype representing all possible value types |
| 20 | // found in a TOML document. |
| 21 | pub type Value = Bool |
| 22 | | Date |
| 23 | | DateTime |
| 24 | | Null |
| 25 | | Number |
| 26 | | Quoted |
| 27 | | Time |
| 28 | | []Value |
| 29 | | map[string]Value |
| 30 | |
| 31 | // str outputs the value in JSON-like format for eased |
| 32 | // debugging |
| 33 | pub fn (v Value) str() string { |
| 34 | match v { |
| 35 | Quoted, Date, DateTime, Time { |
| 36 | return '"${v.text}"' |
| 37 | } |
| 38 | Bool, Null, Number { |
| 39 | return v.text |
| 40 | } |
| 41 | map[string]Value { |
| 42 | mut str := '{' |
| 43 | for key, val in v { |
| 44 | str += ' "${key}": ${val},' |
| 45 | } |
| 46 | str = str.trim_right(',') |
| 47 | str += ' }' |
| 48 | return str |
| 49 | } |
| 50 | []Value { |
| 51 | mut str := '[' |
| 52 | for val in v { |
| 53 | str += ' ${val},' |
| 54 | } |
| 55 | str = str.trim_right(',') |
| 56 | str += ' ]' |
| 57 | return str |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // DateTimeType is a sumtype representing all possible date types |
| 63 | // found in a TOML document. |
| 64 | pub type DateTimeType = Date | DateTime | Time |
| 65 | |
| 66 | // str returns the `string` representation of the `DateTimeType` type. |
| 67 | pub fn (dtt DateTimeType) str() string { |
| 68 | return dtt.text |
| 69 | } |
| 70 | |
| 71 | // Comment is the data representation of a TOML comment (`# This is a comment`). |
| 72 | pub struct Comment { |
| 73 | pub: |
| 74 | text string |
| 75 | pos token.Pos |
| 76 | } |
| 77 | |
| 78 | // str returns the `string` representation of the `Comment` type. |
| 79 | pub fn (c Comment) str() string { |
| 80 | mut s := typeof(c).name + '{\n' |
| 81 | s += ' text: \'${c.text}\'\n' |
| 82 | s += ' pos: ${c.pos}\n' |
| 83 | s += '}' |
| 84 | return s |
| 85 | } |
| 86 | |
| 87 | // Null is used in sumtype checks as a "default" value when nothing else is possible. |
| 88 | pub struct Null { |
| 89 | pub: |
| 90 | text string |
| 91 | pos token.Pos |
| 92 | } |
| 93 | |
| 94 | // str returns the `string` representation of the `Null` type |
| 95 | pub fn (n Null) str() string { |
| 96 | return n.text |
| 97 | } |
| 98 | |
| 99 | // Quoted is the data representation of a TOML quoted type (`"quoted-key" = "I'm a quoted value"`). |
| 100 | // Quoted types can appear both as keys and values in TOML documents. |
| 101 | pub struct Quoted { |
| 102 | pub mut: |
| 103 | text string |
| 104 | pub: |
| 105 | pos token.Pos |
| 106 | is_multiline bool |
| 107 | quote u8 |
| 108 | } |
| 109 | |
| 110 | // str returns the `string` representation of the `Quoted` type. |
| 111 | pub fn (q Quoted) str() string { |
| 112 | mut str := typeof(q).name + '{\n' |
| 113 | str += ' text: \'${q.text}\'\n' |
| 114 | str += ' pos: ${q.pos}\n' |
| 115 | str += ' is_multiline: ${q.is_multiline}\n' |
| 116 | str += ' quote: \'${q.quote}\'\n' |
| 117 | str += '}' |
| 118 | return str |
| 119 | } |
| 120 | |
| 121 | // Bare is the data representation of a TOML bare type (`bare_key = ...`). |
| 122 | // Bare types can appear only as keys in TOML documents. Otherwise they take the |
| 123 | // form of Bool or Numbers. |
| 124 | pub struct Bare { |
| 125 | pub: |
| 126 | text string |
| 127 | pos token.Pos |
| 128 | } |
| 129 | |
| 130 | // str returns the `string` representation of the `Bare` type. |
| 131 | pub fn (b Bare) str() string { |
| 132 | mut str := typeof(b).name + '{\n' |
| 133 | str += ' text: \'${b.text}\'\n' |
| 134 | str += ' pos: ${b.pos}\n' |
| 135 | str += '}' |
| 136 | return str |
| 137 | } |
| 138 | |
| 139 | // Bool is the data representation of a TOML boolean type (`... = true`). |
| 140 | // Bool types can appear only as values in TOML documents. Keys named `true` or `false` |
| 141 | // are considered as Bare types. |
| 142 | pub struct Bool { |
| 143 | pub: |
| 144 | text string |
| 145 | pos token.Pos |
| 146 | } |
| 147 | |
| 148 | // str returns the `string` representation of the `Bool` type. |
| 149 | pub fn (b Bool) str() string { |
| 150 | mut str := typeof(b).name + '{\n' |
| 151 | str += ' text: \'${b.text}\'\n' |
| 152 | str += ' pos: ${b.pos}\n' |
| 153 | str += '}' |
| 154 | return str |
| 155 | } |
| 156 | |
| 157 | // Number is the data representation of a TOML number type (`25 = 5e2`). |
| 158 | // Number types can appear both as keys and values in TOML documents. |
| 159 | // Number can be integers, floats, infinite, NaN - they can have exponents (`5e2`) and be sign prefixed (`+2`). |
| 160 | pub struct Number { |
| 161 | pub: |
| 162 | pos token.Pos |
| 163 | pub mut: |
| 164 | text string |
| 165 | } |
| 166 | |
| 167 | // str returns the `string` representation of the `Number` type. |
| 168 | pub fn (n Number) str() string { |
| 169 | mut str := typeof(n).name + '{\n' |
| 170 | str += ' text: \'${n.text}\'\n' |
| 171 | str += ' pos: ${n.pos}\n' |
| 172 | str += '}' |
| 173 | return str |
| 174 | } |
| 175 | |
| 176 | // i64 returns the `n Number` as an `i64` value. |
| 177 | pub fn (n Number) i64() i64 { |
| 178 | if n.text.starts_with('0x') { |
| 179 | hex := n.text.all_after('0x').to_upper().replace('_', '') |
| 180 | return strconv.parse_int(hex, 16, 64) or { i64(0) } |
| 181 | } else if n.text.starts_with('0o') { |
| 182 | oct := n.text.all_after('0o').replace('_', '') |
| 183 | return strconv.parse_int(oct, 8, 64) or { i64(0) } |
| 184 | } else if n.text.starts_with('0b') { |
| 185 | bin := n.text.all_after('0b').replace('_', '') |
| 186 | return strconv.parse_int(bin, 2, 64) or { i64(0) } |
| 187 | } |
| 188 | return strconv.parse_int(n.text, 0, 64) or { i64(0) } |
| 189 | } |
| 190 | |
| 191 | // f64 returns the `n Number` as an `f64` value. |
| 192 | pub fn (n Number) f64() f64 { |
| 193 | return n.text.replace('_', '').f64() |
| 194 | } |
| 195 | |
| 196 | // Date is the data representation of a TOML date type (`YYYY-MM-DD`). |
| 197 | // Date types can appear both as keys and values in TOML documents. |
| 198 | // Keys named like dates e.g. `1980-12-29` are considered Bare key types. |
| 199 | pub struct Date { |
| 200 | pub: |
| 201 | text string |
| 202 | pos token.Pos |
| 203 | } |
| 204 | |
| 205 | // str returns the `string` representation of the `Date` type. |
| 206 | pub fn (d Date) str() string { |
| 207 | mut str := typeof(d).name + '{\n' |
| 208 | str += ' text: \'${d.text}\'\n' |
| 209 | str += ' pos: ${d.pos}\n' |
| 210 | str += '}' |
| 211 | return str |
| 212 | } |
| 213 | |
| 214 | // Time is the data representation of a TOML time type (`HH:MM:SS.milli`). |
| 215 | // Time types can appear only as values in TOML documents. |
| 216 | pub struct Time { |
| 217 | pub: |
| 218 | text string |
| 219 | offset int |
| 220 | pos token.Pos |
| 221 | } |
| 222 | |
| 223 | // str returns the `string` representation of the `Time` type. |
| 224 | pub fn (t Time) str() string { |
| 225 | mut str := typeof(t).name + '{\n' |
| 226 | str += ' text: \'${t.text}\'\n' |
| 227 | str += ' offset: \'${t.offset}\'\n' |
| 228 | str += ' pos: ${t.pos}\n' |
| 229 | str += '}' |
| 230 | return str |
| 231 | } |
| 232 | |
| 233 | // DateTime is the data representation of a TOML date-time type (`YYYY-MM-DDTHH:MM:SS.milli`). |
| 234 | // DateTime types can appear only as values in TOML documents. |
| 235 | pub struct DateTime { |
| 236 | pub mut: |
| 237 | text string |
| 238 | pub: |
| 239 | pos token.Pos |
| 240 | date Date |
| 241 | time Time |
| 242 | } |
| 243 | |
| 244 | // str returns the `string` representation of the `DateTime` type. |
| 245 | pub fn (dt DateTime) str() string { |
| 246 | mut str := typeof(dt).name + '{\n' |
| 247 | str += ' text: \'${dt.text}\'\n' |
| 248 | str += ' date: \'${dt.date}\'\n' |
| 249 | str += ' time: \'${dt.time}\'\n' |
| 250 | str += ' pos: ${dt.pos}\n' |
| 251 | str += '}' |
| 252 | return str |
| 253 | } |
| 254 | |
| 255 | // EOF is the data representation of the end of the TOML document. |
| 256 | pub struct EOF { |
| 257 | pub: |
| 258 | pos token.Pos |
| 259 | } |
| 260 | |
| 261 | // str returns the `string` representation of the `EOF` type. |
| 262 | pub fn (e EOF) str() string { |
| 263 | mut str := typeof(e).name + '{\n' |
| 264 | str += ' pos: ${e.pos}\n' |
| 265 | str += '}' |
| 266 | return str |
| 267 | } |
| 268 | |