BashSpark
Loading...
Searching...
No Matches
shell_node.h
Go to the documentation of this file.
1
34#pragma once
35
36#include <memory>
37#include <stdexcept>
38#include <string>
39#include <vector>
40
42
43namespace bs {
44 class shell_session;
45}
46
47namespace bs {
56 enum class shell_node_type {
57 SNT_COMMAND_EXPRESSION,
58 SNT_STR_SIMPLE,
59 SNT_STR_DOUBLE,
60 SNT_STR_BACK,
61 SNT_WORD,
62 SNT_UNICODE,
63 SNT_ARG,
64 SNT_VARIABLE,
65 SNT_DOLLAR_SPECIAL,
66 SNT_DOLLAR_VARIABLE,
67 SNT_DOLLAR_VARIABLE_DHOP,
68 SNT_DOLLAR_ARG,
69 SNT_DOLLAR_ARG_DHOP,
70 SNT_DOLLAR_COMMAND,
71 SNT_BACKGROUND,
72 SNT_AND,
73 SNT_PIPE,
74 SNT_OR,
75 // Structure
76 SNT_IF,
77 SNT_TEST,
78 SNT_FOR,
79 SNT_WHILE,
80 SNT_UNTIL,
81 SNT_BREAK,
82 SNT_CONTINUE,
83 SNT_FUNCTION,
84 // Executable
85 SNT_NULL_COMMAND,
86 SNT_COMMAND,
87 SNT_COMMAND_BLOCK,
88 SNT_COMMAND_BLOCK_SUBSHELL,
89 };
90
98 class shell_node_invalid_argument final : public std::invalid_argument {
99 public:
104 explicit shell_node_invalid_argument(const std::string &sMessage)
105 : std::invalid_argument(sMessage) {
106 }
107 };
108
117 public:
119 virtual ~shell_node() = default;
120
121 protected:
128 const shell_node_type nType,
129 const std::size_t nPos
130 )
131 : m_nType(nType),
132 m_nPos(nPos) {
133 }
134
135 public:
140 [[nodiscard]] shell_node_type get_type() const noexcept {
141 return this->m_nType;
142 }
143
148 [[nodiscard]] std::size_t get_pos() const noexcept {
149 return this->m_nPos;
150 }
151
152 protected:
154 const std::size_t m_nPos;
155 };
156
164 class shell_node_evaluable : public virtual shell_node {
165 public:
172 const shell_node_type nType,
173 const std::size_t nPos
174 )
175 : shell_node(nType, nPos) {
176 }
177
178 public:
184 virtual shell_status evaluate(shell_session &oSession) const =0;
185 };
186
194 class shell_node_expandable : public virtual shell_node {
195 public:
202 const shell_node_type nType,
203 const std::size_t nPos
204 )
205 : shell_node(nType, nPos) {
206 }
207
208 public:
215 virtual void expand(
216 std::vector<std::string> &vTokens,
217 shell_session &oSession,
218 bool bSplit
219 ) const =0;
220 };
221
230 public:
239 std::vector<std::unique_ptr<shell_node_expandable> > &&vChildren
240 );
241
242 public:
247 void expand(
248 std::vector<std::string> &vTokens,
249 shell_session &oSession,
250 bool bSplit
251 ) const override;
252
253 public:
258 [[nodiscard]] const std::vector<std::unique_ptr<shell_node_expandable> > &get_children() const noexcept {
259 return this->m_vChildren;
260 }
261
262 private:
263 std::vector<std::unique_ptr<shell_node_expandable> > m_vChildren;
264 };
265
274 protected:
283 shell_node_type nType,
284 std::size_t nPos,
285 std::vector<std::unique_ptr<shell_node_expandable> > &&vChildren
286 );
287
288 public:
293 void expand(
294 std::vector<std::string> &vTokens,
295 shell_session &oSession,
296 bool bSplit
297 ) const override;
298
303 [[nodiscard]] const std::vector<std::unique_ptr<shell_node_expandable> > &get_children() const noexcept {
304 return this->m_vChildren;
305 }
306
307 private:
308 std::vector<std::unique_ptr<shell_node_expandable> > m_vChildren;
309 };
310
318 public:
325 const std::size_t nPos,
326 std::vector<std::unique_ptr<shell_node_expandable> > &&vChildren
327 )
328 : shell_node(shell_node_type::SNT_STR_SIMPLE, nPos),
329 shell_node_str(shell_node_type::SNT_STR_SIMPLE, nPos, std::move(vChildren)) {
330 }
331 };
332
340 public:
347 const std::size_t nPos,
348 std::vector<std::unique_ptr<shell_node_expandable> > &&vChildren
349 )
350 : shell_node(shell_node_type::SNT_STR_DOUBLE, nPos),
351 shell_node_str(shell_node_type::SNT_STR_DOUBLE, nPos, std::move(vChildren)) {
352 }
353 };
354
363 public:
371 std::size_t nPos,
372 std::unique_ptr<shell_node_evaluable> &&pCommand
373 );
374
375 public:
380 void expand(
381 std::vector<std::string> &vTokens,
382 shell_session &oSession,
383 bool bSplit
384 ) const override;
385
390 [[nodiscard]] const shell_node_evaluable *get_command() const noexcept {
391 return this->m_pCommand.get();
392 }
393
394 private:
395 std::unique_ptr<shell_node_evaluable> m_pCommand;
396 };
397
405 public:
412 const std::size_t nPos,
413 std::string sText
414 ) : shell_node(shell_node_type::SNT_WORD, nPos),
415 shell_node_expandable(shell_node_type::SNT_WORD, nPos),
416 m_sText(std::move(sText)) {
417 }
418
419 public:
424 void expand(
425 std::vector<std::string> &vTokens,
426 shell_session &oSession,
427 bool bSplit
428 ) const override;
429
430 public:
435 [[nodiscard]] std::string get_text() const noexcept {
436 return this->m_sText;
437 }
438
439 private:
440 std::string m_sText;
441 };
442
451 public:
458 const std::size_t nPos,
459 const char32_t cCharacter
460 ) : shell_node(shell_node_type::SNT_UNICODE, nPos),
461 shell_node_expandable(shell_node_type::SNT_UNICODE, nPos),
462 m_cCharacter(cCharacter) {
463 }
464
465 public:
469 void expand(
470 std::vector<std::string> &vTokens,
471 shell_session &oSession,
472 bool bSplit
473 ) const override;
474
475 public:
480 [[nodiscard]] char32_t get_character() const noexcept {
481 return this->m_cCharacter;
482 }
483
484 private:
485 char32_t m_cCharacter;
486 };
487
496 public:
503 const shell_node_type nType,
504 const std::size_t nPos
505 )
506 : shell_node(nType, nPos),
507 shell_node_expandable(nType, nPos) {
508 }
509
510 public:
517 void expand(
518 std::vector<std::string> &vTokens,
519 shell_session &oSession,
520 bool bSplit
521 ) const override;
522
523 public:
529 [[nodiscard]] virtual std::string get_value(const shell_session &oSession) const =0;
530 };
531
537 public:
544 const std::size_t nPos,
545 const std::uint64_t nArg
546 ) : shell_node(shell_node_type::SNT_ARG, nPos),
548 m_nArg(nArg) {
549 }
550
551 public:
557 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
558
563 [[nodiscard]] std::uint64_t get_arg() const noexcept {
564 return this->m_nArg;
565 }
566
567 private:
568 std::uint64_t m_nArg;
569 };
570
578 public:
585 const std::size_t nPos,
586 std::string sVariable
587 ) : shell_node(shell_node_type::SNT_VARIABLE, nPos),
589 m_sVariable(std::move(sVariable)) {
590 }
591
592 public:
598 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
599
604 [[nodiscard]] std::string get_variable() const noexcept {
605 return this->m_sVariable;
606 }
607
608 private:
609 std::string m_sVariable;
610 };
611
619 public:
626 const std::size_t nPos,
627 std::string sVariable
628 ) : shell_node(shell_node_type::SNT_DOLLAR_VARIABLE, nPos),
629 shell_node_session_extractor(shell_node_type::SNT_DOLLAR_VARIABLE, nPos),
630 m_sVariable(std::move(sVariable)) {
631 }
632
633 public:
639 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
640
645 [[nodiscard]] std::string get_variable() const noexcept {
646 return this->m_sVariable;
647 }
648
649 private:
650 std::string m_sVariable;
651 };
652
658 public:
665 const std::size_t nPos,
666 const std::uint64_t nArg
667 ) : shell_node(shell_node_type::SNT_DOLLAR_ARG, nPos),
668 shell_node_session_extractor(shell_node_type::SNT_DOLLAR_ARG, nPos),
669 m_nArg(nArg) {
670 }
671
672 public:
678 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
679
684 [[nodiscard]] std::uint64_t get_arg() const noexcept {
685 return this->m_nArg;
686 }
687
688 private:
689 std::uint64_t m_nArg;
690 };
691
697 public:
704 const std::size_t nPos,
705 const std::uint64_t nArg
706 ) : shell_node(shell_node_type::SNT_DOLLAR_ARG_DHOP, nPos),
707 shell_node_session_extractor(shell_node_type::SNT_DOLLAR_ARG_DHOP, nPos),
708 m_nArg(nArg) {
709 }
710
711 public:
717 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
718
723 [[nodiscard]] std::uint64_t get_arg() const noexcept {
724 return this->m_nArg;
725 }
726
727 private:
728 std::uint64_t m_nArg;
729 };
730
736 public:
743 const std::size_t nPos,
744 std::string sVariable
745 ) : shell_node(shell_node_type::SNT_DOLLAR_VARIABLE_DHOP, nPos),
746 shell_node_session_extractor(shell_node_type::SNT_DOLLAR_VARIABLE_DHOP, nPos),
747 m_sVariable(std::move(sVariable)) {
748 }
749
750 public:
756 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
757
762 [[nodiscard]] std::string get_variable() const noexcept {
763 return this->m_sVariable;
764 }
765
766 private:
767 std::string m_sVariable;
768 };
769
777 public:
785 std::size_t nPos,
786 std::unique_ptr<shell_node_evaluable> &&pCommand
787 );
788
789 public:
794 void expand(
795 std::vector<std::string> &vTokens,
796 shell_session &oSession,
797 bool bSplit
798 ) const override;
799
804 [[nodiscard]] const shell_node_evaluable *get_command() const noexcept {
805 return this->m_pCommand.get();
806 }
807
808 private:
809 std::unique_ptr<shell_node_evaluable> m_pCommand;
810 };
811
817 public:
824 const std::size_t nPos,
825 const char cItem
826 )
827 : shell_node(shell_node_type::SNT_DOLLAR_SPECIAL, nPos),
828 shell_node_session_extractor(shell_node_type::SNT_DOLLAR_SPECIAL, nPos),
829 m_cItem(cItem) {
830 }
831
832 public:
838 [[nodiscard]] std::string get_value(const shell_session &oSession) const override;
839
844 [[nodiscard]] char get_item() const noexcept {
845 return this->m_cItem;
846 }
847
848 private:
849 char m_cItem;
850 };
851
857 public:
863 const std::size_t nPos
864 )
865 : shell_node(shell_node_type::SNT_NULL_COMMAND, nPos),
866 shell_node_evaluable(shell_node_type::SNT_NULL_COMMAND, nPos) {
867 }
868
869 public:
875 shell_status evaluate(shell_session &oSession) const override;
876 };
877
886 public:
892 explicit shell_node_command(
893 std::unique_ptr<shell_node_command_expression> &&pCommand
894 );
895
896 public:
902 shell_status evaluate(shell_session &oSession) const override;
903
908 [[nodiscard]] const shell_node_command_expression *get_command() const noexcept {
909 return this->m_pCommand.get();
910 }
911
912 private:
913 std::unique_ptr<shell_node_command_expression> m_pCommand;
914 };
915
921 public:
928 const std::size_t nPos,
929 std::vector<std::unique_ptr<shell_node_evaluable> > &&vSubCommands
930 )
931 : shell_node(shell_node_type::SNT_COMMAND_BLOCK, nPos),
932 shell_node_evaluable(shell_node_type::SNT_COMMAND_BLOCK, nPos),
933 m_vSubCommands(std::move(vSubCommands)) {
934 }
935
936 public:
942 shell_status evaluate(shell_session &oSession) const override;
943
948 [[nodiscard]] const std::vector<std::unique_ptr<shell_node_evaluable> > &get_children() const noexcept {
949 return this->m_vSubCommands;
950 }
951
952 private:
953 std::vector<std::unique_ptr<shell_node_evaluable> > m_vSubCommands;
954 };
955
961 public:
968 const std::size_t nPos,
969 std::vector<std::unique_ptr<shell_node_evaluable> > &&vSubCommands
970 )
971 : shell_node(shell_node_type::SNT_COMMAND_BLOCK_SUBSHELL, nPos),
972 shell_node_evaluable(shell_node_type::SNT_COMMAND_BLOCK_SUBSHELL, nPos),
973 m_vSubCommands(std::move(vSubCommands)) {
974 }
975
976 public:
982 shell_status evaluate(shell_session &oSession) const override;
983
988 [[nodiscard]] const std::vector<std::unique_ptr<shell_node_evaluable> > &get_children() const noexcept {
989 return this->m_vSubCommands;
990 }
991
992 private:
993 std::vector<std::unique_ptr<shell_node_evaluable> > m_vSubCommands;
994 };
995
1003 public:
1011 std::size_t nPos,
1012 std::unique_ptr<shell_node_evaluable> &&pCommand
1013 );
1014
1015 public:
1021 shell_status evaluate(shell_session &oSession) const override;
1022
1023 public:
1028 [[nodiscard]] const shell_node_evaluable *get_command() const noexcept {
1029 return this->m_pCommand.get();
1030 }
1031
1032 private:
1033 std::unique_ptr<shell_node_evaluable> m_pCommand;
1034 };
1035
1044 public:
1046 constexpr static int PRIORITY_PIPE = 5;
1048 constexpr static int PRIORITY_AND = 4;
1050 constexpr static int PRIORITY_OR = 3;
1051
1052 public:
1067 static std::unique_ptr<shell_node_evaluable> make(
1068 shell_node_type nType,
1069 std::size_t nPos,
1070 std::unique_ptr<shell_node_evaluable> &&pLeft,
1071 std::unique_ptr<shell_node_evaluable> &&pRight
1072 );
1073
1074 protected:
1085 shell_node_type nType,
1086 std::size_t nPos,
1087 int nPriority,
1088 std::unique_ptr<shell_node_evaluable> &&pLeft,
1089 std::unique_ptr<shell_node_evaluable> &&pRight
1090 );
1091
1092 protected:
1103 shell_node_type nType,
1104 std::size_t nPos,
1105 int nPriority
1106 );
1107
1108 public:
1113 [[nodiscard]] int get_priority() const noexcept {
1114 return this->m_nPriority;
1115 }
1116
1121 [[nodiscard]] const shell_node_evaluable *get_left() const noexcept {
1122 return this->m_pLeft.get();
1123 }
1124
1129 [[nodiscard]] const shell_node_evaluable *get_right() const noexcept {
1130 return this->m_pRight.get();
1131 }
1132
1139 std::unique_ptr<shell_node_evaluable> swap_left(std::unique_ptr<shell_node_evaluable> &&pLeft);
1140
1147 std::unique_ptr<shell_node_evaluable> swap_right(std::unique_ptr<shell_node_evaluable> &&pRight);
1148
1149 private:
1150 int m_nPriority;
1151 std::unique_ptr<shell_node_evaluable> m_pLeft;
1152 std::unique_ptr<shell_node_evaluable> m_pRight;
1153 };
1154
1160 friend class shell_node_operator;
1161
1162 public:
1171 const std::size_t nPos,
1172 std::unique_ptr<shell_node_evaluable> &&pLeft,
1173 std::unique_ptr<shell_node_evaluable> &&pRight
1174 )
1175 : shell_node(shell_node_type::SNT_AND, nPos),
1177 shell_node_type::SNT_AND, nPos, PRIORITY_AND,
1178 std::move(pLeft), std::move(pRight)
1179 ) {
1180 }
1181
1182 public:
1188 shell_status evaluate(shell_session &oSession) const override;
1189
1190 private:
1191 explicit shell_node_and(
1192 const std::size_t nPos
1193 )
1194 : shell_node(shell_node_type::SNT_AND, nPos),
1196 }
1197 };
1198
1204 friend class shell_node_operator;
1205
1206 public:
1215 const std::size_t nPos,
1216 std::unique_ptr<shell_node_evaluable> &&pLeft,
1217 std::unique_ptr<shell_node_evaluable> &&pRight
1218 )
1219 : shell_node(shell_node_type::SNT_PIPE, nPos),
1221 shell_node_type::SNT_PIPE, nPos, PRIORITY_PIPE,
1222 std::move(pLeft), std::move(pRight)
1223 ) {
1224 }
1225
1226 public:
1232 shell_status evaluate(shell_session &oSession) const override;
1233
1234 private:
1235 explicit shell_node_pipe(
1236 const std::size_t nPos
1237 )
1238 : shell_node(shell_node_type::SNT_PIPE, nPos),
1240 }
1241 };
1242
1247 class shell_node_or final : public shell_node_operator {
1248 friend class shell_node_operator;
1249
1250 public:
1259 const std::size_t nPos,
1260 std::unique_ptr<shell_node_evaluable> &&pLeft,
1261 std::unique_ptr<shell_node_evaluable> &&pRight
1262 )
1263 : shell_node(shell_node_type::SNT_OR, nPos),
1265 shell_node_type::SNT_OR, nPos, PRIORITY_OR,
1266 std::move(pLeft), std::move(pRight)
1267 ) {
1268 }
1269
1270 public:
1276 shell_status evaluate(shell_session &oSession) const override;
1277
1278 private:
1279 explicit shell_node_or(
1280 const std::size_t nPos
1281 )
1282 : shell_node(shell_node_type::SNT_OR, nPos),
1284 }
1285 };
1286
1297 public:
1305 std::size_t nPos,
1306 std::unique_ptr<shell_node_expandable> &&pTest
1307 );
1308
1309 public:
1321 shell_status evaluate(shell_session &oSession) const override;
1322
1323 public:
1328 [[nodiscard]] const shell_node_expandable *get_test() const noexcept {
1329 return this->m_pTest.get();
1330 }
1331
1332 private:
1334 std::unique_ptr<shell_node_expandable> m_pTest;
1335 };
1336
1337
1346 public:
1356 std::size_t nPos,
1357 std::unique_ptr<shell_node_evaluable> &&pCondition,
1358 std::unique_ptr<shell_node_evaluable> &&pCaseIf,
1359 std::unique_ptr<shell_node_evaluable> &&pCaseElse
1360 );
1361
1362 public:
1374 shell_status evaluate(shell_session &oSession) const override;
1375
1376 public:
1381 [[nodiscard]] const shell_node_evaluable *get_condition() const noexcept {
1382 return this->m_pCondition.get();
1383 }
1384
1389 [[nodiscard]] const shell_node_evaluable *get_case_if() const noexcept {
1390 return this->m_pCaseIf.get();
1391 }
1392
1397 [[nodiscard]] const shell_node_evaluable *get_case_else() const noexcept {
1398 return this->m_pCaseElse.get();
1399 }
1400
1401 private:
1403 std::unique_ptr<shell_node_evaluable> m_pCondition;
1405 std::unique_ptr<shell_node_evaluable> m_pCaseIf;
1407 std::unique_ptr<shell_node_evaluable> m_pCaseElse;
1408 };
1409
1419 public:
1427 class continue_signal final : public std::runtime_error {
1428 public:
1430 continue_signal() : std::runtime_error("") {
1431 }
1432 };
1433
1434 public:
1446 shell_status evaluate(shell_session &oSession) const override;
1447
1448 public:
1453 explicit shell_node_continue(const std::size_t nPos)
1454 : shell_node(shell_node_type::SNT_CONTINUE, nPos),
1455 shell_node_evaluable(shell_node_type::SNT_CONTINUE, nPos) {
1456 }
1457 };
1458
1467 public:
1475 class break_signal final : public std::runtime_error {
1476 public:
1478 break_signal() : std::runtime_error("") {
1479 }
1480 };
1481
1482 public:
1493 shell_status evaluate(shell_session &oSession) const override;
1494
1495 public:
1500 explicit shell_node_break(const std::size_t nPos)
1501 : shell_node(shell_node_type::SNT_CONTINUE, nPos),
1502 shell_node_evaluable(shell_node_type::SNT_CONTINUE, nPos) {
1503 }
1504 };
1505
1514 public:
1524 std::size_t nPos,
1525 std::string sVariable,
1526 std::unique_ptr<shell_node_expandable> &&pSequence,
1527 std::unique_ptr<shell_node_evaluable> &&pIterative
1528 );
1529
1530 public:
1542 shell_status evaluate(shell_session &oSession) const override;
1543
1544 public:
1549 [[nodiscard]] const std::string &get_variable() const noexcept {
1550 return this->m_sVariable;
1551 }
1552
1557 [[nodiscard]] const shell_node_expandable *get_sequence() const noexcept {
1558 return this->m_pSequence.get();
1559 }
1560
1565 [[nodiscard]] const shell_node_evaluable *get_iterative() const noexcept {
1566 return this->m_pIterative.get();
1567 }
1568
1569 private:
1570 std::string m_sVariable;
1571 std::unique_ptr<shell_node_expandable> m_pSequence;
1572 std::unique_ptr<shell_node_evaluable> m_pIterative;
1573 };
1574
1575
1587 public:
1596 std::size_t nPos,
1597 std::unique_ptr<shell_node_evaluable> &&pCondition,
1598 std::unique_ptr<shell_node_evaluable> &&pIterative
1599 );
1600
1601 public:
1612 shell_status evaluate(shell_session &oSession) const override;
1613
1614 public:
1619 [[nodiscard]] const shell_node_evaluable *get_condition() const noexcept {
1620 return this->m_pCondition.get();
1621 }
1622
1627 [[nodiscard]] const shell_node_evaluable *get_iterative() const noexcept {
1628 return this->m_pIterative.get();
1629 }
1630
1631 private:
1633 std::unique_ptr<shell_node_evaluable> m_pCondition;
1635 std::unique_ptr<shell_node_evaluable> m_pIterative;
1636 };
1637
1638
1647 public:
1656 std::size_t nPos,
1657 std::unique_ptr<shell_node_evaluable> &&pCondition,
1658 std::unique_ptr<shell_node_evaluable> &&pIterative
1659 );
1660
1661 public:
1672 shell_status evaluate(shell_session &oSession) const override;
1673
1674 public:
1679 [[nodiscard]] const shell_node_evaluable *get_condition() const noexcept {
1680 return this->m_pCondition.get();
1681 }
1682
1687 [[nodiscard]] const shell_node_evaluable *get_iterative() const noexcept {
1688 return this->m_pIterative.get();
1689 }
1690
1691 private:
1693 std::unique_ptr<shell_node_evaluable> m_pCondition;
1695 std::unique_ptr<shell_node_evaluable> m_pIterative;
1696 };
1697
1703 public:
1712 std::size_t nPos,
1713 std::unique_ptr<shell_node_expandable> &&pName,
1714 std::unique_ptr<shell_node_evaluable> &&pBody
1715 );
1716
1717 public:
1724 shell_status evaluate(shell_session &oSession) const override;
1725
1726 public:
1731 [[nodiscard]] const shell_node_expandable *get_name() const noexcept {
1732 return this->m_pName.get();
1733 }
1734
1739 [[nodiscard]] const shell_node_evaluable *get_body() const noexcept {
1740 return this->m_pBody.get();
1741 }
1742
1743 private:
1745 std::unique_ptr<shell_node_expandable> m_pName;
1746 std::unique_ptr<shell_node_evaluable> m_pBody;
1747 };
1748} // namespace bs
Logical AND operator node (executes right only if left succeeded).
Definition shell_node.h:1159
shell_status evaluate(shell_session &oSession) const override
Evaluate logical AND semantics.
Definition shell_node_evaluate.cpp:106
shell_node_and(const std::size_t nPos, std::unique_ptr< shell_node_evaluable > &&pLeft, std::unique_ptr< shell_node_evaluable > &&pRight)
Construct an AND node.
Definition shell_node.h:1170
Extract positional argument from the session (e.g. $1, $2).
Definition shell_node.h:536
shell_node_arg(const std::size_t nPos, const std::uint64_t nArg)
Construct an argument extractor.
Definition shell_node.h:543
std::string get_value(const shell_session &oSession) const override
Return the argument value from the session.
Definition shell_node_expand.cpp:139
std::uint64_t get_arg() const noexcept
Get the argument index this node references.
Definition shell_node.h:563
Evaluates a subcommand in background semantics (implementation-specific).
Definition shell_node.h:1002
shell_status evaluate(shell_session &oSession) const override
Evaluate the background node (typically launches and returns immediately).
Definition shell_node_evaluate.cpp:102
const shell_node_evaluable * get_command() const noexcept
Get pointer to the underlying command.
Definition shell_node.h:1028
Exception used internally to signal a loop "break".
Definition shell_node.h:1475
break_signal()
Construct a break signal exception.
Definition shell_node.h:1478
Represents a break statement inside a loop.
Definition shell_node.h:1466
shell_status evaluate(shell_session &oSession) const override
Evaluate the break node.
Definition shell_node_evaluate.cpp:179
shell_node_break(const std::size_t nPos)
Construct a break node.
Definition shell_node.h:1500
Command block executed in a subshell (may isolate environment changes).
Definition shell_node.h:960
shell_status evaluate(shell_session &oSession) const override
Evaluate the block in a subshell context.
Definition shell_node_evaluate.cpp:90
const std::vector< std::unique_ptr< shell_node_evaluable > > & get_children() const noexcept
Access the subshell's subcommands.
Definition shell_node.h:988
shell_node_command_block_subshell(const std::size_t nPos, std::vector< std::unique_ptr< shell_node_evaluable > > &&vSubCommands)
Construct a subshell command block.
Definition shell_node.h:967
A sequence of evaluable nodes executed in order (a block).
Definition shell_node.h:920
shell_node_command_block(const std::size_t nPos, std::vector< std::unique_ptr< shell_node_evaluable > > &&vSubCommands)
Construct a command block.
Definition shell_node.h:927
const std::vector< std::unique_ptr< shell_node_evaluable > > & get_children() const noexcept
Access subcommands.
Definition shell_node.h:948
shell_status evaluate(shell_session &oSession) const override
Evaluate each subcommand in order.
Definition shell_node_evaluate.cpp:83
Represents a command expression composed of expandable children.
Definition shell_node.h:229
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand the expression by expanding its children in order. Expand the node into textual tokens....
Definition shell_node_expand.cpp:38
const std::vector< std::unique_ptr< shell_node_expandable > > & get_children() const noexcept
Access the child nodes.
Definition shell_node.h:258
Wraps a command expression and executes it as a command.
Definition shell_node.h:885
const shell_node_command_expression * get_command() const noexcept
Get pointer to the underlying command expression.
Definition shell_node.h:908
shell_status evaluate(shell_session &oSession) const override
Evaluate (execute) the command expression.
Definition shell_node_evaluate.cpp:39
Exception used internally to signal a loop "continue".
Definition shell_node.h:1427
continue_signal()
Construct a continue signal exception.
Definition shell_node.h:1430
Represents a continue statement inside a loop.
Definition shell_node.h:1418
shell_status evaluate(shell_session &oSession) const override
Evaluate the continue node.
Definition shell_node_evaluate.cpp:175
shell_node_continue(const std::size_t nPos)
Construct a continue node.
Definition shell_node.h:1453
Dollar-argument with "double-hop" semantics (implementation-specific).
Definition shell_node.h:696
std::uint64_t get_arg() const noexcept
Get the argument index.
Definition shell_node.h:723
std::string get_value(const shell_session &oSession) const override
Get the value for this double-hop argument.
Definition shell_node_expand.cpp:171
shell_node_dollar_arg_dhop(const std::size_t nPos, const std::uint64_t nArg)
Construct a double-hop dollar argument node.
Definition shell_node.h:703
Extract an argument referenced with a leading $ (e.g. $@, $* or $1).
Definition shell_node.h:657
shell_node_dollar_arg(const std::size_t nPos, const std::uint64_t nArg)
Construct a dollar-argument extractor.
Definition shell_node.h:664
std::uint64_t get_arg() const noexcept
Get referenced argument index.
Definition shell_node.h:684
std::string get_value(const shell_session &oSession) const override
Retrieve the argument value via the session.
Definition shell_node_expand.cpp:145
Command-substitution node used in $() or backticks when appearing inside other contexts.
Definition shell_node.h:776
const shell_node_evaluable * get_command() const noexcept
Get non-owning pointer to the subcommand.
Definition shell_node.h:804
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand by executing the subcommand and inserting its textual output. Expand the node into textual tok...
Definition shell_node_expand.cpp:202
Special dollar items such as $?, $#, $$, etc. (implementation-specific).
Definition shell_node.h:816
shell_node_dollar_special(const std::size_t nPos, const char cItem)
Construct a dollar-special node.
Definition shell_node.h:823
std::string get_value(const shell_session &oSession) const override
Return the special item's textual value.
Definition shell_node_expand.cpp:223
char get_item() const noexcept
Get the identifying character for this special item.
Definition shell_node.h:844
Dollar-variable with double-hop lookup semantics.
Definition shell_node.h:735
std::string get_value(const shell_session &oSession) const override
Retrieve the variable's double-hop value.
Definition shell_node_expand.cpp:184
std::string get_variable() const noexcept
Get the referenced variable name.
Definition shell_node.h:762
shell_node_dollar_variable_dhop(const std::size_t nPos, std::string sVariable)
Construct a double-hop dollar variable node.
Definition shell_node.h:742
Dollar-prefixed variable node (may have different semantics).
Definition shell_node.h:618
std::string get_variable() const noexcept
Return the referenced variable name.
Definition shell_node.h:645
shell_node_dollar_variable(const std::size_t nPos, std::string sVariable)
Construct a dollar-variable extractor.
Definition shell_node.h:625
std::string get_value(const shell_session &oSession) const override
Get the value for this dollar-variable from the session.
Definition shell_node_expand.cpp:161
Base interface for nodes that can be evaluated (executed).
Definition shell_node.h:164
shell_node_evaluable(const shell_node_type nType, const std::size_t nPos)
Construct an evaluable node.
Definition shell_node.h:171
virtual shell_status evaluate(shell_session &oSession) const =0
Evaluate (execute) the node.
Base interface for nodes that expand to tokens (strings).
Definition shell_node.h:194
virtual void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const =0
Expand the node into textual tokens.
shell_node_expandable(const shell_node_type nType, const std::size_t nPos)
Construct an expandable node.
Definition shell_node.h:201
Iterative 'for' loop node that iterates over an expandable sequence.
Definition shell_node.h:1513
const shell_node_expandable * get_sequence() const noexcept
Get the sequence expandable.
Definition shell_node.h:1557
shell_status evaluate(shell_session &oSession) const override
Evaluate the for-loop.
Definition shell_node_evaluate.cpp:183
const shell_node_evaluable * get_iterative() const noexcept
Get the iterative block executed on each element.
Definition shell_node.h:1565
const std::string & get_variable() const noexcept
Get the loop variable name.
Definition shell_node.h:1549
Create function and adds to the shell session.
Definition shell_node.h:1702
shell_status evaluate(shell_session &oSession) const override
Adds function to the shell session vtable.
Definition shell_node_evaluate.cpp:226
const shell_node_expandable * get_name() const noexcept
Get the function name node.
Definition shell_node.h:1731
const shell_node_evaluable * get_body() const noexcept
Get the function body node.
Definition shell_node.h:1739
Conditional execution node (if-then-else).
Definition shell_node.h:1345
shell_status evaluate(shell_session &oSession) const override
Evaluate the if statement.
Definition shell_node_evaluate.cpp:162
const shell_node_evaluable * get_condition() const noexcept
Get the condition node.
Definition shell_node.h:1381
const shell_node_evaluable * get_case_if() const noexcept
Get the 'if' branch node.
Definition shell_node.h:1389
const shell_node_evaluable * get_case_else() const noexcept
Get the 'else' branch node.
Definition shell_node.h:1397
Exception thrown when a node is constructed with invalid args.
Definition shell_node.h:98
shell_node_invalid_argument(const std::string &sMessage)
Construct the exception with a descriptive message.
Definition shell_node.h:104
Represents a no-op command node (useful as placeholder).
Definition shell_node.h:856
shell_node_null_command(const std::size_t nPos)
Construct a null command node.
Definition shell_node.h:862
shell_status evaluate(shell_session &oSession) const override
Evaluate the null command (typically returns success).
Definition shell_node_evaluate.cpp:77
Base class for binary operator nodes (pipe, and, or) with priority handling.
Definition shell_node.h:1043
static std::unique_ptr< shell_node_evaluable > make(shell_node_type nType, std::size_t nPos, std::unique_ptr< shell_node_evaluable > &&pLeft, std::unique_ptr< shell_node_evaluable > &&pRight)
Makes an operator node.
Definition shell_node.cpp:157
std::unique_ptr< shell_node_evaluable > swap_right(std::unique_ptr< shell_node_evaluable > &&pRight)
Swap-in a new right operand, returning the old one.
Definition shell_node.cpp:231
std::unique_ptr< shell_node_evaluable > swap_left(std::unique_ptr< shell_node_evaluable > &&pLeft)
Swap-in a new left operand, returning the old one.
Definition shell_node.cpp:222
int get_priority() const noexcept
Get the operator priority.
Definition shell_node.h:1113
const shell_node_evaluable * get_right() const noexcept
Get (non-owning) pointer to the right operand.
Definition shell_node.h:1129
static constexpr int PRIORITY_OR
Priority for operator or.
Definition shell_node.h:1050
static constexpr int PRIORITY_PIPE
Priority for operator pipe.
Definition shell_node.h:1046
const shell_node_evaluable * get_left() const noexcept
Get (non-owning) pointer to the left operand.
Definition shell_node.h:1121
static constexpr int PRIORITY_AND
Priority for operator and.
Definition shell_node.h:1048
Logical OR operator node (executes right only if left failed).
Definition shell_node.h:1247
shell_node_or(const std::size_t nPos, std::unique_ptr< shell_node_evaluable > &&pLeft, std::unique_ptr< shell_node_evaluable > &&pRight)
Construct an OR node.
Definition shell_node.h:1258
shell_status evaluate(shell_session &oSession) const override
Evaluate logical OR semantics.
Definition shell_node_evaluate.cpp:113
Pipe operator node (connects stdout of left to stdin of right).
Definition shell_node.h:1203
shell_status evaluate(shell_session &oSession) const override
Evaluate pipe semantics (setup streams and execute both sides).
Definition shell_node_evaluate.cpp:120
shell_node_pipe(const std::size_t nPos, std::unique_ptr< shell_node_evaluable > &&pLeft, std::unique_ptr< shell_node_evaluable > &&pRight)
Construct a pipe node.
Definition shell_node.h:1214
Base for nodes that extract values from the session (args/vars/etc.).
Definition shell_node.h:495
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand by retrieving the value via get_value() and appending it.
Definition shell_node_expand.cpp:127
virtual std::string get_value(const shell_session &oSession) const =0
Retrieve the textual value from the given session.
shell_node_session_extractor(const shell_node_type nType, const std::size_t nPos)
Construct a session extractor node.
Definition shell_node.h:502
Command-substitution string node (backticks or $(...)).
Definition shell_node.h:362
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand by executing the subcommand and inserting its output. Expand the node into textual tokens....
Definition shell_node_expand.cpp:109
const shell_node_evaluable * get_command() const noexcept
Get the underlying evaluable subcommand.
Definition shell_node.h:390
Double-quoted string node.
Definition shell_node.h:339
shell_node_str_double(const std::size_t nPos, std::vector< std::unique_ptr< shell_node_expandable > > &&vChildren)
Construct a double-quoted string node.
Definition shell_node.h:346
Simple (unquoted) string node.
Definition shell_node.h:317
shell_node_str_simple(const std::size_t nPos, std::vector< std::unique_ptr< shell_node_expandable > > &&vChildren)
Construct an unquoted string node.
Definition shell_node.h:324
Base for string-like nodes composed of expandable fragments.
Definition shell_node.h:273
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand by concatenating child fragments into one token. Expand the node into textual tokens....
Definition shell_node_expand.cpp:94
const std::vector< std::unique_ptr< shell_node_expandable > > & get_children() const noexcept
Get the children fragments.
Definition shell_node.h:303
Node that evaluates an expandable expression as a test/condition.
Definition shell_node.h:1296
shell_status evaluate(shell_session &oSession) const override
Evaluate the test.
Definition shell_node_evaluate.cpp:129
const shell_node_expandable * get_test() const noexcept
Get the underlying expandable test node.
Definition shell_node.h:1328
A single Unicode codepoint node.
Definition shell_node.h:450
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand by converting the codepoint into UTF-8 and appending it.
Definition shell_node_expand.cpp:85
char32_t get_character() const noexcept
Returns the stored UTF-32 codepoint.
Definition shell_node.h:480
shell_node_unicode(const std::size_t nPos, const char32_t cCharacter)
Construct a unicode codepoint node.
Definition shell_node.h:457
'Until' loop node that executes the iterative block until the condition succeeds.
Definition shell_node.h:1646
shell_status evaluate(shell_session &oSession) const override
Evaluate the until-loop.
Definition shell_node_evaluate.cpp:212
const shell_node_evaluable * get_condition() const noexcept
Get the stopping condition node.
Definition shell_node.h:1679
const shell_node_evaluable * get_iterative() const noexcept
Get the iterative block executed while the condition fails.
Definition shell_node.h:1687
Extract a named shell variable from the session environment.
Definition shell_node.h:577
std::string get_value(const shell_session &oSession) const override
Get the variable's value from the session.
Definition shell_node_expand.cpp:151
std::string get_variable() const noexcept
Name of the referenced variable.
Definition shell_node.h:604
shell_node_variable(const std::size_t nPos, std::string sVariable)
Construct a variable extractor.
Definition shell_node.h:584
While-loop node executing the iterative block while the condition succeeds.
Definition shell_node.h:1586
const shell_node_evaluable * get_iterative() const noexcept
Get the iterative block node.
Definition shell_node.h:1627
shell_status evaluate(shell_session &oSession) const override
Evaluate the while-loop.
Definition shell_node_evaluate.cpp:198
const shell_node_evaluable * get_condition() const noexcept
Get the condition node.
Definition shell_node.h:1619
A plain word token node.
Definition shell_node.h:404
void expand(std::vector< std::string > &vTokens, shell_session &oSession, bool bSplit) const override
Expand by appending the literal text as a token. Expand the node into textual tokens....
Definition shell_node_expand.cpp:78
shell_node_word(const std::size_t nPos, std::string sText)
Construct a word node.
Definition shell_node.h:411
std::string get_text() const noexcept
Get the stored text.
Definition shell_node.h:435
Base class for all parser nodes.
Definition shell_node.h:116
std::size_t get_pos() const noexcept
Get node position in the original input stream.
Definition shell_node.h:148
virtual ~shell_node()=default
Virtual default destructor for polymorphic deletion.
shell_node_type get_type() const noexcept
Get the node type.
Definition shell_node.h:140
const std::size_t m_nPos
Position in the input stream.
Definition shell_node.h:154
const shell_node_type m_nType
Node type.
Definition shell_node.h:153
shell_node(const shell_node_type nType, const std::size_t nPos)
Protected constructor used by derived classes.
Definition shell_node.h:127
Represents an execution environment for a shell instance.
Definition shell_session.h:57
BashSpark main namespace.
Definition command.h:39
shell_node_type
Types of nodes recognized by the shell parser.
Definition shell_node.h:56
shell_status
Shell status codes.
Definition shell_status.h:47
Defines status codes for the shell.