1
0

parser.pegjs 7.1 KB

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