User-defined Error Reporting API

The Cangjie ast package provides a user-defined error reporting API diagReport, so that users defining macros can perform user-defined error reporting on incorrect Tokens during parsing of input Tokens.

The user-defined error reporting API provides the same error output format as the native compiler and allows users to report WARNING and ERROR messages.

Usage Example

Macro definition:

// macro_definition.cj
macro package macro_definition

import std.ast.*

public macro testDef(input: Tokens): Tokens {
    for (i in 0..input.size) {
        if (input[i].kind == IDENTIFIER) {
            diagReport(DiagReportLevel.ERROR, input[i..(i + 1)], "This expression is not allowed to contain identifier",   "Here is the illegal identifier")
        }
    }
    return input
}

Macro call:

// macro_call.cj
package macro_calling

import std.ast.*
import macro_definition.*

main(): Int64 {
    let a = @testDef(1)
    let b = @testDef(a)
    let c = @testDef(1 + a)
    return 0
}

Compilation commands:

# Compile the macro definition file.
cjc macro_definition.cj --compile-macro

# Compile the file of the macro to be used.
cjc macro_call.cj -o demo

Error message:

error: This expression is not allowed to contain identifier
 ==> call.cj:9:22:
  |
9 |     let b = @testDef(a)
  |                      ^ Here is the illegal identifier
  |

error: This expression is not allowed to contain identifier
  ==> call.cj:10:26:
   |
10 |     let c = @testDef(1 + a)
   |                          ^ Here is the illegal identifier
   |

2 errors generated, 2 errors printed.

Example of a Call Not in Macro Expansion

import std.ast.*

func A(input: Tokens) {
    diagReport(DiagReportLevel.ERROR, input, "Message", "Hint")  // The call is in common function **A**. **diagReport** does not perform any operation.
}

main() {
    let tokens = quote(var a = 0)
    A(tokens)
}