Motoko Syntax Errors: "unexpected token 'private'" and Type Definition Issues

Subject: Motoko Syntax Errors: “unexpected token ‘private’” and Type Definition Issues

Hi everyone,

I’m running into persistent Motoko syntax errors in my project, and after following all the usual advice, I still can’t resolve them. Here’s what’s happening:


Main Error:

unexpected token 'private', expected one of token or <phrase> sequence:
  <eof>
  seplist(<dec>,<semicolon>) (e.g. 'let x : Int = 0')

Context:

  • The error appears right after my type definitions at the top of my actor in main.mo.
  • I have several custom types, both variants and records, e.g.:
    type SearchRequest = {
        query: Text;
        scope: SearchScope;
        filters: ?SearchFilter;
        sortBy: ?SearchSortBy;
        pagination: ?PaginationParams
    } // <--- I tried with and without a semicolon here
    
  • I’ve tried:
    • Adding/removing semicolons after every field and after the closing brace of record types.
    • Ensuring no semicolon after the last tag in variant types, but a semicolon after the closing brace.
    • Reordering and reformatting the type definitions.
  • No matter what I do, the parser always throws an error at the first private declaration after the types.

What am I missing?

  • Is there a subtle Motoko syntax rule I’m overlooking for type definitions inside an actor?
  • Could this be a tooling or compiler bug?
  • Any working example of a Motoko actor with multiple custom types (both variant and record) at the top would be super helpful!

Thanks in advance for any help or pointers!

This is most likely caused by a missing semi directly above the line number mentioned in the error message.

Motoko requires semicolons between all declarations (types, methods, classes, etc.). For instance:

actor {
  type SearchRequest = {
    ...
  }; // Required semicolon

  public func search(request : SearchRequest) : async ?Text {
    ...
  }; // Optional semicolon since it's at the end of the code block
};

When in doubt, you can add semicolons at the end of every declaration.

The error message is saying that there should be a semicolon before the unexpected private token, so you just need to add one to the declaration above the line/column mentioned in the error.

If this doesn’t work, feel free to send me the file as a DM or reply with the lines of code surrounding the error message.

I’ll also bring this up in an internal discussion to see if we can improve the errors for missing semicolons.

Thank its helped. But caught with a different problem “Unbound variable caller” Error in Motoko Actor - Tried Multiple Solutions

Are you using AI? It’s possible that the code is missing shared({ caller }) on the enclosing actor method, which can happen with LLM-generated Motoko programs.

Example:

public shared({ caller }) func example() : async Principal {
  caller; // Return the caller
};

Here is the documentation for what you’re most likely looking for. Otherwise, I would need to see some code to be able to help further.

https://internetcomputer.org/docs/motoko/icp-features/caller-identification

Yes, I was using AI. Thanks for helping.