Add a "SEPARATOR" token type

This commit is contained in:
Username404 2021-09-10 22:15:05 +02:00
parent 37a6c0e047
commit 6de05633d9
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
5 changed files with 15 additions and 14 deletions

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& in, const char separatorCharacter)
{ {
vector<tok> resVal; vector<tok> resVal;
unsigned long lineNumber = 1; unsigned long lineNumber = 1;
@ -21,9 +21,9 @@ vector<tok> lex(const string& in)
case LBRACE: case RBRACE: case LBRACKET: case RBRACKET: case LBRACE: case RBRACE: case LBRACKET: case RBRACKET:
case PLUS: case HYPHEN: case LCOMP: case RCOMP: case PLUS: case HYPHEN: case LCOMP: case RCOMP:
case DOT: case DOLLAR_SIGN: case SQUOTE: case DOT: case DOLLAR_SIGN: case SQUOTE:
resVal.emplace_back(static_cast<tok::type>(current), string(1, current), lineNumber); resVal.emplace_back(static_cast<tok::type>(current), string(1, current), lineNumber); break;
[[likely]] case '\n': ++lineNumber; case ';': resVal.emplace_back(SEPARATOR, string(1, separatorCharacter), lineNumber);
[[likely]] case ' ': case '\t': case '\r': break; [[likely]] case ' ': case '\t': case '\r': break;
[[likely]] case '\n': ++lineNumber; break;
default: { default: {
tok::type type = getIdentifierCharType(current); tok::type type = getIdentifierCharType(current);
bool isTypeString = (type == STRING); bool isTypeString = (type == STRING);
@ -32,8 +32,8 @@ vector<tok> lex(const string& in)
case IDENTIFIER: case NUMBER: { case IDENTIFIER: case NUMBER: {
string formedString; string formedString;
for (;i < in.size(); ++i) { for (;i < in.size(); ++i) {
tok::type currentCharType = getIdentifierCharType(in[i]); const tok::type currentCharType = getIdentifierCharType(in[i]);
bool isString = currentCharType == STRING; const bool isString = currentCharType == STRING;
if (i == in.size() - 1 && not isString) throw tok::LexerException("A never ending string was found", lineNumber); if (i == in.size() - 1 && not isString) throw tok::LexerException("A never ending string was found", lineNumber);
if ((currentCharType == type || isTypeString) && !isString) { if ((currentCharType == type || isTypeString) && !isString) {
formedString += string(1, in[i]); formedString += string(1, in[i]);

View File

@ -11,7 +11,7 @@ struct tok {
using Yerbacon::Exception::Exception; using Yerbacon::Exception::Exception;
}; };
enum type: const unsigned short { enum type: const unsigned short {
UNEXPECTED = std::numeric_limits<unsigned char>::max() + 1, IDENTIFIER, NUMBER, ALPHACHAR, UNEXPECTED = std::numeric_limits<unsigned char>::max() + 1, IDENTIFIER, NUMBER, ALPHACHAR, SEPARATOR,
DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.', DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.',
PLUS = '+', LPAR = '(', LBRACE = '{', LBRACKET = '[', RPAR = ')', PLUS = '+', LPAR = '(', LBRACE = '{', LBRACKET = '[', RPAR = ')',
RBRACE = '}', RBRACKET = ']', RBRACE = '}', RBRACKET = ']',
@ -22,9 +22,9 @@ struct tok {
const type toktype; const type toktype;
const std::string toktext; const std::string toktext;
const unsigned long line = 0; const unsigned long line = 0;
tok(type Type, std::string_view Text, auto line): toktype(Type), toktext(Text), line(line) {} tok(type Type, std::string_view Text, decltype(line) line): toktype(Type), toktext(Text), line(line) {}
friend std::ostream& operator<<(std::ostream& output, const tok& it) { return output << it.toktext; } friend std::ostream& operator<<(std::ostream& output, const tok& it) { return output << it.toktext; }
}; };
std::vector<tok> lex(const std::string& in); std::vector<tok> lex(const std::string& in, char separatorCharacter);
#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::parseVector(lex(toParse)); } inline auto parseString(const string& toParse, const char lineSeparator) { return Parser::parseVector(lex(toParse, lineSeparator)); }
#endif //YERBACON_MISC_HPP #endif //YERBACON_MISC_HPP

View File

@ -100,7 +100,7 @@ shared_ptr<Target> Target::forName(string_view name) {
} }
} }
shared_ptr<Target> target; shared_ptr<Target> target;
#define ADDTARGET(X) target = make_shared<X>(X()); #define ADDTARGET(X) target = shared_ptr<X>(new (X));
switch (selected) { switch (selected) {
#ifdef LUA_HPP #ifdef LUA_HPP
case LUA: ADDTARGET(LUA_HPP); break; case LUA: ADDTARGET(LUA_HPP); break;

View File

@ -14,6 +14,7 @@ int main(int argc, char* argv[]) {
string target = ".lua"; string target = ".lua";
bool printResult = false; bool printResult = false;
bool parallel = false; bool parallel = false;
bool newLinesSeparator = true;
if (argc > 0) { if (argc > 0) {
vector<string_view> files; vector<string_view> files;
@ -22,15 +23,15 @@ int main(int argc, char* argv[]) {
const string_view currentArg (argv[i]); const string_view currentArg (argv[i]);
if (currentArg == ArgumentShort("printresult")) printResult = true; if (currentArg == ArgumentShort("printresult")) printResult = true;
else if (currentArg == ArgumentAssignable("target")) { else if (currentArg == ArgumentAssignable("target")) {
target.clear(); const string value = ArgumentAssignable::getValueFor(currentArg.data());
string value = ArgumentAssignable::getValueFor(currentArg.data());
if (!value.empty()) (target = '.') += value; if (!value.empty()) (target = '.') += value;
} }
else if (currentArg == Argument("parallel")) parallel = true; else if (currentArg == Argument("parallel")) parallel = true;
else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg); else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg);
else if (currentArg == Argument("newlinesoff")) newLinesSeparator = true;
} }
const auto compile = [&target](string_view name) { const auto compile = [&target, &newLinesSeparator](string_view name) {
string transpiledString = transpile(parseString(getFileContent(name.data())), target); string transpiledString = transpile(parseString(getFileContent(name.data()), newLinesSeparator ? ';' : '\n'), target);
name.remove_suffix(6); name.remove_suffix(6);
string outputFile; string outputFile;
(outputFile = name).append(target); (outputFile = name).append(target);