Skip to content

Commit

Permalink
ADD: Add lyric
Browse files Browse the repository at this point in the history
  • Loading branch information
bajdcc committed Jul 27, 2020
1 parent 221a92b commit 3c7d945
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 57 deletions.
30 changes: 27 additions & 3 deletions CCGameFramework/base/clibjs/cjsgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,8 +1498,31 @@ namespace clib {
str.Format(L"屏幕(%d)键盘输入:%d 0x%x %c", screen_ptr, c, c, isprint(c) ? wchar_t(c) : L'?');
add_stat(str);
}
if (!input_state)
if (!input_state) {
if (c & GUI_SPECIAL_MASK) {
switch (c & 0xff) {
case VK_UP:
view = max(0, view - 1);
break;
case VK_DOWN:
view = min(max(line - rows, 0), view + 1);
break;
case VK_HOME:
view = 0;
break;
case VK_END:
view = max(0, line - rows + 1);
break;
case VK_NEXT: // page down
view = min(line - rows + 1, view + rows);
break;
case VK_PRIOR: // page up
view = max(0, view - rows);
break;
}
}
return;
}
if (global_state.input_s->input_single) {
if (c > 0 && c < 256 && (std::isprint(c) || c == '\r')) {
if (c == '\r')
Expand Down Expand Up @@ -1639,7 +1662,7 @@ namespace clib {
case VK_END:
{
if (!input_state || js_key_pressing(VK_CONTROL)) {
view = max(0, line - rows);
view = max(0, line - rows + 1);
return;
}
else if (js_key_pressing(VK_MENU)) {
Expand Down Expand Up @@ -1815,7 +1838,8 @@ namespace clib {
OpenClipboard(window->GetWindowHandle());
std::vector<char> outs;
for (auto i = 0; i <= line; ++i) {
for (auto j = 0; j < valid[i]; ++j) {
auto end = min(valid[i], cols - 1);
for (auto j = 0; j <= end; ++j) {
const auto& b = buffer[i * cols + j];
if (b == 0) {
break;
Expand Down
2 changes: 1 addition & 1 deletion CCGameFramework/base/clibjs/cjslexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ namespace clib {
u.t = END;
u.id = id;
units.push_back(u);
no_line.push_back(false);
no_line.push_back(true);
}
}

Expand Down
49 changes: 30 additions & 19 deletions CCGameFramework/base/clibjs/cjsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
#define TRACE_PARSING_AST 1
#define DUMP_LEXER 0
#define DUMP_PDA 0
#define DUMP_PDA_FILE "js_PDA.txt"
#define DUMP_PDA_FILE "js_PDA.log"
#define DEBUG_AST 0
#define CHECK_AST 0

namespace clib {

std::unique_ptr<cjsunit> cjsparser::unit;

bool cjsparser::parse(const std::string &str, std::string &error_string, csemantic *s) {
semantic = s;
lexer = std::make_unique<cjslexer>();
Expand All @@ -43,8 +45,10 @@ namespace clib {
// 清空AST
ast->reset();
// 产生式
if (unit.get_pda().empty())
if (!unit) {
unit = std::make_unique<cjsunit>();
gen();
}
// 语法分析(递归下降)
auto r = program(error_string, str);
return r;
Expand All @@ -58,6 +62,13 @@ namespace clib {
ast.reset();
}

void cjsparser::dump_pda(std::ostream& os)
{
#if DUMP_PDA
unit->dump(os);
#endif
}

void cjsparser::next() {
current = &lexer->get_current_unit();
lexer->inc_index();
Expand All @@ -66,9 +77,9 @@ namespace clib {
void cjsparser::gen() {
// REFER: antlr/grammars-v4
// URL: https://github.com/antlr/grammars-v4/blob/master/javascript/javascript/JavaScriptParser.g4
#define DEF_LEXER(name) auto &_##name = unit.token(name);
#define DEF_LEXER(name) auto &_##name = unit->token(name);
#define DEF_LEXER_RULE(name) DEF_LEXER(name)
#define DEF_LEXER_RULE_LA(name) auto &_##name = unit.token(name, true);
#define DEF_LEXER_RULE_LA(name) auto &_##name = unit->token(name, true);
DEF_LEXER(NUMBER)
DEF_LEXER(ID)
DEF_LEXER(REGEX)
Expand Down Expand Up @@ -169,8 +180,8 @@ namespace clib {
DEF_LEXER(T_ELLIPSIS)
DEF_LEXER(T_ARROW)
#undef DEF_LEXER
#define DEF_RULE(name) auto &name = unit.rule(#name, c_##name);
#define DEF_RULE_ATTR(name, attr) auto &name = unit.rule(#name, c_##name, attr);
#define DEF_RULE(name) auto &name = unit->rule(#name, c_##name);
#define DEF_RULE_ATTR(name, attr) auto &name = unit->rule(#name, c_##name, attr);
#define DEF_RULE_NOT_GREED(name) DEF_RULE_ATTR(name, r_not_greed)
#define DEF_RULE_EXP(name) DEF_RULE_ATTR(name, r_exp)
DEF_RULE(program)
Expand Down Expand Up @@ -500,16 +511,16 @@ namespace clib {
| _K_CLASS
| _K_SUPER
| _K_LET;
unit.adjust(&functionExpression, &anonymousFunction, e_shift, 1);
unit.adjust(&iterationStatement, &forInStatement, e_shift, -1);
unit.adjust(&statement, &expressionStatement, e_shift, 1);
unit.adjust(&iterationStatement, &forStatement, e_shift, 0, (void *) &pred_for);
unit.adjust(&newExpression, &newExpressionArgument, e_shift, 1);
unit.adjust(&inExpression, &inExpression, e_left_recursion, 0, (void *) &pred_in);
unit.gen(&program);
unit->adjust(&functionExpression, &anonymousFunction, e_shift, 1);
unit->adjust(&iterationStatement, &forInStatement, e_shift, -1);
unit->adjust(&statement, &expressionStatement, e_shift, 1);
unit->adjust(&iterationStatement, &forStatement, e_shift, 0, (void *) &pred_for);
unit->adjust(&newExpression, &newExpressionArgument, e_shift, 1);
unit->adjust(&inExpression, &inExpression, e_left_recursion, 0, (void *) &pred_in);
unit->gen(&program);
#if DUMP_PDA
std::ofstream of(DUMP_PDA_FILE);
unit.dump(of);
unit->dump(of);
#endif
}

Expand Down Expand Up @@ -545,7 +556,7 @@ namespace clib {
std::ofstream log(REPORT_ERROR_FILE, std::ios::app | std::ios::out);
#endif
next();
auto &pdas = unit.get_pda();
auto &pdas = unit->get_pda();
auto root = ast->new_node(a_collection);
root->line = root->column = 0;
root->data._coll = pdas[0].coll;
Expand Down Expand Up @@ -592,7 +603,7 @@ namespace clib {
for (;;) {
#if TRACE_PARSING
idx++;
//fflush(stdout);
fflush(stdout);
#endif
auto& current_state = pdas[bk->current_state];
if (current->t == END) {
Expand Down Expand Up @@ -778,7 +789,7 @@ namespace clib {

void cjsparser::print_bk(std::vector<std::shared_ptr<backtrace_t>>& bks) const
{
const auto& pdas = unit.get_pda();
const auto& pdas = unit->get_pda();
for (size_t i = 0; i < bks.size(); ++i) {
auto& _bk = bks[i];
fprintf(stdout,
Expand Down Expand Up @@ -975,7 +986,7 @@ namespace clib {
bk.state_stack.push_back(bk.current_state);
auto new_node = ast->new_node(a_collection);
new_node->line = new_node->column = 0;
auto &pdas = unit.get_pda();
auto &pdas = unit->get_pda();
new_node->data._coll = pdas[trans.jump].coll;
#if DEBUG_AST
fprintf(stdout, "[DEBUG] Shift: top=%p, new=%p, CS=%d\n", ast_stack.back(), new_node,
Expand Down Expand Up @@ -1049,7 +1060,7 @@ namespace clib {
return false;
if (!token->LA)
return true;
auto& pdas = unit.get_pda();
auto& pdas = unit->get_pda();
auto& target = pdas[trans.jump];
auto& t = target.trans;
auto id = token->type - RULE_START - 1;
Expand Down
3 changes: 2 additions & 1 deletion CCGameFramework/base/clibjs/cjsparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace clib {
bool parse(const std::string &str, std::string &, csemantic *s = nullptr);
js_ast_node *root() const;
void clear_ast();
void dump_pda(std::ostream& os);

using pda_coll_pred_cb = js_pda_coll_pred(*)(const cjslexer *, int idx);
using terminal_cb = void (*)(const cjslexer *, int idx,
Expand Down Expand Up @@ -88,7 +89,7 @@ namespace clib {
const lexer_unit *current{nullptr};

private:
cjsunit unit;
static std::unique_ptr<cjsunit> unit;
std::unique_ptr<cjslexer> lexer;
csemantic *semantic{nullptr};
std::unique_ptr<cjsast> ast;
Expand Down
6 changes: 5 additions & 1 deletion CCGameFramework/base/clibjs/cjsruntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace clib {
API_setInterval,
API_clearTimeout,
API_clearInterval,
API_parseInt,
API_eval,
API_http,
API_music,
Expand Down Expand Up @@ -424,7 +425,8 @@ namespace clib {
void init(const std::string& str, const std::string& flag);
bool test(const std::string& str);
std::string replace(const std::string& origin, const std::string& replacer);
bool match(const std::string& origin, std::vector<std::tuple<std::string, bool>>& matches);
bool match(const std::string& origin, std::vector<std::tuple<std::string, bool, int>>& matches);
bool match(const std::string& origin, std::vector<std::tuple<std::string, int>>& matches);
static std::string replace(const std::string& origin, const std::string& pat, const std::string& replacer);
std::string str_origin;
std::string str;
Expand Down Expand Up @@ -638,6 +640,7 @@ namespace clib {
jsv_function::ref global_setInterval;
jsv_function::ref global_clearTimeout;
jsv_function::ref global_clearInterval;
jsv_function::ref global_parseInt;
// proto
jsv_object::ref _proto_boolean;
jsv_object::ref _proto_function;
Expand All @@ -650,6 +653,7 @@ namespace clib {
jsv_function::ref _proto_object_valueOf;
jsv_object::ref _proto_string;
jsv_function::ref _proto_string_replace;
jsv_function::ref _proto_string_match;
jsv_object::ref _proto_root;
// console
jsv_object::ref console;
Expand Down
Loading

0 comments on commit 3c7d945

Please sign in to comment.