Add a "--text" argument
Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
parent
7a18bfad59
commit
21137f5beb
|
@ -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
|
||||
|
|
|
@ -8,3 +8,4 @@ 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"
|
||||
complete -c ybcon -x -s t -l text -d "Transpile text provided after this argument (implies -p)"
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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=<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
|
||||
echo " --version Print the version"
|
||||
echo " --buildInfo Print informations about how the ybcon executable was built"
|
||||
|
@ -20,6 +20,7 @@ usage() {
|
|||
echo " --target=<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
|
||||
|
|
|
@ -9,7 +9,7 @@ tok::type getIdentifierCharType(const char& Char) {
|
|||
else return UNEXPECTED;
|
||||
}
|
||||
|
||||
vector<tok> lex(const string& in)
|
||||
vector<tok> lex(const string_view& in)
|
||||
{
|
||||
vector<tok> resVal;
|
||||
unsigned long lineNumber = 1;
|
||||
|
|
|
@ -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
|
|
@ -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
|
53
src/main.cpp
53
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<string, optional<Yerbacon::Exception>>;
|
||||
using unit = future<unit_result>;
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue