parser.pegjs 10 KB

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