2
0

parser.pegjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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("")}
  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
  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 = jsSequence / stSequence
  103. stSequence = temps:temps? ws statements:statements? ws {
  104. return smalltalk.SequenceNode._new()
  105. ._temps_(temps || [])
  106. ._nodes_(statements || [])
  107. }
  108. jsSequence = jsStatement
  109. block = '[' ws params:blockParamList? ws sequence:sequence? ws ']' {
  110. return smalltalk.BlockNode._new()
  111. ._parameters_(params || [])
  112. ._nodes_([sequence._asBlockSequenceNode()])
  113. }
  114. operand = literal / reference / subexpression
  115. unaryMessage = ws selector:unarySelector ![:] {
  116. return smalltalk.SendNode._new()
  117. ._selector_(selector)
  118. }
  119. unaryTail = message:unaryMessage ws tail:unaryTail? ws {
  120. if(tail) {
  121. return tail._valueForReceiver_(message);
  122. }
  123. else {
  124. return message;
  125. }
  126. }
  127. unarySend = receiver:operand ws tail:unaryTail? {
  128. if(tail) {
  129. return tail._valueForReceiver_(receiver);
  130. }
  131. else {
  132. return receiver;
  133. }
  134. }
  135. binaryMessage = ws selector:binarySelector ws arg:(unarySend / operand) {
  136. return smalltalk.SendNode._new()
  137. ._selector_(selector)
  138. ._arguments_([arg])
  139. }
  140. binaryTail = message:binaryMessage tail:binaryTail? {
  141. if(tail) {
  142. return tail._valueForReceiver_(message);
  143. }
  144. else {
  145. return message;
  146. }
  147. }
  148. binarySend = receiver:unarySend tail:binaryTail? {
  149. if(tail) {
  150. return tail._valueForReceiver_(receiver);
  151. }
  152. else {
  153. return receiver;
  154. }
  155. }
  156. keywordMessage = ws pairs:(pair:keywordPair ws {return pair})+ {
  157. var selector = [];
  158. var args = [];
  159. for(var i=0;i<pairs.length;i++) {
  160. selector.push(pairs[i].key);
  161. args.push(pairs[i].arg);
  162. }
  163. return smalltalk.SendNode._new()
  164. ._selector_(selector.join(""))
  165. ._arguments_(args)
  166. }
  167. keywordSend = receiver:binarySend tail:keywordMessage {
  168. return tail._valueForReceiver_(receiver);
  169. }
  170. message = binaryMessage / unaryMessage / keywordMessage
  171. cascade = ws send:(keywordSend / binarySend) messages:(ws ";" ws mess:message ws {return mess})+ {
  172. var cascade = [];
  173. cascade.push(send);
  174. for(var i=0;i<messages.length;i++) {
  175. cascade.push(messages[i]);
  176. }
  177. return smalltalk.CascadeNode._new()
  178. ._receiver_(send._receiver())
  179. ._nodes_(cascade)
  180. }
  181. jsStatement = "<" val:((">>" {return ">"} / [^>])*) ">" {
  182. return smalltalk.JSStatementNode._new()
  183. ._source_(val.join(""))
  184. }
  185. method = ws pattern:(keywordPattern / binaryPattern / unaryPattern) ws sequence:sequence? ws {
  186. return smalltalk.MethodNode._new()
  187. ._selector_(pattern[0])
  188. ._arguments_(pattern[1])
  189. ._nodes_([sequence])
  190. }