parser.pegjs 9.2 KB

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