Add a "--text" argument

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-04-09 22:43:52 +02:00
parent 7a18bfad59
commit 21137f5beb
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
8 changed files with 54 additions and 56 deletions

View File

@ -8,7 +8,7 @@ _ybconAutoComplete() {
COMPREPLY=() COMPREPLY=()
current="${COMP_WORDS[COMP_CWORD]}" current="${COMP_WORDS[COMP_CWORD]}"
previous="${COMP_WORDS[COMP_CWORD-1]}" 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 if [[ "${current}" == -* ]]; then
YCompReply "$(compgen -W "$options" -- "$current")" YCompReply "$(compgen -W "$options" -- "$current")"
return 0 return 0

View File

@ -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 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 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 -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" 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)"

View File

@ -9,6 +9,7 @@ _ybcon() {
--target='[Set the transpilation target]:language:(lua js py)' \ --target='[Set the transpilation target]:language:(lua js py)' \
--newlines='[Enable or disable new lines]:state:(on off)' \ --newlines='[Enable or disable new lines]:state:(on off)' \
{-p,--printresult}'[Enable printing the transpilation result to stdout]' \ {-p,--printresult}'[Enable printing the transpilation result to stdout]' \
{-t,--text}'[Transpile text provided after this argument (implies -p)]' \
"*:$completeyfile" "*:$completeyfile"
return 0 return 0
} }

View File

@ -11,7 +11,7 @@ defaultBinLocation="$scriptDir/../libexec/ybcon"
usage() { usage() {
if [ "$1" = false ]; then echo "Invalid arguments, usage:"; fi if [ "$1" = false ]; then echo "Invalid arguments, usage:"; fi
echo "$EXENAME [--version] [--buildInfo] [-h|--help] [--parallel] [--target=<target>] [--newlines=on/off] [-p|--printresult] <file>" echo "$EXENAME [--version] [--buildInfo] [-h|--help] [--parallel] [--target=<target>] [--newlines=on/off] [-p|--printresult] [-t|--text] <file>"
if [ "$1" = true ]; then if [ "$1" = true ]; then
echo " --version Print the version" echo " --version Print the version"
echo " --buildInfo Print informations about how the ybcon executable was built" echo " --buildInfo Print informations about how the ybcon executable was built"
@ -20,6 +20,7 @@ usage() {
echo " --target=<target> Set the transpilation target" echo " --target=<target> Set the transpilation target"
echo " --newlines=on/off Enable or disable new lines" echo " --newlines=on/off Enable or disable new lines"
echo " -p or --printresult Enable printing the transpilation result to stdout" 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" printf "\n"
fi fi
} }
@ -29,57 +30,51 @@ usageExit() {
exit 0 exit 0
}; helpExit() { usage true; exit 0; } }; helpExit() { usage true; exit 0; }
args=""; arguments="";
run=false; run=false;
newArgs() {
if [ "$args" = "" ]; then
args="$args$1"
else
args="$args $1"
fi
}
if [ "$#" != 0 ]; then if [ "$#" != 0 ]; then
if [ "$#" = 1 ]; then if [ "$#" = 1 ]; then
case "$1" in case "$1" in
-h | --help ) -h | --help )
helpExit ;; helpExit ;;
--version | --buildInfo ) --version | --buildInfo | *.ybcon )
run=true; args="$1" ;; run=true ;;
*.ybcon )
newArgs "$1"; run=true ;;
* ) * )
usageExit ;; usageExit ;;
esac esac
else else
text_provided=false
for it in "$@" for it in "$@"
do do
case "$it" in case "$it" in
-p | --printresult | --parallel | --target=* | --newlines=on | --newlines=off ) -p | --printresult | --parallel | --target=* | --newlines=on | --newlines=off )
if test "${args#*$it}" = "$args"; then if test "${arguments#*$it}" != "$arguments"; then
newArgs "$it" usageExit
fi ;;
-t | --text )
text_provided=true ;;
* )
if [ $text_provided = true ] || test "${it%*.ybcon}" != "$it"; then
run=true
else else
usageExit usageExit
fi ;; fi ;;
*.ybcon )
newArgs "$it"
run=true ;;
* )
usageExit ;;
esac esac
if [ "$arguments" != "" ]; then
arguments="$arguments "
fi
arguments="$arguments$it"
done done
if test "${args#*".ybcon"}" = "$args" ; then usageExit; fi if test $run = false ; then usageExit; fi
fi fi
else else
usage false usage false
fi fi
runIt() { eval "$1" " $args"; }
if [ "$run" = true ]; then if [ "$run" = true ]; then
if [ -f "$defaultBinLocation" ]; then if [ -f "$defaultBinLocation" ]; then
runIt "$defaultBinLocation" exec "$defaultBinLocation" "$@"
else else
echo "Yerbacon executable not found at $defaultBinLocation" echo "Yerbacon executable not found at $defaultBinLocation"
fi fi

