LambdaTerm = Application / Abstraction / Variable

Application = LPAREN _? left:LambdaTerm _ argList:(LambdaTerm _)* lastArg:LambdaTerm _? RPAREN { 
  const args = lastArg || argList.length ? [...argList.map(x => x[0]), lastArg] : [];
  return { application: { left, args } };
}

Abstraction = LPAREN _? LAMBDA _? LPAREN _? param:Variable _? RPAREN _? DOT _? body:LambdaTerm _? RPAREN { return { abstraction: { param, body } }; }

Variable = param:[a-zA-Z0-9]+ { return param.join(""); }

LPAREN = "("
RPAREN = ")"
DOT = "."
LAMBDA = "λ" / "\\"

_ = ("\n" / " " / "\t" / "\r\n")+