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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright contributors to the openqasm-parser project
// SPDX-License-Identifier: Apache-2.0

use super::*;

// use self::SyntaxKind::{INT_KW, BIT_KW, QUBIT_KW};
impl SyntaxKind {
    // This is now is_classical_type, written in codegen.
    // pub fn is_scalar_type_name(self) -> bool {
    //     matches!(self,
    //              BIT_KW
    //              | INT_KW
    //              | UINT_KW
    //              | FLOAT_KW
    //              | ANGLE_KW
    //              | BOOL_KW
    //              | DURATION_KW
    //              | STRETCH_KW
    //              | COMPLEX_KW
    //     )
    // }
    pub fn is_classical_type(self) -> bool {
        self.is_scalar_type() || matches!(self, ARRAY_KW)
    }
    pub fn is_type_name(self) -> bool {
        self.is_classical_type() || matches!(self, QUBIT_KW)
    }
}

pub(super) fn param_list_gate_params(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::GateParams, None);
}
pub(super) fn param_list_gate_qubits(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::GateQubits, None);
}

pub(super) fn arg_list_gate_call_qubits(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::GateCallQubits, None);
}

pub(super) fn param_list_def_params(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::DefParams, None);
}
pub(super) fn param_list_defcal_params(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::DefCalParams, None);
}
pub(super) fn param_list_defcal_qubits(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::DefCalQubits, None);
}

// pub(super) fn set_expression(p: &mut Parser<'_>) {
//     _param_list_openqasm(p, DefFlavor::SetExpression, None);
// }

pub(super) fn expression_list(p: &mut Parser<'_>) {
    _param_list_openqasm(p, DefFlavor::ExpressionList, None);
}

// Here and elsewhere "Gate" means gate def, and "GateCall" means gate call.
#[derive(Debug, Clone, Copy)]
enum DefFlavor {
    // Same syntax for: gate def params, function call params, gate call params.
    // But for various reasons, we separate this into GateParams and CallOrGateCallParams
    // One reason: we can disallow here empty parens in gate def.
    GateParams, // parens,    no type
    // For the moment, following is handled in expressions::arg_list instead.
    GateQubits,     // no parens, no type, '{' terminates
    GateCallQubits, // no parens, no type, ';' terminates
    DefParams,      // parens,    type
    DefCalParams,   // parens,    opt type
    DefCalQubits,   // no parens, no type, '{' or '->' terminates
    //    SetExpression,
    ExpressionList,
}

