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

have line/char available in AST? #1439

Open
jlmgtech opened this issue May 29, 2023 · 2 comments
Open

have line/char available in AST? #1439

jlmgtech opened this issue May 29, 2023 · 2 comments
Assignees

Comments

@jlmgtech
Copy link

Not a bug, more of a feature. I wasn't sure where to ask. I'm writing an interpreter for SQL and I'd like to tell the user exactly where they made a semantic error. I was going to use sqlify() to render the branch of the tree the problem occurred on, but sqlify only accepts top-level ast nodes (for instance, I can't sqlify a column_ref). Is there something I can use to visually show the user where their error was made (perhaps line numbers somewhere or an offset, or maybe another sqlify function?).

Thank you.

@aok-solutions
Copy link

this already exists...when a parsing error is thrown, the error object contains it's exact location, e.g.:

{
  "message": "Expected \"!=\", \"#-\", \"#>\", \"#>>\", \"%\", \"&&\", \"'\", \"(\", \"*\", \"+\", \",\", \"-\", \"--\", \"->\", \"->>\", \".\", \"/\", \"/*\", \"//\", \"::\", \";\", \"<\", \"<=\", \"<>\", \"<@\", \"=\", \">\", \">=\", \"?\", \"?&\", \"?|\", \"@>\", \"AND\", \"AS\", \"BETWEEN\", \"FROM\", \"GROUP\", \"HAVING\", \"ILIKE\", \"IN\", \"INTO\", \"IS\", \"LIKE\", \"LIMIT\", \"NOT\", \"OFFSET\", \"OR\", \"ORDER\", \"REGEXP\", \"SIMILAR\", \"UNION\", \"WHERE\", \"WINDOW\", \"[\", \"\\\"\", \"`\", \"||\", [ \\t\\n\\r], [A-Za-z0-9_\\-$一-龥], [A-Za-z0-9_一-龥], [A-Za-z_一-龥], or end of input but \":\" found.",
  "found": ":",
  "location": {
    "start": {
      "offset": 10,
      "line": 1,
      "column": 11
    },
    "end": {
      "offset": 11,
      "line": 1,
      "column": 12
    }
  },
  "name": "SyntaxError",
  "isEvalError": true,
}

@zwallacedev
Copy link

To add on to @aok-solutions , you will need to implement a specific parameter in your options: includeLocations.

In the README.md, it specifies:

Get node location in the AST

const { Parser } = require('node-sql-parser');
const parser = new Parser();
const ast = parser.astify('SELECT * FROM t', { parseOptions: { includeLocations: true } });

console.log(ast);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants