Comments (3)
I thought of a problem if we wanted to add comments to the AST:
/* comments outside of a statement might work alright */
SELECT pants -- But what about
FROM -- these kinds of comments
laundry -- what would the AST look like?
WHERE
clean = true;
Any ideas? Could we put these in the AST in a way that they wouldn't be disruptive and could also be used to reconstruct the original query with the comments in the right places?
from sqlite-parser.
I've been thinking about this for a little while now and looking around to see what others are doing. I think esprima has a reasonably good way of handling this. They tie the comment to the nearest node as either a leadingComment
(before the node) or trailingComment
(after the node). They are handled as follows:
// Life, Universe, and Everything
var answer /* this is still legal*/ = 6 * 7; // this is trailing
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "answer",
"trailingComments": [
{
"type": "Block",
"value": " this is still legal",
"range": [
45,
69
]
}
]
},
"init": {
"type": "BinaryExpression",
"operator": "*",
"left": {
"type": "Literal",
"value": 6,
"raw": "6"
},
"right": {
"type": "Literal",
"value": 7,
"raw": "7"
},
"leadingComments": [
{
"type": "Block",
"value": " this is still legal",
"range": [
45,
69
]
}
]
}
}
],
"kind": "var",
"leadingComments": [
{
"type": "Line",
"value": " Life, Universe, and Everything",
"range": [
0,
33
]
}
],
"trailingComments": [
{
"type": "Line",
"value": " this is trailing",
"range": [
79,
98
]
}
]
}
],
"sourceType": "script"
}
The area where it gets a bit weird is inside a function declaration with no args:
function noArg(/* I have no node to attach to*/){};
This creates an innerComment
like so:
{
"type": "Program",
"body": [
{
"type": "FunctionDeclaration",
"id": {
"type": "Identifier",
"name": "noArg"
},
"params": [],
"body": {
"type": "BlockStatement",
"body": [],
"innerComments": [
{
"type": "Block",
"value": " I have no node to attach to",
"range": [
15,
47
]
}
]
},
"generator": false,
"expression": false
},
{
"type": "EmptyStatement"
}
],
"sourceType": "script"
}
I'm not sure if this would be needed (or worth the effort initially)? It would still be valid to do this in SQL though. What do you think?
from sqlite-parser.
I want to do this, but it would take huge changes to the entire grammar for the parser. I started experimenting with how this might be done, and came up with something that worked for comments that are between statements, but to get all the inline comments would take grammar changes every time the o
rule is used in the grammar, which is ~230 times currently.
from sqlite-parser.
Related Issues (20)
- Node Structure Change: Change format in condition type node to variant HOT 4
- Node Structure Change: Add variant to module node of create virtual table statement. HOT 1
- "too much recursion" in browserified version HOT 7
- Trailing spaces in table or column names deleted
- Get full text of expression HOT 2
- operator precedence of AND/OR in where HOT 3
- use from browser without nodejs HOT 1
- Issue with Object.assign is not a function HOT 3
- Allow for / in the table names HOT 1
- Each expression is evaluated 4 times
- Why are invalid statements parsed without errors? HOT 1
- Incorrect AST when using "LIKE" expression HOT 2
- Support for Interval HOT 2
- Feature Request: add stringify method HOT 1
- Confusion about parsing of "not" operator HOT 1
- BUG: Named constraints lose names if other constraints are present
- split into multiple statements
- Do you have the AST definition document? HOT 1
- OVER clause incorrect validation
- Syntax error when use tokenizer arguments
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlite-parser.