2
0

parser.pegjs 11 KB

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