swc_ecma_fast_parser/parser/
util.rs

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
//! Utility functions for the parser

use swc_atoms::Atom;
use swc_common::{Span, SyntaxContext};
use swc_ecma_ast::{BindingIdent, Ident, Number, Str};

use crate::{
    error::{Error, ErrorKind},
    token::{Token, TokenType, TokenValue},
};

/// Convert a token value to an identifier
pub fn token_value_to_ident(token: &Token, span: Span) -> Ident {
    if let TokenValue::Str { value, .. } = &token.value {
        Ident {
            span,
            sym: value.clone(),
            optional: false,
            ctxt: SyntaxContext::empty(),
        }
    } else {
        // Default to empty identifier if token is not a string
        Ident {
            span,
            sym: Atom::from(""),
            optional: false,
            ctxt: SyntaxContext::empty(),
        }
    }
}

/// Convert a token value to a binding identifier
pub fn token_value_to_binding_ident(token: &Token, span: Span) -> BindingIdent {
    BindingIdent {
        id: token_value_to_ident(token, span),
        type_ann: None,
    }
}

/// Convert a token value to a string literal
pub fn token_value_to_str(token: &Token, span: Span) -> Str {
    if let TokenValue::Str { value, raw } = &token.value {
        Str {
            span,
            value: value.clone(),
            raw: Some(raw.clone()),
        }
    } else {
        // Default to empty string if token is not a string
        Str {
            span,
            value: Atom::from(""),
            raw: None,
        }
    }
}

/// Convert a token value to a number literal
pub fn token_value_to_number(token: &Token, span: Span) -> Number {
    if let TokenValue::Num { value, raw } = &token.value {
        Number {
            span,
            value: *value,
            raw: Some(raw.clone()),
        }
    } else {
        // Default to 0 if token is not a number
        Number {
            span,
            value: 0.0,
            raw: None,
        }
    }
}

/// Check if a token type is a binary operator
pub fn is_binary_operator(token_type: TokenType) -> bool {
    matches!(
        token_type,
        TokenType::EqEq
            | TokenType::NotEq
            | TokenType::EqEqEq
            | TokenType::NotEqEq
            | TokenType::Lt
            | TokenType::LtEq
            | TokenType::Gt
            | TokenType::GtEq
            | TokenType::LShift
            | TokenType::RShift
            | TokenType::ZeroFillRShift
            | TokenType::Plus
            | TokenType::Minus
            | TokenType::Asterisk
            | TokenType::Slash
            | TokenType::Percent
            | TokenType::Pipe
            | TokenType::Ampersand
            | TokenType::Caret
            | TokenType::Exp
            | TokenType::LogicalOr
            | TokenType::LogicalAnd
            | TokenType::NullishCoalescing
            | TokenType::In
            | TokenType::InstanceOf
    )
}

/// Create an error for invalid await usage
pub fn invalid_await_error(span: Span) -> Error {
    Error {
        kind: ErrorKind::General {
            message: "await is only valid in async functions".to_string(),
        },
        span,
    }
}

/// Create an error for invalid yield usage
pub fn invalid_yield_error(span: Span) -> Error {
    Error {
        kind: ErrorKind::General {
            message: "yield is only valid in generator functions".to_string(),
        },
        span,
    }
}