Can not parse file error

I am new to Zephir and am struggling with the lack of a line number (or some other indication) of where my error is. I am including a zep script that is returning a "can not parse" error. Any help or sugguestions would be appreciated. Regards, Joe

namespace blinker;
class init {
 public function run(var cnt = 0){
  var tick = 5;
  var state;
  var ram = "/mnt/ramdisk/blink";
  exec("gpio mode 0 out");
  exec("gpio write 0 0");
  if cnt == 0 {
   DoState(cnt);
  } else {
   loop {
    if(!file_exists(ram)){
     sleep(tick);
    } else {
     let state = trim(file_get_contents(ram));
     if(state == ""){ state = 0; }
     DoState(state);
    } 
   } 
  }
 } 
 private function DoState(var cnt){
  var tick = 5;
  switch(cnt){
   case 0: // off
    exec("gpio write 0 0");
    sleep(tick);
    break;
   case 1: // on
    exec("gpio write 0 1");
    sleep(tick);
    break;
   case 2:
    Flash(2);    
    break;
   case 3: 
    Flash(3);    
    break;
   case 4:
    Flash(4);    
    break;
   case 5:
    Flash(5);    
    break;
  }
 } 
 //-------------------------------------------- 
 function private Flash(var i){
  var tick = 5;
  var dx = i;
  if dx >= tick { dx = 0; }// If this happens, make TICK larger
  for x in range(1,dx){
   exec("gpio write 0 1");
   usleep(250000);
   exec("gpio write 0 0");
   usleep(250000);
  }
  var ticks = (tick -i) * 2;
  if ticks > 0 { usleep(ticks * 250000); }
 } 
} 
[blinker.php]
<?php
 blinker\init::run(5);
?>
this->DoState(cnt); // instead of DoState(cnt);
this->Flash(2); // instead of Flash(2);


225
edited Jul '16

I made the changes suggested but it still fails with the Cannot parse error. Here is the updated script:

namespace blinker;
class init {
 public function run(var cnt = 0){
  var tick = 5;
  var state;
  var ram = "/mnt/ramdisk/blink";
  exec("gpio mode 0 out");
  exec("gpio write 0 0");
  if cnt == 0 {
   this->DoState(cnt);
  } else {
   loop {
    if(!file_exists(ram)){
     sleep(tick);
    } else {
     let state = trim(file_get_contents(ram));
     if(state == ""){ state = 0; }
     this->DoState(state);
    } 
   } 
  }
 } 
 private function DoState(var cnt){
  var tick = 5;
  switch(cnt){
   case 0: // off
    exec("gpio write 0 0");
    sleep(tick);
    break;
   case 1: // on
    exec("gpio write 0 1");
    sleep(tick);
    break;
   case 2:
    this->Flash(2);    
    break;
   case 3: 
    this->Flash(3);    
    break;
   case 4:
    this->Flash(4);    
    break;
   case 5:
    this->Flash(5);    
    break;
  }
 } 
 //-------------------------------------------- 
 private function Flash(var i){
  var tick = 5;
  var dx = i;
  if dx >= tick { dx = 0; }// If this happens, make TICK larger
  for x in range(1,dx){
   exec("gpio write 0 1");
   usleep(250000);
   exec("gpio write 0 0");
   usleep(250000);
  }
  var ticks = (tick -i) * 2;
  if ticks > 0 { usleep(ticks * 250000); }
 } 
}
  1. Infinite loop without at least a 'break' statement is not allowed
  2. for x in range(1,dx){ - The x variable is undefined
  3. if dx >= tick { dx = 0; } -> let dx = 0;
  4. if(state == ""){ state = 0; } -> let state = 0;
  5. etc

Compiled example

public function run(int cnt = 0)
{
    int i = 0, tick = 5;
    var state, ram = "/mnt/ramdisk/blink";

    exec("gpio mode 0 out");
    exec("gpio write 0 0");

    if cnt == 0 {
        this->DoState(cnt);
        return;
    }

    loop {
        // Infinite loop without at least a 'break' statement is not allowed
        if i == 10 {
            break;
        }

        let i = i + 1;

        if !file_exists(ram) {
            sleep(tick);
        } else {
            let state = trim(file_get_contents(ram));
            if state == "" {
                let state = 0;
            }
            this->DoState(state);
        }
    }
}

private function DoState(int cnt)
{
    int tick = 5;

    switch cnt {
        case 0: // off
            exec("gpio write 0 0");
            sleep(tick);
        break;
        case 1: // on
            exec("gpio write 0 1");
            sleep(tick);
        break;
        case 2:
            this->Flash(2);
        break;
        case 3:
            this->Flash(3);
        break;
        case 4:
            this->Flash(4);
        break;
        case 5:
            this->Flash(5);
        break;
    }
}

private function Flash(int i)
{
    var x;
    int tick = 5, dx = i, ticks;

    // If this happens, make TICK larger
    if dx >= tick {
      let dx = 0;
    }

    for x in range(1, dx) {
        exec("gpio write 0 1");
        usleep(250000);
        exec("gpio write 0 0");
        usleep(250000);
    }

    let ticks = (tick - i) * 2;

    if ticks > 0 {
        usleep(ticks * 250000);
    }
}


225

I appreciate your time and effort.

I did a direct cut and paste of your code and it still would not compile. I am using an arm7 device (raspberry pi model 3) - could that have something to do with it?

it still would not compile

There any logs in copiller.log?



225
edited Jul '16

No, just the config.json file along with the directories:

  • .temp
  • blinker
  • ext

Step by step comment your code and try to compile. It seems to me all ok. But might be there any CPU-related issue