123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- start = method
- separator = [ \t\v\f\u00A0\uFEFF\n\r\u2028\u2029]+
- comments = ('"' [^"]* '"')+
- ws = (separator / comments)*
- maybeDotsWs = ("." / separator / comments)*
- identifier = first:[a-zA-Z] others:[a-zA-Z0-9]* {return first + others.join("");}
- keyword = first:identifier last:":" {return first + last;}
- selector = first:[a-zA-Z] others:[a-zA-Z0-9\:]* {return first + others.join("");}
- className = first:[A-Z] others:[a-zA-Z0-9]* {return first + others.join("");}
- string = "'" val:(("''" {return "'";} / [^'])*) "'" {
- return $globals.ValueNode._new()
- ._location_(location())
- ._source_(text())
- ._value_(val.join(""));
- }
- character = "$" char:.
- {
- return $globals.ValueNode._new()
- ._location_(location())
- ._source_(text())
- ._value_(char);
- }
- symbol = "#" rest:bareSymbol {return rest;}
- bareSymbol = val:(selector / binarySelector / node:string {return node._value();})
- {
- return $globals.ValueNode._new()
- ._location_(location())
- ._source_(text())
- ._value_(val);
- }
- number = n:(numberExp / hex / float / integer) {
- return $globals.ValueNode._new()
- ._location_(location())
- ._source_(text())
- ._value_(n);
- }
- numberExp = n:((float / integer) "e" integer) {return parseFloat(n.join(""));}
- hex = neg:"-"? "16r" num:[0-9a-fA-F]+ {return parseInt(((neg || '') + num.join("")), 16);}
- float = neg:"-"? digits:[0-9]+ "." dec:[0-9]+ {return parseFloat(((neg || '') + digits.join("") + "." + dec.join("")), 10);}
- integer = neg:"-"? digits:[0-9]+ {return (parseInt((neg || '') + digits.join(""), 10));}
- literalArray = "#(" rest:wsLiteralArrayContents ws ")" {
- return rest
- ._location_(location())
- ._source_(text());
- }
- bareLiteralArray = "(" rest:wsLiteralArrayContents ws ")" {
- return rest
- ._location_(location())
- ._source_(text());
- }
- literalArrayElement = parseTimeLiteral / bareLiteralArray / bareSymbol
- wsLiteralArrayContents = lits:(ws lit:literalArrayElement {return lit._value();})* {
- return $globals.ValueNode._new()
- ._value_(lits);
- }
- dynamicArray = "{" ws expressions:expressions? maybeDotsWs "}" {
- return $globals.DynamicArrayNode._new()
- ._location_(location())
- ._source_(text())
- ._nodes_(expressions || []);
- }
- dynamicDictionary = "#{" ws expressions:associations? maybeDotsWs "}" {
- return $globals.DynamicDictionaryNode._new()
- ._location_(location())
- ._source_(text())
- ._nodes_(expressions || []);
- }
- pseudoVariable = val:(
- 'true' {return true;}
- / 'false' {return false;}
- / 'nil' {return nil;}) {
- return $globals.ValueNode._new()
- ._location_(location())
- ._source_(text())
- ._value_(val);
- }
- parseTimeLiteral = pseudoVariable / number / literalArray / string / symbol / character
- runtimeLiteral = dynamicDictionary / dynamicArray / block
- literal = runtimeLiteral / parseTimeLiteral
- variable = identifier:identifier {
- return $globals.VariableNode._new()
- ._location_(location())
- ._source_(text())
- ._value_(identifier);
- }
- reference = variable
- binarySelector = bin:[\\+*/=><,@%~|&-]+ {return bin.join("");}
- unarySelector = identifier
- wsKeywordPattern = pairs:(ws key:keyword ws arg:identifier {return {key:key, arg:arg};})+ {
- var selector = "";
- var params = [];
- for(var i = 0; i < pairs.length; i++){
- selector += pairs[i].key;
- params.push(pairs[i].arg);
- }
- return [selector, params];
- }
- wsBinaryPattern = ws selector:binarySelector ws arg:identifier {return [selector, [arg]];}
- wsUnaryPattern = ws selector:unarySelector {return [selector, []];}
- expression = assignment / cascade / keywordSend
- wsExpressionsRest = ws "." maybeDotsWs expression:expression {return expression;}
- expressions = first:expression others:wsExpressionsRest* { return [first].concat(others); }
- assignment = variable:variable ws ':=' ws expression:expression {
- return $globals.AssignmentNode._new()
- ._location_(location())
- ._source_(text())
- ._left_(variable)
- ._right_(expression);
- }
- ret = '^' ws expression:expression {
- return $globals.ReturnNode._new()
- ._location_(location())
- ._source_(text())
- ._nodes_([expression]);
- }
-
- temps = "|" vars:(ws variable:identifier {return variable;})* ws "|" {return vars;}
- wsBlockParamList = params:((ws ":" ws param:identifier {return param;})+) ws "|" {return params;}
- subexpression = '(' ws expression:expression ws ')' {return expression;}
- statementsWs = ret:ret maybeDotsWs {return [ret];}
- / exps:expressions ws "." maybeDotsWs ret:ret maybeDotsWs {
- var expressions = exps;
- expressions.push(ret);
- return expressions;
- }
- / expressions:expressions? maybeDotsWs {
- return expressions || [];
- }
- wsSequenceWs = (ws js:jsSequence ws { return js; }) / wsStSequenceWs
- wsStSequenceWs = ws temps:temps? maybeDotsWs statements:statementsWs? {
- return $globals.SequenceNode._new()
- ._location_(location())
- ._source_(text())
- ._temps_(temps || [])
- ._nodes_(statements || []);
- }
- jsSequence = jsStatement
- block = '[' params:wsBlockParamList? sequence:wsSequenceWs? ']' {
- return $globals.BlockNode._new()
- ._location_(location())
- ._source_(text())
- ._parameters_(params || [])
- ._nodes_([sequence._asBlockSequenceNode()]);
- }
- operand = literal / reference / subexpression
- augment = "(" send:(wsBinaryTail / wsKeywordMessage / wsUnaryTail) ws ")" {
- return send._asBranchSendNode()
- ._location_(location())
- ._source_(text());
- }
- augmentCascade = "(" first:wsMessage rest:(ws ";" mess:wsMessage {return mess;})+ ws ")" {
- var send = rest.reduce(function (prev, curr) {
- return curr._asBranchSendNode()._valueForReceiver_(prev);
- }, first._asBranchSendNode());
- return send;
- }
- wsAugmentTail = ws message:(augment / augmentCascade) tail:wsAugmentTail? {
- if(tail) {
- return tail._valueForReceiver_(message);
- }
- else {
- return message;
- }
- }
- augmentedOperand = operand:operand tail:wsAugmentTail? {
- if(tail) {
- return tail._valueForReceiver_(operand);
- }
- else {
- return operand;
- }
- }
- wsUnaryMessage = ws selector:unarySelector !":" {
- return $globals.SendNode._new()
- ._location_(location())
- ._source_(text())
- ._selector_(selector);
- }
- wsUnaryTail = message:wsUnaryMessage tail:wsUnaryTail? {
- if(tail) {
- return tail._valueForReceiver_(message);
- }
- else {
- return message;
- }
- }
- unarySend = receiver:augmentedOperand tail:wsUnaryTail? {
- if(tail) {
- return tail._valueForReceiver_(receiver);
- }
- else {
- return receiver;
- }
- }
- wsBinaryMessage = ws selector:binarySelector ws arg:unarySend {
- return $globals.SendNode._new()
- ._location_(location())
- ._source_(text())
- ._selector_(selector)
- ._arguments_([arg]);
- }
- wsBinaryTail = message:wsBinaryMessage tail:wsBinaryTail? {
- if(tail) {
- return tail._valueForReceiver_(message);
- }
- else {
- return message;
- }
- }
- binarySend = receiver:unarySend tail:wsBinaryTail? {
- if(tail) {
- return tail._valueForReceiver_(receiver);
- }
- else {
- return receiver;
- }
- }
- wsKeywordMessage = pairs:(ws key:keyword ws arg:binarySend {return {key:key, arg:arg};})+ {
- var selector = "";
- var args = [];
- for(var i = 0; i < pairs.length; i++) {
- selector += pairs[i].key;
- args.push(pairs[i].arg);
- }
- return $globals.SendNode._new()
- ._location_(location())
- ._source_(text())
- ._selector_(selector)
- ._arguments_(args);
- }
- keywordSend = receiver:binarySend tail:wsKeywordMessage? {
- if(tail) {
- return tail._valueForReceiver_(receiver);
- }
- else {
- return receiver;
- }
- }
- wsMessage = wsBinaryMessage / wsUnaryMessage / wsKeywordMessage
- cascade = first:keywordSend & { return first._isSendNode(); } rest:(ws ";" mess:wsMessage {return mess;})+ {
- var last = rest.pop();
- var send = rest.reduce(function (prev, curr) {
- return curr._asBranchSendNode()._valueForReceiver_(prev);
- }, first._asBranchSendNode());
- return last._valueForReceiver_(send);
- }
- jsStatement = "<" val:((">>" {return ">";} / [^>])*) ">" {
- return $globals.JSStatementNode._new()
- ._location_(location())
- ._source_(val.join(""))
- }
- method = pattern:(wsKeywordPattern / wsBinaryPattern / wsUnaryPattern) sequence:wsSequenceWs? {
- return $globals.MethodNode._new()
- ._location_(location())
- ._source_(text())
- ._selector_(pattern[0])
- ._arguments_(pattern[1])
- ._nodes_([sequence]);
- }
- associationSend = send:binarySend & { return send._isSendNode() && send._selector() === "->" } { return [send._receiver(), send._arguments()[0]]; }
- wsAssociationsRest = ws "." maybeDotsWs expression:associationSend {return expression;}
- associations = first:associationSend others:wsAssociationsRest* { return first.concat.apply(first, others); }
|