Best Practice formatting PLSQL in Java with ANTLR4
I'm creating a configurable code formatter in Java with ANTLR4. I was able to create the parser and the lexer out of the grammar file found on github (https://github.com/antlr/grammars-v4/tree/master/plsql)
This way I also found out, why everyone said this will be a lot of work. I was a little bit overwhelmed at first about the sheer mass of functions created by ANTLR.
But after a while i found out that, (most) of the Enter and Exit-Trigger are needed for formatting PLSQL.
So I'm starting with this, to read a file:
CharStream s = CharStreams.fromPath(Paths.get("D:\dev\sample\green3.sql"));
CaseChangingCharStream upper = new CaseChangingCharStream(s, true);
PlSqlLexer lexer = new PlSqlLexer(upper);
PlSqlParser parser = new PlSqlParser(new CommonTokenStream(lexer));
ParseTree tree = parser.sql_statement();
//ParseTree tree = parser.sql_script();
ParseTreeWalker walker = new ParseTreeWalker();
PlSqlParserBaseListener listener = new PlSqlParserBaseListener();
walker.walk(listener, tree);
String output = listener.getOutput();
System.out.println("[" + output + "]");
So i need to give the ParseTree the information if it is either SQL-script or just a select-statement. Thats ok, i want to be able to do both variants, but with Implementation I'm starting with select-statements only.
e.g. SELECT and FROM have no special rule for themselves so I write "SELECT" to output:
@Override public void enterFrom_clause(PlSqlParser.From_clauseContext ctx) {
indentlevel--;
output = output + " " + "nFROM" + " ";
}
Or if i find multiple output columns in the, i need to find out if it's the first column (this gets set in a different function: enterSelect_list_elements)
@Override public void enterExpression(PlSqlParser.ExpressionContext ctx) {
if (firstcol) {
output = output + " " + ctx.getText();
indentlevel++;
firstcol= false;
}
else {
output = output + ", " + ctx.getText();
}
}
My PlSqlParserBaseListener feels like a state machine now. I have multiple states for when i need to indent or when specific tasks need to be done.
Is this the way to do it? I'm asking, because this is gonna be a lot of work and a lot of things and to consider. Therefore i want to do it the "right way" and create a efficient solution.
My aim is to make it possible to specify spacing, lower/upper case, operators, punctuation, list arrangements and maybe alignment of comments.
Thanks for your feedback! Greetings from Austria
plsql antlr4 code-formatting
add a comment |
I'm creating a configurable code formatter in Java with ANTLR4. I was able to create the parser and the lexer out of the grammar file found on github (https://github.com/antlr/grammars-v4/tree/master/plsql)
This way I also found out, why everyone said this will be a lot of work. I was a little bit overwhelmed at first about the sheer mass of functions created by ANTLR.
But after a while i found out that, (most) of the Enter and Exit-Trigger are needed for formatting PLSQL.
So I'm starting with this, to read a file:
CharStream s = CharStreams.fromPath(Paths.get("D:\dev\sample\green3.sql"));
CaseChangingCharStream upper = new CaseChangingCharStream(s, true);
PlSqlLexer lexer = new PlSqlLexer(upper);
PlSqlParser parser = new PlSqlParser(new CommonTokenStream(lexer));
ParseTree tree = parser.sql_statement();
//ParseTree tree = parser.sql_script();
ParseTreeWalker walker = new ParseTreeWalker();
PlSqlParserBaseListener listener = new PlSqlParserBaseListener();
walker.walk(listener, tree);
String output = listener.getOutput();
System.out.println("[" + output + "]");
So i need to give the ParseTree the information if it is either SQL-script or just a select-statement. Thats ok, i want to be able to do both variants, but with Implementation I'm starting with select-statements only.
e.g. SELECT and FROM have no special rule for themselves so I write "SELECT" to output:
@Override public void enterFrom_clause(PlSqlParser.From_clauseContext ctx) {
indentlevel--;
output = output + " " + "nFROM" + " ";
}
Or if i find multiple output columns in the, i need to find out if it's the first column (this gets set in a different function: enterSelect_list_elements)
@Override public void enterExpression(PlSqlParser.ExpressionContext ctx) {
if (firstcol) {
output = output + " " + ctx.getText();
indentlevel++;
firstcol= false;
}
else {
output = output + ", " + ctx.getText();
}
}
My PlSqlParserBaseListener feels like a state machine now. I have multiple states for when i need to indent or when specific tasks need to be done.
Is this the way to do it? I'm asking, because this is gonna be a lot of work and a lot of things and to consider. Therefore i want to do it the "right way" and create a efficient solution.
My aim is to make it possible to specify spacing, lower/upper case, operators, punctuation, list arrangements and maybe alignment of comments.
Thanks for your feedback! Greetings from Austria
plsql antlr4 code-formatting
add a comment |
I'm creating a configurable code formatter in Java with ANTLR4. I was able to create the parser and the lexer out of the grammar file found on github (https://github.com/antlr/grammars-v4/tree/master/plsql)
This way I also found out, why everyone said this will be a lot of work. I was a little bit overwhelmed at first about the sheer mass of functions created by ANTLR.
But after a while i found out that, (most) of the Enter and Exit-Trigger are needed for formatting PLSQL.
So I'm starting with this, to read a file:
CharStream s = CharStreams.fromPath(Paths.get("D:\dev\sample\green3.sql"));
CaseChangingCharStream upper = new CaseChangingCharStream(s, true);
PlSqlLexer lexer = new PlSqlLexer(upper);
PlSqlParser parser = new PlSqlParser(new CommonTokenStream(lexer));
ParseTree tree = parser.sql_statement();
//ParseTree tree = parser.sql_script();
ParseTreeWalker walker = new ParseTreeWalker();
PlSqlParserBaseListener listener = new PlSqlParserBaseListener();
walker.walk(listener, tree);
String output = listener.getOutput();
System.out.println("[" + output + "]");
So i need to give the ParseTree the information if it is either SQL-script or just a select-statement. Thats ok, i want to be able to do both variants, but with Implementation I'm starting with select-statements only.
e.g. SELECT and FROM have no special rule for themselves so I write "SELECT" to output:
@Override public void enterFrom_clause(PlSqlParser.From_clauseContext ctx) {
indentlevel--;
output = output + " " + "nFROM" + " ";
}
Or if i find multiple output columns in the, i need to find out if it's the first column (this gets set in a different function: enterSelect_list_elements)
@Override public void enterExpression(PlSqlParser.ExpressionContext ctx) {
if (firstcol) {
output = output + " " + ctx.getText();
indentlevel++;
firstcol= false;
}
else {
output = output + ", " + ctx.getText();
}
}
My PlSqlParserBaseListener feels like a state machine now. I have multiple states for when i need to indent or when specific tasks need to be done.
Is this the way to do it? I'm asking, because this is gonna be a lot of work and a lot of things and to consider. Therefore i want to do it the "right way" and create a efficient solution.
My aim is to make it possible to specify spacing, lower/upper case, operators, punctuation, list arrangements and maybe alignment of comments.
Thanks for your feedback! Greetings from Austria
plsql antlr4 code-formatting
I'm creating a configurable code formatter in Java with ANTLR4. I was able to create the parser and the lexer out of the grammar file found on github (https://github.com/antlr/grammars-v4/tree/master/plsql)
This way I also found out, why everyone said this will be a lot of work. I was a little bit overwhelmed at first about the sheer mass of functions created by ANTLR.
But after a while i found out that, (most) of the Enter and Exit-Trigger are needed for formatting PLSQL.
So I'm starting with this, to read a file:
CharStream s = CharStreams.fromPath(Paths.get("D:\dev\sample\green3.sql"));
CaseChangingCharStream upper = new CaseChangingCharStream(s, true);
PlSqlLexer lexer = new PlSqlLexer(upper);
PlSqlParser parser = new PlSqlParser(new CommonTokenStream(lexer));
ParseTree tree = parser.sql_statement();
//ParseTree tree = parser.sql_script();
ParseTreeWalker walker = new ParseTreeWalker();
PlSqlParserBaseListener listener = new PlSqlParserBaseListener();
walker.walk(listener, tree);
String output = listener.getOutput();
System.out.println("[" + output + "]");
So i need to give the ParseTree the information if it is either SQL-script or just a select-statement. Thats ok, i want to be able to do both variants, but with Implementation I'm starting with select-statements only.
e.g. SELECT and FROM have no special rule for themselves so I write "SELECT" to output:
@Override public void enterFrom_clause(PlSqlParser.From_clauseContext ctx) {
indentlevel--;
output = output + " " + "nFROM" + " ";
}
Or if i find multiple output columns in the, i need to find out if it's the first column (this gets set in a different function: enterSelect_list_elements)
@Override public void enterExpression(PlSqlParser.ExpressionContext ctx) {
if (firstcol) {
output = output + " " + ctx.getText();
indentlevel++;
firstcol= false;
}
else {
output = output + ", " + ctx.getText();
}
}
My PlSqlParserBaseListener feels like a state machine now. I have multiple states for when i need to indent or when specific tasks need to be done.
Is this the way to do it? I'm asking, because this is gonna be a lot of work and a lot of things and to consider. Therefore i want to do it the "right way" and create a efficient solution.
My aim is to make it possible to specify spacing, lower/upper case, operators, punctuation, list arrangements and maybe alignment of comments.
Thanks for your feedback! Greetings from Austria
plsql antlr4 code-formatting
plsql antlr4 code-formatting
asked Nov 22 '18 at 11:03
krstn420krstn420
32
32
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429521%2fbest-practice-formatting-plsql-in-java-with-antlr4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429521%2fbest-practice-formatting-plsql-in-java-with-antlr4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown