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: