parser.pegjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. start = method
  2. separator =
  3. [ \t\v\f\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF\n\r\u2028\u2029]+
  4. comments = ('"' [^"]* '"')+
  5. ws = (separator / comments)*
  6. maybeDotsWs = ('.' / separator / comments)*
  7. someDotsWs = ws '.' maybeDotsWs
  8. identifier = $([a-zA-Z] [a-zA-Z0-9]*)
  9. keyword = $(identifier ':')
  10. className = $([A-Z] [a-zA-Z0-9]*)
  11. string = val:rawString {
  12. return $globals.ValueNode._new()
  13. ._location_(location())
  14. ._source_(text())
  15. ._value_(val);
  16. }
  17. rawString = '\'' val:(('\'\'' {return '\'';} / [^'])*) '\'' {return val.join('');}
  18. character = '$' char:. {
  19. return $globals.ValueNode._new()
  20. ._location_(location())
  21. ._source_(text())
  22. ._value_(char);
  23. }
  24. symbol = '#' rest:bareSymbol {return rest;}
  25. bareSymbol = val:($(keyword+) / binarySelector / unarySelector / rawString) {
  26. return $globals.ValueNode._new()
  27. ._location_(location())
  28. ._source_(text())
  29. ._value_(val);
  30. }
  31. number = val:rawNumber {
  32. return $globals.ValueNode._new()
  33. ._location_(location())
  34. ._source_(text())
  35. ._value_(val);
  36. }
  37. rawNumber = numberExp / hex / float / integer
  38. numberExp = n:$((float / integer) 'e' integer) {return parseFloat(n);}
  39. hex = neg:'-'? '16r' num:$[0-9a-fA-F]+ {
  40. return parseInt(((neg || '') + num), 16);
  41. }
  42. float = n:$('-'? [0-9]+ '.' [0-9]+) {return parseFloat(n, 10);}
  43. integer = n:$('-'? [0-9]+) {return parseInt(n, 10);}
  44. literalArray = '#(' rest:wsLiteralArrayContents ws ')' {
  45. return rest
  46. ._location_(location())
  47. ._source_(text());
  48. }
  49. bareLiteralArray = '(' rest:wsLiteralArrayContents ws ')' {
  50. return rest
  51. ._location_(location())
  52. ._source_(text());
  53. }
  54. literalArrayElement = parseTimeLiteral / bareLiteralArray / bareSymbol
  55. wsLiteralArrayContents =
  56. lits:(ws lit:literalArrayElement {return lit._value();})* {
  57. return $globals.ValueNode._new()
  58. ._value_(lits);
  59. }
  60. dynamicArray = '{' expressions:wsExpressions? maybeDotsWs '}' {
  61. return $globals.DynamicArrayNode._new()
  62. ._location_(location())
  63. ._source_(text())
  64. ._dagChildren_(expressions || []);
  65. }
  66. dynamicDictionary = '#{' expressions:wsAssociations? maybeDotsWs '}' {
  67. return $globals.DynamicDictionaryNode._new()
  68. ._location_(location())
  69. ._source_(text())
  70. ._dagChildren_(expressions || []);
  71. }
  72. pseudoVariable = val:(
  73. 'true' {return true;} /
  74. 'false' {return false;} /
  75. 'nil' {return null;}
  76. ) {
  77. return $globals.ValueNode._new()
  78. ._location_(location())
  79. ._source_(text())
  80. ._value_(val);
  81. }
  82. parseTimeLiteral =
  83. pseudoVariable / number / literalArray / string / symbol / character
  84. runtimeLiteral = dynamicDictionary / dynamicArray / block
  85. literal = runtimeLiteral / parseTimeLiteral
  86. variable = identifier:identifier {
  87. return $globals.VariableNode._new()
  88. ._location_(location())
  89. ._source_(text())
  90. ._value_(identifier);
  91. }
  92. reference = variable
  93. binarySelector = $[\\+*/=><,@%~|&-]+
  94. unarySelector = identifier
  95. wsKeywordPattern =
  96. pairs:(ws key:keyword ws arg:identifier {return {key:key, arg:arg};})+ {
  97. var selector = '';
  98. var params = [];
  99. for(var i = 0; i < pairs.length; i++) {
  100. selector += pairs[i].key;
  101. params.push(pairs[i].arg);
  102. }
  103. return [selector, params];
  104. }
  105. wsBinaryPattern = ws selector:binarySelector ws arg:identifier {
  106. return [selector, [arg]];
  107. }
  108. wsUnaryPattern = ws selector:unarySelector {return [selector, []];}
  109. expression = assignment / cascade / keywordSend
  110. wsExpressionsRest = someDotsWs expression:expression {
  111. return expression;
  112. }
  113. wsExpressions = maybeDotsWs first:expression others:wsExpressionsRest* {
  114. return [first].concat(others);
  115. }
  116. assignment = variable:variable ws ':=' ws expression:expression {
  117. return $globals.AssignmentNode._new()
  118. ._location_(location())
  119. ._source_(text())
  120. ._left_(variable)
  121. ._right_(expression);
  122. }
  123. ret = '^' ws expression:expression {
  124. return $globals.ReturnNode._new()
  125. ._location_(location())
  126. ._source_(text())
  127. ._dagChildren_([expression]);
  128. }
  129. temps = '|' vars:(ws variable:identifier {return variable;})* ws '|' {
  130. return vars;
  131. }
  132. wsBlockParamList =
  133. params:((ws ':' ws param:identifier {return param;})+) ws '|' {
  134. return params;
  135. }
  136. subexpression = '(' ws expression:expression ws ')' {
  137. return expression;
  138. }
  139. wsStatements =
  140. maybeDotsWs ret:ret {return [ret];} /
  141. exps:wsExpressions someDotsWs ret:ret {
  142. var expressions = exps;
  143. expressions.push(ret);
  144. return expressions;
  145. } /
  146. expressions:wsExpressions? {return expressions || [];}
  147. wsSequenceWs = (ws js:jsSequence ws {return js;}) / wsStSequenceWs
  148. wsStSequenceWs = ws temps:temps? statements:wsStatements? maybeDotsWs {
  149. return $globals.SequenceNode._new()
  150. ._location_(location())
  151. ._source_(text())
  152. ._temps_(temps || [])
  153. ._dagChildren_(statements || []);
  154. }
  155. jsSequence = jsStatement
  156. block = '[' params:wsBlockParamList? sequence:wsSequenceWs ']' {
  157. return $globals.BlockNode._new()
  158. ._location_(location())
  159. ._source_(text())
  160. ._parameters_(params || [])
  161. ._dagChildren_([sequence._asBlockSequenceNode()]);
  162. }
  163. operand = literal / reference / subexpression
  164. wsUnaryMessage = ws selector:unarySelector !':' {
  165. return $globals.SendNode._new()
  166. ._location_(location())
  167. ._source_(text())
  168. ._selector_(selector);
  169. }
  170. unarySend = receiver:operand tail:wsUnaryMessage* {
  171. return receiver._withTail_(tail);
  172. }
  173. wsBinaryMessage = ws selector:binarySelector ws arg:unarySend {
  174. return $globals.SendNode._new()
  175. ._location_(location())
  176. ._source_(text())
  177. ._selector_(selector)
  178. ._arguments_([arg]);
  179. }
  180. binarySend = receiver:unarySend tail:wsBinaryMessage* {
  181. return receiver._withTail_(tail);
  182. }
  183. wsKeywordMessage =
  184. pairs:(ws key:keyword ws arg:binarySend {return {key:key, arg:arg};})+ {
  185. var selector = '';
  186. var args = [];
  187. for(var i = 0; i < pairs.length; i++) {
  188. selector += pairs[i].key;
  189. args.push(pairs[i].arg);
  190. }
  191. return $globals.SendNode._new()
  192. ._location_(location())
  193. ._source_(text())
  194. ._selector_(selector)
  195. ._arguments_(args);
  196. }
  197. keywordSend = receiver:binarySend tail:wsKeywordMessage? {
  198. return tail ? receiver._withTail_([tail]) : receiver;
  199. }
  200. wsMessage = wsBinaryMessage / wsUnaryMessage / wsKeywordMessage
  201. cascade =
  202. send:keywordSend & {return send._isSendNode();}
  203. messages:(ws ';' mess:wsMessage {return mess;})+ {
  204. messages.unshift(send);
  205. return $globals.CascadeNode._new()
  206. ._location_(location())
  207. ._source_(text())
  208. ._dagChildren_(messages);
  209. }
  210. jsStatement = pragmaJsStatement / legacyJsStatement
  211. legacyJsStatement =
  212. '<' val:(('>>' {return '>';} / [^>])*) '>'
  213. & {return !/^\s*inlineJS/.test(val.join(''));} {
  214. console.warn('Use of <...js code...> is deprecated, in:\n' + val.join(''));
  215. return $globals.JSStatementNode._new()
  216. ._location_(location())
  217. ._source_(val.join(''))
  218. }
  219. pragmaJsStatement = '<' ws 'inlineJS:' ws val:rawString ws '>' {
  220. return $globals.JSStatementNode._new()
  221. ._location_(location())
  222. ._source_(val)
  223. }
  224. method =
  225. pattern:(wsKeywordPattern / wsBinaryPattern / wsUnaryPattern)
  226. sequence:wsSequenceWs {
  227. return $globals.MethodNode._new()
  228. ._location_(location())
  229. ._source_(text())
  230. ._selector_(pattern[0])
  231. ._arguments_(pattern[1])
  232. ._dagChildren_([sequence]);
  233. }
  234. associationSend =
  235. send:binarySend
  236. & { return send._isSendNode() && send._selector() === '->' } {
  237. return [send._receiver(), send._arguments()[0]];
  238. }
  239. wsAssociationsRest = someDotsWs expression:associationSend {
  240. return expression;
  241. }
  242. wsAssociations = maybeDotsWs first:associationSend others:wsAssociationsRest* {
  243. return first.concat.apply(first, others);
  244. }