wright/ast/literal.rs
1//! AST node models representing literal values in source code.
2
3use super::AstNode;
4use crate::source_tracking::fragment::Fragment;
5use num::BigUint;
6
7/// Any literal value in source code.
8#[derive(Debug, Clone, PartialEq)]
9#[allow(missing_docs)]
10pub enum Literal {
11 Integer(IntegerLiteral),
12 Float(FloatLiteral),
13 Boolean(BooleanLiteral),
14 String(StringLiteral),
15 FormatString(FormatStringLiteral),
16 Char(CharLiteral),
17}
18
19impl AstNode for Literal {
20 fn matching_source(&self) -> &Fragment {
21 match self {
22 Literal::Integer(lit) => &lit.fragment,
23 Literal::Float(lit) => &lit.fragment,
24 Literal::Boolean(lit) => &lit.fragment,
25 Literal::String(lit) => &lit.fragment,
26 Literal::FormatString(lit) => &lit.fragment,
27 Literal::Char(lit) => &lit.fragment,
28 }
29 }
30}
31
32/// An integer literal from source. This only contains unsigned integers as writing negative numbers is considered
33/// to be a combination of an integer literal with a unary negation.
34#[derive(Debug, Clone, PartialEq)]
35pub struct IntegerLiteral {
36 /// The [Fragment] of source code containing this integer literal.
37 pub fragment: Fragment,
38
39 /// The value of the integer parsed from the matching source.
40 pub value: BigUint,
41}
42
43/// A floating point literal from source, such as `1.5` or `2e-3`. Like [IntegerLiteral]s, these are never negative.
44#[derive(Debug, Clone, PartialEq)]
45pub struct FloatLiteral {
46 /// The [Fragment] of source code containing this float literal.
47 pub fragment: Fragment,
48
49 /// The value of the float (parsed at the highest available precision -- type checking decides whether this is
50 /// an `f32` or an `f64`).
51 pub value: f64,
52}
53
54/// A boolean literal from source.
55#[derive(Debug, Clone, PartialEq)]
56pub struct BooleanLiteral {
57 /// The [Fragment] of source code containing this boolean literal.
58 pub fragment: Fragment,
59
60 /// The value of the boolean literal.
61 pub value: bool,
62}
63
64/// A string literal from source, such as `"hello\n"`.
65#[derive(Debug, Clone, PartialEq)]
66pub struct StringLiteral {
67 /// The [Fragment] of source code containing this literal (including the quotes).
68 pub fragment: Fragment,
69
70 /// The value of the string, with escape sequences processed.
71 pub value: String,
72}
73
74/// A format string literal from source, such as `` `hello {name}` ``.
75#[derive(Debug, Clone, PartialEq)]
76pub struct FormatStringLiteral {
77 /// The [Fragment] of source code containing this literal (including the backticks).
78 pub fragment: Fragment,
79
80 /// The contents of the format string, with escape sequences processed. Interpolated segments are not parsed
81 /// yet, and are left in the string as written.
82 pub value: String,
83}
84
85/// A character literal from source, such as `'a'` or `'\u{1F600}'`.
86#[derive(Debug, Clone, PartialEq)]
87pub struct CharLiteral {
88 /// The [Fragment] of source code containing this literal (including the quotes).
89 pub fragment: Fragment,
90
91 /// The value of the character, with escape sequences processed.
92 pub value: char,
93}