Guest User

Untitled

a guest
May 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. /*
  2. PostfixExp -> PrimaryExp ( "[" Exp "]"
  3. | . id "(" ExpList ")"
  4. | . length )*
  5. */
  6.  
  7. object MyParser {
  8. type E = Expression
  9.  
  10. def postfixExp = primaryExp ~ rep(
  11. "[" ~> expr <~ "]" ^^ { e => ElementExpression(_:E, e) }
  12. | "." ~ "length" ^^^ LengthExpression
  13. | "." ~> ident ~ ("(" ~> repsep(expr, ",") <~ ")") ^^ flatten2 { (f, args) =>
  14. CallMethodExpression(_:E, f, args)
  15. }
  16. ) ^^ flatten2 { (e, ls) => collapse(ls)(e) }
  17.  
  18. def expr: Parser[E] = ...
  19.  
  20. def collapse(ls: List[E=>E])(e: E) = {
  21. ls.foldLeft(e) { (e, f) => f(e) }
  22. }
  23. }
Add Comment
Please, Sign In to add comment