simple shorter function syntax

Cameron McCormack cam at mcc.id.au
Mon Jul 26 03:56:11 PDT 2010


Cameron McCormack:
> >  mapPairs(someObject, { (a, b) => a + '=' + b) });

Maciej Stachowiak:
> If you allow multiple arguments with this syntax, either with or
> without parens, you're back to unbounded lookahead required for an LL
> parser.

Shouldn’t be.  Take the parens required option.  After parsing the open
brace, there are only two options: either you get an identifier or
string literal, in which case the open brace started an object literal,
or you get an open parenthesis, in which case it’s a function.

  expr ->
      LBRACE expr-brace-rest
    | … (various other productions, none of which begin with LBRACE)

  expr-brace-rest ->
      LPAREN args RPAREN ARROW expr RBRACE
    | obj-literal-prop more-obj-literal-props RBRACE

  args ->
      IDENT more-args
    | <empty>

  more-args ->
      COMMA IDENT more-args
    | <empty>

  obj-literal-prop ->
      IDENT COLON expr
    | STRING COLON expr

  more-obj-literal-props ->
      COMMA obj-literal-prop more-obj-literal-props
    | <empty>

That’s LL(1).  With the optional parens case, you know whether it’s an
object literal or a function either if you find an opening paren or, in
the case of omitted parens in a function, when you find the arrow.  It’d
even work if the parens could be omitted with multiple arguments —
finding a comma instead of a colon means that it’s a function.

  expr ->
      LBRACE expr-brace-rest
    | … (various other productions, none of which begin with LBRACE)

  expr-brace-rest ->
      LPAREN args RPAREN ARROW expr RBRACE
    | ARROW expr RBRACE
    | STRING COLON expr more-obj-literal-props RBRACE
    | IDENT obj-literal-or-function-rest RBRACE

  args ->
      IDENT more-args
    | <empty>

  more-args ->
      COMMA IDENT more-args
    | <empty>

  obj-literal-prop ->
      IDENT COLON expr
    | STRING COLON expr

  more-obj-literal-props ->
      COMMA obj-literal-prop more-obj-literal-props
    | <empty>

  obj-literal-or-function-rest ->
      COMMA IDENT more-args ARROW expr
    | ARROW expr

That’s LL(1) too.  (Or I’m missing something, also quite possible.  I
didn’t thoroughly check the above productions, but they should be
approximately correct, simplifications and assumptions notwithstanding.)

-- 
Cameron McCormack ≝ http://mcc.id.au/


More information about the es-discuss mailing list