parser.pegjs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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:jsSequence 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. jsSequence = jsStatement
  155. block = '[' params:wsBlockParamList? sequence:wsSequenceWs ']' {
  156. return $globals.BlockNode._new()
  157. ._location_(location())
  158. ._source_(text())
  159. ._parameters_(params || [])
  160. ._dagChildren_([sequence._asBlockSequenceNode()]);
  161. }
  162. operand = literal / reference / subexpression
  163. wsUnaryMessage = ws selector:unarySelector !':' {
  164. return $globals.SendNode._new()
  165. ._location_(location())
  166. ._source_(text())
  167. ._selector_(selector);
  168. }
  169. unarySend = receiver:operand tail:wsUnaryMessage* {
  170. return receiver._withTail_(tail);
  171. }
  172. wsBinaryMessage = ws selector:binarySelector ws arg:unarySend {
  173. return $globals.SendNode._new()
  174. ._location_(location())
  175. ._source_(text())
  176. ._selector_(selector)
  177. ._arguments_([arg]);
  178. }
  179. binarySend = receiver:unarySend tail:wsBinaryMessage* {
  180. return receiver._withTail_(tail);
  181. }
  182. wsKeywordMessage =
  183. pairs:(ws key:keyword ws arg:binarySend {return {key:key, arg:arg};})+ {
  184. var selector = '';
  185. var args = [];
  186. for(var i = 0; i < pairs.length; i++) {
  187. selector += pairs[i].key;
  188. args.push(pairs[i].arg);
  189. }
  190. return $globals.SendNode._new()
  191. ._location_(location())
  192. ._source_(text())
  193. ._selector_(selector)
  194. ._arguments_(args);
  195. }
  196. keywordSend = receiver:binarySend tail:wsKeywordMessage? {
  197. return tail ? receiver._withTail_([tail]) : receiver;
  198. }
  199. wsMessage = wsBinaryMessage / wsUnaryMessage / wsKeywordMessage
  200. cascade =
  201. send:keywordSend & {return send._isSendNode();}
  202. messages:(ws ';' mess:wsMessage {return mess;})+ {
  203. messages.unshift(send);
  204. return $globals.CascadeNode._new()
  205. ._location_(location())
  206. ._source_(text())
  207. ._dagChildren_(messages);
  208. }
  209. jsStatement = '<' ws 'inlineJS:' ws val:rawString ws '>' {
  210. return $globals.JSStatementNode._new()
  211. ._location_(location())
  212. ._source_(val)
  213. }
  214. method =
  215. pattern:(wsKeywordPattern / wsBinaryPattern / wsUnaryPattern)
  216. sequence:wsSequenceWs {
  217. return $globals.MethodNode._new()
  218. ._location_(location())
  219. ._source_(text())
  220. ._selector_(pattern[0])
  221. ._arguments_(pattern[1])
  222. ._dagChildren_([sequence]);
  223. }
  224. associationSend =
  225. send:binarySend
  226. & { return send._isSendNode() && send._selector() === '->' } {
  227. return [send._receiver(), send._arguments()[0]];
  228. }
  229. wsAssociationsRest = someDotsWs expression:associationSend {
  230. return expression;
  231. }
  232. wsAssociations = maybeDotsWs first:associationSend others:wsAssociationsRest* {
  233. return first.concat.apply(first, others);
  234. }