2
0

parser.pegjs 7.3 KB

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