1
0

parser.pegjs 12 KB

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