syntaxe issues : no viable alternative at input

Former Member
Former Member

Hello there, I'm a beginner in Monkey C and I encountered a syntax issue. Could someone check my code?

code in app.mc : 

using Toybox.WatchUi as Ui;
using Toybox.System;
using Toybox.Time;
using Toybox.Lang;
using Toybox.IO;

class WATCH_FACE extends Ui.WatchFace {
    var timeLabel, eventLabel;
    var events;

    function initialize() {
        WatchFace.initialize();
       
        timeLabel = new Ui.SimpleText();
        timeLabel.text = "";
        timeLabel.color = Ui.Color.BLACK;
        timeLabel.font = Ui.FONT_LARGE;
        timeLabel.position = [72, 60];

        eventLabel = new Ui.SimpleText();
        eventLabel.text = "Loading...";
        eventLabel.color = Ui.Color.BLACK;
        eventLabel.font = Ui.FONT_SMALL;
        eventLabel.position = [72, 100];

        loadEvents();
    }

    function onLayout(dc as Dc) {
        timeLabel.update(dc);
        eventLabel.update(dc);
    }

    function onUpdate(dc as Dc) {
        var time = System.getClockTime();
        timeLabel.setText(time.toHourMinuteString());

        var currentEvent = getCurrentEvent(time);
        if (currentEvent != null) {
            eventLabel.setText(currentEvent["title"]);
        } else {
            eventLabel.setText("No event");
        }

        timeLabel.update(dc);
        eventLabel.update(dc);
    }

    function loadEvents() {
        var file = File.open("source/events.json", File.READ);
        if (file != null) {
            var jsonString = file.readText();
            events = Json.decode(jsonString)["events"];
            file.close();
        }
    }

    function getCurrentEvent(currentTime) {
        var event;
        var iter = events.iterator();
        while ((event = iter.next()) != null) {
            var start = parseTime(event["start"]);
            var end = parseTime(event["end"]);
            if (currentTime >= start && currentTime <= end) {
                return event;
            }
        }
        return null;
    }

    function parseTime(timeString) {
        var parts = timeString.split(":");
        return Time.makeTime(parts[0].toNumber(), parts[1].toNumber(), 0, 0);
    }
}

terminal
WARNING: fr255: The launcher icon (30x30) isn't compatible with the specified launcher icon size of the device 'fr255' (40x40). Image will be scaled to the target size.
ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:61,15: no viable alternative at input '(event ='
ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:61,35: mismatched input ')' expecting ';'

