parsing - What do these lines mean (Scala / JavaTokenParsers)? -


i trying better understand how mass parsing/interpreting.

i having difficulty understanding lines these:

type l = () => long 

and later there other lines like

val term = chainl1(mynum, "[*/]".r ^^ {   case "*" => (u1:l, u2:l) => () => u1() * u2()   case "/" => (u1:l, u2:l) => () => u1() / u2() }) 

i trying understand underlying structure here. () here? logical flow of these val statements?

i assume means "if find * or / in mynum, match on these 2 cases depending on 1 is, , then... long flow of logic don't understand"

the answer of ka4ell correct, not mention why use () => long instead of using long: () => long or l execution of calculation returns long delayed. execute functions @ moment want actual result, called lazy evaluation.

in case:

case "*" =>    // return function takes 2 ls : u1 , u2   // , returns l : () => u1() * u2()   (u1:l, u2:l) => () => u1() * u2() 

let's define simple function returns l :

// return function return long // potentially expensive calculation def givemeanl(n: long) = () => {   println("givemeanl called")   n } 

if use analogous function 1 in case:

// analogous case "*" => ... def multiply(u1:l, u2:l) = () => {   println("multiply called")   u1() * u2() }  // create 2 ls val (l1, l2) = (givemeanl(5l), givemeanl(2l))  val productl = multiply(l1, l2) // productl of type l 

the value productl contains l can calculate product of 2 values. @ point, neither product nor 2 values calculated. if call productl value, 2 values calculated , product of these values calculated.

val product = productl() // multiply called // givemeanl called // givemeanl called // product: long = 10 

if somewhere in parsing steps, want ignore l values, results of these ls never calculated, improves performance.

case "multiply first 5" =>    (u1:l, u2:l) => () => u1() * 5l // u2 never executed 

Comments