Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accurate position of syntax error when parsing #416

Open
dragoncoder047 opened this issue Dec 9, 2024 · 9 comments
Open

accurate position of syntax error when parsing #416

dragoncoder047 opened this issue Dec 9, 2024 · 9 comments
Labels
enhancement New feature or request

Comments

@dragoncoder047
Copy link
Contributor

When I call lips.parse() and it gives me a syntax error there seems to be no way to see at what position in the source the parse error occurred, cause the comments and whitespace get thrown out.

Screenshot 2024-12-09 at 11 13 47 AM

I also tried the "parse with metadata" option you shared earlier in #414 (comment), but it gave the same result (no __line__ or __col__ appeared).

is there a better way to find the exact index of a syntax error?

@jcubic
Copy link
Owner

jcubic commented Dec 9, 2024

It may not be as simple as adding the metadata.

@jcubic
Copy link
Owner

jcubic commented Dec 10, 2024

Actually, I just checked, and all parse errors came from the parser that have info about the line and col. I will see if I can add this information to the error when meta is used.

@jcubic jcubic added the enhancement New feature or request label Dec 10, 2024
jcubic added a commit that referenced this issue Dec 10, 2024
@jcubic
Copy link
Owner

jcubic commented Dec 10, 2024

Added the same metadata to the parse errors.

@dragoncoder047
Copy link
Contributor Author

Hmmm. I updated my html files to point to https://cdn.jsdelivr.net/gh/LIPS-scheme/lips@b372c7e4f6a1963788192612e34170a56976dca4/dist/lips.js which is this plus the #417 fix, and am using this function to parse

/**
 * @param {string} source
 * @returns {Promise<lips.Pair[]>}
 */
export async function parseWithMetadata(source) {
    const parser = new lips.Parser({ env: lips.env, meta: true });
    parser.prepare(source);
    return await lips.parse(parser);
}

but am not getting __line__ and __col__ on the errors thrown. Must be a browser caching problem, will update later...

@jcubic
Copy link
Owner

jcubic commented Dec 10, 2024

It works, I've put your code into CodPen: https://codepen.io/jcubic/pen/MYgexza?editors=0012

@dragoncoder047
Copy link
Contributor Author

Now that I've force-reloaded many times, the cache has cleared and I am getting line/columns on some of the syntax errors now.

However I am still not getting position info for the "Parser: expected parenthesis but eof found" error. Indeed, when you open the codepen you shared above and change the ")" to "(", the position information all shows up, but is all undefined.

@jcubic
Copy link
Owner

jcubic commented Dec 11, 2024

The problem was that lexer was returning eof object instead of token with metadata, when it reaches the end of the input. But the last token is still accessible from parser.__lexer__.__token__.

@jcubic
Copy link
Owner

jcubic commented Dec 11, 2024

I've updated the demo with the latest version https://codepen.io/jcubic/pen/MYgexza?editors=0012

jcubic added a commit that referenced this issue Dec 11, 2024
@jcubic
Copy link
Owner

jcubic commented Dec 11, 2024

Added line number to all error messages, when using higher level lips.Interpreter interface.

jcubic added a commit that referenced this issue Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants