summaryrefslogtreecommitdiff
path: root/src/stages/syntactic_tokens.rs
blob: 5a0ac9e1290774c77913c976700b64a32d198f44 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::*;

pub enum SyntacticToken {
    LabelDefinition(ScopedSymbol),
    MacroDefinition(SyntacticMacroDefinition),

    IntegerLiteral(isize),
    StringLiteral(StringLiteral),
    WordTemplate(WordTemplate),

    BlockLiteral(Vec<Tracked<SyntacticToken>>),
    Expression(Vec<Tracked<SyntacticToken>>),

    Symbol(ScopedSymbol),

    Separator,
    Condition,
    Pin,
}

pub struct SyntacticMacroDefinition {
    pub name: Tracked<String>,
    pub body: Vec<Tracked<SyntacticToken>>,
}

#[derive(Clone)]
pub struct StringLiteral {
    pub string: String,
    pub chars: Vec<Tracked<isize>>,
}

impl std::fmt::Display for StringLiteral {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        self.string.fmt(f)
    }
}

pub enum ScopedSymbol {
    Local(String),
    Global(String),
}

impl std::fmt::Display for ScopedSymbol {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match self {
            ScopedSymbol::Local(name) => write!(f, "~{name}"),
            ScopedSymbol::Global(name) => write!(f, "{name}"),
        }
    }
}


pub enum SyntacticError {
    UnterminatedBlock,
    UnterminatedExpression,
    UnterminatedComment,
    UnterminatedCharacterLiteral,
    UnterminatedStringLiteral,
    UnterminatedMacroDefinition(String),

    UnmatchedBlockTerminator,
    UnmatchedExpressionTerminator,
    UnmatchedCommentTerminator,
    UnmatchedMacroTerminator,

    ExpectedSingleCharacter,

    DuplicateFieldNameInWord(char),
    InvalidCharacterInWord(char),

    InvalidDecimalLiteral(String),
    InvalidHexadecimalLiteral(String),
    InvalidBinaryLiteral(String),
    InvalidOctalLiteral(String),
}


pub fn report_syntactic_errors(errors: &[Tracked<SyntacticError>], source_code: &str) {
    for error in errors {
        report_syntactic_error(error, source_code);
    }
}

fn report_syntactic_error(error: &Tracked<SyntacticError>, source_code: &str) {
    let context = Context { source_code: &source_code, source: &error.source };
    let message = match &error.value {
        SyntacticError::UnterminatedBlock =>
            "Block was not terminated, add a '}}' character to terminate",
        SyntacticError::UnterminatedExpression =>
            "Expression was not terminated, add a ']' character to terminate",
        SyntacticError::UnterminatedComment =>
            "Comment was not terminated, add a ')' character to terminate",
        SyntacticError::UnterminatedCharacterLiteral =>
            "Character was not terminated, add a ' character to terminate",
        SyntacticError::UnterminatedStringLiteral =>
            "String was not terminated, add a '\"' character to terminate",
        SyntacticError::UnterminatedMacroDefinition(name) =>
            &format!("The '{name}' macro definition was not terminated, add a ';' character to terminate"),

        SyntacticError::UnmatchedBlockTerminator =>
            "Attempted to terminate a block, but no block was in progress",
        SyntacticError::UnmatchedExpressionTerminator =>
            "Attempted to terminate an expression, but no expression was in progress",
        SyntacticError::UnmatchedCommentTerminator =>
            "Attempted to terminate a comment, but no comment was in progress",
        SyntacticError::UnmatchedMacroTerminator =>
            "Attempted to terminate a macro definition, but no macro definition was in progress",

        SyntacticError::ExpectedSingleCharacter =>
            "A character literal must contain exactly one character",

        SyntacticError::DuplicateFieldNameInWord(name) =>
            &format!("The field '{name}' has already been used in this word"),
        SyntacticError::InvalidCharacterInWord(c) =>
            &format!("The character '{c}' cannot be used in a word"),

        SyntacticError::InvalidDecimalLiteral(string) =>
            &format!("The string '{string}' is not a valid decimal literal"),
        SyntacticError::InvalidHexadecimalLiteral(string) =>
            &format!("The string '{string}' is not a valid hexadecimal literal"),
        SyntacticError::InvalidBinaryLiteral(string) =>
            &format!("The string '{string}' is not a valid binary literal"),
        SyntacticError::InvalidOctalLiteral(string) =>
            &format!("The string '{string}' is not a valid octal literal"),
    };

    report_source_issue(LogLevel::Error, &context, message);
}


pub fn print_syntactic_token(i: usize, token: &SyntacticToken) {
    match token {
        SyntacticToken::LabelDefinition(symbol) => indent!(i, "LabelDefinition({symbol})"),
        SyntacticToken::MacroDefinition(definition) => {
            indent!(i, "MacroDefinition({})", definition.name);
            for token in &definition.body {
                print_syntactic_token(i+1, token);
            }
        }

        SyntacticToken::IntegerLiteral(value) => indent!(i, "IntegerLiteral({value})"),
        SyntacticToken::StringLiteral(literal) => indent!(i, "StringLiteral({literal})"),
        SyntacticToken::WordTemplate(template) => indent!(i, "WordTemplate({template})"),

        SyntacticToken::BlockLiteral(tokens) => {
            indent!(i, "BlockLiteral");
            for token in tokens {
                print_syntactic_token(i+1, token);
            }
        }
        SyntacticToken::Expression(tokens) => {
            indent!(i, "Expression");
            for token in tokens {
                print_syntactic_token(i+1, token);
            }
        }

        SyntacticToken::Symbol(symbol) => indent!(i, "Symbol({symbol})"),

        SyntacticToken::Separator => indent!(i, "Separator"),
        SyntacticToken::Condition => indent!(i, "Condition"),
        SyntacticToken::Pin => indent!(i, "Pin"),
    }
}