acir/parser/
token.rs

1use acir_field::FieldElement;
2use noirc_span::{Position, Span, Spanned};
3
4#[derive(Debug)]
5pub(crate) struct SpannedToken(Spanned<Token>);
6
7impl SpannedToken {
8    pub(crate) fn new(token: Token, span: Span) -> SpannedToken {
9        SpannedToken(Spanned::from(span, token))
10    }
11
12    pub(crate) fn span(&self) -> Span {
13        self.0.span()
14    }
15
16    pub(crate) fn token(&self) -> &Token {
17        &self.0.contents
18    }
19
20    pub(crate) fn into_token(self) -> Token {
21        self.0.contents
22    }
23}
24
25/// Token types used in the ACIR text format.
26#[derive(Debug, Clone, PartialEq)]
27pub(crate) enum Token {
28    /// Identifier such as `RANGE`, `AND`, etc.
29    Ident(String),
30    /// Reserved identifiers such as `CONSTRAIN`.
31    /// Most words in ACIR's human readable are expected to be keywords
32    Keyword(Keyword),
33    /// Witness index, like `w42`
34    Witness(u32),
35    /// Block index, like `b42`
36    Block(u32),
37    /// Integer value represented using the underlying native field element
38    Int(FieldElement),
39    /// :
40    Colon,
41    /// ;
42    Semicolon,
43    /// ,
44    Comma,
45    /// [
46    LeftBracket,
47    /// ]
48    RightBracket,
49    /// (
50    LeftParen,
51    /// )
52    RightParen,
53    /// +
54    Plus,
55    /// -
56    Minus,
57    /// *
58    Star,
59    /// =
60    Equal,
61    Eof,
62}
63
64impl Token {
65    pub(super) fn into_single_span(self, position: Position) -> SpannedToken {
66        self.into_span(position, position)
67    }
68
69    pub(super) fn into_span(self, start: Position, end: Position) -> SpannedToken {
70        SpannedToken(Spanned::from_position(start, end, self))
71    }
72}
73
74impl std::fmt::Display for Token {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        match self {
77            Token::Ident(ident) => write!(f, "{ident}"),
78            Token::Keyword(keyword) => write!(f, "{keyword}"),
79            Token::Witness(index) => write!(f, "w{index}"),
80            Token::Block(index) => write!(f, "b{index}"),
81            Token::Int(int) => write!(f, "{int}"),
82            Token::Colon => write!(f, ":"),
83            Token::Semicolon => write!(f, ";"),
84            Token::Comma => write!(f, ","),
85            Token::LeftBracket => write!(f, "["),
86            Token::RightBracket => write!(f, "]"),
87            Token::LeftParen => write!(f, "("),
88            Token::RightParen => write!(f, ")"),
89            Token::Plus => write!(f, "+"),
90            Token::Minus => write!(f, "-"),
91            Token::Star => write!(f, "*"),
92            Token::Equal => write!(f, "="),
93            Token::Eof => write!(f, "(end of stream)"),
94        }
95    }
96}
97
98/// ACIR human readable text format keywords
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
100pub(crate) enum Keyword {
101    /// private
102    Private,
103    /// parameters
104    Parameters,
105    /// public
106    Public,
107    /// return
108    Return,
109    /// value
110    Value,
111    /// values
112    Values,
113    /// ASSERT
114    Assert,
115    /// BLACKBOX
116    BlackBoxFuncCall,
117    /// INIT
118    MemoryInit,
119    /// READ
120    MemoryRead,
121    /// WRITE
122    MemoryWrite,
123    /// BRILLIG
124    Brillig,
125    /// CALL
126    Call,
127    /// predicate
128    Predicate,
129    /// CALLDATA
130    CallData,
131    /// RETURNDATA
132    ReturnData,
133    /// func
134    Function,
135    /// input
136    Input,
137    /// input1
138    Input1,
139    /// input2
140    Input2,
141    /// inputs
142    Inputs,
143    /// output
144    Output,
145    /// outputs
146    Outputs,
147    /// bits
148    Bits,
149    /// iv
150    Iv,
151    /// key
152    Key,
153    /// lhs
154    Lhs,
155    /// rhs
156    Rhs,
157    /// public_key_x
158    PublicKeyX,
159    /// public_key_y
160    PublicKeyY,
161    /// signature
162    Signature,
163    /// hashed_message
164    HashedMessage,
165    /// points
166    Points,
167    /// scalars
168    Scalars,
169    /// verification_key
170    VerificationKey,
171    /// proof
172    Proof,
173    /// public_inputs
174    PublicInputs,
175    /// key_hash
176    KeyHash,
177    /// proof_type
178    ProofType,
179    /// hash_values
180    HashValues,
181}
182
183impl Keyword {
184    pub(super) fn lookup_keyword(word: &str) -> Option<Token> {
185        let keyword = match word {
186            "private" => Keyword::Private,
187            "parameters" => Keyword::Parameters,
188            "public" => Keyword::Public,
189            "return" => Keyword::Return,
190            "value" => Keyword::Value,
191            "values" => Keyword::Values,
192            "ASSERT" => Keyword::Assert,
193            "BLACKBOX" => Keyword::BlackBoxFuncCall,
194            "INIT" => Keyword::MemoryInit,
195            "READ" => Keyword::MemoryRead,
196            "WRITE" => Keyword::MemoryWrite,
197            "BRILLIG" => Keyword::Brillig,
198            "CALL" => Keyword::Call,
199            "predicate" => Keyword::Predicate,
200            "CALLDATA" => Keyword::CallData,
201            "RETURNDATA" => Keyword::ReturnData,
202            "func" => Keyword::Function,
203            "input" => Keyword::Input,
204            "input1" => Keyword::Input1,
205            "input2" => Keyword::Input2,
206            "inputs" => Keyword::Inputs,
207            "output" => Keyword::Output,
208            "outputs" => Keyword::Outputs,
209            "bits" => Keyword::Bits,
210            "iv" => Keyword::Iv,
211            "key" => Keyword::Key,
212            "lhs" => Keyword::Lhs,
213            "rhs" => Keyword::Rhs,
214            "public_key_x" => Keyword::PublicKeyX,
215            "public_key_y" => Keyword::PublicKeyY,
216            "signature" => Keyword::Signature,
217            "hashed_message" => Keyword::HashedMessage,
218            "points" => Keyword::Points,
219            "scalars" => Keyword::Scalars,
220            "verification_key" => Keyword::VerificationKey,
221            "proof" => Keyword::Proof,
222            "public_inputs" => Keyword::PublicInputs,
223            "key_hash" => Keyword::KeyHash,
224            "proof_type" => Keyword::ProofType,
225            "hash_values" => Keyword::HashValues,
226            _ => return None,
227        };
228        Some(Token::Keyword(keyword))
229    }
230}
231
232impl std::fmt::Display for Keyword {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        match self {
235            Keyword::Private => write!(f, "private"),
236            Keyword::Parameters => write!(f, "parameters"),
237            Keyword::Public => write!(f, "public"),
238            Keyword::Return => write!(f, "return"),
239            Keyword::Value => write!(f, "value"),
240            Keyword::Values => write!(f, "values"),
241            Keyword::Assert => write!(f, "ASSERT"),
242            Keyword::BlackBoxFuncCall => write!(f, "BLACKBOX"),
243            Keyword::MemoryInit => write!(f, "INIT"),
244            Keyword::MemoryRead => write!(f, "READ"),
245            Keyword::MemoryWrite => write!(f, "WRITE"),
246            Keyword::Brillig => write!(f, "BRILLIG"),
247            Keyword::Call => write!(f, "CALL"),
248            Keyword::Predicate => write!(f, "predicate"),
249            Keyword::CallData => write!(f, "CALLDATA"),
250            Keyword::ReturnData => write!(f, "RETURNDATA"),
251            Keyword::Function => write!(f, "func"),
252            Keyword::Input => write!(f, "input"),
253            Keyword::Input1 => write!(f, "input1"),
254            Keyword::Input2 => write!(f, "input2"),
255            Keyword::Inputs => write!(f, "inputs"),
256            Keyword::Output => write!(f, "output"),
257            Keyword::Outputs => write!(f, "outputs"),
258            Keyword::Bits => write!(f, "bits"),
259            Keyword::Iv => write!(f, "iv"),
260            Keyword::Key => write!(f, "key"),
261            Keyword::Lhs => write!(f, "lhs"),
262            Keyword::Rhs => write!(f, "rhs"),
263            Keyword::PublicKeyX => write!(f, "public_key_x"),
264            Keyword::PublicKeyY => write!(f, "public_key_y"),
265            Keyword::Signature => write!(f, "signature"),
266            Keyword::HashedMessage => write!(f, "hashed_message"),
267            Keyword::Points => write!(f, "points"),
268            Keyword::Scalars => write!(f, "scalars"),
269            Keyword::VerificationKey => write!(f, "verification_key"),
270            Keyword::Proof => write!(f, "proof"),
271            Keyword::PublicInputs => write!(f, "public_inputs"),
272            Keyword::KeyHash => write!(f, "key_hash"),
273            Keyword::ProofType => write!(f, "proof_type"),
274            Keyword::HashValues => write!(f, "hash_values"),
275        }
276    }
277}