View File

@ -9,7 +9,7 @@ tok::type getIdentifierCharType(const char& Char) {
else return UNEXPECTED; else return UNEXPECTED;
} }
vector<tok> lex(const string& in) vector<tok> lex(const string_view& in)
{ {
vector<tok> resVal; vector<tok> resVal;
unsigned long lineNumber = 1; unsigned long lineNumber = 1;

View File

@ -43,6 +43,6 @@ auto find_corresponding(std::input_iterator auto begin, std::input_iterator auto
}); });
} }
std::vector<tok> lex(const std::string& in); std::vector<tok> lex(const std::string_view& in);
#endif //YERBACON_TEST_H #endif //YERBACON_TEST_H

View File

@ -6,6 +6,6 @@ void outputFileContent(const string& file, string_view content);
#include "lex.hpp" #include "lex.hpp"
#include "parsing/Parser.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 #endif //YERBACON_MISC_HPP

View File

@ -14,16 +14,11 @@ int main(int argc, char* argv[]) {
string target = "lua"; string target = "lua";
bool printResult = false, bool printResult = false,
parallel = false, parallel = false,
newLines = true; newLines = true,
text_provided = false;
using unit_result = pair<string, optional<Yerbacon::Exception>>; using unit_result = pair<string, optional<Yerbacon::Exception>>;
using unit = future<unit_result>; using unit = future<unit_result>;
map<string_view, unit> Units; map<string_view, unit> 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) for (signed int i = 1; i < argc; ++i)
{ {
const string_view currentArg (argv[i]); const string_view currentArg (argv[i]);
@ -42,25 +37,31 @@ int main(int argc, char* argv[]) {
newLines = true; newLines = true;
} else goto invalid_argument; } 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]() { else if (currentArg == ArgumentShort("text")) { text_provided = true; printResult = true; }
unit_result resultingPair; else if ((currentArg.ends_with(".ybcon") && !text_provided) || text_provided)
try { Units.insert_or_assign(currentArg, async(not parallel ? launch::deferred : launch::async, [currentArg, &text_provided, &target, &newLines]() {
resultingPair.first = compile(fileName); unit_result resultingPair;
} catch (const Yerbacon::Exception& error) { try {
size_t lastSlash = 0; resultingPair.first = Target::forName(target, newLines)->transpileWithTree(
const size_t position1 = fileName.find_last_of('/'); parseString(text_provided ? currentArg : getFileContent(currentArg.data()))
if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1; );
if constexpr(filesystem::path::preferred_separator != '/') { if (not text_provided) outputFileContent(string(currentArg.substr(0, currentArg.size() - 6)) + '.' + target, resultingPair.first);
const size_t position2 = fileName.find_last_of(filesystem::path::preferred_separator); } catch (const Yerbacon::Exception& error) {
if (cmp_not_equal(position2, string_view::npos)) { size_t lastSlash = 0;
lastSlash = max(lastSlash, position2); 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); return resultingPair;
resultingPair.second.emplace(error); }));
} else {
return resultingPair;
})); else {
if (argc == 2) { if (argc == 2) {
if (currentArg == Argument("version")) { if (currentArg == Argument("version")) {
cout << Yerbacon::getVersion(); cout << Yerbacon::getVersion();
@ -83,11 +84,11 @@ int main(int argc, char* argv[]) {
cout << result.first << '\n'; cout << result.first << '\n';
} }
} else { } 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'; cerr << result.second.value().what() << '\n';
return EXIT_FAILURE; 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; return EXIT_SUCCESS;
} }