diff --git a/scripts/completions/bash-completion.sh b/scripts/completions/bash-completion.sh index 520c8c5..fa1546e 100644 --- a/scripts/completions/bash-completion.sh +++ b/scripts/completions/bash-completion.sh @@ -8,7 +8,7 @@ _ybconAutoComplete() { COMPREPLY=() current="${COMP_WORDS[COMP_CWORD]}" previous="${COMP_WORDS[COMP_CWORD-1]}" - options='-h -p --help --parallel --target= --newlines= --printresult --version --buildInfo' + options='-h -p -t --help --parallel --target= --newlines= --printresult --text --version --buildInfo' if [[ "${current}" == -* ]]; then YCompReply "$(compgen -W "$options" -- "$current")" return 0 diff --git a/scripts/completions/fish-completion.fish b/scripts/completions/fish-completion.fish index 9977067..b88b157 100644 --- a/scripts/completions/fish-completion.fish +++ b/scripts/completions/fish-completion.fish @@ -7,4 +7,5 @@ complete -c ybcon -x -l buildInfo -d "Print informations about how the ybcon exe complete -c ybcon -l parallel -k -d "Transpile files in parallel mode" complete -c ybcon -l target -k -f -a 'lua js py' -d "Set the transpilation target" complete -c ybcon -l newlines -k -f -a 'on off' -d "Enable or disable new lines" -complete -c ybcon -s p -l printresult -d "Enable printing the transpilation result to stdout" \ No newline at end of file +complete -c ybcon -s p -l printresult -d "Enable printing the transpilation result to stdout" +complete -c ybcon -x -s t -l text -d "Transpile text provided after this argument (implies -p)" \ No newline at end of file diff --git a/scripts/completions/zsh-completion.zsh b/scripts/completions/zsh-completion.zsh index ea58753..4d227d7 100644 --- a/scripts/completions/zsh-completion.zsh +++ b/scripts/completions/zsh-completion.zsh @@ -9,6 +9,7 @@ _ybcon() { --target='[Set the transpilation target]:language:(lua js py)' \ --newlines='[Enable or disable new lines]:state:(on off)' \ {-p,--printresult}'[Enable printing the transpilation result to stdout]' \ + {-t,--text}'[Transpile text provided after this argument (implies -p)]' \ "*:$completeyfile" return 0 } diff --git a/scripts/ybcon b/scripts/ybcon index f086ed0..9b21044 100755 --- a/scripts/ybcon +++ b/scripts/ybcon @@ -11,7 +11,7 @@ defaultBinLocation="$scriptDir/../libexec/ybcon" usage() { if [ "$1" = false ]; then echo "Invalid arguments, usage:"; fi - echo "$EXENAME [--version] [--buildInfo] [-h|--help] [--parallel] [--target=] [--newlines=on/off] [-p|--printresult] " + echo "$EXENAME [--version] [--buildInfo] [-h|--help] [--parallel] [--target=] [--newlines=on/off] [-p|--printresult] [-t|--text] " if [ "$1" = true ]; then echo " --version Print the version" echo " --buildInfo Print informations about how the ybcon executable was built" @@ -20,6 +20,7 @@ usage() { echo " --target= Set the transpilation target" echo " --newlines=on/off Enable or disable new lines" echo " -p or --printresult Enable printing the transpilation result to stdout" + echo " -t or --text Transpile text provided after this argument (implies -p)" printf "\n" fi } @@ -29,57 +30,51 @@ usageExit() { exit 0 }; helpExit() { usage true; exit 0; } -args=""; +arguments=""; run=false; -newArgs() { - if [ "$args" = "" ]; then - args="$args$1" - else - args="$args $1" - fi -} - if [ "$#" != 0 ]; then if [ "$#" = 1 ]; then case "$1" in -h | --help ) helpExit ;; - --version | --buildInfo ) - run=true; args="$1" ;; - *.ybcon ) - newArgs "$1"; run=true ;; + --version | --buildInfo | *.ybcon ) + run=true ;; * ) usageExit ;; esac else + text_provided=false for it in "$@" do case "$it" in -p | --printresult | --parallel | --target=* | --newlines=on | --newlines=off ) - if test "${args#*$it}" = "$args"; then - newArgs "$it" + if test "${arguments#*$it}" != "$arguments"; then + usageExit + fi ;; + -t | --text ) + text_provided=true ;; + * ) + if [ $text_provided = true ] || test "${it%*.ybcon}" != "$it"; then + run=true else usageExit fi ;; - *.ybcon ) - newArgs "$it" - run=true ;; - * ) - usageExit ;; esac + if [ "$arguments" != "" ]; then + arguments="$arguments " + fi + arguments="$arguments$it" done - if test "${args#*".ybcon"}" = "$args" ; then usageExit; fi + if test $run = false ; then usageExit; fi fi else usage false fi -runIt() { eval "$1" " $args"; } - if [ "$run" = true ]; then if [ -f "$defaultBinLocation" ]; then - runIt "$defaultBinLocation" + exec "$defaultBinLocation" "$@" else echo "Yerbacon executable not found at $defaultBinLocation" fi diff --git a/src/etc/lexer.cpp b/src/etc/lexer.cpp index ca773ff..b11187d 100644 --- a/src/etc/lexer.cpp +++ b/src/etc/lexer.cpp @@ -9,7 +9,7 @@ tok::type getIdentifierCharType(const char& Char) { else return UNEXPECTED; } -vector lex(const string& in) +vector lex(const string_view& in) { vector resVal; unsigned long lineNumber = 1; diff --git a/src/headers/lex.hpp b/src/headers/lex.hpp index f2c3fc2..5aab51c 100644 --- a/src/headers/lex.hpp +++ b/src/headers/lex.hpp @@ -43,6 +43,6 @@ auto find_corresponding(std::input_iterator auto begin, std::input_iterator auto }); } -std::vector lex(const std::string& in); +std::vector lex(const std::string_view& in); #endif //YERBACON_TEST_H \ No newline at end of file diff --git a/src/headers/misc.hpp b/src/headers/misc.hpp index f741593..ac29321 100644 --- a/src/headers/misc.hpp +++ b/src/headers/misc.hpp @@ -6,6 +6,6 @@ void outputFileContent(const string& file, string_view content); #include "lex.hpp" #include "parsing/Parser.hpp" -inline auto parseString(const string& toParse) { return Parser::parse(lex(toParse)); } +inline auto parseString(const string_view& toParse) { return Parser::parse(lex(toParse)); } #endif //YERBACON_MISC_HPP \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2f16f21..fb74d80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,16 +14,11 @@ int main(int argc, char* argv[]) { string target = "lua"; bool printResult = false, parallel = false, - newLines = true; + newLines = true, + text_provided = false; using unit_result = pair>; using unit = future; map Units; - const auto compile = [&target, &newLines](string_view name) -> string { - string transpiledString = Target::forName(target, newLines)->transpileWithTree(parseString(getFileContent(name.data()))); - name.remove_suffix(6); - outputFileContent(string(name) + '.' + target, transpiledString); - return transpiledString; - }; for (signed int i = 1; i < argc; ++i) { const string_view currentArg (argv[i]); @@ -42,25 +37,31 @@ int main(int argc, char* argv[]) { newLines = true; } else goto invalid_argument; } - else if (currentArg.ends_with(".ybcon")) Units.insert_or_assign(currentArg, async(not parallel ? launch::deferred : launch::async, [fileName = currentArg, &compile]() { - unit_result resultingPair; - try { - resultingPair.first = compile(fileName); - } catch (const Yerbacon::Exception& error) { - size_t lastSlash = 0; - const size_t position1 = fileName.find_last_of('/'); - if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1; - if constexpr(filesystem::path::preferred_separator != '/') { - const size_t position2 = fileName.find_last_of(filesystem::path::preferred_separator); - if (cmp_not_equal(position2, string_view::npos)) { - lastSlash = max(lastSlash, position2); + else if (currentArg == ArgumentShort("text")) { text_provided = true; printResult = true; } + else if ((currentArg.ends_with(".ybcon") && !text_provided) || text_provided) + Units.insert_or_assign(currentArg, async(not parallel ? launch::deferred : launch::async, [currentArg, &text_provided, &target, &newLines]() { + unit_result resultingPair; + try { + resultingPair.first = Target::forName(target, newLines)->transpileWithTree( + parseString(text_provided ? currentArg : getFileContent(currentArg.data())) + ); + if (not text_provided) outputFileContent(string(currentArg.substr(0, currentArg.size() - 6)) + '.' + target, resultingPair.first); + } catch (const Yerbacon::Exception& error) { + size_t lastSlash = 0; + const size_t position1 = currentArg.find_last_of('/'); + if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1; + if constexpr(filesystem::path::preferred_separator != '/') { + const size_t position2 = currentArg.find_last_of(filesystem::path::preferred_separator); + if (cmp_not_equal(position2, string_view::npos)) { + lastSlash = max(lastSlash, position2); + } } + resultingPair.first = currentArg.substr(lastSlash + 1); + resultingPair.second.emplace(error); } - resultingPair.first = fileName.substr(lastSlash + 1); - resultingPair.second.emplace(error); - } - return resultingPair; - })); else { + return resultingPair; + })); + else { if (argc == 2) { if (currentArg == Argument("version")) { cout << Yerbacon::getVersion(); @@ -83,11 +84,11 @@ int main(int argc, char* argv[]) { cout << result.first << '\n'; } } else { - cout << "Compilation of " << result.first << " has failed with the following error:" << endl; + cout << "Compilation"; if (not text_provided) cout << " of " << result.first; cout << " has failed with the following error:" << endl; cerr << result.second.value().what() << '\n'; return EXIT_FAILURE; } } - } else cout << "No valid file provided.\n"; + } else cout << (!text_provided ? "No valid file provided" : "No text was provided") << ".\n"; return EXIT_SUCCESS; } \ No newline at end of file