================================================================================
If, elif and else directives
================================================================================

#if WIN32
  string os = "Win32";
#elif MACOS
  string os = "MacOS";
#else
  string os = "Unknown";
#endif

--------------------------------------------------------------------------------

(compilation_unit
  (preproc_if
    condition: (identifier)
    (local_declaration_statement
      (variable_declaration
        type: (predefined_type)
        (variable_declarator
          name: (identifier)
          (string_literal
            (string_literal_content)))))
    alternative: (preproc_elif
      condition: (identifier)
      (local_declaration_statement
        (variable_declaration
          type: (predefined_type)
          (variable_declarator
            name: (identifier)
            (string_literal
              (string_literal_content)))))
      alternative: (preproc_else
        (local_declaration_statement
          (variable_declaration
            type: (predefined_type)
            (variable_declarator
              name: (identifier)
              (string_literal
                (string_literal_content)))))))))

================================================================================
If conditions
================================================================================

#if !MACOS
#endif

#if WIN32==true
#endif

#if !MACOS!=false
#endif

#if A && B || C
#endif

#if (A)
#endif

#if (A || B)
#endif

#if (A && B) || C
#endif

--------------------------------------------------------------------------------

(compilation_unit
  (preproc_if
    condition: (unary_expression
      argument: (identifier)))
  (preproc_if
    condition: (binary_expression
      left: (identifier)
      right: (boolean_literal)))
  (preproc_if
    condition: (binary_expression
      left: (unary_expression
        argument: (identifier))
      right: (boolean_literal)))
  (preproc_if
    condition: (binary_expression
      left: (binary_expression
        left: (identifier)
        right: (identifier))
      right: (identifier)))
  (preproc_if
    condition: (parenthesized_expression
      (identifier)))
  (preproc_if
    condition: (parenthesized_expression
      (binary_expression
        left: (identifier)
        right: (identifier))))
  (preproc_if
    condition: (binary_expression
      left: (parenthesized_expression
        (binary_expression
          left: (identifier)
          right: (identifier)))
      right: (identifier))))

================================================================================
Define and undefine directives
================================================================================

#define SOMETHING
#undef BAD

--------------------------------------------------------------------------------

(compilation_unit
  (preproc_define
    (preproc_arg))
  (preproc_undef
    (preproc_arg)))

================================================================================
Line directives
================================================================================

class Of1879 {
  void AMethod() {
#line 2001 "A Space" // Comment
#line hidden
#line default
#line (1, 1) - (1, 3) 1 "a.cs"
#line (2, 1) - (2, 3) "a.cs"
# line 2001 "A Space"
#  line hidden
#    line default
  }
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (method_declaration
        returns: (predefined_type)
        name: (identifier)
        parameters: (parameter_list)
        body: (block
          (preproc_line
            (integer_literal)
            (string_literal
              (string_literal_content))
            (comment))
          (preproc_line)
          (preproc_line)
          (preproc_line
            (integer_literal)
            (integer_literal)
            (integer_literal)
            (integer_literal)
            (integer_literal)
            (string_literal
              (string_literal_content)))
          (preproc_line
            (integer_literal)
            (integer_literal)
            (integer_literal)
            (integer_literal)
            (string_literal
              (string_literal_content)))
          (preproc_line
            (integer_literal)
            (string_literal
              (string_literal_content)))
          (preproc_line)
          (preproc_line))))))

================================================================================
Directives not in strings or comments
================================================================================

class Of1879 {
  void AMethod() {
    var s = @"Only a string
    #if NOPE
";
    /* Only a comment
    #if NOPE
    */
  }
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (method_declaration
        returns: (predefined_type)
        name: (identifier)
        parameters: (parameter_list)
        body: (block
          (local_declaration_statement
            (variable_declaration
              type: (implicit_type)
              (variable_declarator
                name: (identifier)
                (verbatim_string_literal))))
          (comment))))))

