parser.pegjs 8.9 KB

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