syntax.txt 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. we have long ago accepted the notion of a marked extended literal syntax,
  2. ~foo(lit,er,als) { ... a-random-string ... }
  3. but we are going to have a symmetrical *destructuring* syntax as well, "match":
  4. match ~foo(lit,er,als) {
  5. case (case1-a-random-string) { ... normal rust code ... }
  6. ...
  7. case (caseN-a-random-string) { ... normal rust code ... }
  8. default { ... }
  9. }
  10. this permits the syntax-expander to compile a complicated
  11. pattern-matching switch in some domain-specific syntax into an
  12. optimized automaton. this is *extremely common* (regexps, insn decode,
  13. AST-destructuring), almost as common as literals, and benefits real
  14. applications tremendously since recognizer-automaton performance is
  15. typically crucial and this lets you optimize aggressively in any case
  16. where the patterns are static and you can lay them down in code.
  17. example?
  18. match ~rx {
  19. case ((?=<word>\w+)) { print("found a word: " + word); }
  20. case ([:punct:]) { npuncts++; }
  21. }
  22. note that the rx-matcher is completely within its rights to introduce
  23. locals and make assignments: it just needs to embed the case arm
  24. inside a block that performs the local bindings.