return re.match(token);
}
+template<typename T>
+RefPtr<T> Parser::create_node()
+{
+ RefPtr<T> node = new T;
+ node->source = source_index;
+ node->line = tokenizer.get_location().line;
+ return node;
+}
+
template<typename T>
RefPtr<T> Parser::parse_with_recovery(RefPtr<T> (Parser::*parse_func)())
{
token = tokenizer.peek_token();
if(is_interface_qualifier(token) && tokenizer.peek_token(1)==";")
{
- RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
- iface_lo->source = source_index;
- iface_lo->line = tokenizer.get_location().line;
+ RefPtr<InterfaceLayout> iface_lo = create_node<InterfaceLayout>();
iface_lo->layout.qualifiers = layout->qualifiers;
iface_lo->interface = tokenizer.parse_token();
tokenizer.expect(";");
return parse_return();
else if(token=="break" || token=="continue" || token=="discard")
{
- RefPtr<Jump> jump = new Jump;
- jump->source = source_index;
- jump->line = tokenizer.get_location().line;
+ RefPtr<Jump> jump = create_node<Jump>();
jump->keyword = tokenizer.parse_token();
tokenizer.expect(";");
}
else if(!token.empty())
{
- RefPtr<ExpressionStatement> expr = new ExpressionStatement;
- expr->source = source_index;
- expr->line = tokenizer.get_location().line;
+ RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
expr->expression = parse_expression();
tokenizer.expect(";");
throw invalid_shader_source(tokenizer.get_location(), "Imports are only allowed in the shared section");
tokenizer.expect("import");
- RefPtr<Import> import = new Import;
- import->source = source_index;
- import->line = tokenizer.get_location().line;
+ RefPtr<Import> import = create_node<Import>();
import->module = expect_identifier();
tokenizer.expect(";");
return import;
RefPtr<Precision> Parser::parse_precision()
{
tokenizer.expect("precision");
- RefPtr<Precision> precision = new Precision;
- precision->source = source_index;
- precision->line = tokenizer.get_location().line;
+ RefPtr<Precision> precision = create_node<Precision>();
precision->precision = tokenizer.parse_token();
if(!is_precision_qualifier(precision->precision))
{
tokenizer.expect("layout");
tokenizer.expect("(");
- RefPtr<Layout> layout = new Layout;
+ RefPtr<Layout> layout = create_node<Layout>();
while(1)
{
string token = tokenizer.parse_token();
}
else if(token==".")
{
- RefPtr<MemberAccess> memacc = new MemberAccess;
+ RefPtr<MemberAccess> memacc = create_node<MemberAccess>();
memacc->left = left;
memacc->oper = oper;
tokenizer.parse_token();
}
else if(oper && oper->type==Operator::POSTFIX)
{
- RefPtr<UnaryExpression> unary = new UnaryExpression;
+ RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
unary->oper = oper;
tokenizer.parse_token();
unary->expression = left;
if(token=="(")
{
tokenizer.parse_token();
- RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
+ RefPtr<ParenthesizedExpression> parexpr = create_node<ParenthesizedExpression>();
parexpr->expression = parse_expression();
tokenizer.expect(")");
left = parexpr;
}
else if(is_identifier(token))
{
- RefPtr<VariableReference> var = new VariableReference;
+ RefPtr<VariableReference> var = create_node<VariableReference>();
var->name = expect_identifier();
left = var;
left_var = var.get();
}
else if(oper && oper->type==Operator::PREFIX)
{
- RefPtr<UnaryExpression> unary = new UnaryExpression;
+ RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
unary->oper = oper;
tokenizer.parse_token();
unary->expression = parse_expression(oper->precedence);
RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator &oper)
{
- RefPtr<BinaryExpression> binary = (oper.precedence==16 ? new Assignment : new BinaryExpression);
+ RefPtr<BinaryExpression> binary = (oper.precedence==16 ?
+ static_cast<RefPtr<BinaryExpression> >(create_node<Assignment>()) : create_node<BinaryExpression>());
binary->left = left;
binary->oper = &oper;
tokenizer.expect(oper.token);
RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
{
- RefPtr<FunctionCall> call = new FunctionCall;
+ RefPtr<FunctionCall> call = create_node<FunctionCall>();
call->name = var.name;
call->constructor = is_type(call->name);
call->oper = &Operator::get_operator("(", Operator::POSTFIX);
RefPtr<StructDeclaration> Parser::parse_struct_declaration()
{
tokenizer.expect("struct");
- RefPtr<StructDeclaration> strct = new StructDeclaration;
- strct->source = source_index;
- strct->line = tokenizer.get_location().line;
+ RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
strct->name = expect_identifier();
parse_block(strct->members, true, &Parser::parse_variable_declaration);
RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
{
- RefPtr<VariableDeclaration> var = new VariableDeclaration;
- var->source = source_index;
- var->line = tokenizer.get_location().line;
+ RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
string token = tokenizer.peek_token();
while(is_qualifier(token))
RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
{
- RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
- func->source = source_index;
- func->line = tokenizer.get_location().line;
+ RefPtr<FunctionDeclaration> func = create_node<FunctionDeclaration>();
func->return_type = expect_type();
func->name = expect_identifier();
if(!func->parameters.empty())
tokenizer.expect(",");
- RefPtr<VariableDeclaration> var = new VariableDeclaration;
+ RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
string token = tokenizer.peek_token();
if(token=="in" || token=="out" || token=="inout")
var->interface = tokenizer.parse_token();
RefPtr<InterfaceBlock> Parser::parse_interface_block()
{
- RefPtr<InterfaceBlock> iface = new InterfaceBlock;
- iface->source = source_index;
- iface->line = tokenizer.get_location().line;
+ RefPtr<InterfaceBlock> iface = create_node<InterfaceBlock>();
iface->interface = tokenizer.parse_token();
if(!is_interface_qualifier(iface->interface))
RefPtr<Conditional> Parser::parse_conditional()
{
tokenizer.expect("if");
- RefPtr<Conditional> cond = new Conditional;
- cond->source = source_index;
- cond->line = tokenizer.get_location().line;
+ RefPtr<Conditional> cond = create_node<Conditional>();
tokenizer.expect("(");
cond->condition = parse_expression();
tokenizer.expect(")");
RefPtr<Iteration> Parser::parse_for()
{
tokenizer.expect("for");
- RefPtr<Iteration> loop = new Iteration;
- loop->source = source_index;
- loop->line = tokenizer.get_location().line;
+ RefPtr<Iteration> loop = create_node<Iteration>();
tokenizer.expect("(");
string token = tokenizer.peek_token();
if(is_type(token))
{
if(token!=";")
{
- RefPtr<ExpressionStatement> expr = new ExpressionStatement;
+ RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
expr->expression = parse_expression();
loop->init_statement = expr;
}
RefPtr<Iteration> Parser::parse_while()
{
tokenizer.expect("while");
- RefPtr<Iteration> loop = new Iteration;
- loop->source = source_index;
- loop->line = tokenizer.get_location().line;
+ RefPtr<Iteration> loop = create_node<Iteration>();
tokenizer.expect("(");
loop->condition = parse_expression();
tokenizer.expect(")");
RefPtr<Passthrough> Parser::parse_passthrough()
{
tokenizer.expect("passthrough");
- RefPtr<Passthrough> pass = new Passthrough;
- pass->source = source_index;
- pass->line = tokenizer.get_location().line;
+ RefPtr<Passthrough> pass = create_node<Passthrough>();
if(cur_stage->type==Stage::GEOMETRY)
{
tokenizer.expect("[");
RefPtr<Return> Parser::parse_return()
{
tokenizer.expect("return");
- RefPtr<Return> ret = new Return;
- ret->source = source_index;
- ret->line = tokenizer.get_location().line;
+ RefPtr<Return> ret = create_node<Return>();
if(tokenizer.peek_token()!=";")
ret->expression = parse_expression();
tokenizer.expect(";");