Skip to main content

conjure_cp_essence_parser/diagnostics/
diagnostics_api.rs

1use serde::{Deserialize, Serialize};
2
3use crate::diagnostics::error_detection::collect_errors::detect_errors;
4
5// structs for lsp stuff
6
7// position / range
8#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(rename_all = "camelCase")]
10pub struct Position {
11    pub line: u32,
12    pub character: u32,
13}
14
15#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
16#[serde(rename_all = "camelCase")]
17pub struct Range {
18    pub start: Position,
19    pub end: Position,
20}
21
22// the actual values can be chnaged later, if needed
23#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
24#[serde(rename_all = "camelCase")]
25pub enum Severity {
26    Error = 1,
27    Warn = 2,
28    Info = 3,
29    Hint = 4,
30}
31
32// the actual diagnostic struct
33#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
34#[serde(rename_all = "camelCase")]
35pub struct Diagnostic {
36    pub range: Range,
37    pub severity: Severity,
38    pub message: String,
39    pub source: &'static str,
40}
41
42// document symbol struct is used to denote a single token / node
43// this will be used for syntax highlighting and hovering
44#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46pub enum SymbolKind {
47    Integer = 0,
48    Decimal = 1,
49    Function = 2,
50    Letting = 3,
51    Find = 4,
52    Variable = 5,
53    Constant = 6,
54    Domain = 7,
55} // to be extended
56
57// each type of token / symbol in the essence grammar will be
58// assigned an integer, which would be mapped to a colour
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(rename_all = "camelCase")]
61pub struct DocumentSymbol {
62    pub name: String,
63    pub detail: Option<String>,
64    pub kind: SymbolKind,
65    pub range: Range,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub children: Option<Vec<DocumentSymbol>>,
68}
69
70// getting the actual diagnostic
71pub fn get_diagnostics(source: &str) -> Vec<Diagnostic> {
72    let mut diagnostics = Vec::new();
73
74    diagnostics.extend(detect_errors(source));
75
76    diagnostics
77}
78
79// get document symbols for semantic highlighting