2
0

parser.pegjs 10 KB

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