================================================================================
Shebang directive
================================================================================

#!/usr/bin/env scriptcs

--------------------------------------------------------------------------------

(compilation_unit
  (shebang_directive))

================================================================================
Ignored directives for file-based apps (C# 14)
================================================================================

#!/usr/bin/env dotnet
#:package Humanizer@2.14.1
#:package Microsoft.Build@17.0.0 ExcludeAssets=runtime PrivateAssets=all
#:package Package @ 1.0.0
#:package "Humanizer"@2.0
#:sdk Microsoft.NET.Sdk.Web
#:sdk Aspire.AppHost.Sdk@9.0.0
#:property LangVersion=preview
#:property Xyz = "abc "
#:project ../Lib/Lib.csproj
#:ref ../lib/lib.cs
#:include ./**/*.cs
#:exclude obj/**
#:unknown // not a comment
#:sdk
#:
#: package Malformed@1

using System;

var package = 1;

--------------------------------------------------------------------------------

(compilation_unit
  (shebang_directive)
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    name: (directive_name)
    separator: (directive_separator)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind)
    value: (directive_value))
  (ignored_directive
    kind: (directive_kind))
  (ignored_directive)
  (ignored_directive
    value: (directive_value))
  (using_directive
    (identifier))
  (global_statement
    (local_declaration_statement
      (variable_declaration
        type: (implicit_type)
        (variable_declarator
          name: (identifier)
          (integer_literal))))))

================================================================================
Else clause inside a preprocessor block
================================================================================

class C {
  void M(object arg, bool hadHandler) {
    if (!hadHandler) {
      Refresh();
    }
#if DEBUG
    else if (arg is not null) {
      Log();
    }
#endif
    if (a) { x = 1; }
#if !NETFRAMEWORK
    else if (Try(arg, out var v)) { x = v; }
#else
    else if (arg is double d) { x = 2; }
    else if (arg is float f) { x = 3; }
#endif
    else if (arg is string s) { x = 4; }
  }
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (method_declaration
        returns: (predefined_type)
        name: (identifier)
        parameters: (parameter_list
          (parameter
            type: (predefined_type)
            name: (identifier))
          (parameter
            type: (predefined_type)
            name: (identifier)))
        body: (block
          (if_statement
            condition: (prefix_unary_expression
              (identifier))
            consequence: (block
              (expression_statement
                (invocation_expression
                  function: (identifier)
                  arguments: (argument_list))))
            (preproc_if
              condition: (identifier)
              (if_statement
                condition: (is_pattern_expression
                  expression: (identifier)
                  pattern: (negated_pattern
                    (constant_pattern
                      (null_literal))))
                consequence: (block
                  (expression_statement
                    (invocation_expression
                      function: (identifier)
                      arguments: (argument_list)))))))
          (if_statement
            condition: (identifier)
            consequence: (block
              (expression_statement
                (assignment_expression
                  left: (identifier)
                  right: (integer_literal))))
            (preproc_if
              condition: (unary_expression
                argument: (identifier))
              (if_statement
                condition: (invocation_expression
                  function: (identifier)
                  arguments: (argument_list
                    (argument
                      (identifier))
                    (argument
                      (declaration_expression
                        type: (implicit_type)
                        name: (identifier)))))
                consequence: (block
                  (expression_statement
                    (assignment_expression
                      left: (identifier)
                      right: (identifier)))))
              alternative: (preproc_else
                (if_statement
                  condition: (is_pattern_expression
                    expression: (identifier)
                    pattern: (declaration_pattern
                      type: (predefined_type)
                      name: (identifier)))
                  consequence: (block
                    (expression_statement
                      (assignment_expression
                        left: (identifier)
                        right: (integer_literal))))
                  alternative: (if_statement
                    condition: (is_pattern_expression
                      expression: (identifier)
                      pattern: (declaration_pattern
                        type: (predefined_type)
                        name: (identifier)))
                    consequence: (block
                      (expression_statement
                        (assignment_expression
                          left: (identifier)
                          right: (integer_literal))))))))
            alternative: (if_statement
              condition: (is_pattern_expression
                expression: (identifier)
                pattern: (declaration_pattern
                  type: (predefined_type)
                  name: (identifier)))
              consequence: (block
                (expression_statement
                  (assignment_expression
                    left: (identifier)
                    right: (integer_literal)))))))))))