// Parse a list of parameters.
// FIXME: m: Option<Marker> is unused, always called with None. Can remove.
fn _param_list_openqasm(p: &mut Parser<'_>, flavor: DefFlavor, m: Option<Marker>) {
    use DefFlavor::*;
    let list_marker = p.start();
    let want_parens = matches!(flavor, GateParams | DefParams | DefCalParams);
    match flavor {
        GateParams | DefParams | DefCalParams => p.bump(T!['(']),
        //        SetExpression => p.bump(T!['{']),
        _ => (),
    }
    // FIXME: find implementation that does not require [T![')'], T![')']]
    // I tried using TokenSet, which should give exactly the same result.
    // But it does not. May be because -> is a compound token.
    let list_end_tokens = match flavor {
        // GateParams | DefParams | DefCalParams => {TokenSet::new(&[T![')']])},
        // GateQubits => {TokenSet::new(&[T!['{']])},
        // DefCalQubits => {TokenSet::new(&[T!['{'], T![->]])},
        ExpressionList => [T![']'], T![']']],
        GateParams | DefParams | DefCalParams => [T![')'], T![')']],
        // When no parens are present `{` terminates the list of parameters.
        GateQubits => [T!['{'], T!['{']],
        GateCallQubits => [SEMICOLON, SEMICOLON],
        DefCalQubits => [T!['{'], T![->]],
        //        SetExpression => [T!['}'], T!['}']],
    };
    let mut param_marker = m;
    // let mut param_marker = None;
    let mut num_params: usize = 0;
    // Would be nice if we could used the following line instead of hacked roll your own two lines down.
    //  while !p.at(EOF) && !p.at_ts(list_end_tokens) {
    while !p.at(EOF) && !list_end_tokens.iter().any(|x| p.at(*x)) {
        let m = param_marker.take().unwrap_or_else(|| p.start());
        if !(p.current().is_type_name() || p.at_ts(PARAM_FIRST)) {
            p.error("expected value parameter");
            m.abandon(p);
            break;
        }
        let found_param = match flavor {
            ExpressionList => {
                m.abandon(p);
                expressions::expr_or_range_expr(p);
                true
            }
            GateCallQubits => arg_gate_call_qubit(p, m),
            DefParams | DefCalParams => param_typed(p, m),
            _ => param_untyped(p, m),
        };
        if !found_param {
            break;
        }
        num_params += 1;
        // FIXME: This is only needed to support `->` as terminating tokens.
        // Not for `{`. But I don't know why. prbly because `->` is compound.
        // FIXME: use at_ts()
        if list_end_tokens.iter().any(|x| p.at(*x)) {
            //        if p.at_ts(list_end_tokens) {
            break;
        }
        // Params must be separated by commas.
        if !p.at(T![,]) {
            if p.at_ts(PARAM_FIRST) {
                p.error("Expected `,`");
            } else {
                break;
            }
        } else {
            // We found the expected comma, so consume it.
            p.bump(T![,]);
        }
    }
    match flavor {
        GateParams | ExpressionList if num_params < 1 => {
            p.error("expected one or more parameters");
        }
        _ => {}
    };
    if let Some(m) = param_marker {
        m.abandon(p);
    }
    // FIXME: rewrite followig as match statement.
    // Error if we don't find closing paren.
    if want_parens {
        p.expect(T![')']);
    }
    // if matches!(flavor, SetExpression) {
    //     p.expect(T!['}']);
    // }
    let kind = match flavor {
        //        SetExpression => SET_EXPRESSION,
        GateQubits => PARAM_LIST,
        DefCalQubits => QUBIT_LIST,
        GateCallQubits => QUBIT_LIST,
        ExpressionList => EXPRESSION_LIST,
        _ => PARAM_LIST,
    };
    list_marker.complete(p, kind);
    // let is_qubits = matches!(flavor, GateQubits | DefCalQubits);
    // list_marker.complete(p, if is_qubits {QUBIT_LIST} else {PARAM_LIST});
}

const PATTERN_FIRST: TokenSet = expressions::LITERAL_FIRST
    .union(expressions::atom::PATH_FIRST)
    .union(TokenSet::new(&[
        T![box],
        T![const],
        T!['('],
        T!['['],
        T![&],
        T![_],
        T![-],
        T![.],
    ]));

const TYPE_FIRST: TokenSet = expressions::atom::PATH_FIRST.union(TokenSet::new(&[
    T!['('],
    T!['['],
    T![<],
    T![!],
    T![*],
    T![&],
    T![_],
    T![extern],
]));

const PARAM_FIRST: TokenSet = PATTERN_FIRST.union(TYPE_FIRST);

// TODO: Look again at the r-a code to copy the idioms there.
// We have removed all of the code that can serve as an example.
// In OQ 3, parameters in gate defs don't have type annotations.
fn param_untyped(p: &mut Parser<'_>, m: Marker) -> bool {
    if !p.at(IDENT) {
        p.error("Expected parameter name");
        m.abandon(p);
        return false;
    }
    p.bump(IDENT);
    m.complete(p, PARAM);
    true
}

fn param_typed(p: &mut Parser<'_>, m: Marker) -> bool {
    let mut success = true;
    if p.current().is_type_name() {
        p.bump_any();
    } else {
        p.error("expected type annotation");
        success = false;
    }
    if !p.at(IDENT) {
        p.error("expected parameter name");
        m.abandon(p);
        return false;
    }
    p.bump(IDENT);
    m.complete(p, PARAM);
    success
}

// These can be cast to GateOperand
pub(crate) fn arg_gate_call_qubit(p: &mut Parser<'_>, m: Marker) -> bool {
    if p.at(HARDWAREIDENT) {
        p.bump(HARDWAREIDENT);
        m.complete(p, HARDWARE_QUBIT);
        return true;
    }

    if !p.at(IDENT) {
        p.error("Expected parameter name");
        m.abandon(p);
        return false;
    }
    //    let mcomp = expressions::atom::identifier(p);
    p.bump(IDENT);
    let mcomp = m.complete(p, IDENTIFIER);
    if p.at(T!['[']) {
        //        expressions::index_expr(p, mcomp);
        expressions::indexed_identifer(p, mcomp);
        return true;
    }
    true
}