Added comments
This commit is contained in:
@ -29,7 +29,7 @@ namespace fsh {
|
|||||||
|
|
||||||
class AstFactory {
|
class AstFactory {
|
||||||
public:
|
public:
|
||||||
// Generates an abstract syntax tree
|
// Generise abstraktno sintaksno drvo po ebnf formuli gore
|
||||||
static std::shared_ptr<ExecutableNode> generate_ast(std::list<Token>& list) {
|
static std::shared_ptr<ExecutableNode> generate_ast(std::list<Token>& list) {
|
||||||
auto it = list.begin();
|
auto it = list.begin();
|
||||||
return CommandLineNode::build(it);
|
return CommandLineNode::build(it);
|
||||||
@ -41,4 +41,4 @@ namespace fsh {
|
|||||||
AstFactory(const AstFactory&) = default;
|
AstFactory(const AstFactory&) = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
@ -33,6 +33,7 @@ namespace fsh {
|
|||||||
using TokenType = Lexer::TokenType;
|
using TokenType = Lexer::TokenType;
|
||||||
AstNode(NodeType type, std::string name) : type(type), name(name) {}
|
AstNode(NodeType type, std::string name) : type(type), name(name) {}
|
||||||
|
|
||||||
|
// Pokusa da napravi od narednih tokena zadati Node ako ne uspe vrati null
|
||||||
template <class T>
|
template <class T>
|
||||||
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it, std::shared_ptr<T>& node_ptr);
|
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it, std::shared_ptr<T>& node_ptr);
|
||||||
|
|
||||||
@ -42,6 +43,7 @@ namespace fsh {
|
|||||||
return Optional<T>(it, node_ptr);
|
return Optional<T>(it, node_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pravi od narednih tokena zadati Node ako ne uspe izbaci AstBuildError
|
||||||
template <class T>
|
template <class T>
|
||||||
static std::shared_ptr<T> Mandatory(std::list<Token>::iterator& it) {
|
static std::shared_ptr<T> Mandatory(std::list<Token>::iterator& it) {
|
||||||
return T::build(it);
|
return T::build(it);
|
||||||
@ -70,4 +72,4 @@ namespace fsh {
|
|||||||
AstBuildError(std::string err) : std::runtime_error(err) {}
|
AstBuildError(std::string err) : std::runtime_error(err) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
@ -80,4 +80,4 @@ namespace fsh {
|
|||||||
bool append = false;
|
bool append = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
@ -66,4 +66,4 @@ namespace fsh {
|
|||||||
std::shared_ptr<ExecutableNode> pipe;
|
std::shared_ptr<ExecutableNode> pipe;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
namespace fsh {
|
namespace fsh {
|
||||||
|
|
||||||
|
// Genericni node namenjen kao wrapper za tokene
|
||||||
template <Lexer::TokenType T>
|
template <Lexer::TokenType T>
|
||||||
class TokenNode : public AstNode {
|
class TokenNode : public AstNode {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -34,21 +34,25 @@ namespace fsh {
|
|||||||
bool extends; //Not implemented
|
bool extends; //Not implemented
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Dodavanje argumenta koji sluzi za ulaz
|
||||||
void add_input_rule() {
|
void add_input_rule() {
|
||||||
has_input = true;
|
has_input = true;
|
||||||
pos_arg_rules.push_back({ &_Argument::create<ArgInput>, false, false });
|
pos_arg_rules.push_back({ &_Argument::create<ArgInput>, false, false });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dodavanje pozicionog argumenta sa automatskim prebacivanjem
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void add_rule(bool mandatory, bool extends = false) {
|
void add_rule(bool mandatory, bool extends = false) {
|
||||||
pos_arg_rules.push_back({ &_Argument::create<Argument<T> >, mandatory, extends });
|
pos_arg_rules.push_back({ &_Argument::create<Argument<T> >, mandatory, extends });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dodavanje pravila za flag (sa mogucnoscu da se pokupi argument priljepljen npr. -n<count>)
|
||||||
template <typename T = bool>
|
template <typename T = bool>
|
||||||
void add_rule(const std::string name, bool capturing = false) {
|
void add_rule(const std::string name, bool capturing = false) {
|
||||||
flag_rules[name] = { _Argument::create<Argument<T> >, false, capturing, false };
|
flag_rules[name] = { _Argument::create<Argument<T> >, false, capturing, false };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Popunjava ArgManager (Mozda bolji naziv bi bio populate?)
|
||||||
void parse(ArgManager& manager, ArgNodes& vec, FlagNode flag);
|
void parse(ArgManager& manager, ArgNodes& vec, FlagNode flag);
|
||||||
|
|
||||||
bool ghas_input() { return has_input; }
|
bool ghas_input() { return has_input; }
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
namespace fsh {
|
namespace fsh {
|
||||||
|
|
||||||
|
// Ovaj singleton odrzava podatke o komanadama
|
||||||
class CommandRegistry {
|
class CommandRegistry {
|
||||||
public:
|
public:
|
||||||
static CommandRegistry& instance() {
|
static CommandRegistry& instance() {
|
||||||
@ -27,6 +28,7 @@ namespace fsh {
|
|||||||
Command& operator[](const std::string n) { return get(n); }
|
Command& operator[](const std::string n) { return get(n); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Nove komande bi registrovali ovde:
|
||||||
CommandRegistry() {
|
CommandRegistry() {
|
||||||
cmds["wc"] = Command::register_cmd<CmdWc>();
|
cmds["wc"] = Command::register_cmd<CmdWc>();
|
||||||
cmds["date"] = Command::register_cmd<CmdDate>();
|
cmds["date"] = Command::register_cmd<CmdDate>();
|
||||||
@ -40,4 +42,4 @@ namespace fsh {
|
|||||||
std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
|
std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
@ -27,7 +27,10 @@ namespace fsh {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// Register_flags se zove jednom pri registraciji komande i sluzi da u ArgFactory postavis
|
||||||
|
// pravila za argumente i opcije
|
||||||
virtual void register_flags() {};
|
virtual void register_flags() {};
|
||||||
|
// Korisnik u ovoj funkciju implementira glavnu funkcionalnost komande
|
||||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) {};
|
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) {};
|
||||||
ArgFactory& get_factory() { return arg_factory; }
|
ArgFactory& get_factory() { return arg_factory; }
|
||||||
|
|
||||||
|
|||||||
@ -8,9 +8,11 @@
|
|||||||
|
|
||||||
namespace fsh {
|
namespace fsh {
|
||||||
|
|
||||||
|
//Glavna singleton klasa za shell
|
||||||
class fsh {
|
class fsh {
|
||||||
public:
|
public:
|
||||||
std::unordered_map<std::string, std::string> environment;
|
std::unordered_map<std::string, std::string>
|
||||||
|
environment; //Omogucava komandama da cuvaju informacije kroz upotrebe
|
||||||
|
|
||||||
static fsh& instance() {
|
static fsh& instance() {
|
||||||
static fsh f;
|
static fsh f;
|
||||||
|
|||||||
@ -6,13 +6,13 @@
|
|||||||
|
|
||||||
namespace fsh {
|
namespace fsh {
|
||||||
|
|
||||||
|
// Ova klasa vrsi leksicku analizu ulaznog string-a
|
||||||
class Lexer {
|
class Lexer {
|
||||||
public:
|
public:
|
||||||
enum TokenType {
|
enum TokenType {
|
||||||
WHITESPACE,
|
WHITESPACE,
|
||||||
WORD,
|
WORD,
|
||||||
STRING_LITERAL,
|
STRING_LITERAL,
|
||||||
OPT,
|
|
||||||
FLAG,
|
FLAG,
|
||||||
LREDIRECTION,
|
LREDIRECTION,
|
||||||
RREDIRECTION,
|
RREDIRECTION,
|
||||||
@ -23,8 +23,11 @@ namespace fsh {
|
|||||||
|
|
||||||
using Token = std::pair<Lexer::TokenType, std::string>;
|
using Token = std::pair<Lexer::TokenType, std::string>;
|
||||||
|
|
||||||
|
// Daje nam sledeci karakter bez povecavanjem brojaca
|
||||||
int peek();
|
int peek();
|
||||||
|
// Daje nam sledeci karakter sa povecavanjem brojaca
|
||||||
int consume();
|
int consume();
|
||||||
|
// Trazi za sledeci token
|
||||||
Token next_token();
|
Token next_token();
|
||||||
|
|
||||||
static std::list<Token> process(std::string line);
|
static std::list<Token> process(std::string line);
|
||||||
@ -87,4 +90,4 @@ namespace fsh {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
@ -7,6 +7,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Neke "workarounds" za terminale na Unix i Windows
|
||||||
namespace fsh::util {
|
namespace fsh::util {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
// Razlicite pomocne funkcije za tekstove
|
||||||
namespace fsh::util {
|
namespace fsh::util {
|
||||||
|
|
||||||
extern const char* tokens[];
|
extern const char* tokens[];
|
||||||
|
|||||||
@ -26,6 +26,8 @@ namespace fsh {
|
|||||||
manager.push_arg(build_arg(pos_arg_rules[i].build, arg));
|
manager.push_arg(build_arg(pos_arg_rules[i].build, arg));
|
||||||
if (!pos_arg_rules[i].extends) i++;
|
if (!pos_arg_rules[i].extends) i++;
|
||||||
}
|
}
|
||||||
|
if (pos_arg_rules.size() > i && pos_arg_rules[i].mandatory)
|
||||||
|
throw std::invalid_argument("Excpected More arguments");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) {
|
void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) {
|
||||||
|
|||||||
@ -19,4 +19,4 @@ namespace fsh {
|
|||||||
in.clear();
|
in.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace fsh
|
}
|
||||||
95
util.hpp
Normal file
95
util.hpp
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/** CmakeLists.txt
|
||||||
|
cmake_minimum_required(VERSION 3.12)
|
||||||
|
|
||||||
|
project(fsh LANGUAGES CXX)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
set(FSH_SOURCE_FILES
|
||||||
|
src/main.cpp
|
||||||
|
src/fsh.cpp
|
||||||
|
src/lexer.cpp
|
||||||
|
src/ast/ast.cpp
|
||||||
|
src/ast/ast_exec.cpp
|
||||||
|
src/ast/ast_print.cpp
|
||||||
|
src/util/input.cpp
|
||||||
|
src/util/text.cpp
|
||||||
|
src/cmd/arg.cpp
|
||||||
|
src/cmd/cmd_base.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(include)
|
||||||
|
|
||||||
|
add_executable(fsh ${FSH_SOURCE_FILES})
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** .clang-format
|
||||||
|
|
||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: Mozilla
|
||||||
|
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignConsecutiveMacros: true
|
||||||
|
AlignConsecutiveAssignments: true
|
||||||
|
AlignEscapedNewlines: Right
|
||||||
|
AlignOperands: false
|
||||||
|
AlignTrailingComments: true
|
||||||
|
AllowAllArgumentsOnNextLine: true
|
||||||
|
AllowAllConstructorInitializersOnNextLine: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: true
|
||||||
|
AllowShortCaseLabelsOnASingleLine: true
|
||||||
|
AllowShortFunctionsOnASingleLine: true
|
||||||
|
AllowShortIfStatementsOnASingleLine: Always
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeTernaryOperators: false
|
||||||
|
BreakConstructorInitializers: AfterColon
|
||||||
|
ColumnLimit: 120
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: false
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IndentCaseLabels: true
|
||||||
|
IndentWidth: 4
|
||||||
|
PointerAlignment: Left
|
||||||
|
ReflowComments: false
|
||||||
|
SortIncludes: false
|
||||||
|
SortUsingDeclarations: false
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInContainerLiterals: false
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Auto
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
|
||||||
|
AllowShortEnumsOnASingleLine: false
|
||||||
|
|
||||||
|
BraceWrapping:
|
||||||
|
AfterEnum: false
|
||||||
|
|
||||||
|
AlignConsecutiveDeclarations:
|
||||||
|
Enabled: true
|
||||||
|
|
||||||
|
NamespaceIndentation: All
|
||||||
|
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user