1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{fmt::Display, hash::Hash};
use serde::{Serialize, Deserialize};

use super::Handle;
use crate::utils::string_intern;


#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[derive(Serialize, Deserialize)]
pub struct Location {
    pub path: Handle<String>,
    pub line: usize,
    pub colum: usize,
    pub pos: usize,
}

impl Location {
    pub fn new(path: Handle<String>, line: usize, colum: usize, pos: usize) -> Self {
        Self { path, line, colum, pos }
    }
}


#[derive(Debug, Clone, Eq)]
#[derive(Serialize, Deserialize)]
pub struct Symbol (pub Handle<String>, pub Location);

impl Symbol {
    pub fn new(i: &str) -> Self {
        Symbol (string_intern(i), Location::default())
    }

    pub fn from(i: &str, pos: &Location) -> Self {
        Symbol (string_intern(i), pos.clone())
    }
}

impl PartialEq for Symbol {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl Display for Symbol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.0, f)
    }
}

impl Hash for Symbol {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

unsafe impl Sync for Symbol {}
unsafe impl Send for Symbol {}