Sat, 14 Jul 2012 16:31:21 +0300
Moved command parser to a new function.
parser.cxx | file | annotate | diff | comparison | revisions | |
scriptreader.h | file | annotate | diff | comparison | revisions |
--- a/parser.cxx Sat Jul 14 16:28:42 2012 +0300 +++ b/parser.cxx Sat Jul 14 16:31:21 2012 +0300 @@ -121,59 +121,9 @@ } else { // Check if it's a command. CommandDef* comm = GetCommandByName (token); - if (comm) { - w->Write<long> (DH_COMMAND); - w->Write<long> (comm->number); - w->Write<long> (comm->maxargs); - MustNext ("("); - int curarg = 0; - while (1) { - if (curarg >= comm->maxargs) { - if (!PeekNext().compare (",")) - ParserError ("got `,` while expecting command-terminating `)`, are you passing too many parameters? (max %d)", - comm->maxargs); - MustNext (")"); - curarg++; - break; - } - - if (!Next ()) - ParserError ("unexpected end-of-file, unterminated command"); - - // If we get a ")" now, the user probably gave too few parameters - if (!token.compare (")")) - ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); - - // For now, it takes in just numbers. - // Needs to cater for string arguments too... - if (!token.isnumber()) - ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); - - int i = atoi (token.chars ()); - w->Write<long> (i); - - if (curarg < comm->numargs - 1) { - MustNext (","); - } else if (curarg < comm->maxargs - 1) { - // Can continue, but can terminate as well. - if (!PeekNext ().compare (")")) { - MustNext (")"); - curarg++; - break; - } else - MustNext (","); - } - - curarg++; - } - MustNext (";"); - - // If the script skipped a few arguments, fill in defaults. - while (curarg < comm->maxargs) { - w->Write<long> (comm->defvals[curarg]); - curarg++; - } - } else + if (comm) + ParseCommand (comm, w); + else ParserError ("unknown keyword `%s`!", token.chars()); } } @@ -199,3 +149,57 @@ w->Write (DH_ENDEVENT); */ } + +void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { + w->Write<long> (DH_COMMAND); + w->Write<long> (comm->number); + w->Write<long> (comm->maxargs); + MustNext ("("); + int curarg = 0; + while (1) { + if (curarg >= comm->maxargs) { + if (!PeekNext().compare (",")) + ParserError ("got `,` while expecting command-terminating `)`, are you passing too many parameters? (max %d)", + comm->maxargs); + MustNext (")"); + curarg++; + break; + } + + if (!Next ()) + ParserError ("unexpected end-of-file, unterminated command"); + + // If we get a ")" now, the user probably gave too few parameters + if (!token.compare (")")) + ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); + + // For now, it takes in just numbers. + // Needs to cater for string arguments too... + if (!token.isnumber()) + ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); + + int i = atoi (token.chars ()); + w->Write<long> (i); + + if (curarg < comm->numargs - 1) { + MustNext (","); + } else if (curarg < comm->maxargs - 1) { + // Can continue, but can terminate as well. + if (!PeekNext ().compare (")")) { + MustNext (")"); + curarg++; + break; + } else + MustNext (","); + } + + curarg++; + } + MustNext (";"); + + // If the script skipped any optional arguments, fill in defaults. + while (curarg < comm->maxargs) { + w->Write<long> (comm->defvals[curarg]); + curarg++; + } +} \ No newline at end of file
--- a/scriptreader.h Sat Jul 14 16:28:42 2012 +0300 +++ b/scriptreader.h Sat Jul 14 16:31:21 2012 +0300 @@ -44,6 +44,7 @@ #include <stdio.h> #include "str.h" #include "objwriter.h" +#include "commands.h" // Where is the parser at? enum parsermode { @@ -81,6 +82,7 @@ // parser.cxx: void BeginParse (ObjWriter* w); + void ParseCommand (CommandDef* comm, ObjWriter* w); private: bool atnewline;