NoNormalCreeper / braligne-formatter

🚧 Align braces and semicolons in a code, for fun
1 stars 0 forks source link

Can't format array correctly #1

Open NoNormalCreeper opened 2 years ago

NoNormalCreeper commented 2 years ago

example:

int[][] matrix = { { 2, 3, 4 }, { 5, 6, 4 } };
Suryasai369 commented 2 years ago

can you just add some little more information.

NoNormalCreeper commented 2 years ago

Example

Original:

int row = 2, column = 3;
int[][] matrix = { { 2, 3, 4 }, { 5, 6, 4 } };

Expected:

int row = 2, column = 3                        ;
int[][] matrix = { { 2, 3, 4 }, { 5, 6, 4 } }  ;

Actual:

int row = 2, column = 3                        ;}}}
int[][] matrix =                               { { 2, 3, 4 , { 5, 6, 4    ;

Analysis

That's because this formatter thinks the } here is used to close a block rather than define an array.

But I don't know how to analyze if a } in code is used to close a block or to define an array, etc.

Suryasai369 commented 2 years ago

I think I must go through the code. What you are asking is very tricky and I think we have to use Stack to store parenthesis information and format according to it. IF you have any idea how to solve it, let me know.

NoNormalCreeper commented 2 years ago

I think I must go through the code. What you are asking is very tricky and I think we have to use Stack to store parenthesis information and format according to it. IF you have any idea how to solve it, let me know.

I have similar ideas with you. I think this involves syntax analysis in compilation principles as well, so I'll learn more about this.

BTW, my code is a bit messy and without comment, so if you have any problems understanding the code, feel free to let me know.

NoNormalCreeper commented 2 years ago

After looking through some materials, I realized that my method of using regex and other string processing is totally wrong.

So, in my opinion, it needs some syntax analysis tools to help me with that, such as antlr/antlr4.

Using this kind of tools, I can easily analyze the symbols in a code.