parser.cc

来自「这个程序是关于OpenC++的反射植入机制的编译器」· CC 代码 · 共 3,111 行 · 第 1/5 页

CC
3,111 行
字号
	}	else{	    lex->GetToken(tk);	    body = PtreeUtil::Nconc(body, PtreeUtil::List(name, new Leaf(tk)));	}    }}/*  class.spec  : {userdef.keyword} class.key class.body  | {userdef.keyword} class.key name {class.body}  | {userdef.keyword} class.key name ':' base.specifiers class.body  class.key  : CLASS | STRUCT | UNION*/bool Parser::rClassSpec(Ptree*& spec, Encoding& encode){    Ptree *head, *bases, *body, *name;    Token tk;    head = 0;    if(lex->LookAhead(0) == UserKeyword)	if(!rUserdefKeyword(head))	    return false;    int t = lex->GetToken(tk);    if(t != CLASS && t != STRUCT && t != UNION)	return false;    Ptree* comments = lex->GetComments();    spec = new PtreeClassSpec(new LeafReserved(tk), 0, comments);    if(head != 0)	spec = new PtreeClassSpec(head, spec, comments);    if(lex->LookAhead(0) == '{'){	encode.NoName();	spec = PtreeUtil::Snoc(spec, PtreeUtil::List(0, 0));    }    else{	if(!rName(name, encode))	    return false;	spec = PtreeUtil::Snoc(spec, name);	t = lex->LookAhead(0);	if(t == ':'){	    if(!rBaseSpecifiers(bases))		return false;	    spec = PtreeUtil::Snoc(spec, bases);	}	else if(t == '{')	    spec = PtreeUtil::Snoc(spec, 0);	else{	    ((PtreeClassSpec*)spec)->encoded_name = encode.Get();	    return true;	// class.key Identifier	}    }    ((PtreeClassSpec*)spec)->encoded_name = encode.Get();    if(!rClassBody(body))	return false;    spec = PtreeUtil::Snoc(spec, body);    return true;}/*  base.specifiers  : ':' base.specifier (',' base.specifier)*  base.specifier  : {{VIRTUAL} (PUBLIC | PROTECTED | PRIVATE) {VIRTUAL}} name*/bool Parser::rBaseSpecifiers(Ptree*& bases){    Token tk;    int t;    Ptree* name;    Encoding encode;    if(lex->GetToken(tk) != ':')	return false;    bases = PtreeUtil::List(new Leaf(tk));    for(;;){	Ptree* super = 0;	t = lex->LookAhead(0);	if(t == VIRTUAL){	    lex->GetToken(tk);	    super = PtreeUtil::Snoc(super, new LeafVIRTUAL(tk));	    t = lex->LookAhead(0);	}	if(t == PUBLIC || t == PROTECTED || t == PRIVATE){	    Ptree* lf;	    switch(lex->GetToken(tk)){	    case PUBLIC :		lf = new LeafPUBLIC(tk);		break;	    case PROTECTED :		lf = new LeafPROTECTED(tk);		break;	    case PRIVATE :		lf = new LeafPRIVATE(tk);		break;	    default :		errorLog_.Report(		    MopMsg(Msg::Fatal, "rBaseSpecifiers()", "fatal"));		lf = 0;		break;	    }	    super = PtreeUtil::Snoc(super, lf);	    t = lex->LookAhead(0);	}	if(t == VIRTUAL){	    lex->GetToken(tk);	    super = PtreeUtil::Snoc(super, new LeafVIRTUAL(tk));	}	encode.Clear();	if(!rName(name, encode))	    return false;	if(!name->IsLeaf())	    name = new PtreeName(name, encode.Get());	super = PtreeUtil::Snoc(super, name);	bases = PtreeUtil::Snoc(bases, super);	if(lex->LookAhead(0) != ',')	    return true;	else{	    lex->GetToken(tk);	    bases = PtreeUtil::Snoc(bases, new Leaf(tk));	}    }}/*  class.body : '{' (class.member)* '}'*/bool Parser::rClassBody(Ptree*& body){    Token tk;    Ptree *mems, *m;    if(lex->GetToken(tk) != '{')	return false;    Ptree* ob = new Leaf(tk);    mems = 0;    while(lex->LookAhead(0) != '}'){	if(!rClassMember(m)){	    if(!SyntaxError())		return false;	// too many errors	    SkipTo('}');	    lex->GetToken(tk);	    body = PtreeUtil::List(ob, 0, new Leaf(tk));	    return true;	// error recovery	}	lex->GetComments();	mems = PtreeUtil::Snoc(mems, m);    }    lex->GetToken(tk);    body = new PtreeClassBody(ob, mems, new Leaf(tk));    return true;}/*  class.member  : (PUBLIC | PROTECTED | PRIVATE) ':'  | user.access.spec  | ';'  | typedef.stmt  | template.decl  | using.declaration  | metaclass.decl  | declaration  | access.decl  Note: if you modify this function, see ClassWalker::TranslateClassSpec()  as well.*/bool Parser::rClassMember(Ptree*& mem){    Token tk1, tk2;    int t = lex->LookAhead(0);    if(t == PUBLIC || t == PROTECTED || t == PRIVATE){	Ptree* lf;	switch(lex->GetToken(tk1)){	case PUBLIC :	    lf = new LeafPUBLIC(tk1);	    break;	case PROTECTED :	    lf = new LeafPROTECTED(tk1);	    break;	case PRIVATE :	    lf = new LeafPRIVATE(tk1);	    break;	default :	    errorLog_.Report(MopMsg(Msg::Fatal, "rClassMember()", "fatal"));	    lf = 0;	    break;	}	if(lex->GetToken(tk2) != ':')	    return false;	mem = new PtreeAccessSpec(lf, PtreeUtil::List(new Leaf(tk2)));	return true;    }    else if(t == UserKeyword4)	return rUserAccessSpec(mem);    else if(t == ';')	return rNullDeclaration(mem);    else if(t == TYPEDEF)	return rTypedef(mem);    else if(t == TEMPLATE)	return rTemplateDecl(mem);    else if(t == USING)	return rUsing(mem);    else if(t == METACLASS)	return rMetaclassDecl(mem);    else{	char* pos = lex->Save();	Ptree* comments = lex->GetComments2();	if(rDeclaration(mem)) {	    PtreeUtil::SetDeclaratorComments(mem, comments);	    return true;	}	lex->Restore(pos);	return rAccessDecl(mem);    }}/*  access.decl  : name ';'		e.g. <qualified class>::<member name>;*/bool Parser::rAccessDecl(Ptree*& mem){    Ptree* name;    Encoding encode;    Token tk;    if(!rName(name, encode))	return false;    if(lex->GetToken(tk) != ';')	return false;    mem = new PtreeAccessDecl(new PtreeName(name, encode.Get()),			       PtreeUtil::List(new Leaf(tk)));    return true;}/*  user.access.spec  : UserKeyword4 ':'  | UserKeyword4 '(' function.arguments ')' ':'*/bool Parser::rUserAccessSpec(Ptree*& mem){    Token tk1, tk2, tk3, tk4;    Ptree* args;    if(lex->GetToken(tk1) != UserKeyword4)	return false;    int t = lex->GetToken(tk2);    if(t == ':'){	mem = new PtreeUserAccessSpec(new Leaf(tk1),				      PtreeUtil::List(new Leaf(tk2)));	return true;    }    else if(t == '('){	if(!rFunctionArguments(args))	    return false;	if(lex->GetToken(tk3) != ')')	    return false;	if(lex->GetToken(tk4) != ':')	    return false;	mem = new PtreeUserAccessSpec(new Leaf(tk1),				      PtreeUtil::List(new Leaf(tk2), args,						  new Leaf(tk3),						  new Leaf(tk4)));	return true;    }    else	return false;}/*  comma.expression  : expression  | comma.expression ',' expression	(left-to-right)*/bool Parser::rCommaExpression(Ptree*& exp){    Token tk;    Ptree *right;    if(!rExpression(exp))	return false;    while(lex->LookAhead(0) == ','){	lex->GetToken(tk);	if(!rExpression(right))	    return false;	exp = new PtreeCommaExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  expression  : conditional.expr {(AssignOp | '=') expression}	right-to-left*/bool Parser::rExpression(Ptree*& exp){    Token tk;    Ptree *left, *right;    if(!rConditionalExpr(left))	return false;    int t = lex->LookAhead(0);    if(t != '=' && t != AssignOp)	exp = left;    else{	lex->GetToken(tk);	if(!rExpression(right))	    return false;	exp = new PtreeAssignExpr(left, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  conditional.expr  : logical.or.expr {'?' comma.expression ':' conditional.expr}  right-to-left*/bool Parser::rConditionalExpr(Ptree*& exp){    Token tk1, tk2;    Ptree *then, *otherwise;    if(!rLogicalOrExpr(exp, false))	return false;    if(lex->LookAhead(0) == '?'){	lex->GetToken(tk1);	if(!rCommaExpression(then))	    return false;	if(lex->GetToken(tk2) != ':')	    return false;	if(!rConditionalExpr(otherwise))	    return false;	exp = new PtreeCondExpr(exp, PtreeUtil::List(new Leaf(tk1), then,						 new Leaf(tk2), otherwise));    }    return true;}/*  logical.or.expr  : logical.and.expr  | logical.or.expr LogOrOp logical.and.expr		left-to-right*/bool Parser::rLogicalOrExpr(Ptree*& exp, bool temp_args){    Token tk;    Ptree *right;    if(!rLogicalAndExpr(exp, temp_args))	return false;    while(lex->LookAhead(0) == LogOrOp){	lex->GetToken(tk);	if(!rLogicalAndExpr(right, temp_args))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  logical.and.expr  : inclusive.or.expr  | logical.and.expr LogAndOp inclusive.or.expr*/bool Parser::rLogicalAndExpr(Ptree*& exp, bool temp_args){    Token tk;    Ptree *right;    if(!rInclusiveOrExpr(exp, temp_args))	return false;    while(lex->LookAhead(0) == LogAndOp){	lex->GetToken(tk);	if(!rInclusiveOrExpr(right, temp_args))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  inclusive.or.expr  : exclusive.or.expr  | inclusive.or.expr '|' exclusive.or.expr*/bool Parser::rInclusiveOrExpr(Ptree*& exp, bool temp_args){    Token tk;    Ptree *right;    if(!rExclusiveOrExpr(exp, temp_args))	return false;    while(lex->LookAhead(0) == '|'){	lex->GetToken(tk);	if(!rExclusiveOrExpr(right, temp_args))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  exclusive.or.expr  : and.expr  | exclusive.or.expr '^' and.expr*/bool Parser::rExclusiveOrExpr(Ptree*& exp, bool temp_args){    Token tk;    Ptree *right;    if(!rAndExpr(exp, temp_args))	return false;    while(lex->LookAhead(0) == '^'){	lex->GetToken(tk);	if(!rAndExpr(right, temp_args))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  and.expr  : equality.expr  | and.expr '&' equality.expr*/bool Parser::rAndExpr(Ptree*& exp, bool temp_args){    Token tk;    Ptree *right;    if(!rEqualityExpr(exp, temp_args))	return false;    while(lex->LookAhead(0) == '&'){	lex->GetToken(tk);	if(!rEqualityExpr(right, temp_args))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  equality.expr  : relational.expr  | equality.expr EqualOp relational.expr*/bool Parser::rEqualityExpr(Ptree*& exp, bool temp_args){    Token tk;    Ptree *right;    if(!rRelationalExpr(exp, temp_args))	return false;    while(lex->LookAhead(0) == EqualOp){	lex->GetToken(tk);	if(!rRelationalExpr(right, temp_args))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  relational.expr  : shift.expr  | relational.expr (RelOp | '<' | '>') shift.expr*/bool Parser::rRelationalExpr(Ptree*& exp, bool temp_args){    int t;    Token tk;    Ptree *right;    if(!rShiftExpr(exp))	return false;    while(t = lex->LookAhead(0),	  (t == RelOp || t == '<' || (t == '>' && !temp_args))){	lex->GetToken(tk);	if(!rShiftExpr(right))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  shift.expr  : additive.expr  | shift.expr ShiftOp additive.expr*/bool Parser::rShiftExpr(Ptree*& exp){    Token tk;    Ptree *right;    if(!rAdditiveExpr(exp))	return false;    while(lex->LookAhead(0) == ShiftOp){	lex->GetToken(tk);	if(!rAdditiveExpr(right))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  additive.expr  : multiply.expr  | additive.expr ('+' | '-') multiply.expr*/bool Parser::rAdditiveExpr(Ptree*& exp){    int t;    Token tk;    Ptree *right;    if(!rMultiplyExpr(exp))	return false;    while(t = lex->LookAhead(0), (t == '+' || t == '-')){	lex->GetToken(tk);	if(!rMultiplyExpr(right))	    return false;	exp = new PtreeInfixExpr(exp, PtreeUtil::List(new Leaf(tk), right));    }    return true;}/*  multiply.expr  : pm.expr  | multiply.expr ('*' | '/' | '%') pm.expr*/bool Parser::rMultiplyExpr(Ptree*& exp){    int t;    Token tk;    Ptree *right;    if(!rPmExpr(exp))	return false;    while(t = lex->LookAhead(0), (t == '*' || t == '/' || t == '%')){	lex->GetToken(tk);	if(!rPmExpr(rig

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?