parser.pegjs~ 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. start = method
  2. separator = [ \t\v\f\u00A0\uFEFF\n\r\u2028\u2029]+
  3. comments = (["][^"]*["])+
  4. ws = (separator / comments)*
  5. identifier = first:[a-z] others:[a-zA-Z0-9]* {return first + others.join("")}
  6. keyword = first:identifier last:[:] {return first + last}
  7. className = first:[A-Z] others:[a-zA-Z0-9]* {return first + others.join("")}
  8. string = ['] val:(("''" {return "'"} / [^'])*) ['] {
  9. return smalltalk.ValueNode._new()
  10. ._value_(val.join("").replace(/\"/ig, '"'))
  11. }
  12. symbol = "#"val:[a-zA-Z0-9]* {
  13. return smalltalk.ValueNode._new()
  14. ._value_(val.join("").replace(/\"/ig, '"'))
  15. }
  16. number = n:(float / integer) {
  17. return smalltalk.ValueNode._new()
  18. ._value_(n)
  19. }
  20. float = neg:[-]?int:integer "." dec:integer {return parseFloat((neg+int+"."+dec), 10)}
  21. integer = neg:[-]?digits:[0-9]+ {return (parseInt(neg+digits.joind(""), 10))}
  22. literalArray = "#(" ws lits:(lit:literal ws {return lit._value()})* ws ")" {
  23. return smalltalk.ValueNode._new()
  24. ._value_(lits)
  25. }
  26. literal = number / literalArray / string / symbol / block
  27. variable = identifier:identifier {
  28. return smalltalk.VariableNode._new()
  29. ._value_(identifier)
  30. }
  31. classReference = className:className {
  32. return smalltalk.ClassReferenceNode._new()
  33. ._value_(className)
  34. }
  35. reference = variable / classReference
  36. keywordPair = key:keyword ws arg:binarySend ws {return {key:key, arg: arg}}
  37. binarySelector = bin:[+*/=><,@%~|&-]+ {return bin.join("")}
  38. unarySelector = identifier
  39. keywordPattern = pairs:(ws key:keyword ws arg:identifier {return {key:key, arg: arg}})+ {
  40. var keywords = [];
  41. var params = [];
  42. for(var i=0;i<pairs.length;i++){
  43. keywords.push(pairs[i].key);
  44. }
  45. for(var i=0;i<pairs.length;i++){
  46. params.push(pairs[i].arg);
  47. }
  48. return [keywords.join(""), params]
  49. }
  50. binaryPattern = ws selector:binarySelector ws arg:identifier {return [selector, [arg]]}
  51. unaryPattern = ws selector:unarySelector {return [selector, []]}
  52. expression = assignment / cascade / keywordSend / binarySend / jsStatement
  53. expressionList = ws "." ws expression:expression {return expression}
  54. expressions = first:expression others:expressionList* {
  55. var result = [first];
  56. for(var i=0;i<others.length;i++) {
  57. result.push(others[i]);
  58. }
  59. return result;
  60. }
  61. assignment = variable:variable ws ':=' ws expression:expression {
  62. return smalltalk.AssignmentNode._new()
  63. ._left_(variable)
  64. ._right_(expression)
  65. }
  66. ret = '^' ws expression:expression ws '.'? {
  67. return smalltalk.ReturnNode._new()
  68. ._nodes_([expression])
  69. }
  70. temps = "|" vars:(ws variable:identifier ws {return variable})* "|" {return vars}
  71. blockParamList = params:((ws ":" ws param:identifier {return param})+) ws "|" {return params}
  72. subexpression = '(' ws expression:expression ws ')' {return expression}
  73. statements = ret:ret [.]* {return [ret]}
  74. / exps:expressions ws [.]+ ws ret:ret [.]* {
  75. var expressions = exps;
  76. expressions.push(ret);
  77. return expressions
  78. }
  79. / expressions:expressions? [.]* {
  80. return expressions || []
  81. }
  82. sequence = temps:temps? ws statements:statements? ws {
  83. return smalltalk.SequenceNode._new()
  84. ._temps_(temps || [])
  85. ._nodes_(statements || [])
  86. }
  87. block = '[' ws params:blockParamList? ws sequence:sequence? ws ']' {
  88. return smalltalk.BlockNode._new()
  89. ._parameters_(params || [])
  90. ._nodes_([sequence._asBlockSequenceNode()])
  91. }
  92. operand = literal / reference / subexpression
  93. unaryMessage = ws selector:unarySelector ![:] {
  94. return smalltalk.SendNode._new()
  95. ._selector_(selector)
  96. }
  97. unaryTail = message:unaryMessage ws tail:unaryTail? ws {
  98. if(tail) {
  99. return tail._valueForReceiver_(message);
  100. }
  101. else {
  102. return message;
  103. }
  104. }
  105. unarySend = receiver:operand ws tail:unaryTail? {
  106. if(tail) {
  107. return tail._valueForReceiver_(receiver);
  108. }
  109. else {
  110. return receiver;
  111. }
  112. }
  113. binaryMessage = ws selector:binarySelector ws arg:(unarySend / operand) {
  114. return smalltalk.SendNode._new()
  115. ._selector_(selector)
  116. ._arguments_([arg])
  117. }
  118. binaryTail = message:binaryMessage tail:binaryTail? {
  119. if(tail) {
  120. return tail._valueForReceiver_(message);
  121. }
  122. else {
  123. return message;
  124. }
  125. }
  126. binarySend = receiver:unarySend tail:binaryTail? {
  127. if(tail) {
  128. return tail._valueForReceiver_(receiver);
  129. }
  130. else {
  131. return receiver;
  132. }
  133. }
  134. keywordMessage = ws pairs:(pair:keywordPair ws {return pair})+ {
  135. var selector = [];
  136. var args = [];
  137. for(var i=0;i<pairs.length;i++) {
  138. selector.push(pairs[i].key);
  139. args.push(pairs[i].arg);
  140. }
  141. return smalltalk.SendNode._new()
  142. ._selector_(selector.join(""))
  143. ._arguments_(args)
  144. }
  145. keywordSend = receiver:binarySend tail:keywordMessage {
  146. return tail._valueForReceiver_(receiver);
  147. }
  148. message = binaryMessage / unaryMessage / keywordMessage
  149. cascade = ws send:(keywordSend / binarySend) messages:(ws ";" ws mess:message ws {return mess})+
  150. {
  151. var cascade = [];
  152. cascade.push(send);
  153. for(var i=0;i<messages.length;i++) {
  154. cascade.push(messages[i]);
  155. }
  156. return smalltalk.CascadeNode._new()
  157. ._receiver_(send._receiver())
  158. ._nodes_(cascade)
  159. }
  160. jsStatement = "<" val:((">>" {return ">"} / [^>])*) ">"
  161. {
  162. return smalltalk.JSStatementNode._new()
  163. ._source_(val.join(""))
  164. }
  165. method = ws pattern:(keywordPattern / binaryPattern / unaryPattern) ws sequence:sequence? ws {
  166. return smalltalk.MethodNode._new()
  167. ._selector_(pattern[0])
  168. ._arguments_(pattern[1])
  169. ._nodes_([sequence])
  170. }