Skip to content

Commit

Permalink
fix(Gen): 修复了代码块生成的过多问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wangziwenhk committed Jul 29, 2024
1 parent 2434c48 commit 3aa160b
Show file tree
Hide file tree
Showing 11 changed files with 922 additions and 642 deletions.
14 changes: 13 additions & 1 deletion RiddleParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ statement_ed
| Endl
;

//只包含定义的语句
defineStatement_ed
: defineStatement Semi? Endl?
| Semi
| Endl
;

statement
: packStatement
| importStatement
Expand All @@ -35,6 +42,11 @@ statement
| LeftCurly statement_ed* RightCurly
;

defineStatement
: funcDefine
| varDefineStatement
;

packStatement
: Package packName=id
;
Expand Down Expand Up @@ -87,7 +99,7 @@ classStatement
;

classBody
: statement_ed*
: defineStatement_ed*
;

// 这一块就是使用
Expand Down
6 changes: 3 additions & 3 deletions parser/RiddleLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void riddlelexerLexerInitialize() {
std::vector<std::string>{
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"},
std::vector<std::string>{
"DEFAULT_MODE"
"DEFAULT_MODE"
},
std::vector<std::string>{
"", "'var'", "'val'", "'for'", "'while'", "'if'", "'else'", "'fun'",
Expand All @@ -96,8 +96,8 @@ void riddlelexerLexerInitialize() {
7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21,
7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 2, 25, 7, 25, 2, 26, 7, 26, 2, 27, 7, 27, 2, 28,
7, 28, 2, 29, 7, 29, 2, 30, 7, 30, 2, 31, 7, 31, 2, 32, 7, 32, 2, 33, 7, 33, 2, 34, 7, 34, 2, 35,
7,35,2,36,7,36,2,37,7,37,2,38,7,38,2,39,7,39,2,40,7,40,2,41,7,41,2,42,
7,42,2,43,7,43,2,44,7,44,2,45,7,45,2,46,7,46,2,47,7,47,2,48,7,48,2,49,
7, 35, 2, 36, 7, 36, 2, 37, 7, 37, 2, 38, 7, 38, 2, 39, 7, 39, 2, 40, 7, 40, 2, 41, 7, 41, 2, 42,
7,42,2,43,7,43,2,44,7,44,2,45,7,45,2,46,7,46,2,47,7,47,2,48,7,48,2,49,
7,49,2,50,7,50,2,51,7,51,2,52,7,52,2,53,7,53,2,54,7,54,2,55,7,55,2,56,
7, 56, 2, 57, 7, 57, 2, 58, 7, 58, 2, 59, 7, 59, 2, 60, 7, 60, 2, 61, 7, 61, 1, 0, 1, 0, 1, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4,
Expand Down
2 changes: 1 addition & 1 deletion parser/RiddleLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class RiddleLexer : public antlr4::Lexer {

explicit RiddleLexer(antlr4::CharStream *input);

~RiddleLexer() override;
~RiddleLexer() override;


std::string getGrammarFileName() const override;
Expand Down
1,359 changes: 783 additions & 576 deletions parser/RiddleParser.cpp

Large diffs are not rendered by default.

135 changes: 85 additions & 50 deletions parser/RiddleParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,36 @@ class RiddleParser : public antlr4::Parser {
enum {
RuleProgram = 0,
RuleStatement_ed = 1,
RuleStatement = 2,
RulePackStatement = 3,
RuleImportStatement = 4,
RuleVarDefineStatement = 5,
RuleArgsExpr = 6,
RuleDefineArgs = 7,
RuleFuncDefine = 8,
RuleFuncBody = 9,
RuleForStatement = 10,
RuleWhileStatement = 11,
RuleIfStatement = 12,
RuleReturnStatement = 13,
RuleClassStatement = 14,
RuleClassBody = 15,
RuleExprPtr = 16,
RuleExpression = 17,
RuleId = 18,
RuleNumber = 19,
RuleBoolean = 20,
RuleString = 21,
RuleFloat = 22,
RuleInteger = 23,
RuleTemplateArg = 24,
RuleTemplateArgs = 25,
RuleTypeName = 26
RuleDefineStatement_ed = 2,
RuleStatement = 3,
RuleDefineStatement = 4,
RulePackStatement = 5,
RuleImportStatement = 6,
RuleVarDefineStatement = 7,
RuleArgsExpr = 8,
RuleDefineArgs = 9,
RuleFuncDefine = 10,
RuleFuncBody = 11,
RuleForStatement = 12,
RuleWhileStatement = 13,
RuleIfStatement = 14,
RuleReturnStatement = 15,
RuleClassStatement = 16,
RuleClassBody = 17,
RuleExprPtr = 18,
RuleExpression = 19,
RuleId = 20,
RuleNumber = 21,
RuleBoolean = 22,
RuleString = 23,
RuleFloat = 24,
RuleInteger = 25,
RuleTemplateArg = 26,
RuleTemplateArgs = 27,
RuleTypeName = 28
};

explicit RiddleParser(antlr4::TokenStream *input);
explicit RiddleParser(antlr4::TokenStream *input);

RiddleParser(antlr4::TokenStream *input, const antlr4::atn::ParserATNSimulatorOptions &options);

Expand All @@ -124,7 +126,9 @@ class RiddleParser : public antlr4::Parser {

class ProgramContext;
class Statement_edContext;
class DefineStatement_edContext;
class StatementContext;
class DefineStatementContext;
class PackStatementContext;
class ImportStatementContext;
class VarDefineStatementContext;
Expand Down Expand Up @@ -184,6 +188,22 @@ class RiddleParser : public antlr4::Parser {

Statement_edContext* statement_ed();

class DefineStatement_edContext : public antlr4::ParserRuleContext {
public:
DefineStatement_edContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
DefineStatementContext *defineStatement();
antlr4::tree::TerminalNode *Semi();
antlr4::tree::TerminalNode *Endl();

virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

DefineStatement_edContext *defineStatement_ed();

class StatementContext : public antlr4::ParserRuleContext {
public:
StatementContext(antlr4::ParserRuleContext *parent, size_t invokingState);
Expand Down Expand Up @@ -212,6 +232,21 @@ class RiddleParser : public antlr4::Parser {

StatementContext* statement();

class DefineStatementContext : public antlr4::ParserRuleContext {
public:
DefineStatementContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
FuncDefineContext *funcDefine();
VarDefineStatementContext *varDefineStatement();

virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

DefineStatementContext *defineStatement();

class PackStatementContext : public antlr4::ParserRuleContext {
public:
RiddleParser::IdContext *packName = nullptr;
Expand Down Expand Up @@ -253,7 +288,7 @@ class RiddleParser : public antlr4::Parser {
RiddleParser::ExpressionContext *value = nullptr;
VarDefineStatementContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *Var();
antlr4::tree::TerminalNode *Var();
antlr4::tree::TerminalNode *Colon();
antlr4::tree::TerminalNode *Identifier();
TypeNameContext *typeName();
Expand Down Expand Up @@ -467,8 +502,8 @@ class RiddleParser : public antlr4::Parser {
public:
ClassBodyContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<Statement_edContext *> statement_ed();
Statement_edContext *statement_ed(size_t i);
std::vector<DefineStatement_edContext *> defineStatement_ed();
DefineStatement_edContext *defineStatement_ed(size_t i);

virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
Expand Down Expand Up @@ -553,10 +588,10 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *expr = nullptr;
std::vector<antlr4::tree::TerminalNode *> Sub();
antlr4::tree::TerminalNode* Sub(size_t i);
antlr4::tree::TerminalNode* Sub(size_t i);
ExprPtrContext *exprPtr();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
Expand All @@ -567,7 +602,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *And();
antlr4::tree::TerminalNode *And();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -666,7 +701,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Star();
antlr4::tree::TerminalNode *Star();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand All @@ -682,7 +717,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Xor();
antlr4::tree::TerminalNode *Xor();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -729,7 +764,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Add();
antlr4::tree::TerminalNode *Add();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand All @@ -745,7 +780,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Or();
antlr4::tree::TerminalNode *Or();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -813,7 +848,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Div();
antlr4::tree::TerminalNode *Div();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -860,10 +895,10 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *expr = nullptr;
std::vector<antlr4::tree::TerminalNode *> Sub();
antlr4::tree::TerminalNode* Sub(size_t i);
antlr4::tree::TerminalNode* Sub(size_t i);
ExprPtrContext *exprPtr();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
Expand Down Expand Up @@ -904,10 +939,10 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Assign();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
Expand All @@ -919,10 +954,10 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *expr = nullptr;
std::vector<antlr4::tree::TerminalNode *> Add();
antlr4::tree::TerminalNode* Add(size_t i);
antlr4::tree::TerminalNode* Add(size_t i);
ExprPtrContext *exprPtr();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
Expand All @@ -933,7 +968,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Sub();
antlr4::tree::TerminalNode *Sub();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -1020,10 +1055,10 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *expr = nullptr;
std::vector<antlr4::tree::TerminalNode *> Add();
antlr4::tree::TerminalNode* Add(size_t i);
antlr4::tree::TerminalNode* Add(size_t i);
ExprPtrContext *exprPtr();
virtual void enterRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;
virtual void exitRule(antlr4::tree::ParseTreeListener *listener) override;

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
Expand All @@ -1034,7 +1069,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *RightRightRight();
antlr4::tree::TerminalNode *RightRightRight();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -1066,7 +1101,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *RightRight();
antlr4::tree::TerminalNode *RightRight();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down Expand Up @@ -1125,7 +1160,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *LeftLeft();
antlr4::tree::TerminalNode *LeftLeft();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand All @@ -1141,7 +1176,7 @@ class RiddleParser : public antlr4::Parser {

RiddleParser::ExprPtrContext *left = nullptr;
RiddleParser::ExpressionContext *right = nullptr;
antlr4::tree::TerminalNode *Mod();
antlr4::tree::TerminalNode *Mod();
antlr4::tree::TerminalNode *Assign();
ExprPtrContext *exprPtr();
ExpressionContext *expression();
Expand Down
Loading

0 comments on commit 3aa160b

Please sign in to comment.