Skip to content

Commit

Permalink
Unary functions using ~>
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Riou committed Aug 4, 2009
1 parent 7bbbc35 commit c89bd33
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/org/mozilla/javascript/Parser.java
Expand Up @@ -536,12 +536,12 @@ private AstRoot parse() throws IOException
}

AstNode n;
if (tt == Token.FUNCTION) {
if (tt == Token.FUNCTION || tt == Token.FUNCTION_UNARY) {
consumeToken();
try {
n = function(calledByCompileFunction
? FunctionNode.FUNCTION_EXPRESSION
: FunctionNode.FUNCTION_STATEMENT);
: FunctionNode.FUNCTION_STATEMENT, tt == Token.FUNCTION_UNARY);
} catch (ParserException e) {
break;
}
Expand Down Expand Up @@ -609,8 +609,9 @@ private AstNode parseFunctionBody()
break bodyLoop;

case Token.FUNCTION:
case Token.FUNCTION_UNARY:
consumeToken();
n = function(FunctionNode.FUNCTION_STATEMENT);
n = function(FunctionNode.FUNCTION_STATEMENT, tt == Token.FUNCTION_UNARY);
break;
default:
n = statement();
Expand Down Expand Up @@ -700,7 +701,7 @@ private AstNode parseFunctionBodyExpr()
return n;
}

private FunctionNode function(int type)
private FunctionNode function(int type, boolean unary)
throws IOException
{
int syntheticType = type;
Expand All @@ -709,7 +710,7 @@ private FunctionNode function(int type)
Name name = null;
AstNode memberExprNode = null;

if (matchToken(Token.NAME)) {
if (!unary && matchToken(Token.NAME)) {
name = createNameNode(true, Token.NAME);
if (!matchToken(Token.LP)) {
if (compilerEnv.isAllowMemberExprAsFunctionName()) {
Expand All @@ -728,7 +729,8 @@ private FunctionNode function(int type)
// processed as anonymous function
memberExprNode = memberExpr(false);
}
mustMatchToken(Token.LP, "msg.no.paren.parms");
if (!unary)
mustMatchToken(Token.LP, "msg.no.paren.parms");
}
int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1;

Expand Down Expand Up @@ -760,7 +762,12 @@ private FunctionNode function(int type)

PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
try {
parseFunctionParams(fnNode);
if (unary) {
fnNode.addParam(new Name(functionSourceStart, 1, "_"));
defineSymbol(Token.LP, "_");
} else {
parseFunctionParams(fnNode);
}
fnNode.setBody(parseFunctionBody());
fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
fnNode.setLength(ts.tokenEnd - functionSourceStart);
Expand Down Expand Up @@ -995,8 +1002,9 @@ && peekToken() == Token.SEMI)
return pn;

case Token.FUNCTION:
case Token.FUNCTION_UNARY:
consumeToken();
return function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT);
return function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT, tt == Token.FUNCTION_UNARY);

case Token.DEFAULT :
pn = defaultXmlNamespace();
Expand Down Expand Up @@ -1919,7 +1927,7 @@ void defineSymbol(int declType, String name, boolean ignoreNotInBlock) {
addError(symDeclType == Token.CONST ? "msg.const.redecl" :
symDeclType == Token.LET ? "msg.let.redecl" :
symDeclType == Token.VAR ? "msg.var.redecl" :
symDeclType == Token.FUNCTION ? "msg.fn.redecl" :
symDeclType == Token.FUNCTION || symDeclType == Token.FUNCTION_UNARY ? "msg.fn.redecl" :
"msg.parm.redecl", name);
return;
}
Expand All @@ -1937,6 +1945,7 @@ void defineSymbol(int declType, String name, boolean ignoreNotInBlock) {
case Token.VAR:
case Token.CONST:
case Token.FUNCTION:
case Token.FUNCTION_UNARY:
if (symbol != null) {
if (symDeclType == Token.VAR)
addStrictWarning("msg.var.redecl", name);
Expand Down Expand Up @@ -2710,7 +2719,8 @@ private AstNode primaryExpr()

switch(tt) {
case Token.FUNCTION:
return function(FunctionNode.FUNCTION_EXPRESSION);
case Token.FUNCTION_UNARY:
return function(FunctionNode.FUNCTION_EXPRESSION, tt == Token.FUNCTION_UNARY);

case Token.LB:
return arrayLiteral();
Expand Down Expand Up @@ -3078,7 +3088,7 @@ private ObjectProperty getterSetterProperty(int pos, AstNode propName,
boolean isGetter)
throws IOException
{
FunctionNode fn = function(FunctionNode.FUNCTION_EXPRESSION);
FunctionNode fn = function(FunctionNode.FUNCTION_EXPRESSION, false);
// We've already parsed the function name, so fn should be anonymous.
Name name = fn.getFunctionName();
if (name != null && name.length() != 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/org/mozilla/javascript/Token.java
Expand Up @@ -199,6 +199,7 @@ public static enum CommentType {
DEC = 106,
DOT = 107, // member operator (.)
FUNCTION = 108, // function keyword
FUNCTION_UNARY = 161, // unary ~> function keyword
EXPORT = 109, // export keyword
IMPORT = 110, // import keyword
IF = 111, // if keyword
Expand Down Expand Up @@ -262,7 +263,7 @@ public static enum CommentType {
WITHEXPR = 158,
DEBUGGER = 159,
COMMENT = 160,
LAST_TOKEN = 160;
LAST_TOKEN = 161;

/**
* Returns a name for the token. If Rhino is compiled with certain
Expand Down Expand Up @@ -394,6 +395,7 @@ public static String typeToName(int token) {
case DEC: return "DEC";
case DOT: return "DOT";
case FUNCTION: return "FUNCTION";
case FUNCTION_UNARY: return "FUNCTION_UNARY";
case EXPORT: return "EXPORT";
case IMPORT: return "IMPORT";
case IF: return "IF";
Expand Down
6 changes: 5 additions & 1 deletion src/org/mozilla/javascript/TokenStream.java
Expand Up @@ -819,7 +819,11 @@ final int getToken() throws IOException
}

case '~':
return Token.BITNOT;
if (matchChar('>')) {
return Token.FUNCTION_UNARY;
} else {
return Token.BITNOT;
}

case '+':
if (matchChar('=')) {
Expand Down
1 change: 1 addition & 0 deletions src/org/mozilla/javascript/ast/Symbol.java
Expand Up @@ -82,6 +82,7 @@ public int getDeclType() {
*/
public void setDeclType(int declType) {
if (!(declType == Token.FUNCTION
|| declType == Token.FUNCTION_UNARY
|| declType == Token.LP
|| declType == Token.VAR
|| declType == Token.LET
Expand Down

0 comments on commit c89bd33

Please sign in to comment.