蛋疼的业务功能、蛋疼的流程、蛋疼的需求变更.....
彻底地被折磨得不成人样,更加让我无法忍受的是,看着大家代码无数的圈复杂度,一层层地写if、else等逻辑处理语句,来进行控制业务功能的,但是被动的是,你就再怎么细心,也不可能覆盖到所有的测试用例。
这里讲了一个领域的,是定义人类-》删除-》责骂-》剔除-》拥抱,编写g4文件
grammar PersonableGrammar;
prog: stat+ ;
stat: expr NEWLINE? # printExpr
| NEWLINE # blank
;
expr: DEF ID # DefinePerson
| REM ID # RemovePerson
| BLAME ID # BlamePerson
| KICK ID # KickPerson
| HUG ID # HugPerson
;
//people actions
DEF : 'define' ;
REM : 'remove' ;
BLAME: 'blame' ;
HUG: 'hug' ;
ID : [a-zA-Z]+ ;
INT : [0-9]+ ;
NEWLINE:'\r'? '\n' ;
WS : [ \t]+ -> skip ; // toss out whitespace
LINE_COMMENT : '//' .*? '\r'? '\n' -> skip ; // Match "//" stuff '\n'
使用antlrwork生成visitor,实现其接口
public class PersonableVistorImpl extends PersonableGrammarBaseVisitor{
ArrayList<String> personList = new ArrayList<String>();
@Override
public String visitPrintExpr(@NotNull PersonableGrammarParser.PrintExprContext ctx) {
String value = String.valueOf(visit(ctx.expr()));
System.out.println(value);
return value;
}
@Override
public String visitDefinePerson(@NotNull PersonableGrammarParser.DefinePersonContext ctx) {
String value = String.valueOf(ctx.ID());
System.out.println("++ Defining "+value);
personList.add(value);
return personList.toString();
}
@Override
public Object visitRemovePerson(@NotNull PersonableGrammarParser.RemovePersonContext ctx) {
String value = String.valueOf(ctx.ID());
personList.remove(value);
return personList.toString();
}
@Override
public String visitBlamePerson(@NotNull PersonableGrammarParser.BlamePersonContext ctx) {
String value = String.valueOf(ctx.ID());
if (!personList.contains(value)){
System.out.println("++ You must define a person before blaming him or her.");
return personList.toString();
}
//do something to blame person
System.out.println("++ It's all "+value+"'s fault.");
return personList.toString();
}
@Override
public String visitHugPerson(@NotNull PersonableGrammarParser.HugPersonContext ctx) {
String value = String.valueOf(ctx.ID());
if (!personList.contains(value)){
System.out.println("++ You must define a person before Hugging him or her.");
return personList.toString();
}
//do something to hug person
System.out.println("Giving " +value +" a hug.");
return personList.toString();
}
}