Skip to content

Commit

Permalink
(github dnsmkl#18) Fix spacing around operators
Browse files Browse the repository at this point in the history
Issue was registered only regarding paranthesis,
but problem was wider - also ',' (comma) was impacted.

There is downside: now we get strange formatting for '(x)as col_alias'.
  • Loading branch information
dnsmkl committed Aug 6, 2017
1 parent e8fa904 commit 6ab8ec2
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib_fsqlf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(libfsqlf_sources
formatter/lex_wrapper.c
formatter/print_keywords.c
formatter/tokque.c
kw/is_word.c
kw/kw.c
kw/kwmap.c
lex/token.c)
Expand Down
1 change: 0 additions & 1 deletion lib_fsqlf/formatter/print_keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ static struct fsqlf_spacing calculate_spacing_pure(
, beforespacing_of_current.space
, beforespacing_of_current.new_line || beforespacing_of_current.indent);


// adjacent words MUST have some spacing
if (!r.new_line && !r.indent && !r.space &&
isword_of_prev && isword_of_current) {
Expand Down
3 changes: 2 additions & 1 deletion lib_fsqlf/formatter/tokque.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h> // fprintf, fputs
#include <assert.h> // assert
#include <ctype.h> // isalnum
#include "../kw/is_word.h"
#include "../lex/token.h" // struct FSQLF_token, FSQLF_clear_token, FSQLF_set_token
#include "print_keywords.h" // FSQLF_print
#include "tokque.h"
Expand Down Expand Up @@ -32,7 +33,7 @@ static struct fsqlf_kw_conf * dummy_kw_for_txt(char *txt, size_t length)

// Word-vs-operator check.
// Ensure that two adjacent words have spacing inbetween.
kw->is_word = !(length == 1 && !isalnum(txt[0]));
kw->is_word = FSQLF_is_word(txt, length);
return kw;
}

Expand Down
22 changes: 22 additions & 0 deletions lib_fsqlf/kw/is_word.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <ctype.h>
#include <stddef.h>


// "word" as opposed to "operator"/"separator"/"delimiter".
// "SELECT" is a word, "<=" is not a word.
// It is necessary to separate "SELECT" and "column1" with space.
//
// Side-note:
// "<" and "=" also need space preserved between them.
// (in case such malformed SQL is formatted)
int FSQLF_is_word(char* text, size_t length) {
if (length <= 2) {
for (size_t i = 0; i < length; ++i) {
if (!isalnum(text[i])) {
return 0;
}
}
}

return 1;
}
8 changes: 8 additions & 0 deletions lib_fsqlf/kw/is_word.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef IS_WORD_H
#define IS_WORD_H


int FSQLF_is_word(char* txt, size_t length);


#endif
24 changes: 14 additions & 10 deletions lib_fsqlf/kw/kwmap.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <lib_fsqlf.h>
#include "is_word.h"


// Functions that operate on kw-map as a whole.
Expand Down Expand Up @@ -30,19 +31,22 @@ void fsqlf_kwmap_init(fsqlf_kwmap_t *kwmap)
*kwmap = NULL;
#define XMACRO(NAME, gib, nlb, tb, sb, gia, nla, ta, sa, TEXT) \
do { \
size_t length = strlen(TEXT); \
FSQLF_kw_create(kwmap, #NAME); \
fsqlf_kw_get(*kwmap, #NAME)->before.global_indent_change = gib; \
fsqlf_kw_get(*kwmap, #NAME)->before.new_line = nlb; \
fsqlf_kw_get(*kwmap, #NAME)->before.indent = tb; \
fsqlf_kw_get(*kwmap, #NAME)->before.space = sb; \
fsqlf_kw_get(*kwmap, #NAME)->before.new_line = nlb; \
fsqlf_kw_get(*kwmap, #NAME)->before.indent = tb; \
fsqlf_kw_get(*kwmap, #NAME)->before.space = sb; \
fsqlf_kw_get(*kwmap, #NAME)->after.global_indent_change = gia; \
fsqlf_kw_get(*kwmap, #NAME)->after.new_line = nla; \
fsqlf_kw_get(*kwmap, #NAME)->after.indent = ta; \
fsqlf_kw_get(*kwmap, #NAME)->after.space = sa; \
fsqlf_kw_get(*kwmap, #NAME)->print_original_text = FSQLF_KWSPELLING_USE_HARDCODED_DEFAULT; \
fsqlf_kw_get(*kwmap, #NAME)->print_case = FSQLF_KWCASE_UPPER; \
fsqlf_kw_get(*kwmap, #NAME)->text = TEXT; \
fsqlf_kw_get(*kwmap, #NAME)->is_word = 1; \
fsqlf_kw_get(*kwmap, #NAME)->after.new_line = nla; \
fsqlf_kw_get(*kwmap, #NAME)->after.indent = ta; \
fsqlf_kw_get(*kwmap, #NAME)->after.space = sa; \
fsqlf_kw_get(*kwmap, #NAME)->print_original_text = \
FSQLF_KWSPELLING_USE_HARDCODED_DEFAULT; \
fsqlf_kw_get(*kwmap, #NAME)->print_case = FSQLF_KWCASE_UPPER; \
fsqlf_kw_get(*kwmap, #NAME)->text = TEXT; \
fsqlf_kw_get(*kwmap, #NAME)->is_word = \
FSQLF_is_word(TEXT, length); \
} while (0);
#include "kwmap_defaults.def"
#undef XMACRO
Expand Down
2 changes: 1 addition & 1 deletion lib_fsqlf/kw/kwmap_defaults.def
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ XMACRO( kw_right_p_sub , 0, 1, 0, 0, 0, 0, 0, 1, ")" )
XMACRO( kw_create , 0, 2, 0, 0, 0, 0, 0, 0, "CREATE" )
XMACRO( kw_left_p_create , 0, 1, 0, 0, 0, 1, 0, 0, "(" )
XMACRO( kw_right_p_create , 0, 1, 0, 0, 0, 1, 0, 0, ")" )
XMACRO( kw_comma_create , 0, 1, 0, 0, 0, 0, 0, 0, "," )
XMACRO( kw_comma_create , 0, 1, 0, 0, 0, 0, 0, 1, "," )
XMACRO( kw_drop , 0, 2, 0, 0, 0, 0, 0, 0, "DROP" )
XMACRO( kw_table , 0, 0, 0, 1, 0, 0, 0, 0, "TABLE" )
XMACRO( kw_ifexists , 0, 0, 0, 0, 0, 0, 0, 0, "IF EXISTS" )
Expand Down
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ LCOBJ += $(BLD)/lib_fsqlf/formatter/print_keywords.o
LCOBJ += $(BLD)/lib_fsqlf/formatter/tokque.o
LCOBJ += $(BLD)/lib_fsqlf/kw/kw.o
LCOBJ += $(BLD)/lib_fsqlf/kw/kwmap.o
LCOBJ += $(BLD)/lib_fsqlf/kw/is_word.o
LCOBJ += $(BLD)/lib_fsqlf/lex/token.o
LCOBJ += $(BLD)/utils/queue/queue.o
LCOBJ += $(BLD)/utils/stack/stack.o
Expand Down
20 changes: 10 additions & 10 deletions tests/cases/bigquery_expected.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@

SELECT
asbaba AS g
, cast ( bfbfb AS date ) + 1 as x
, cast(bfbfb AS date) + 1 as x
, 2 - 1 as b1
, 2 * 1 as c1
, 2 / 1 as d1
, "str" || "cat"as concatted /*/*/
, min ( a ) over ( partition by x,c order by r rows between 1 preceeding and 1 preceeding )
, min(a)over(partition by x,c order by r rows between 1 preceeding and 1 preceeding)
, 'dfgdf/*gdfgd*/fg''dfgdf/*gdfgd*/fg''dfgdf/*gdfgd*/fg' as c /*a*a*a*/
, cast ( 19.2 AS decimal ( 18,2 ) ) /******/
, cast(19.2 AS decimal(18,2))/******/
, ' */ ' /**/
, cast ( 19.2 AS decimal ( 18,2 ) ) /* sdsd *****---*/
, asextract ( day FROM t ) as _toto -- comment case select from
FROM ( bobo as gogo
, cast(19.2 AS decimal(18,2))/* sdsd *****---*/
, asextract(day FROM t)as _toto -- comment case select from
FROM (bobo as gogo
LEFT JOIN
(
SELECT
Expand All @@ -36,13 +36,13 @@ LEFT JOIN
2
) T
) baba
ON 1 IN ( 1,2,3 )
ON 1 IN (1,2,3)
OR x <> 1
AND 1 = 1
AND 1 = 1
AND "a" LIKE "b")
LEFT JOIN ( baba
CROSS JOIN gaga )
LEFT JOIN (baba
CROSS JOIN gaga)
ON baba.g = T.z
WHERE toto
AND EXISTS
Expand Down Expand Up @@ -70,7 +70,7 @@ AND a IN
1
FROM hsd
)
AND b IN ( 19,2,3 )
AND b IN (19,2,3)
;


Expand Down
8 changes: 4 additions & 4 deletions tests/cases/create_table_expected.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
CREATE TABLE schema_name.table_name
(
id int
, col2 varchar ( 30 )
, col3 non standart keywords char ( 1 )
, col2 varchar(30)
, col3 non standart keywords char(1)
)
;

Expand All @@ -14,7 +14,7 @@ id int
CREATE volatile multiset TABLE schema_name.table_name
(
id int
, col2 varchar ( 30 )
, col3 non standart keywords char ( 1 )
, col2 varchar(30)
, col3 non standart keywords char(1)
)
;
4 changes: 2 additions & 2 deletions tests/cases/group_order_expected.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ SELECT
1
, 2
FROM x
GROUP BY 1 , 2 , 3 , 4
ORDER BY 1 , 2
GROUP BY 1, 2, 3, 4
ORDER BY 1, 2
;
2 changes: 1 addition & 1 deletion tests/cases/using_expected.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ SELECT
*
FROM table1
JOIN table2 USING
( nicekey )
(nicekey)

0 comments on commit 6ab8ec2

Please sign in to comment.