parser.pegjs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. start = method
  2. separator =
  3. [ \t\v\f\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF\n\r\u2028\u2029]+
  4. comments = ('"' [^"]* '"')+
  5. ws = (separator / comments)*
  6. maybeDotsWs = ('.' / separator / comments)*
  7. someDotsWs = ws '.' maybeDotsWs
  8. identifier = $([a-zA-Z] [a-zA-Z0-9]*)
  9. keyword = $(identifier ':')
  10. className = $([A-Z] [a-zA-Z0-9]*)
  11. string = val:rawString {
  12. return $globals.ValueNode._new()
  13. ._location_(location())
  14. ._source_(text())
  15. ._value_(val);
  16. }
  17. rawString = '\'' val:(('\'\'' {return '\'';} / [^'])*) '\'' {return val.join('');}
  18. character = '$' char:. {
  19. return $globals.ValueNode._new()
  20. ._location_(location())
  21. ._source_(text())
  22. ._value_(char);
  23. }
  24. symbol = '#' rest:bareSymbol {return rest;}
  25. bareSymbol = val:($(keyword+) / binarySelector / unarySelector / rawString) {
  26. return $globals.ValueNode._new()
  27. ._location_(location())
  28. ._source_(text())
  29. ._value_(val);
  30. }
  31. number = val:rawNumber {
  32. return $globals.ValueNode._new()
  33. ._location_(location())
  34. ._source_(text())
  35. ._value_(val);
  36. }
  37. rawNumber = numberExp / hex / float / integer
  38. numberExp = n:$((float / integer) 'e' integer) {return parseFloat(n);}
  39. hex = neg:'-'? '16r' num:$[0-9a-fA-F]+ {
  40. return parseInt(((neg || '') + num), 16);
  41. }
  42. float = n:$('-'? [0-9]+ '.' [0-9]+) {return parseFloat(n, 10);}
  43. integer = n:$('-'? [0-9]+) {return parseInt(n, 10);}
  44. literalArray = '#(' rest:wsLiteralArrayContents ws ')' {
  45. return rest
  46. ._location_(location())
  47. ._source_(text());
  48. }
  49. bareLiteralArray = '(' rest:wsLiteralArrayContents ws ')' {
  50. return rest
  51. ._location_(location())
  52. ._source_(text());
  53. }
  54. literalArrayElement = parseTimeLiteral / bareLiteralArray / bareSymbol
  55. wsLiteralArrayContents =
  56. lits:(ws lit:literalArrayElement {return lit._value();})* {
  57. return $globals.ValueNode._new()
  58. ._value_(lits);
  59. }
  60. dynamicArray = '{' expressions:wsExpressions? maybeDotsWs '}' {
  61. return $globals.DynamicArrayNode._new()
  62. ._location_(location())
  63. ._source_(text())
  64. ._dagChildren_(expressions || []);
  65. }
  66. dynamicDictionary = '#{' expressions:wsAssociations? maybeDotsWs '}' {
  67. return $globals.DynamicDictionaryNode._new()
  68. ._location_(location())
  69. ._source_(text())
  70. ._dagChildren_(expressions || []);
  71. }
  72. pseudoVariable = val:(
  73. 'true' {return true;} /
  74. 'false' {return false;} /
  75. 'nil' {return null;}
  76. ) {
  77. return $globals.ValueNode._new()
  78. ._location_(location())
  79. ._source_(text())
  80. ._value_(val);
  81. }
  82. parseTimeLiteral =
  83. pseudoVariable / number / literalArray / string / symbol / character
  84. runtimeLiteral = dynamicDictionary / dynamicArray / block
  85. literal = runtimeLiteral / parseTimeLiteral
  86. variable = identifier:identifier {
  87. return $globals.VariableNode._new()
  88. ._location_(location())
  89. ._source_(text())
  90. ._value_(identifier);
  91. }
  92. reference = variable
  93. binarySelector = $[\\+*/=><,@%~|&-]+
  94. unarySelector = identifier
  95. wsKeywordPattern =
  96. pairs:(ws key:keyword ws arg:identifier {return {key:key, arg:arg};})+ {
  97. var selector = '';
  98. var params = [];
  99. for(var i = 0; i < pairs.length; i++) {
  100. selector += pairs[i].key;
  101. params.push(pairs[i].arg);
  102. }
  103. return [selector, params];
  104. }
  105. wsBinaryPattern = ws selector:binarySelector ws arg:identifier {
  106. return [selector, [arg]];
  107. }
  108. wsUnaryPattern = ws selector:unarySelector {return [selector, []];}
  109. expression = assignment / cascade / keywordSend
  110. wsExpressionsRest = someDotsWs expression:expression {
  111. return expression;
  112. }
  113. wsExpressions = maybeDotsWs first:expression others:wsExpressionsRest* {
  114. return [first].concat(others);
  115. }
  116. assignment = variable:variable ws ':=' ws expression:expression {
  117. return $globals.AssignmentNode._new()
  118. ._location_(location())
  119. ._source_(text())
  120. ._left_(variable)
  121. ._right_(expression);
  122. }
  123. ret = '^' ws expression:expression {
  124. return $globals.ReturnNode._new()
  125. ._location_(location())
  126. ._source_(text())
  127. ._dagChildren_([expression]);
  128. }
  129. temps = '|' vars:(ws variable:identifier {return variable;})* ws '|' {
  130. return vars;
  131. }
  132. wsBlockParamList =
  133. params:((ws ':' ws param:identifier {return param;})+) ws '|' {
  134. return params;
  135. }
  136. subexpression = '(' ws expression:expression ws ')' {
  137. return expression;
  138. }
  139. wsStatements =
  140. maybeDotsWs ret:ret {return [ret];} /
  141. exps:wsExpressions someDotsWs ret:ret {
  142. var expressions = exps;
  143. expressions.push(ret);
  144. return expressions;
  145. } /
  146. expressions:wsExpressions? {return expressions || [];}
  147. wsSequenceWs = ws temps:temps? statements:(ws js:jsStatement ws {return [js];} / st:wsStatements maybeDotsWs {return st;})? {
  148. return $globals.SequenceNode._new()
  149. ._location_(location())
  150. ._source_(text())
  151. ._temps_(temps || [])
  152. ._dagChildren_(statements || []);
  153. }
  154. block = '[' params:wsBlockParamList? sequence:wsSequenceWs ']' {
  155. return $globals.BlockNode._new()
  156. ._location_(location())
  157. ._source_(text())
  158. ._parameters_(params || [])
  159. ._dagChildren_([sequence._asBlockSequenceNode()]);
  160. }
  161. operand = literal / reference / subexpression
  162. wsUnaryMessage = ws selector:unarySelector !':' {
  163. return $globals.SendNode._new()
  164. ._location_(location())
  165. ._source_(text())
  166. ._selector_(selector);
  167. }
  168. unarySend = receiver:operand tail:wsUnaryMessage* {
  169. return receiver._withTail_(tail);
  170. }
  171. wsBinaryMessage = ws selector:binarySelector ws arg:unarySend {
  172. return $globals.SendNode._new()
  173. ._location_(location())
  174. ._source_(text())
  175. ._selector_(selector)
  176. ._arguments_([arg]);
  177. }
  178. binarySend = receiver:unarySend tail:wsBinaryMessage* {
  179. return receiver._withTail_(tail);
  180. }
  181. wsKeywordMessage =
  182. pairs:(ws key:keyword ws arg:binarySend {return {key:key, arg:arg};})+ {
  183. var selector = '';
  184. var args = [];
  185. for(var i = 0; i < pairs.length; i++) {
  186. selector += pairs[i].key;
  187. args.push(pairs[i].arg);
  188. }
  189. return $globals.SendNode._new()
  190. ._location_(location())
  191. ._source_(text())
  192. ._selector_(selector)
  193. ._arguments_(args);
  194. }
  195. keywordSend = receiver:binarySend tail:wsKeywordMessage? {
  196. return tail ? receiver._withTail_([tail]) : receiver;
  197. }
  198. wsMessage = wsBinaryMessage / wsUnaryMessage / wsKeywordMessage
  199. cascade =
  200. send:keywordSend & {return send._isSendNode();}
  201. messages:(ws ';' mess:wsMessage {return mess;})+ {
  202. messages.unshift(send);
  203. return $globals.CascadeNode._new()
  204. ._location_(location())
  205. ._source_(text())
  206. ._dagChildren_(messages);
  207. }
  208. jsStatement = '<' ws 'inlineJS:' ws val:rawString ws '>' {
  209. return $globals.JSStatementNode._new()
  210. ._location_(location())
  211. ._source_(val)
  212. }
  213. method =
  214. pattern:(wsKeywordPattern / wsBinaryPattern / wsUnaryPattern)
  215. sequence:wsSequenceWs {
  216. return $globals.MethodNode._new()
  217. ._location_(location())
  218. ._source_(text())
  219. ._selector_(pattern[0])
  220. ._arguments_(pattern[1])
  221. ._dagChildren_([sequence]);
  222. }
  223. associationSend =
  224. send:binarySend
  225. & { return send._isSendNode() && send._selector() === '->' } {
  226. return [send._receiver(), send._arguments()[0]];
  227. }
  228. wsAssociationsRest = someDotsWs expression:associationSend {
  229. return expression;
  230. }
  231. wsAssociations = maybeDotsWs first:associationSend others:wsAssociationsRest* {
  232. return first.concat.apply(first, others);
  233. }