parser combinators - Scala parsing left-associative subscript operator -


i've mastered syntax building left-associative tree infix operators:

term * (         "+" ^^^ { (a:expr, b:expr) => new functioncall(plus, a::b::nil) } |         "-" ^^^ { (a:expr, b:expr) => new functioncall(minus, a::b::nil) } ) 

though have confess don't understand how works. want achieve similar effect syntax might like

a[b](c)(d)[e] 

which should parse as

sub(call(call(sub(a, b), c), d), e) 

can high-level "^^^" magic extended cover case it's not pure infix operator? or have implement kind of fold-left logic myself? if so, hints might like?

i have solved problem follows. i'm happy solution, if scala experts out there can me improve it, that's welcome.

  def subscript: parser[expr => expr] = {     "[" ~> expr <~ "]" ^^ {       case sub => {         { (base: expr) => new functioncall(subscriptfn, base :: sub :: nil)}       }     }   }    def argumentlist: parser[expr => expr] = {     "(" ~> repsep(expr, ",") <~ ")" ^^ {       case args => {         { (base: expr) => new functioncall(base :: args)}       }     }   }    def postfixexpr: parser[expr] = {     primary ~ rep ( subscript | argumentlist ) ^^ {       case base ~ suffixes => {         (base /: suffixes)((b:expr, f:expr=>expr) => f(b))       }     }   } 

Comments