* The terminal process terminated with exit code: 100.
* Terminal will be reused by tasks, press any key to close it.
  • What you may want to do is something like this:

            event=iter.next();
            while (event != null) {
                var start = parseTime(event["start"]);
                var end = parseTime(event["end"]);
                if (currentTime >= start && currentTime <= end) {
                    return event;
                }
                event=iter.next();
            }

    You are trying to do an assignment inside the while() statement on line 61.  With this, you do the assignment, then enter the while, and do the next assignment at the end of the while block

  • Former Member
    0 Former Member 11 months ago in reply to jim_m_58

    Thank you, i understand now. However something weird happened : 

    code : 

    using Toybox.WatchUi as Ui;
    using Toybox.System;
    using Toybox.Time;
    using Toybox.Lang;
    using Toybox.IO;
    
    class WATCH_FACE extends Ui.WatchFace {
        var timeLabel, eventLabel;
        var events;
    
        function initialize() {
            WatchFace.initialize();
            
            timeLabel = new Ui.SimpleText();
            timeLabel.text = "";
            timeLabel.color = Ui.Color.BLACK;
            timeLabel.font = Ui.FONT_LARGE;
            timeLabel.position = [72, 60];
    
            eventLabel = new Ui.SimpleText();
            eventLabel.text = "Loading...";
            eventLabel.color = Ui.Color.BLACK;
            eventLabel.font = Ui.FONT_SMALL;
            eventLabel.position = [72, 100];
    
            loadEvents();
        }
    
        function onLayout(dc as Dc) {
            timeLabel.update(dc);
            eventLabel.update(dc);
        }
    
        function onUpdate(dc as Dc) {
            var time = System.getClockTime();
            timeLabel.setText(time.toHourMinuteString());
    
            var currentEvent = getCurrentEvent(time);
            if (currentEvent != null) {
                eventLabel.setText(currentEvent["title"]);
            } else {
                eventLabel.setText("No event");
            }
    
            timeLabel.update(dc);
            eventLabel.update(dc);
        }
    
        function loadEvents() {
            var file = File.open("source/events.json", File.READ);
            if (file != null) {
                var jsonString = file.readText();
                events = Json.decode(jsonString)["events"];
                file.close();
            }
        }
    
        function getCurrentEvent(currentTime) {
            var event;
            var iter = events.iterator();
            event = iter.next();
            while (event != null) {
                var start = parseTime(event["start"]);
                var end = parseTime(event["end"]);
                if (currentTime >= start && currentTime <= end) {
                    return event;
                }
                event = iter.next();
            }
        
        function parseTime(timeString) {
            var parts = timeString.split(":");
            return Time.makeTime(parts[0].toNumber(), parts[1].toNumber(), 0, 0);
        }
    }
    

    ok, so basically the terminal says that there is a } missing before the last function. (ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:71,4: missing '}' at 'function') However, when i add it, i have even more issues :

    ERROR: fr255: Cannot find entry point class '$.watch_faceApp'.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:5: Cannot find module '$.Toybox.IO' in using statement.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:14,8: Undefined symbol ':SimpleText' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:14,8: No class found for new object instantiation.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:15,8: Undefined symbol ':text' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:16,8: Undefined symbol ':Color' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:16,8: Undefined symbol ':BLACK' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:20,8: Undefined symbol ':SimpleText' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:20,8: No class found for new object instantiation.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:21,8: Undefined symbol ':text' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:22,8: Undefined symbol ':Color' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:22,8: Undefined symbol ':BLACK' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:29: Cannot resolve type 'Dc'.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:34: Cannot resolve type 'Dc'.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:36,8: Undefined symbol ':toHourMinuteString' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:50,8: Undefined symbol ':File' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:50,8: Undefined symbol ':READ' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:52,12: Undefined symbol ':readText' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:53,12: Undefined symbol ':Json' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:53,12: Undefined symbol ':decode' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:60,8: Undefined symbol ':iterator' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:73,8: Undefined symbol ':split' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:74,8: Undefined symbol ':makeTime' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceView.mc:4: Cannot resolve type 'Dc'.

  • There is no Toybox.IO  What are you trying to do?

  • ok, so basically the terminal says that there is a } missing before the last function. (ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:71,4: missing '}' at 'function') However, when i add it, i have even more issues :

    The reason you have additional issues after adding the "}" is because the syntax of your source file is now correct, so the compiler is finding additional errors at a higher level, which should be unsurprising.

    Cannot find module '$.Toybox.IO' in using statement.

    There's no such module as Toybox.IO. It seems that you're trying to port code from Java given this statement and the references to Json and File, but those packages/modules don't exist in Monkey C. Monkey C doesn't give you direct access to filesystem nor does it give you a direct way to manipulate JSON.

    Cannot resolve type 'Dc'.

    Add import Toybox.Graphics; to the top of your file.

    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:16,8: Undefined symbol ':Color' detected.
    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:16,8: Undefined symbol ':BLACK' detected.

    - Change Ui.Color.BLACK to Graphics.COLOR_BLACK.

    - Change Ui.FONT_LARGE to Graphics.FONT_LARGE.

    - Change Ui.FONT_SMALL to Graphics.FONT_SMALL.

    ERROR: fr255: C:\Users\haita\Desktop\garmin code\watch_face\source\watch_faceApp.mc:73,8: Undefined symbol ':split' detected.

    String.split() doesn't exist in monkey c either.

    You need to go over your code and identify/replace everything that doesn't exist in Monkey C, using the API docs as a reference.

    Some things, such as String.split() can be reimplemented in Monkey C. Other things, such as the File package, cannot. In that kind of case, you will have to take a different approach. The Connect IQ Core Topics doc should be a good resource to help you determine what CIQ is capable of, and how to accomplish things such as persisting data (without direct access to files.)