#include <msp/strings/format.h>
#include <msp/strings/regex.h>
#include <msp/strings/utils.h>
+#include "builtin.h"
#include "glsl_error.h"
#include "parser.h"
{
delete module;
module = new Module;
+
cur_stage = &module->shared;
base_index = index;
source_index = index;
if(index>=0)
source_reference(1, name);
+
+ // TODO Need to somehow get type names from imports
+ if(const Stage *builtin = get_builtins(Stage::SHARED))
+ {
+ for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
+ declared_types.insert(i->first);
+ }
+ else
+ {
+ declared_types.insert("void");
+ declared_types.insert("bool");
+ declared_types.insert("int");
+ declared_types.insert("float");
+ }
+
tokenizer.begin(source, name);
allow_stage_change = true;
while(!tokenizer.peek_token().empty())
is_precision_qualifier(token));
}
-bool Parser::is_builtin_type(const string &token)
-{
- static Regex re("^(void|float|int|bool|[ib]?vec[234]|mat[234](x[234])?|sampler((1D|2D|Cube)(Array)?(Shadow)?|3D))$");
- return re.match(token);
-}
-
bool Parser::is_type(const string &token)
{
- return is_builtin_type(token) || declared_types.count(token);
+ return declared_types.count(token);
}
bool Parser::is_identifier(const string &token)
return var;
}
}
+ else if(token=="typedef")
+ return parse_type_declaration();
else if(token=="struct")
return parse_struct_declaration();
else if(is_interface_qualifier(token))
if(!is_precision_qualifier(precision->precision))
throw parse_error(tokenizer.get_location(), precision->precision, "a precision qualifier");
- precision->type = tokenizer.parse_token();
- // Not entirely accurate; only float, int and sampler types are allowed
- if(!is_builtin_type(precision->type))
- throw parse_error(tokenizer.get_location(), precision->type, "a builtin type");
+ // TODO Add validation for this
+ precision->type = expect_type();
tokenizer.expect(";");
return call;
}
+RefPtr<TypeDeclaration> Parser::parse_type_declaration()
+{
+ tokenizer.expect("typedef");
+
+ RefPtr<TypeDeclaration> type;
+ if(tokenizer.peek_token()=="image")
+ type = parse_image_type_declaration();
+ else
+ type = parse_basic_type_declaration();
+
+ tokenizer.expect(";");
+ declared_types.insert(type->name);
+ return type;
+}
+
+RefPtr<BasicTypeDeclaration> Parser::parse_basic_type_declaration()
+{
+ RefPtr<BasicTypeDeclaration> type = create_node<BasicTypeDeclaration>();
+
+ if(tokenizer.peek_token()=="vector")
+ {
+ type->kind = BasicTypeDeclaration::VECTOR;
+
+ tokenizer.parse_token();
+ tokenizer.expect("(");
+ type->size = expect_integer();
+ tokenizer.expect(")");
+ }
+
+ type->base = expect_type();
+ type->name = expect_identifier();
+
+ if(type->kind==BasicTypeDeclaration::ALIAS && check("["))
+ {
+ type->kind = BasicTypeDeclaration::ARRAY;
+ tokenizer.expect("]");
+ }
+
+ return type;
+}
+
+RefPtr<ImageTypeDeclaration> Parser::parse_image_type_declaration()
+{
+ tokenizer.expect("image");
+ tokenizer.expect("(");
+
+ RefPtr<ImageTypeDeclaration> type = create_node<ImageTypeDeclaration>();
+ while(1)
+ {
+ string token = tokenizer.parse_token();
+ if(token=="dimensions")
+ {
+ tokenizer.expect("=");
+ token = tokenizer.parse_token();
+ if(token=="1")
+ type->dimensions = ImageTypeDeclaration::ONE;
+ else if(token=="2")
+ type->dimensions = ImageTypeDeclaration::TWO;
+ else if(token=="3")
+ type->dimensions = ImageTypeDeclaration::THREE;
+ else if(token=="cube")
+ type->dimensions = ImageTypeDeclaration::CUBE;
+ else
+ throw parse_error(tokenizer.get_location(), token, "dimensions");
+
+ if(check("["))
+ {
+ type->array = true;
+ tokenizer.expect("]");
+ }
+ }
+ else if(token=="sampled")
+ type->sampled = true;
+ else if(token=="shadow")
+ type->shadow = true;
+ else
+ throw parse_error(tokenizer.get_location(), token, "image type attribute");
+
+ token = tokenizer.peek_token();
+ if(token==")")
+ break;
+
+ tokenizer.expect(",");
+ }
+ tokenizer.expect(")");
+
+ type->base = expect_type();
+ type->name = expect_identifier();
+
+ return type;
+}
+
RefPtr<StructDeclaration> Parser::parse_struct_declaration()
{
tokenizer.expect("struct");