module = new Module();
ProgramParser parser;
imported_names.insert(src_name);
- append_module(parser.parse(source, src_name));
+ append_module(parser.parse(source, src_name, 1));
process();
}
module = new Module();
ProgramParser parser;
imported_names.insert(src_name);
- append_module(parser.parse(io, src_name));
+ append_module(parser.parse(io, src_name, 1));
process();
}
if(!io)
throw runtime_error(format("module %s not found", name));
ProgramParser import_parser;
- append_module(import_parser.parse(*io, fn));
+ append_module(import_parser.parse(*io, fn, imported_names.size()));
}
void ProgramCompiler::generate(Stage &stage)
ProgramCompiler::Formatter::Formatter():
+ source_line(1),
indent(0),
parameter_list(false)
{ }
void ProgramCompiler::Formatter::append(const string &text)
{
formatted += text;
+ for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
+ if(*i=='\n')
+ ++source_line;
}
void ProgramCompiler::Formatter::append(char c)
{
formatted += c;
+ if(c=='\n')
+ ++source_line;
+}
+
+void ProgramCompiler::Formatter::set_source(unsigned index, unsigned line)
+{
+ if(index!=source_index || (index && line!=source_line))
+ {
+ if(index==source_index && line==source_line+1)
+ formatted += '\n';
+ else
+ {
+ unsigned l = line;
+ if(stage->required_version<Version(3, 30))
+ --l;
+ formatted += format("#line %d %d\n", l, index);
+ }
+ }
+ source_index = index;
+ source_line = line;
}
void ProgramCompiler::Formatter::visit(Literal &literal)
{
if(i!=block.body.begin())
append('\n');
+ set_source((*i)->source, (*i)->line);
append(spaces);
(*i)->visit(*this);
}
Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
if(cond.else_body.body.size()==1 && else_cond)
{
- append(format("\n%selse ", string(indent*2, ' ')));
+ append('\n');
+ set_source(else_cond->source, else_cond->line);
+ append(format("%selse ", string(indent*2, ' ')));
else_cond->visit(*this);
}
else
delete module;
}
-Module &ProgramParser::parse(const string &s, const string &n)
+Module &ProgramParser::parse(const string &s, const string &n, unsigned i)
{
source = s;
source_name = n;
+ source_index = i;
parse_source();
return *module;
}
-Module &ProgramParser::parse(IO::Base &io, const string &n)
+Module &ProgramParser::parse(IO::Base &io, const string &n, unsigned i)
{
source = string();
source_name = n;
+ source_index = i;
while(!io.eof())
{
char buffer[4096];
if(is_interface_qualifier(token) && peek_token(1)==";")
{
RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
+ iface_lo->source = source_index;
+ iface_lo->line = current_line;
iface_lo->layout.qualifiers = layout->qualifiers;
iface_lo->interface = parse_token();
expect(";");
else if(token=="break" || token=="continue" || token=="discard")
{
RefPtr<Jump> jump = new Jump;
+ jump->source = source_index;
+ jump->line = current_line;
jump->keyword = parse_token();
expect(";");
else if(!token.empty())
{
RefPtr<ExpressionStatement> expr = new ExpressionStatement;
+ expr->source = source_index;
+ expr->line = current_line;
expr->expression = parse_expression();
expect(";");
expect("import");
RefPtr<Import> import = new Import;
+ import->source = source_index;
+ import->line = current_line;
import->module = expect_identifier();
expect(";");
return import;
{
expect("precision");
RefPtr<Precision> precision = new Precision;
+ precision->source = source_index;
+ precision->line = current_line;
precision->precision = parse_token();
if(!is_precision_qualifier(precision->precision))
{
expect("struct");
RefPtr<StructDeclaration> strct = new StructDeclaration;
+ strct->source = source_index;
+ strct->line = current_line;
strct->name = expect_identifier();
parse_block(strct->members, true);
RefPtr<VariableDeclaration> ProgramParser::parse_variable_declaration()
{
RefPtr<VariableDeclaration> var = new VariableDeclaration;
+ var->source = source_index;
+ var->line = current_line;
string token = peek_token();
while(is_qualifier(token))
RefPtr<FunctionDeclaration> ProgramParser::parse_function_declaration()
{
RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
+ func->source = source_index;
+ func->line = current_line;
func->return_type = expect_type();
func->name = expect_identifier();
RefPtr<InterfaceBlock> ProgramParser::parse_interface_block()
{
RefPtr<InterfaceBlock> iface = new InterfaceBlock;
+ iface->source = source_index;
+ iface->line = current_line;
iface->interface = parse_token();
if(!is_interface_qualifier(iface->interface))
RefPtr<Conditional> ProgramParser::parse_conditional()
{
expect("if");
- expect("(");
RefPtr<Conditional> cond = new Conditional;
+ cond->source = source_index;
+ cond->line = current_line;
+ expect("(");
cond->condition = parse_expression();
expect(")");
RefPtr<Iteration> ProgramParser::parse_for()
{
expect("for");
- expect("(");
RefPtr<Iteration> loop = new Iteration;
+ loop->source = source_index;
+ loop->line = current_line;
+ expect("(");
string token = peek_token();
if(is_type(token))
loop->init_statement = parse_statement();
RefPtr<Iteration> ProgramParser::parse_while()
{
expect("while");
- expect("(");
RefPtr<Iteration> loop = new Iteration;
+ loop->source = source_index;
+ loop->line = current_line;
+ expect("(");
loop->condition = parse_expression();
expect(")");
{
expect("passthrough");
RefPtr<Passthrough> pass = new Passthrough;
+ pass->source = source_index;
+ pass->line = current_line;
if(cur_stage->type==GEOMETRY)
{
expect("[");
{
expect("return");
RefPtr<Return> ret = new Return;
+ ret->source = source_index;
+ ret->line = current_line;
if(peek_token()!=";")
ret->expression = parse_expression();
expect(";");