# HG changeset patch # User Teemu Piippo # Date 1342228626 -10800 # Node ID d279af9afd6dbee46ad287141a7313f4c35d575f # Parent c8bfa7e6ae1bace5d0975734a75c123c62f89f33 Added proper string checking diff -r c8bfa7e6ae1b -r d279af9afd6d parser.cxx --- a/parser.cxx Sat Jul 14 04:03:25 2012 +0300 +++ b/parser.cxx Sat Jul 14 04:17:06 2012 +0300 @@ -61,20 +61,19 @@ while (Next()) { // printf ("got token %s\n", token.chars()); if (!token.compare ("#include")) { - MustNext (); + str filepath = MustGetString (); + // First ensure that the file can be opened - FILE* newfile = fopen (token.chars(), "r"); + FILE* newfile = fopen (filepath.chars(), "r"); if (!newfile) - ParserError ("couldn't open included file `%s`!", token.chars()); + ParserError ("couldn't open included file `%s`!", filepath.chars()); fclose (newfile); - ScriptReader* newreader = new ScriptReader (token.chars()); + ScriptReader* newreader = new ScriptReader (filepath.chars()); newreader->BeginParse (w); } else if (!token.compare ("state")) { MUST_TOPLEVEL - MustNext (); - - str statename = token; + str statename = MustGetString (); // State name must be a word. if (statename.first (" ") != statename.len()) @@ -95,11 +94,11 @@ MUST_TOPLEVEL // Event definition - MustNext (); + str evname = MustGetString (); - EventDef* e = FindEventByName (token); + EventDef* e = FindEventByName (evname); if (!e) - ParserError ("bad event! got `%s`\n", token.chars()); + ParserError ("bad event! got `%s`\n", evname.chars()); MustNext ("{"); diff -r c8bfa7e6ae1b -r d279af9afd6d scriptreader.cxx --- a/scriptreader.cxx Sat Jul 14 04:03:25 2012 +0300 +++ b/scriptreader.cxx Sat Jul 14 04:17:06 2012 +0300 @@ -96,29 +96,18 @@ while (!feof (fp)) { char c = ReadChar (); - // Quotation mark. - if (c == '"') { - // End-quote ends the token. - if (quote) - break; - - tokenquoted = true; - quote = !quote; - continue; - } - // Extended delimeters: parenthesis, quote marks, braces and brackets. // These delimeters break the word too. If there was prior data, // the delimeter pushes the cursor back so that the next character // will be the same delimeter. If there isn't, the delimeter itself // is included (and thus becomes a token itself.) - // TODO: quotation marks should be here too.. bool shouldBreak = false; if (extdelimeters) { switch (c) { case '(': case ')': case '{': case '}': case '[': case ']': + case '"': // Push the cursor back if (tmp.len()) fseek (fp, ftell (fp)-1, SEEK_SET); @@ -213,4 +202,27 @@ void ScriptReader::ParserMessage (const char* header, char* message) { fprintf (stderr, "%sIn file %s, on line %d: %s\n", header, filepath.chars(), curline, message); +} + +str ScriptReader::MustGetString () { + if (!extdelimeters) + ParserError ("MustGetString doesn't work with parsers not using extended delimeters!"); + + MustNext ("\""); + + str string; + // Keep reading characters until we find a terminating quote. + while (1) { + // can't end here! + if (feof (fp)) + ParserError ("unterminated string"); + + char c = ReadChar (); + if (c == '"') + break; + + string += c; + } + + return string; } \ No newline at end of file diff -r c8bfa7e6ae1b -r d279af9afd6d scriptreader.h --- a/scriptreader.h Sat Jul 14 04:03:25 2012 +0300 +++ b/scriptreader.h Sat Jul 14 04:17:06 2012 +0300 @@ -66,6 +66,7 @@ // ==================================================================== // METHODS + // scriptreader.cxx: ScriptReader (str path); ~ScriptReader (); char ReadChar (); @@ -76,7 +77,9 @@ void ParserError (const char* message, ...); void ParserWarning (const char* message, ...); void ParserMessage (const char* header, char* message); + str MustGetString (); + // parser.cxx: void BeginParse (ObjWriter* w); };