subexes.txt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Substitution Expressions (subex)
  2. ================================
  3. A *subex* (*Substitution Expression*) represents an advanced string
  4. substitution. In contrast to a `regex`:idx: which deals with string analysis, a
  5. *subex* deals with string synthesis.
  6. Thanks to its conditional construct ``$[0|1|2|else]`` it supports
  7. `internationalization`:idx: of format string literals quite well.
  8. ===================== =====================================================
  9. Notation meaning
  10. ===================== =====================================================
  11. ``$#`` use first or next argument
  12. ``$name`` use named argument, you can wrap the named argument
  13. in curly braces (eg. ``${name}``) to separate it from
  14. the next characters.
  15. ``$$`` produces a single ``$``
  16. ``$1`` use first argument
  17. ``$-1`` use last argument
  18. ``${1..3}`` use arguments 1 to 3
  19. ``${..}`` use all arguments
  20. ``$*`` use all arguments (same as ``${..}``)
  21. ``${#..}`` use all remaining arguments
  22. ``${..-2}`` use all arguments except the last argument
  23. ``${$1}`` use argument X where ``X = parseInt(arg[1])``
  24. ``${$1..$2}`` use arguments X to Y where ``X = parseInt(arg[1])``
  25. and ``Y = parseInt(arg[2])``
  26. ``$','{1..3}`` use arguments 1 to 3 and join them with ','
  27. ``$','80c'\n'{..}`` use all arguments, join them with ','. Insert '\\n'
  28. before the resulting string exceeds 80 chars.
  29. ``$','8i'\n'{..}`` use all arguments, join them with ','. Insert '\\n'
  30. after every 8th item.
  31. ``$' '~{1..3}`` use arguments 1 to 3 with a leading space if the
  32. concatenation of ``1..3`` is not the empty string
  33. ``$[zero|one|def]1`` use ``X = parseInt(arg[1])`` to determine which
  34. branch to use. If ``X == 0`` the 'zero' branch is
  35. selected, if ``X == 1`` the 'one' branch is
  36. selected, etc. Otherwise the 'def' branch is
  37. selected. ``$x`` is interpreted in branches too.
  38. If a branch needs to contain ``|``, ``]`` put
  39. them in single quotes. To produce a verbatim single
  40. quote, use ``''``.
  41. ===================== =====================================================
  42. Examples
  43. ========
  44. .. code-block:: nim
  45. subex"$1($', '{2..})" % ["f", "a", "b", "c"] == "f(a, b, c)"
  46. subex"$1 $[files|file|files]{1} copied" % ["1"] == "1 file copied"
  47. subex"$['''|'|''''|']']#" % "0" == "'|"
  48. subex("type\n TEnum = enum\n $', '40c'\n '{..}") % [
  49. "fieldNameA", "fieldNameB", "fieldNameC", "fieldNameD"]