✨ add token `??` and `?.`
This commit is contained in:
parent
5a165d3255
commit
5e2268e4c5
|
@ -35,12 +35,16 @@ bool lexer::is_str(char c) {
|
|||
return c=='\'' || c=='\"' || c=='`';
|
||||
}
|
||||
|
||||
bool lexer::is_quesmark(char c) {
|
||||
return c=='?';
|
||||
}
|
||||
|
||||
bool lexer::is_single_opr(char c) {
|
||||
return (
|
||||
c=='(' || c==')' || c=='[' || c==']' ||
|
||||
c=='{' || c=='}' || c==',' || c==';' ||
|
||||
c==':' || c=='?' || c=='`' || c=='@' ||
|
||||
c=='%' || c=='$' || c=='\\'
|
||||
c==':' || c=='`' || c=='@' || c=='%' ||
|
||||
c=='$' || c=='\\'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -103,6 +107,8 @@ void lexer::open(const std::string& file) {
|
|||
}
|
||||
|
||||
tok lexer::get_type(const std::string& str) {
|
||||
// search token type from mapper
|
||||
// if cannot find, just return null
|
||||
return token_mapper.count(str)? token_mapper.at(str):tok::tk_null;
|
||||
}
|
||||
|
||||
|
@ -333,6 +339,24 @@ token lexer::str_gen() {
|
|||
};
|
||||
}
|
||||
|
||||
token lexer::quesmark_gen() {
|
||||
u64 begin_line = line;
|
||||
u64 begin_column = column;
|
||||
std::string str(1, res[ptr]);
|
||||
++column;
|
||||
++ptr;
|
||||
if (ptr < res.size() && (res[ptr]=='?' || res[ptr]=='.')) {
|
||||
str += res[ptr];
|
||||
++column;
|
||||
++ptr;
|
||||
}
|
||||
return {
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
get_type(str),
|
||||
str
|
||||
};
|
||||
}
|
||||
|
||||
token lexer::single_opr() {
|
||||
u64 begin_line = line;
|
||||
u64 begin_column = column;
|
||||
|
@ -398,6 +422,8 @@ const error& lexer::scan(const std::string& file) {
|
|||
toks.push_back(num_gen());
|
||||
} else if (is_str(res[ptr])) {
|
||||
toks.push_back(str_gen());
|
||||
} else if (is_quesmark(res[ptr])) {
|
||||
toks.push_back(quesmark_gen());
|
||||
} else if (is_single_opr(res[ptr])) {
|
||||
toks.push_back(single_opr());
|
||||
} else if (res[ptr]=='.') {
|
||||
|
|
|
@ -50,6 +50,8 @@ enum class tok {
|
|||
tk_dot, // .
|
||||
tk_ellipsis, // ...
|
||||
tk_quesmark, // ?
|
||||
tk_quesques, // ??
|
||||
tk_quesdot, // ?.
|
||||
tk_colon, // :
|
||||
tk_add, // operator +
|
||||
tk_sub, // operator -
|
||||
|
@ -79,9 +81,10 @@ enum class tok {
|
|||
};
|
||||
|
||||
struct token {
|
||||
span loc; // location
|
||||
tok type; // token type
|
||||
span loc; // location
|
||||
tok type; // token type
|
||||
std::string str; // content
|
||||
|
||||
token() = default;
|
||||
token(const token&) = default;
|
||||
};
|
||||
|
@ -128,6 +131,8 @@ private:
|
|||
{"." , tok::tk_dot },
|
||||
{"..." , tok::tk_ellipsis},
|
||||
{"?" , tok::tk_quesmark},
|
||||
{"??" , tok::tk_quesques},
|
||||
{"?." , tok::tk_quesdot },
|
||||
{":" , tok::tk_colon },
|
||||
{"+" , tok::tk_add },
|
||||
{"-" , tok::tk_sub },
|
||||
|
@ -162,6 +167,7 @@ private:
|
|||
bool is_oct(char);
|
||||
bool is_dec(char);
|
||||
bool is_str(char);
|
||||
bool is_quesmark(char);
|
||||
bool is_single_opr(char);
|
||||
bool is_calc_opr(char);
|
||||
|
||||
|
@ -173,6 +179,7 @@ private:
|
|||
token id_gen();
|
||||
token num_gen();
|
||||
token str_gen();
|
||||
token quesmark_gen();
|
||||
token single_opr();
|
||||
token dots();
|
||||
token calc_opr();
|
||||
|
|
|
@ -53,6 +53,8 @@ private:
|
|||
{tok::tk_dot , "." },
|
||||
{tok::tk_ellipsis, "..." },
|
||||
{tok::tk_quesmark, "?" },
|
||||
{tok::tk_quesques, "??" },
|
||||
{tok::tk_quesdot , "?." },
|
||||
{tok::tk_colon , ":" },
|
||||
{tok::tk_add , "+" },
|
||||
{tok::tk_sub , "-" },
|
||||
|
|
Loading…
Reference in New Issue