================================================================================
Statement-level #if after an if statement is not an else block
================================================================================

class C {
  bool M(Type t) {
    if (t == typeof(DateTime)) {
      return true;
    }
#if HAVE_DATE_TIME_OFFSET
    if (t == typeof(DateTimeOffset)) {
      return true;
    }
#endif
    if (a) b();
    else c();
    return false;
  }
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (method_declaration
        returns: (predefined_type)
        name: (identifier)
        parameters: (parameter_list
          (parameter
            type: (identifier)
            name: (identifier)))
        body: (block
          (if_statement
            condition: (binary_expression
              left: (identifier)
              right: (typeof_expression
                type: (identifier)))
            consequence: (block
              (return_statement
                (boolean_literal))))
          (preproc_if
            condition: (identifier)
            (if_statement
              condition: (binary_expression
                left: (identifier)
                right: (typeof_expression
                  type: (identifier)))
              consequence: (block
                (return_statement
                  (boolean_literal)))))
          (if_statement
            condition: (identifier)
            consequence: (expression_statement
              (invocation_expression
                function: (identifier)
                arguments: (argument_list)))
            alternative: (expression_statement
              (invocation_expression
                function: (identifier)
                arguments: (argument_list))))
          (return_statement
            (boolean_literal)))))))

================================================================================
Method body that differs per preprocessor branch
================================================================================

class C {
  internal static T Convert<T>(object o)
#if NETFRAMEWORK
  {
    return (T)o;
  }
#else
      where T : INumber<T>
  {
    return T.CreateChecked(o);
  }
#endif

  int Twice(int x)
#if A
    => x * 2;
#else
    { return x + x; }
#endif
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (method_declaration
        (modifier)
        (modifier)
        returns: (identifier)
        name: (identifier)
        type_parameters: (type_parameter_list
          (type_parameter
            name: (identifier)))
        parameters: (parameter_list
          (parameter
            type: (predefined_type)
            name: (identifier)))
        (preproc_if
          condition: (identifier)
          body: (block
            (return_statement
              (cast_expression
                type: (identifier)
                value: (identifier))))
          alternative: (preproc_else
            (type_parameter_constraints_clause
              (identifier)
              (type_parameter_constraint
                type: (generic_name
                  (identifier)
                  (type_argument_list
                    (identifier)))))
            body: (block
              (return_statement
                (invocation_expression
                  function: (member_access_expression
                    expression: (identifier)
                    name: (identifier))
                  arguments: (argument_list
                    (argument
                      (identifier)))))))))
      (method_declaration
        returns: (predefined_type)
        name: (identifier)
        parameters: (parameter_list
          (parameter
            type: (predefined_type)
            name: (identifier)))
        (preproc_if
          condition: (identifier)
          body: (arrow_expression_clause
            (binary_expression
              left: (identifier)
              right: (integer_literal)))
          alternative: (preproc_else
            body: (block
              (return_statement
                (binary_expression
                  left: (identifier)
                  right: (identifier))))))))))

================================================================================
Return of a conditional whose arms end inside a preprocessor block
================================================================================

