ParserMode(i, s),
first(true)
{
- dict[1]=DictEntry("__kwd", "iss");
- dict[2]=DictEntry("__str", "is");
+ dict[1] = DictEntry("__kwd", "iss");
+ dict[2] = DictEntry("__str", "is");
}
Statement BinaryParser::parse()
{
while(1)
{
- Statement st=parse_statement();
+ Statement st = parse_statement();
if(st.keyword=="__kwd")
{
if(st.args.size()!=3)
throw_at(TypeError("Keyword definition must have three arguments"), src);
- const unsigned id=st.args[0].get<unsigned>();
- const string &kw=st.args[1].get<const string &>();
- const string &args=st.args[2].get<const string &>();
- dict[id]=DictEntry(kw, args);
+ const unsigned id = st.args[0].get<unsigned>();
+ const string &kw = st.args[1].get<const string &>();
+ const string &args = st.args[2].get<const string &>();
+ dict[id] = DictEntry(kw, args);
}
else if(st.keyword=="__str")
{
if(st.args.size()!=2)
throw_at(TypeError("String definition must have two arguments"), src);
- const unsigned id=st.args[0].get<unsigned>();
- strings[id]=st.args[1].get<const string &>();
+ const unsigned id = st.args[0].get<unsigned>();
+ strings[id] = st.args[1].get<const string &>();
}
else
return st;
{
while(first && in.peek()=='\n')
in.get();
- first=false;
+ first = false;
- unsigned id=parse_int();
+ unsigned id = parse_int();
if(!in)
return Statement();
- Dictionary::const_iterator i=dict.find(id);
+ Dictionary::const_iterator i = dict.find(id);
if(i==dict.end())
throw_at(KeyError("Unknown statement ID", lexical_cast(id)), src);
- const DictEntry &de=i->second;
+ const DictEntry &de = i->second;
Statement result;
- result.keyword=de.keyword;
- result.source=src;
+ result.keyword = de.keyword;
+ result.source = src;
- for(unsigned j=0; j<de.args.size(); ++j)
+ for(unsigned j = 0; j<de.args.size(); ++j)
{
switch(de.args[j])
{
}
}
- unsigned nsub=parse_int();
- for(unsigned j=0; j<nsub; ++j)
+ unsigned nsub = parse_int();
+ for(unsigned j = 0; j<nsub; ++j)
result.sub.push_back(parse());
- result.valid=true;
+ result.valid = true;
return result;
}
long long BinaryParser::parse_int()
{
- long long result=0;
- unsigned bits=0;
+ long long result = 0;
+ unsigned bits = 0;
while(in)
{
- int c=in.get();
+ int c = in.get();
- result=(result<<7) | (c&0x7F);
- bits+=7;
+ result = (result<<7) | (c&0x7F);
+ bits += 7;
if(!(c&0x80))
break;
}
- const long long mask=1LL<<(bits-1);
- result=(result^mask)-mask;
+ const long long mask = 1LL<<(bits-1);
+ result = (result^mask)-mask;
return result;
}
};
#if BYTE_ORDER == LITTLE_ENDIAN
- for(unsigned i=sizeof(float); i--;)
- d[i]=in.get();
+ for(unsigned i = sizeof(float); i--;)
+ d[i] = in.get();
#else
- for(unsigned i=0; i<sizeof(float); ++i)
- d[i]=in.get();
+ for(unsigned i = 0; i<sizeof(float); ++i)
+ d[i] = in.get();
#endif
return f;
string BinaryParser::parse_string()
{
- int len=parse_int();
+ int len = parse_int();
if(len>=0)
{
string result;
result.reserve(len);
- for(int i=0; i<len; ++i)
- result+=in.get();
+ for(int i = 0; i<len; ++i)
+ result += in.get();
return result;
}
else
const string &BinaryParser::lookup_string(unsigned id) const
{
- StringMap::const_iterator i=strings.find(id);
+ StringMap::const_iterator i = strings.find(id);
if(i==strings.end())
throw_at(KeyError("Unknown string", lexical_cast(id)), src);
return i->second;
next_kwd_id(3),
next_str_id(1)
{
- dict[DictEntry("__kwd", "iss")]=1;
- dict[DictEntry("__str", "is")]=2;
+ dict[DictEntry("__kwd", "iss")] = 1;
+ dict[DictEntry("__str", "is")] = 2;
}
void BinaryWriter::write(const Statement &st)
void BinaryWriter::write_(const Statement &st)
{
- Dictionary::iterator i=dict.find(create_entry(st));
+ Dictionary::iterator i = dict.find(create_entry(st));
if(i==dict.end())
throw InvalidParameterValue("Unknown statement");
write_int(i->second);
- for(ValueArray::const_iterator j=st.args.begin(); j!=st.args.end(); ++j)
+ for(ValueArray::const_iterator j = st.args.begin(); j!=st.args.end(); ++j)
switch(j->get_type())
{
case INTEGER: write_int (j->get<long long>()); break;
}
write_int(st.sub.size());
- for(list<Statement>::const_iterator j=st.sub.begin(); j!=st.sub.end(); ++j)
+ for(list<Statement>::const_iterator j = st.sub.begin(); j!=st.sub.end(); ++j)
write(*j);
}
DictEntry BinaryWriter::create_entry(const Statement &st)
{
- static const char types[]="ifsbe";
+ static const char types[] = "ifsbe";
string args;
- for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
+ for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
{
if(i->get_type()>=5)
throw InvalidParameterValue("Invalid argument type");
- args+=types[i->get_type()];
+ args += types[i->get_type()];
}
return DictEntry(st.keyword, args);
void BinaryWriter::collect_keywords(const Statement &st)
{
- DictEntry de=create_entry(st);
+ DictEntry de = create_entry(st);
if(!dict.count(de))
{
Statement kst;
- kst.keyword="__kwd";
+ kst.keyword = "__kwd";
kst.args.push_back(next_kwd_id);
kst.args.push_back(de.keyword);
kst.args.push_back(de.args);
write_(kst);
- dict[de]=next_kwd_id++;
+ dict[de] = next_kwd_id++;
}
- for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
+ for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
{
- const Type t=i->get_type();
- const string &r=i->get_raw();
+ const Type t = i->get_type();
+ const string &r = i->get_raw();
if((t==ENUM || (t==STRING && r.size()<32)) && !strings.count(r))
{
Statement sst;
- sst.keyword="__str";
+ sst.keyword = "__str";
sst.args.push_back(next_str_id);
sst.args.push_back(r);
write_(sst);
- strings[r]=next_str_id++;
+ strings[r] = next_str_id++;
}
}
- for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+ for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
collect_keywords(*i);
}
void BinaryWriter::write_int(long long n)
{
- unsigned i=sizeof(long long)-1;
+ unsigned i = sizeof(long long)-1;
if(n>=0)
for(; (i>0 && (n>>(i*7-1))==0); --i) ;
void BinaryWriter::write_string(const string &s)
{
- StringMap::const_iterator i=strings.find(s);
+ StringMap::const_iterator i = strings.find(s);
if(i!=strings.end())
write_int(-static_cast<int>(i->second));
else
char d[sizeof(float)];
};
- v=f;
+ v = f;
#if BYTE_ORDER == LITTLE_ENDIAN
- for(unsigned i=sizeof(float); i--;)
+ for(unsigned i = sizeof(float); i--;)
out.put(d[i]);
#else
- for(unsigned i=0; i<sizeof(float); ++i)
+ for(unsigned i = 0; i<sizeof(float); ++i)
out.put(d[i]);
#endif
}
void BinaryWriter::write_enum(const string &e)
{
- StringMap::const_iterator i=strings.find(e);
+ StringMap::const_iterator i = strings.find(e);
if(i==strings.end())
throw InvalidParameterValue("Unknown enum");
write_int(i->second);
Collection::~Collection()
{
- for(ItemMap::iterator i=items.begin(); i!=items.end(); ++i)
+ for(ItemMap::iterator i = items.begin(); i!=items.end(); ++i)
delete i->second;
- for(ItemKeywordSeq::iterator i=keywords.begin(); i!=keywords.end(); ++i)
+ for(ItemKeywordSeq::iterator i = keywords.begin(); i!=keywords.end(); ++i)
delete *i;
- for(ItemCreatorSeq::iterator i=creators.begin(); i!=creators.end(); ++i)
+ for(ItemCreatorSeq::iterator i = creators.begin(); i!=creators.end(); ++i)
delete *i;
}
Collection::Loader::Loader(Collection &c):
coll(c)
{
- for(ItemKeywordSeq::const_iterator i=coll.keywords.begin(); i!=coll.keywords.end(); ++i)
+ for(ItemKeywordSeq::const_iterator i = coll.keywords.begin(); i!=coll.keywords.end(); ++i)
(*i)->add_to_loader(*this);
}
int Input::get()
{
- int c=next;
- next=-1;
- if(c<0) c=in.get();
+ int c = next;
+ next = -1;
+ if(c<0) c = in.get();
if(c=='\n')
++line;
int Input::peek()
{
if(next<0)
- next=in.get();
+ next = in.get();
return next;
}
void Loader::load(const Statement &st)
{
- for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+ for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
load_statement(*i);
finish();
}
{
while(p)
{
- Statement st=p.parse();
+ Statement st = p.parse();
if(st.valid)
load_statement(st);
}
Loader::~Loader()
{
- for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
+ for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i)
delete i->second;
}
void Loader::add(const string &k, LoaderAction *a)
{
- ActionMap::iterator i=actions.find(k);
+ ActionMap::iterator i = actions.find(k);
if(i!=actions.end())
{
delete i->second;
- i->second=a;
+ i->second = a;
}
else
- actions[k]=a;
+ actions[k] = a;
}
void Loader::load_statement(const Statement &st)
{
- cur_st=&st;
- ActionMap::iterator j=actions.find(st.keyword);
+ cur_st = &st;
+ ActionMap::iterator j = actions.find(st.keyword);
if(j==actions.end())
throw_at(KeyError("Unknown keyword", st.keyword), st.get_location());
if(j->second)
throw;
}
}
- cur_st=0;
+ cur_st = 0;
}
} // namespace DataFile
{
while(1)
{
- Statement st=mode->parse();
+ Statement st = mode->parse();
if(st.keyword=="__bin")
{
delete mode;
- mode=new BinaryParser(in, src);
+ mode = new BinaryParser(in, src);
}
else if(st.keyword=="__text")
{
delete mode;
- mode=new TextParser(in, src);
+ mode = new TextParser(in, src);
}
else if(st.keyword=="__src")
{
- string s=st.args[0].get<string>();
+ string s = st.args[0].get<string>();
if(s.empty())
- src=main_src;
+ src = main_src;
else
- src=format("%s[%s]", main_src, s);
+ src = format("%s[%s]", main_src, s);
}
else
return st;
}
catch(const Exception &e)
{
- good=false;
+ good = false;
throw;
}
}
std::string Statement::get_location() const
{
- std::string result=source;
+ std::string result = source;
if(line)
- result+=format(":%d", line);
+ result += format(":%d", line);
return result;
}
Statement TextParser::parse_statement(const Token *t)
{
Statement result;
- bool sub=false;
- bool finish=false;
+ bool sub = false;
+ bool finish = false;
while(in)
{
Token token;
if(t)
{
- token=*t;
- t=0;
+ token = *t;
+ t = 0;
}
else
- token=parse_token();
+ token = parse_token();
if(result.keyword.empty())
{
break;
else if(token.type!=Token::IDENTIFIER)
throw_at(ParseError(format("Syntax error at token '%s' (expected an identifier)", token.str)), get_location());
- result.keyword=token.str;
- result.valid=true;
- result.source=src;
- result.line=in.get_line_number();
+ result.keyword = token.str;
+ result.valid = true;
+ result.source = src;
+ result.line = in.get_line_number();
}
else if(sub)
{
if(token.str=="}")
{
- sub=false;
- finish=true;
+ sub = false;
+ finish = true;
}
else
{
- Statement ss=parse_statement(&token);
+ Statement ss = parse_statement(&token);
result.sub.push_back(ss);
}
}
break;
}
else if(token.str=="{")
- sub=true;
+ sub = true;
else if(token.str==";")
break;
else if(token.type==Token::INTEGER)
Token TextParser::parse_token()
{
- int c=0;
- int comment=0;
+ int c = 0;
+ int comment = 0;
// Skip over comments and whitespace
while(in && comment>=0)
{
- c=in.get();
- int next=in.peek();
+ c = in.get();
+ int next = in.peek();
if(c=='/' && next=='/')
- comment=1;
+ comment = 1;
else if(c=='/' && next=='*')
- comment=2;
+ comment = 2;
else if(c=='\n' && comment==1)
- comment=0;
+ comment = 0;
else if(c=='*' && next=='/' && comment==2)
- comment=3;
+ comment = 3;
else if(comment==3) // Skip the second character of block comment end
- comment=0;
+ comment = 0;
else if(!isspace(c) && !comment)
- comment=-1;
+ comment = -1;
}
if(comment>0) // EOF while in comment
Token::IDENTIFIER
};
- ParseState state=INIT;
+ ParseState state = INIT;
string buf;
- bool escape=false;
+ bool escape = false;
while(in || state==INIT)
{
if(state!=INIT)
- c=in.get();
- int next=in.peek();
+ c = in.get();
+ int next = in.peek();
- buf+=c;
+ buf += c;
switch(state)
{
case INIT:
if(c=='0')
- state=ZERO;
+ state = ZERO;
else if(c=='-' || c=='+')
- state=SIGN;
+ state = SIGN;
else if(c=='.')
- state=FLOAT;
+ state = FLOAT;
else if(c=='"')
- state=STRING;
+ state = STRING;
else if(c=='{' || c=='}' || c==';')
return Token(Token::SPECIAL, string(1, c));
else if(isdigit(c))
- state=DECIMAL;
+ state = DECIMAL;
else if(isalpha(c) || c=='_')
- state=IDENTIFIER;
+ state = IDENTIFIER;
else
parse_error(c, "0-9A-Za-z_.\"{};+-");
break;
case SIGN:
if(c=='0')
- state=ZERO;
+ state = ZERO;
else if(isdigit(c))
- state=DECIMAL;
+ state = DECIMAL;
else if(c=='.')
- state=FLOAT;
+ state = FLOAT;
else
parse_error(c, "0-9.");
break;
case ZERO:
if(c=='x')
- state=HEXADECIMAL;
+ state = HEXADECIMAL;
else if(isdigit(c))
- state=OCTAL;
+ state = OCTAL;
else if(c=='.')
- state=FLOAT;
+ state = FLOAT;
else
parse_error(c, "0-9A-Fa-f.");
break;
case DECIMAL:
if(c=='.')
- state=FLOAT;
+ state = FLOAT;
else if(!isdigit(c))
parse_error(c, "0-9.");
break;
case FLOAT:
if(c=='e' || c=='E')
- state=FLOATEXPINIT;
+ state = FLOATEXPINIT;
else if(!isdigit(c))
parse_error(c, "0-9Ee");
break;
case FLOATEXPINIT:
if(c=='+' || c=='-')
- state=FLOATEXPSIGN;
+ state = FLOATEXPSIGN;
else if(isdigit(c))
- state=FLOATEXP;
+ state = FLOATEXP;
else
parse_error(c, "0-9+-");
break;
case FLOATEXPSIGN:
if(isdigit(c))
- state=FLOATEXP;
+ state = FLOATEXP;
else
parse_error(c, "0-9");
break;
case STRING:
if(c=='\\')
- escape=!escape;
+ escape = !escape;
else if(c=='"' && !escape)
{
try
}
}
else
- escape=false;
+ escape = false;
break;
case IDENTIFIER:
string indent(level, '\t');
IO::print(out, "%s%s", indent, st.keyword);
- for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
+ for(ValueArray::const_iterator i = st.args.begin(); i!=st.args.end(); ++i)
{
out.put(' ');
if(i->get_type()==STRING)
if(!st.sub.empty())
{
IO::print(out, "\n%s{\n", indent);
- for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+ for(list<Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
write_(*i, level+1);
IO::print(out, "%s}", indent);
}
if(b==binary)
return;
- binary=b;
+ binary = b;
Statement st;
if(binary)
- st.keyword="__bin";
+ st.keyword = "__bin";
else
- st.keyword="__text";
+ st.keyword = "__text";
mode->write(st);
delete mode;
if(binary)
- mode=new BinaryWriter(out);
+ mode = new BinaryWriter(out);
else
- mode=new TextWriter(out);
+ mode = new TextWriter(out);
}
} // namespace DataFile
if(reset_src)
{
writer.write((DataFile::Statement("__src"), string()));
- reset_src=false;
+ reset_src = false;
}
- for(list<DataFile::Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+ for(list<DataFile::Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
writer.write(*i);
}
if(st.keyword=="_content")
return true;
- for(vector<DataFile::Value>::iterator i=st.args.begin(); i!=st.args.end(); ++i)
+ for(vector<DataFile::Value>::iterator i = st.args.begin(); i!=st.args.end(); ++i)
if(i->get_type()==DataFile::STRING)
{
if(i->get_raw()=="$filename")
- *i=DataFile::Value(FS::basename(fn.str()));
+ *i = DataFile::Value(FS::basename(fn.str()));
else if(i->get_raw()=="$content")
{
IO::File in(fn.str());
while(!in.eof())
{
char buf[4096];
- unsigned len=in.read(buf, sizeof(buf));
+ unsigned len = in.read(buf, sizeof(buf));
data.append(buf, len);
}
- *i=DataFile::Value(data);
+ *i = DataFile::Value(data);
}
}
- for(list<DataFile::Statement>::iterator i=st.sub.begin(); i!=st.sub.end();)
+ for(list<DataFile::Statement>::iterator i = st.sub.begin(); i!=st.sub.end();)
{
if(process_statement(fn, *i))
{
DataFile::Parser parser(in, fn.str());
while(parser)
{
- DataFile::Statement ss=parser.parse();
+ DataFile::Statement ss = parser.parse();
if(ss.valid)
st.sub.insert(i, ss);
}
- i=st.sub.erase(i);
+ i = st.sub.erase(i);
}
else
++i;
void Compiler::process_file(const FS::Path &fn, const list<DataFile::Statement> &st)
{
writer.write((DataFile::Statement("__src"), FS::basename(fn.str())));
- reset_src=true;
+ reset_src = true;
if(st.empty())
process_file(fn);
else
{
- for(list<DataFile::Statement>::const_iterator i=st.begin(); i!=st.end(); ++i)
+ for(list<DataFile::Statement>::const_iterator i = st.begin(); i!=st.end(); ++i)
{
if(i->keyword=="_content")
process_file(fn);
else
{
- DataFile::Statement s=*i;
+ DataFile::Statement s = *i;
process_statement(fn, s);
writer.write(s);
}
DataFile::Parser parser(in, fn.str());
while(parser)
{
- DataFile::Statement st=parser.parse();
+ DataFile::Statement st = parser.parse();
if(st.valid)
writer.write(st);
}
void ForEach::finish()
{
- list<string> files=FS::list_files(base);
- for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
+ list<string> files = FS::list_files(base);
+ for(list<string>::iterator i = files.begin(); i!=files.end(); ++i)
{
- bool match=false;
- for(list<string>::const_iterator j=patterns.begin(); (j!=patterns.end() && !match); ++j)
- match=Regex(*j).match(*i);
- for(list<string>::const_iterator j=excludes.begin(); (j!=excludes.end() && match); ++j)
- match=!Regex(*j).match(*i);
+ bool match = false;
+ for(list<string>::const_iterator j = patterns.begin(); (j!=patterns.end() && !match); ++j)
+ match = Regex(*j).match(*i);
+ for(list<string>::const_iterator j = excludes.begin(); (j!=excludes.end() && match); ++j)
+ match = !Regex(*j).match(*i);
if(match)
compiler.process_file(base / *i, write_st);
}
getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
getopt(argc, argv);
- const vector<string> &args=getopt.get_args();
+ const vector<string> &args = getopt.get_args();
if(!args.empty())
- in_fn=args[0];
+ in_fn = args[0];
}
int DataTool::main()
{
IO::Base *in;
if(in_fn=="-")
- in=&IO::cin;
+ in = &IO::cin;
else
- in=new IO::File(in_fn);
+ in = new IO::File(in_fn);
IO::Base *out;
if(out_fn=="-")
- out=&IO::cout;
+ out = &IO::cout;
else
- out=new IO::File(out_fn, IO::M_WRITE);
+ out = new IO::File(out_fn, IO::M_WRITE);
{
IO::Buffered in_buf(*in);
{
while(parser)
{
- DataFile::Statement st=parser.parse();
+ DataFile::Statement st = parser.parse();
if(st.valid)
{
writer.write(st);