X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftextparser.cpp;h=930ce875d782a58a43f6063ce30a4c3aab0b66ef;hb=7df5e45c7f414f6a07681dc4ec2abb63b091a309;hp=0f17d3f3cc79808a7659e291e406f6b7bb814137;hpb=256f7238bc60d6dcc31a564988f5cc02a60c4537;p=libs%2Fdatafile.git diff --git a/source/textparser.cpp b/source/textparser.cpp index 0f17d3f..930ce87 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include #include "input.h" @@ -28,19 +21,19 @@ Statement TextParser::parse() 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()) { @@ -48,21 +41,21 @@ Statement TextParser::parse_statement(const Token *t) 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); } } @@ -73,24 +66,23 @@ Statement TextParser::parse_statement(const Token *t) break; } else if(token.str=="{") - sub=true; + sub = true; else if(token.str==";") break; else if(token.type==Token::INTEGER) - result.args.push_back(Value(INTEGER, token.str)); + result.append(lexical_cast(token.str)); else if(token.type==Token::FLOAT) - result.args.push_back(Value(FLOAT, token.str)); + result.append(lexical_cast(token.str)); else if(token.type==Token::STRING) - result.args.push_back(Value(STRING, token.str)); + result.append(token.str); else if(token.type==Token::IDENTIFIER) { if(token.str=="true") - result.args.push_back(Value(BOOLEAN, "1")); + result.append(true); else if(token.str=="false") - result.args.push_back(Value(BOOLEAN, "0")); + result.append(false); else - result.args.push_back(Value(ENUM, token.str)); - //result.args.push_back(resolve_identifiertoken.str); + result.append(Symbol(token.str)); } else if(token.str=="") throw_at(ParseError("Unexcepted end of input"), get_location()); @@ -103,27 +95,27 @@ Statement TextParser::parse_statement(const Token *t) 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; - else if(!isspace(c) && !comment) - comment=-1; + comment = 0; + else if(c!=-1 && !isspace(c) && !comment) + comment = -1; } if(comment>0) // EOF while in comment @@ -145,6 +137,7 @@ Token TextParser::parse_token() OCTAL, FLOAT, FLOATEXP, + STRING_END, IDENTIFIER }; @@ -154,7 +147,7 @@ Token TextParser::parse_token() Token::SPECIAL, Token::SPECIAL, Token::SPECIAL, - Token::STRING, + Token::SPECIAL, Token::SPECIAL, Token::INTEGER, Token::INTEGER, @@ -162,67 +155,68 @@ Token TextParser::parse_token() Token::INTEGER, Token::FLOAT, Token::FLOAT, + Token::STRING, 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; - else if(isalpha(c) || c=='_') - state=IDENTIFIER; + state = DECIMAL; + else if(isalpha(c) || c=='_' || c=='\\') + state = IDENTIFIER; else - parse_error(c, "0-9A-Za-z_.\"{};+-"); + 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; @@ -239,23 +233,23 @@ Token TextParser::parse_token() 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; @@ -267,8 +261,30 @@ Token TextParser::parse_token() case STRING: if(c=='\\') - escape=!escape; + escape = !escape; else if(c=='"' && !escape) + state = STRING_END; + else + escape = false; + break; + + case IDENTIFIER: + if(!isalpha(c) && !isdigit(c) && c!='_' && c!='-' && c!='/') + parse_error(c, "0-9A-Za-z_/-"); + break; + + case STRING_END: + throw_at(ParseError("Garbage after string"), get_location()); + + default: + throw_at(InvalidState("Internal error (bad state)"), get_location()); + } + + if(is_delimiter(next) && state>=ACCEPT) + { + if(state==IDENTIFIER && buf[0]=='\\') + return Token(Token::IDENTIFIER, buf.substr(1)); + else if(state==STRING_END) { try { @@ -281,20 +297,8 @@ Token TextParser::parse_token() } } else - escape=false; - break; - - case IDENTIFIER: - if(!isalpha(c) && !isdigit(c) && c!='_') - parse_error(c, "0-9A-Za-z_"); - break; - - default: - throw_at(InvalidState("Internal error (bad state)"), get_location()); + return Token(token_type[state], buf); } - - if(is_delimiter(next) && state>=ACCEPT) - return Token(token_type[state], buf); } return Token(Token::SPECIAL, "");