raku.peg 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. PROGRAM ← STATEMENTS
  2. STATEMENTS ← ((COMMAND COMMENT? / COMMENT) EOX)+
  3. ~COMMENT ← ("#" / "comment" / "remark" / "rem") (!([\r\n]+).)*
  4. COMMAND ← WORD (ARGUMENTS)?
  5. EKEYWORD ← "calculate" / "calc" / "expression" / "expr"
  6. PKEYWORD ← "evaluate" / "eval" / "perform"
  7. XKEYWORD ← "do" / "end"
  8. BLOCK ← "do" EOX? STATEMENTS? "end" / "{" EOX? STATEMENTS? "}"
  9. PBLOCK ← PKEYWORD EOX? STATEMENTS? "end" / "[" EOX? STATEMENTS? "]"
  10. ARGUMENTS ← (ARGSEP? (BLOCK / PBLOCK / ARGUMENT))*
  11. # ARGUMENTS ← PREPOSITION* ARTICLE* ARGUMENT ((ARGSEP? BLOCK) /
  12. # (ARGSEP? PBLOCK) / (ARGSEP? ARGUMENT))*
  13. ARGSEP ← ',' / ((PREPOSITION / ARTICLE)+)
  14. PREPOSITION ← < ( "else" / "otherwise" /
  15. "aboard" / "about" / "above" / "across" / "after" / "against" /
  16. "alongside"/ "along" / "amidst" / "amidst" / "among" / "around" /
  17. "as" / "at" / "atop" / "before" / "behind"/ "below" /
  18. "beneath" / "besides" / "beside" / "between" / "beyond"/ "but" /
  19. "by" / "circa" / "despite" / "down" / "during"/ "except" /
  20. "for" / "from" / "inside" / "into" / "in" / "less" /
  21. "like" / "near" / "nothwithstanding" / "off" / "onto" /
  22. "on" / "opposite" / "outside" / "out" / "over" / "since" /
  23. "than" / "through" / "thru" / "towards" / "throughout" / "to" /
  24. "underneath"/ "under" / "unlike" / "until" / "upon" / "upside" /
  25. "up" / "versus" / "via" / "within" / "without" / "with" ) > WHITESPACE
  26. ~ARTICLE ← <("a" / "an" / "the")> WHITESPACE
  27. ARGUMENT ← EXPR
  28. # / NUMBER
  29. EXPR ← BINEXPR / UNEXPR
  30. BINEXPR ← (ATOM (BINOP ATOM)*)
  31. UNEXPR ← (UNOP ATOM)
  32. ATOM ← STRING / NAME / NUMBER / '(' EXPR ')' / EKEYWORD EXPR "end"
  33. KWUNOP ← "not" / "negate"
  34. KWBINOP ← "plus" / "minus" / "times" / "divide" / "of" / "'s" / "'" / "and" / "or" / "equals" / "equal" / "is"
  35. BINOP ← < [-+/*] / "<-" / "->" "&&"/ "||" / "==" / KWBINOP >
  36. UNOP ← < [-!] / KWUNOP >
  37. # / "of" / "'s"
  38. STRING ← ESCSTR / RAWSTR
  39. # Raw string macro
  40. RAWSM(O,C) ← O < (!(C).)* > C
  41. NAME ← ARTICLE? WORD+
  42. # Escaped string macro.
  43. ESCSM(O,C) ← O < (!(C)STRCHAR)* > C
  44. ESCSTR ← ESCSM("`", "`") / ESCSM('"', '"')
  45. RAWSTR ← RAWSM('<<', '>>') / RAWSM('«', '»') / RAWSM('‹', '›') / RAWSM('“', '”')
  46. STRESC1 ← "\\" [nrtfv\'\\"\[\]\\]
  47. STRESC2 ← "\\" [0-3] [0-7] [0-7]
  48. STRESC3 ← "\\" [0-7] [0-7]*
  49. STRESC4 ← "\\x" [0-9a-fA-F] [0-9a-fA-F]?
  50. STRESC5 ← "\\u" [0-9a-fA-F]+
  51. STRNOESC ← (!('\\\\').)
  52. STRCHAR ← STRESC1 / STRESC2 / STRESC3 / STRESC4 / STRESC5 / STRNOESC
  53. NUMBER ← DECIMAL / INTEGER
  54. DECIMAL ← < [-]?[0-9]+[.][0-9]+ >
  55. INTEGER ← < [-]?[0-9]+ >
  56. ~EOX ← ([ \t]*[\r\n.]+[ \t]*)+
  57. KEYWORD ← PREPOSITION / ARTICLE / XKEYWORD / EKEYWORD / KWBINOP / KWUNOP
  58. WORD ← !KEYWORD ANYWORD
  59. ANYWORD ← < [a-zA-Z][a-zA-Z0-9]* >
  60. WHITESPACE ← < [ \t]* >
  61. %whitespace ← [ \t]*
  62. %word ← [a-zA-Z][-+/*a-zA-Z0-9]*
  63. ---
  64. # Expression parsing option
  65. %expr = BINEXPR # Rule to apply 'precedence climbing method' to
  66. %binop = L && || and or
  67. %binop = L == equals equal is
  68. %binop = L + - plus minus
  69. %binop = L * / times divide
  70. %binop = L of 's <- -> '
  71. # foo bar baz quux