Skip to content

Commit

Permalink
Sort grammar rewrite and bugfix with multiple fields (#65)
Browse files Browse the repository at this point in the history
* Sort grammar refactoring, fix with multiple commas, unit tests for sort

* Unit tests for sort: minus and plus options with sorting modes
  • Loading branch information
51-code authored Aug 21, 2024
1 parent cd379e2 commit b02551e
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/antlr4/imports/DPLParserTransform_sort.g4
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
parser grammar DPLParserTransform_sort;

sortTransformation
: COMMAND_MODE_SORT (t_sort_integerType | t_sort_limitParameter | t_sort_sortByClauseInstruction ) t_sort_dParameter*? (COMMA? (t_sort_integerType | t_sort_limitParameter | t_sort_sortByClauseInstruction )*? t_sort_dParameter*?)
: COMMAND_MODE_SORT (t_sort_integerType | t_sort_limitParameter | t_sort_dParameter | COMMA? t_sort_sortByClauseInstruction)*
;

t_sort_sortByClauseInstruction
: (t_sort_byMethodAuto|t_sort_byMethodStr|t_sort_byMethodIp|t_sort_byMethodNum)
| (t_sortMinusOption? sortFieldType)
| ((t_sortMinusOption | t_sortPlusOption)? sortFieldType)
;

t_sort_dParameter
Expand All @@ -64,24 +64,27 @@ t_sort_limitParameter
;

t_sort_byMethodAuto
: ( t_sortMinusOption? COMMAND_SORT_MODE_AUTO ) COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
: ( (t_sortMinusOption | t_sortPlusOption)? COMMAND_SORT_MODE_AUTO ) COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
;

t_sort_byMethodStr
: ( t_sortMinusOption? COMMAND_SORT_MODE_STR ) COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
: ( (t_sortMinusOption | t_sortPlusOption)? COMMAND_SORT_MODE_STR ) COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
;

t_sort_byMethodIp
: t_sortMinusOption? COMMAND_SORT_MODE_IP COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
: (t_sortMinusOption | t_sortPlusOption)? COMMAND_SORT_MODE_IP COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
;

t_sort_byMethodNum
: t_sortMinusOption? COMMAND_SORT_MODE_NUM COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
: (t_sortMinusOption | t_sortPlusOption)? COMMAND_SORT_MODE_NUM COMMAND_SORT_MODE_PARENTHESIS_L fieldType COMMAND_SORT_MODE_PARENTHESIS_R
;

t_sortMinusOption
: COMMAND_SORT_MODE_MINUS
;

t_sortPlusOption
: COMMAND_SORT_MODE_PLUS
| COMMAND_SORT_MODE_MINUS
;

t_sort_integerType
Expand Down
298 changes: 298 additions & 0 deletions src/test/java/com/teragrep/pth_03/tests/SortSyntaxTests.java

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions src/test/resources/antlr4/commands/sort/sort.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->
| sort foo, bar, foobar
46 changes: 46 additions & 0 deletions src/test/resources/antlr4/commands/sort/sortdesc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->
| sort field1 desc, field2 d
46 changes: 46 additions & 0 deletions src/test/resources/antlr4/commands/sort/sortlimitbyint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->
| sort 5 field
46 changes: 46 additions & 0 deletions src/test/resources/antlr4/commands/sort/sortminusplus.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->
| sort -field1 +field2
47 changes: 47 additions & 0 deletions src/test/resources/antlr4/commands/sort/sortmodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->

| sort auto(field), num(field2), str(field3), ip(field4) limit=5
47 changes: 47 additions & 0 deletions src/test/resources/antlr4/commands/sort/sortmodesminus.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->

| sort -auto(field), -num(field2), -str(field3), -ip(field4)
47 changes: 47 additions & 0 deletions src/test/resources/antlr4/commands/sort/sortmodesplus.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- /*
* Teragrep Data Processing Language Parser Library PTH-03
* Copyright (C) 2019, 2020, 2021, 2022 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/ -->

| sort +auto(field), +num(field2), +str(field3), +ip(field4)

0 comments on commit b02551e

Please sign in to comment.