parser.pegjs 11 KB

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