class C {
  Task<int> ReadAsync(TextReader reader, CancellationToken ct) {
    return ct.IsCancellationRequested ? FromCanceled<int>(ct) :
#if NET6_0_OR_GREATER
      reader.ReadAsync(buffer, ct).AsTask();
#else
      reader.ReadAsync(buffer);
#endif
  }
  int Plain(bool a) {
    return a ? 1 : 2;
  }
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (method_declaration
        returns: (generic_name
          (identifier)
          (type_argument_list
            (predefined_type)))
        name: (identifier)
        parameters: (parameter_list
          (parameter
            type: (identifier)
            name: (identifier))
          (parameter
            type: (identifier)
            name: (identifier)))
        body: (block
          (return_statement
            (conditional_expression
              condition: (member_access_expression
                expression: (identifier)
                name: (identifier))
              consequence: (invocation_expression
                function: (generic_name
                  (identifier)
                  (type_argument_list
                    (predefined_type)))
                arguments: (argument_list
                  (argument
                    (identifier))))
              alternative: (preproc_if
                condition: (identifier)
                (invocation_expression
                  function: (member_access_expression
                    expression: (invocation_expression
                      function: (member_access_expression
                        expression: (identifier)
                        name: (identifier))
                      arguments: (argument_list
                        (argument
                          (identifier))
                        (argument
                          (identifier))))
                    name: (identifier))
                  arguments: (argument_list))
                alternative: (preproc_else
                  (invocation_expression
                    function: (member_access_expression
                      expression: (identifier)
                      name: (identifier))
                    arguments: (argument_list
                      (argument
                        (identifier))))))))))
      (method_declaration
        returns: (predefined_type)
        name: (identifier)
        parameters: (parameter_list
          (parameter
            type: (predefined_type)
            name: (identifier)))
        body: (block
          (return_statement
            (conditional_expression
              condition: (identifier)
              consequence: (integer_literal)
              alternative: (integer_literal))))))))

================================================================================
Preprocessor block holding whole elements at the end of a list
================================================================================

class C {
  object[] a = { (Half)42, Half.NaN,
#if HAVE_INT128
    (Int128)42, Int128.MinValue,
#endif
  };
  object[] b = [
    6, 5.001,
#if !NETFRAMEWORK
    (Half)6,
    (UInt128)6,
#else
    (int)6,
#endif
  ];
  object[] c = { 1,
#if X
    2,
#endif
    3 };
}

--------------------------------------------------------------------------------

(compilation_unit
  (class_declaration
    name: (identifier)
    body: (declaration_list
      (field_declaration
        (variable_declaration
          type: (array_type
            type: (predefined_type)
            rank: (array_rank_specifier))
          (variable_declarator
            name: (identifier)
            (initializer_expression
              (cast_expression
                type: (identifier)
                value: (integer_literal))
              (member_access_expression
                expression: (identifier)
                name: (identifier))
              (preproc_if
                condition: (identifier)
                (cast_expression
                  type: (identifier)
                  value: (integer_literal))
                (member_access_expression
                  expression: (identifier)
                  name: (identifier)))))))
      (field_declaration
        (variable_declaration
          type: (array_type
            type: (predefined_type)
            rank: (array_rank_specifier))
          (variable_declarator
            name: (identifier)
            (collection_expression
              (collection_element
                (expression_element
                  (integer_literal)))
              (collection_element
                (expression_element
                  (real_literal)))
              (preproc_if
                condition: (unary_expression
                  argument: (identifier))
                (collection_element
                  (expression_element
                    (cast_expression
                      type: (identifier)
                      value: (integer_literal))))
                (collection_element
                  (expression_element
                    (cast_expression
                      type: (identifier)
                      value: (integer_literal))))
                alternative: (preproc_else
                  (collection_element
                    (expression_element
                      (cast_expression
                        type: (predefined_type)
                        value: (integer_literal))))))))))
      (field_declaration
        (variable_declaration
          type: (array_type
            type: (predefined_type)
            rank: (array_rank_specifier))
          (variable_declarator
            name: (identifier)
            (initializer_expression
              (integer_literal)
              (preproc_if
                condition: (identifier)
                (integer_literal))
              (integer_literal))))))))
