AST Generator for N1QL

I want to use only the AST for ouchbase. Can I use the Parser function or module in the link provided independently. I am trying to use the AST generator in the following link : https://github.com/couchbase/query
Can anybody guide me through the process.
OR
If there is any other open source Couchbase AST Generator. Can you guide me through the process for that AST. I am working on N1QL functions and want to use these with Node.js and want AST for some further development of my project.

Hiya! when you say N1QL functions, do you mean user defined functions (currently in developer preview mode)?
If you could explain what you are trying to achieve with the AST, I’ll try to explain how to use it (although I struggle to see how you could be using it with node.js)

I want to use the N1ql functions with the Firebase and making an algorithm that automatically convert N1ql queries into firebase correponding functions so to my algorithm to work i first need an AST so that i can work on the query

OK - so you are not talking about User Defined Functions, correct?
You want to take a N1QL statement and parse it, and get an AST out.
So, 1st, our service is written in golang - which means that if you want to use your parser to generate an AST, you will have to be able to call go code from whatever language you are using.
I don’t believe this is possible in node.js.

That said, if you want an example on how to get an AST, have a look at server/server.go:getPrepared() - that has a nice example on how the parser is called and used.

If you can’t use golang in node.js, parser/n1ql/n1ql.y has the whole grammar, and the algebra package contains the AST representation for each clause.
You will have to replicate that in node.js.

1 Like

I am trying to build server.go but it gives me error also the n1ql.go gives error when I build them .can u guide me if I can build these functons . If they could be build then kindly guide me the steps to run these .Here is the error

@Zagham_Arif - the client-side Query Workbench has its own version of the N1QL parser to assist with query handling in the UI. It is written in Jison/JavaScript, and generates an AST. The code is kind of ugly - it’s not intended for public use, but it may do what you need. Check out the files here:

The n1ql-validator.js shows some examples of using the parser.

1 Like

I have executed the javascript files and I get the following output. Can u guide me how can I get the AST of the queries I have inserted in queries.txt file. I have inserted the following query in query.txt file SELECT MAX(age) AS MaximumAge FROM Users;

Here’s an example of parsing your query, and printing out the AST to the console:

eben$ node

Welcome to Node.js v12.16.1.

Type ".help" for more information.

> var parser = require("./n1ql").parser;

undefined

> console.log(JSON.stringify(parser.parse("SELECT MAX(age) AS MaximumAge FROM Users;"),null,2));
[
  {
    "type": "Select",
    "ops": {
      "select_terms": {
        "type": "Subselect",
        "ops": {
          "with_expr": null,
          "from": {
            "type": "KeyspaceTerm",
            "ops": {
              "namespace": "",
              "keyspace": "Users",
              "as_alias": ""
            }
          },
          "let": null,
          "where": null,
          "group": null,
          "select": {
            "type": "Projection",
            "ops": {
              "distinct": false,
              "projects": [
                {
                  "type": "ResultTerm",
                  "ops": {
                    "expression": {
                      "type": "Function",
                      "ops": {
                        "fname": "MAX",
                        "param_expr": [
                          {
                            "type": "Identifier",
                            "ops": {
                              "identifier": "age"
                            }
                          }
                        ]
                      }
                    },
                    "star": false,
                    "as_alias": "MaximumAge"
                  }
                }
              ]
            }
          }
        }
      },
      "order_by": null,
      "offset": null,
      "limit": null
    },
    "pathsUsed": [
      "Users",
      [
        "Users"
      ]
    ]
  }
]
undefined
> 
1 Like

Thank you very much for your time . It helped me a lot. and it is working now