|
| 1 | + |
| 2 | +let lsts-parse-lhs-list(tokens: List<Token>): Tuple<AST,List<Token>> = ( |
| 3 | + let base = mk-eof(); |
| 4 | + lsts-parse-expect(c"[", tokens); |
| 5 | + let loc = head(tokens).location; |
| 6 | + tokens = tail(tokens); |
| 7 | + if lsts-parse-head(tokens) == c"]" { |
| 8 | + base = Lit ( c"MACRO::LHS-LIST-SENTINEL", with-location(mk-token("MACRO::LHS-LIST-SENTINEL"),loc) ); |
| 9 | + base = mk-app(base,base); # TODO FIX MACRO, this is for compatibility with macros 1.0 |
| 10 | + } else { |
| 11 | + let base-rest = lsts-parse-lhs-big(tokens); |
| 12 | + base = base-rest.first; |
| 13 | + tokens = base-rest.second; |
| 14 | + let bases = [base]; |
| 15 | + while non-zero(tokens) and lsts-parse-head(tokens)==c"." { |
| 16 | + lsts-parse-expect(c".", tokens); tokens = tail(tokens); |
| 17 | + lsts-parse-expect(c".", tokens); tokens = tail(tokens); |
| 18 | + if lsts-parse-head(tokens)==c"]" { |
| 19 | + let in-base = Lit ( c"MACRO::LHS-LIST-SENTINEL", with-location(mk-token("MACRO::LHS-LIST-SENTINEL"),loc) ); |
| 20 | + in-base = mk-app(in-base,in-base); # TODO FIX MACRO, this is for compatibility with macros 1.0 |
| 21 | + bases = cons( in-base, bases ); |
| 22 | + } else { |
| 23 | + let next-rest = lsts-parse-lhs-big(tokens); |
| 24 | + let next = next-rest.first; |
| 25 | + tokens = next-rest.second; |
| 26 | + bases = cons( next, bases ); |
| 27 | + } |
| 28 | + }; |
| 29 | + base = head(bases); bases = tail(bases); |
| 30 | + for b in bases { |
| 31 | + base = mk-app( |
| 32 | + Var( c"macro::lhs-head", with-location(mk-token("macro::lhs-head"),loc) ), |
| 33 | + mk-cons(b, base) |
| 34 | + ); |
| 35 | + } |
| 36 | + }; |
| 37 | + lsts-parse-expect(c"]", tokens); tokens = tail(tokens); |
| 38 | + Tuple ( base, tokens ) |
| 39 | +); |
| 40 | + |
| 41 | +let lsts-parse-lhs-big(tokens: List<Token>): Tuple<AST,List<Token>> = ( |
| 42 | + (let base, tokens) = lsts-parse-lhs-one(tokens); |
| 43 | + if lsts-parse-head(tokens) == c"=" { |
| 44 | + let loc = head(tokens).location; |
| 45 | + lsts-parse-expect(c"=", tokens); tokens = tail(tokens); |
| 46 | + if base.is-var then base = mk-app( mk-var(c"macro::let-binding").with-location(loc), base ); |
| 47 | + (let val, tokens) = lsts-parse-lhs-one(tokens); |
| 48 | + base = mk-app( |
| 49 | + mk-var(c"macro::lhs-bind").with-location(loc), |
| 50 | + mk-cons(base, val) |
| 51 | + ); |
| 52 | + }; |
| 53 | + (base, tokens) |
| 54 | +); |
| 55 | + |
| 56 | +let lsts-parse-lhs(tokens: List<Token>): Tuple<AST,List<Token>> = ( |
| 57 | + (let base, tokens) = lsts-parse-lhs-one(tokens); |
| 58 | + |
| 59 | + while non-zero(tokens) and (lsts-parse-head(tokens)==c"." or lsts-parse-head(tokens)==c"[") { |
| 60 | + match tokens { |
| 61 | + [Token{key:c"."}.. Token{key:c"."}.. rest] => ( |
| 62 | + let loc = head(tokens).location; |
| 63 | + lsts-parse-expect(c".", tokens); tokens = tail(tokens); |
| 64 | + lsts-parse-expect(c".", tokens); tokens = tail(tokens); |
| 65 | + let next-rest = lsts-parse-lhs-one(tokens); |
| 66 | + let next = next-rest.first; |
| 67 | + tokens = next-rest.second; |
| 68 | + base = mk-app( |
| 69 | + Var( c"macro::lhs-prefix-or-suffix", with-location(mk-token("macro::lhs-prefix-or-suffix"),loc) ), |
| 70 | + mk-cons(base, next) |
| 71 | + ); |
| 72 | + ); |
| 73 | + [Token{key:c"."}.. rest] => ( |
| 74 | + let loc = head(tokens).location; |
| 75 | + lsts-parse-expect(c".", tokens); tokens = tail(tokens); |
| 76 | + (let field-name, tokens) = lsts-parse-identifier(tokens); |
| 77 | + base = mk-app( |
| 78 | + Var( c"macro::lhs-field", with-location(mk-token("macro::lhs-field"),loc) ), |
| 79 | + mk-cons(base, mk-var(field-name)) |
| 80 | + ); |
| 81 | + ); |
| 82 | + [Token{key:c"["}.. rest] => ( |
| 83 | + let loc = head(tokens).location; |
| 84 | + lsts-parse-expect(c"[", tokens); tokens = tail(tokens); |
| 85 | + let next-rest = lsts-parse-expression(tokens); |
| 86 | + let next = next-rest.first; |
| 87 | + tokens = next-rest.second; |
| 88 | + lsts-parse-expect(c"]", tokens); tokens = tail(tokens); |
| 89 | + base = mk-app( |
| 90 | + Var( c"macro::lhs-index", with-location(mk-token("macro::lhs-index"),loc) ), |
| 91 | + mk-cons(base, next) |
| 92 | + ); |
| 93 | + ); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + (base, tokens) |
| 98 | +); |
| 99 | + |
| 100 | +let lsts-parse-lhs-one(tokens: List<Token>): Tuple<AST,List<Token>> = ( |
| 101 | + let base = match tokens { |
| 102 | + [ Token{key:c"["}.. _ ] => ( |
| 103 | + (let l, tokens) = lsts-parse-lhs-list(tokens); |
| 104 | + l |
| 105 | + ); |
| 106 | + [ Token{key:c"("}.. _] => ( |
| 107 | + let loc = head(tokens).location; |
| 108 | + lsts-parse-expect(c"(", tokens); tokens = tail(tokens); |
| 109 | + (let base, tokens) = lsts-parse-lhs-big(tokens); |
| 110 | + let bases = mk-vector(type(AST)).push(base); |
| 111 | + while lsts-parse-head(tokens)==c"," { |
| 112 | + lsts-parse-expect(c",", tokens); tokens = tail(tokens); |
| 113 | + (base, tokens) = lsts-parse-lhs-big(tokens); |
| 114 | + bases = bases.push(base); |
| 115 | + }; |
| 116 | + lsts-parse-expect(c")", tokens); tokens = tail(tokens); |
| 117 | + if bases.length==1 then bases[0] |
| 118 | + else { |
| 119 | + base = mk-nil(); |
| 120 | + let bi = 0_sz; |
| 121 | + while bi < bases.length { |
| 122 | + let f = match bi { |
| 123 | + 0 => c".first"; |
| 124 | + 1 => c".second"; |
| 125 | + 2 => c".third"; |
| 126 | + 3 => c".fourth"; |
| 127 | + }; |
| 128 | + let field-base = mk-app( |
| 129 | + mk-var(c"macro::lhs-fields-field").with-location(loc), |
| 130 | + mk-cons( |
| 131 | + mk-var(f).with-location(loc), |
| 132 | + mk-app( |
| 133 | + mk-var(c"macro::lhs-bind").with-location(loc), |
| 134 | + mk-cons( |
| 135 | + bases[bi], |
| 136 | + mk-var(c"_").with-location(loc) |
| 137 | + ) |
| 138 | + ) |
| 139 | + ) |
| 140 | + ); |
| 141 | + base = mk-app( |
| 142 | + mk-var(c"macro::lhs-fields").with-location(loc), |
| 143 | + mk-cons(base, field-base) |
| 144 | + ); |
| 145 | + bi = bi + 1; |
| 146 | + }; |
| 147 | + mk-app( |
| 148 | + mk-var(c"macro::lhs-tagged").with-location(loc), |
| 149 | + mk-cons( |
| 150 | + mk-lit(c"Tuple").with-location(loc), |
| 151 | + base |
| 152 | + ) |
| 153 | + ) |
| 154 | + }; |
| 155 | + ); |
| 156 | + [ Token{key:c"let"}.. _] => ( |
| 157 | + let loc = head(tokens).location; |
| 158 | + lsts-parse-expect(c"let", tokens); tokens = tail(tokens); |
| 159 | + (let field, tokens) = lsts-parse-lhs-one(tokens); |
| 160 | + mk-app( |
| 161 | + mk-var(c"macro::let-binding").with-location(loc), |
| 162 | + field |
| 163 | + ).with-location(loc); |
| 164 | + ); |
| 165 | + [ Token{key:c"set"}.. _] => ( |
| 166 | + let loc = head(tokens).location; |
| 167 | + lsts-parse-expect(c"set", tokens); tokens = tail(tokens); |
| 168 | + (let field, tokens) = lsts-parse-lhs-one(tokens); |
| 169 | + mk-app( |
| 170 | + mk-var(c"macro::set-binding").with-location(loc), |
| 171 | + field |
| 172 | + ).with-location(loc); |
| 173 | + ); |
| 174 | + _ => ( |
| 175 | + if lsts-parse-head(tokens)==c"uuid" or lsts-is-ident-head(lsts-parse-head(tokens)) { |
| 176 | + (let id1, tokens) = lsts-parse-lhs-identifier(tokens); |
| 177 | + id1 |
| 178 | + } else if lsts-parse-head(tokens)==c"&" { |
| 179 | + let amp = head(tokens); tokens = tail(tokens); |
| 180 | + (let lhs, tokens) = lsts-parse-lhs-one(tokens); |
| 181 | + mk-cons( mk-var(c"&"), lhs ) |
| 182 | + } else if lsts-is-lit(lsts-parse-head(tokens)) and (not(non-zero(tokens)) or lsts-parse-head(tail(tokens))!=c"{") { |
| 183 | + (let l, tokens) = lsts-parse-lit(tokens); |
| 184 | + l |
| 185 | + # TODO, make tag optional here |
| 186 | + } else if non-zero(tokens) and lsts-parse-head(tail(tokens))==c"{" { |
| 187 | + let loc = head(tokens).location; |
| 188 | + let tag = lsts-parse-head(tokens); tokens = tail(tokens); |
| 189 | + lsts-parse-expect(c"{", tokens); tokens = tail(tokens); |
| 190 | + let des-args = mk-nil(); |
| 191 | + let early-tokens = tokens; |
| 192 | + while non-zero(tokens) and lsts-parse-head(tokens)!=c"}" and (is(tokens,early-tokens) or lsts-parse-head(tokens)==c",") { |
| 193 | + if lsts-parse-head(tokens) == c"," { |
| 194 | + lsts-parse-expect(c",", tokens); tokens = tail(tokens); |
| 195 | + }; |
| 196 | + let attr-loc = head(tokens).location; |
| 197 | + let binding = c""; |
| 198 | + let attr-key = c""; |
| 199 | + let raw = false; |
| 200 | + if lsts-parse-head(tokens) == c"raw" { |
| 201 | + raw = true; tokens = tail(tokens); |
| 202 | + }; |
| 203 | + if lsts-is-ident-head(lsts-parse-head(tokens)) { |
| 204 | + attr-key = lsts-parse-head(tokens); tokens = tail(tokens); |
| 205 | + }; |
| 206 | + let is-let = true; |
| 207 | + if lsts-parse-head(tokens)==c"=" { |
| 208 | + binding = attr-key; tokens = tail(tokens); |
| 209 | + if lsts-parse-head(tokens) == c"set" { |
| 210 | + is-let = false; tokens = tail(tokens); |
| 211 | + }; |
| 212 | + if lsts-parse-head(tokens) == c"raw" { |
| 213 | + raw = true; tokens = tail(tokens); |
| 214 | + }; |
| 215 | + if lsts-is-ident-head(lsts-parse-head(tokens)) { |
| 216 | + attr-key = lsts-parse-head(tokens); tokens = tail(tokens); |
| 217 | + } else { lsts-parse-expect(c"[Identifier]", tokens); }; |
| 218 | + }; |
| 219 | + let val = if lsts-parse-head(tokens)==c":" { |
| 220 | + tokens = tail(tokens); |
| 221 | + (let val, tokens) = lsts-parse-lhs-one(tokens); |
| 222 | + val |
| 223 | + } else { mk-eof() }; |
| 224 | + if not(non-zero(val)) and not(non-zero(binding)) and not(non-zero(attr-key)) { |
| 225 | + lsts-parse-expect(c"[Struct LHS]", tokens); tokens = tail(tokens); |
| 226 | + }; |
| 227 | + if not(non-zero(val)) { |
| 228 | + val = Var( c"_", with-location(mk-token("_"),attr-loc) ); |
| 229 | + }; |
| 230 | + if non-zero(binding) { |
| 231 | + let macro-binding = if is-let |
| 232 | + then mk-var(c"macro::let-binding").with-location(attr-loc); |
| 233 | + else mk-var(c"macro::set-binding").with-location(attr-loc); |
| 234 | + val = mk-app( |
| 235 | + Var( c"macro::lhs-bind", with-location(mk-token("macro::lhs-bind"),attr-loc) ), |
| 236 | + mk-cons( |
| 237 | + mk-app( macro-binding, mk-var(binding).with-location(attr-loc) ), |
| 238 | + val |
| 239 | + ) |
| 240 | + ); |
| 241 | + if raw { |
| 242 | + val = mk-app( |
| 243 | + Var( c"macro::lhs-bind-raw", with-location(mk-token("macro::lhs-bind-raw"),attr-loc) ), |
| 244 | + val |
| 245 | + ); |
| 246 | + }; |
| 247 | + }; |
| 248 | + if non-zero(attr-key) { |
| 249 | + attr-key = c"." + attr-key; |
| 250 | + val = mk-app( |
| 251 | + Var( c"macro::lhs-fields-field", with-location(mk-token("macro::lhs-fields-field"),attr-loc) ), |
| 252 | + mk-cons( |
| 253 | + Var( attr-key, with-location(mk-token(attr-key),attr-loc) ), |
| 254 | + val |
| 255 | + ) |
| 256 | + ); |
| 257 | + }; |
| 258 | + des-args = mk-app( |
| 259 | + mk-var(c"macro::lhs-fields").with-location(loc), |
| 260 | + mk-cons( |
| 261 | + des-args, |
| 262 | + val |
| 263 | + ) |
| 264 | + ); |
| 265 | + }; |
| 266 | + if not(non-zero(des-args)) { des-args = mk-nil(); }; |
| 267 | + lsts-parse-expect(c"}", tokens); tokens = tail(tokens); |
| 268 | + let lhs-tag = if tag!=c"_" |
| 269 | + then mk-lit(tag).with-location(loc) |
| 270 | + else mk-nil(); |
| 271 | + mk-app( |
| 272 | + mk-var(c"macro::lhs-tagged").with-location(loc), |
| 273 | + mk-cons( |
| 274 | + lhs-tag, |
| 275 | + des-args |
| 276 | + ) |
| 277 | + ); |
| 278 | + } else { |
| 279 | + lsts-parse-expect(c"[LHS]", tokens); |
| 280 | + mk-eof(); |
| 281 | + } |
| 282 | + ); |
| 283 | + }; |
| 284 | + |
| 285 | + ( base, tokens ) |
| 286 | +); |
| 287 | + |
| 288 | +let lsts-parse-assign(tokens: List<Token>): Tuple<AST,List<Token>> = ( |
| 289 | + let base = mk-eof(); |
| 290 | + if lsts-has-assign(tokens) and lsts-parse-head(tokens)!=c"for" and lsts-parse-head(tokens)!=c"while" and lsts-parse-head(tokens)!=c"if" { |
| 291 | + let loc = head(tokens).location; |
| 292 | + (let lhs, tokens) = lsts-parse-lhs(tokens); |
| 293 | + lsts-parse-expect(c"=", tokens); tokens = tail(tokens); |
| 294 | + (let rhs, tokens) = lsts-parse-assign(tokens); |
| 295 | + base = mk-app( |
| 296 | + mk-var(c"macro::assign").with-location(loc), |
| 297 | + mk-cons(lhs, rhs).with-location(loc) |
| 298 | + ); |
| 299 | + } else { |
| 300 | + (base, tokens) = lsts-parse-ascript(tokens); |
| 301 | + }; |
| 302 | + ( base, tokens ) |
| 303 | +); |
| 304 | + |
| 305 | +let lsts-parse-lhs-identifier(tokens: List<Token>): (AST,List<Token>) = ( |
| 306 | + let loc = if non-zero(tokens) then head(tokens).location else mk-location(); |
| 307 | + if lsts-parse-head(tokens)==c"uuid" { |
| 308 | + lsts-parse-expect(c"uuid", tokens); tokens = tail(tokens); |
| 309 | + lsts-parse-expect(c"(", tokens); tokens = tail(tokens); |
| 310 | + (let id2, tokens) = lsts-parse-identifier(tokens); |
| 311 | + lsts-parse-expect(c")", tokens); tokens = tail(tokens); |
| 312 | + (mk-app( |
| 313 | + mk-var(c"uuid").with-location(loc), |
| 314 | + mk-var(id2).with-location(loc) |
| 315 | + ), tokens) |
| 316 | + } else { |
| 317 | + (let id3, tokens) = lsts-parse-identifier(tokens); |
| 318 | + (mk-var(id3).with-location(loc), tokens) |
| 319 | + } |
| 320 | +); |
0 commit comments