-
- return v;
- }
-
- void Error_InvalidDirective ()
- {
- Report.Error (1517, Location, "Invalid pre-processor directive");
- }
-
- void Error_UnexpectedDirective (string extra)
- {
- Report.Error (
- 1028, Location,
- "Unexpected processor directive (" + extra + ")");
- }
-
- void Error_TokensSeen ()
- {
- Report.Error (
- 1032, Location,
- "Cannot define or undefine pre-processor symbols after a token in the file");
- }
-
- //
- // if true, then the code continues processing the code
- // if false, the code stays in a loop until another directive is
- // reached.
- //
- bool handle_preprocessing_directive (bool caller_is_taking)
- {
- char [] blank = { ' ', '\t' };
- string cmd, arg;
- bool region_directive = false;
-
- get_cmd_arg (out cmd, out arg);
- // Eat any trailing whitespaces and single-line comments
- if (arg.IndexOf ("//") != -1)
- arg = arg.Substring (0, arg.IndexOf ("//"));
- arg = arg.TrimEnd (' ', '\t');
-
- //
- // The first group of pre-processing instructions is always processed
- //
- switch (cmd.ToLower()){
- case "line":
- if (!PreProcessLine (arg))
- Report.Error (
- 1576, Location,
- "Argument to #line directive is missing or invalid");
- return true;
-
- case "region":
- region_directive = true;
- arg = "true";
- goto case "if";
-
- case "end region":
- region_directive = true;
- goto case "end if";
-
- case "if":
- if (arg == ""){
- Error_InvalidDirective ();
- return true;
- }
- bool taking = false;
- if (ifstack == null)
- ifstack = new Stack ();
-
- if (ifstack.Count == 0){
- taking = true;
- } else {
- int state = (int) ifstack.Peek ();
- if ((state & TAKING) != 0)
- taking = true;
- }
-
- if (eval (arg) && taking){
- int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
- if (region_directive)
- push |= REGION;
- ifstack.Push (push);
- return true;
- } else {
- int push = (taking ? PARENT_TAKING : 0);
- if (region_directive)
- push |= REGION;
- ifstack.Push (push);
- return false;
- }
-
- case "end if":
- if (ifstack == null || ifstack.Count == 0){
- Error_UnexpectedDirective ("no #if for this #end if");
- return true;
- } else {
- int pop = (int) ifstack.Pop ();
-
- if (region_directive && ((pop & REGION) == 0))
- Report.Error (1027, Location, "#end if directive expected");
- else if (!region_directive && ((pop & REGION) != 0))
- Report.Error (1038, Location, "#end region directive expected");
-
- if (ifstack.Count == 0)
- return true;
- else {
- int state = (int) ifstack.Peek ();
-
- if ((state & TAKING) != 0)
- return true;
- else
- return false;
- }
- }
-
- case "elseif":
- if (ifstack == null || ifstack.Count == 0){
- Error_UnexpectedDirective ("no #if for this #elif");
- return true;
- } else {
- int state = (int) ifstack.Peek ();
-
- if ((state & REGION) != 0) {
- Report.Error (1038, Location, "#end region directive expected");
- return true;
- }
-
- if ((state & ELSE_SEEN) != 0){
- Error_UnexpectedDirective ("#elif not valid after #else");
- return true;
- }
-
- if ((state & (TAKEN_BEFORE | TAKING)) != 0)
- return false;
-
- if (eval (arg) && ((state & PARENT_TAKING) != 0)){
- state = (int) ifstack.Pop ();
- ifstack.Push (state | TAKING | TAKEN_BEFORE);
- return true;
- } else
- return false;
- }
-
- case "else":
- if (ifstack == null || ifstack.Count == 0){
- Report.Error (
- 1028, Location,
- "Unexpected processor directive (no #if for this #else)");
- return true;
- } else {
- int state = (int) ifstack.Peek ();
-
- if ((state & REGION) != 0) {
- Report.Error (1038, Location, "#end region directive expected");
- return true;
- }
-
- if ((state & ELSE_SEEN) != 0){
- Error_UnexpectedDirective ("#else within #else");
- return true;
- }
-
- ifstack.Pop ();
-
- bool ret;
- if ((state & TAKEN_BEFORE) == 0){
- ret = ((state & PARENT_TAKING) != 0);
- } else
- ret = false;
-
- if (ret)
- state |= TAKING;
- else
- state &= ~TAKING;
-
- ifstack.Push (state | ELSE_SEEN);
-
- return ret;
- }
- }
-
- //
- // These are only processed if we are in a `taking' block
- //
- if (!caller_is_taking)
- return false;
-
- switch (cmd.ToLower()){
- case "define":
- /* if (any_token_seen){
- Error_TokensSeen ();
- return true;
- } */
- PreProcessDefinition (true, arg);
- return true;
-
- case "undef":
- /* if (any_token_seen){
- Error_TokensSeen ();
- return true;
- } */
- PreProcessDefinition (false, arg);
- return true;
-
- case "error":
- Report.Error (1029, Location, "#error: '" + arg + "'");
- return true;
-
- case "warning":
- Report.Warning (1030, Location, "#warning: '" + arg + "'");
- return true;
- }
-
- Report.Error (1024, Location, "Preprocessor directive expected (got: " + cmd + ")");
- return true;