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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::ops::Range;

use derive_more::Display;
use logos::Logos;
use nom::InputLength;

use crate::error::ParserError;

/// All of the possible tokens in a PDDL file
#[derive(Logos, Debug, Display, Clone, PartialEq)]
#[logos(skip r"[ \t\n\f\r]+")]
#[logos(error = ParserError)]
pub enum Token {
    /// A colon `:`
    #[token(":")]
    Colon,

    /// An open bracket `[`
    #[token("[")]
    OpenBracket,

    /// A close bracket `]`
    #[token("]")]
    CloseBracket,

    /// An open parenthesis `(`
    #[regex(r"\([ \t\n\f]*")]
    OpenParen,

    /// A close parenthesis `)`
    #[regex(r"\)[ \t\n\f]*")]
    CloseParen,

    // PDDL Keywords
    /// The `define` keyword
    #[token("define", ignore(ascii_case))]
    Define,

    /// The `problem` keyword
    #[token("problem", ignore(ascii_case))]
    Problem,

    /// The `:objects` keyword
    #[token(":objects", ignore(ascii_case))]
    Objects,

    /// The `domain` keyword (without the colon, used in the domain file to define the domain)
    #[token("domain", ignore(ascii_case))]
    Domain,

    /// The `:domain` keyword (with the colon, used in the problem file to specify the domain)
    #[token(":domain", ignore(ascii_case))]
    ProblemDomain,

    /// The `:requirements` keyword
    #[token(":requirements", ignore(ascii_case))]
    Requirements,

    /// The `:types` keyword
    #[token(":types", ignore(ascii_case))]
    Types,

    /// The `:constants` keyword
    #[token(":constants", ignore(ascii_case))]
    Constants,

    /// The `:predicates` keyword
    #[token(":predicates", ignore(ascii_case))]
    Predicates,

    /// The `:functions` keyword
    #[token(":functions", ignore(ascii_case))]
    Functions,

    /// The `:action` keyword
    #[token(":action", ignore(ascii_case))]
    Action,

    /// The `:durative-action` keyword
    #[token(":durative-action", ignore(ascii_case))]
    DurativeAction,

    /// The `:parameters` keyword
    #[token(":parameters", ignore(ascii_case))]
    Parameters,

    /// The `:duration` keyword
    #[token(":duration", ignore(ascii_case))]
    Duration,

    /// The `:precondition` keyword
    #[token(":precondition", ignore(ascii_case))]
    Precondition,

    /// The `:condition` keyword
    #[token(":condition", ignore(ascii_case))]
    Condition,

    /// The `:effect` keyword
    #[token(":effect", ignore(ascii_case))]
    Effect,

    /// The `:init` keyword
    #[token(":init", ignore(ascii_case))]
    Init,

    /// The `:goal` keyword
    #[token(":goal", ignore(ascii_case))]
    Goal,

    /// The `and` keyword
    #[token("and", ignore(ascii_case))]
    And,

    /// The `not` keyword
    #[token("not", ignore(ascii_case))]
    Not,

    /// The `either` keyword
    #[token("either", ignore(ascii_case))]
    Either,

    /// The `assign` keyword
    #[token("assign", ignore(ascii_case))]
    Assign,

    /// The `scale-up` keyword
    #[token("scale-up", ignore(ascii_case))]
    ScaleUp,

    /// The `scale-down` keyword
    #[token("scale-down", ignore(ascii_case))]
    ScaleDown,

    /// The `increase` keyword
    #[token("increase", ignore(ascii_case))]
    Increase,

    /// The `decrease` keyword
    #[token("decrease", ignore(ascii_case))]
    Decrease,

    /// The `forall` keyword
    #[token("forall", ignore(ascii_case))]
    Forall,

    /// The `at` keyword
    #[token("at", ignore(ascii_case))]
    At,

    /// The `over` keyword
    #[token("over", ignore(ascii_case))]
    Over,

    /// The `all` keyword
    #[token("all", ignore(ascii_case))]
    All,

    /// The `start` keyword
    #[token("start", ignore(ascii_case))]
    Start,

    /// The `end` keyword
    #[token("end", ignore(ascii_case))]
    End,

    /// A number (positive or negative, e.g. `1` or `-1`)
    #[regex(r"-?[0-9]+", |lex| lex.slice().parse())]
    Integer(i64),

    /// A floating point number (e.g. `1.0`)
    #[regex(r"[0-9]+\.[0-9]+", |lex| lex.slice().parse())]
    Float(f64),

    // Math operators
    /// The `+` operator
    #[token("+")]
    Plus,

    /// The `*` operator
    #[token("*")]
    Times,

    /// The `-` operator
    #[token("/")]
    Divide,

    /// The `=` operator
    #[token("=")]
    Equal,

    /// The `:strips` requirement (PDDL 1)
    #[token(":strips", ignore(ascii_case))]
    Strips,

    /// The `:typing` requirement (PDDL 1)
    #[token(":typing", ignore(ascii_case))]
    Typing,

    /// The `:negative-preconditions` requirement (PDDL 1)
    #[token(":disjunctive-preconditions", ignore(ascii_case))]
    DisjunctivePreconditions,

    /// The `:disjunctive-preconditions` requirement (PDDL 1)
    #[token(":equality", ignore(ascii_case))]
    Equality,

    /// The `:existential-preconditions` requirement (PDDL 1)
    #[token(":existential-preconditions", ignore(ascii_case))]
    ExistentialPreconditions,

    /// The `:universal-preconditions` requirement (PDDL 1)
    #[token(":universal-preconditions", ignore(ascii_case))]
    UniversalPreconditions,

    /// The `:quantified-preconditions` requirement (PDDL 1)
    #[token(":quantified-preconditions", ignore(ascii_case))]
    QuantifiedPreconditions,

    /// The `:conditional-effects` requirement (PDDL 1)
    #[token(":conditional-effects", ignore(ascii_case))]
    ConditionalEffects,

    /// The `:action-expansions` requirement (PDDL 1)
    #[token(":action-expansions", ignore(ascii_case))]
    ActionExpansions,

    /// The `:foreach-expansions` requirement (PDDL 1)
    #[token(":foreach-expansions", ignore(ascii_case))]
    ForeachExpansions,

    /// The `:dag-expansions` requirement (PDDL 1)
    #[token(":dag-expansions", ignore(ascii_case))]
    DagExpansions,

    /// The `:domain-axioms` requirement (PDDL 1)
    #[token(":domain-axioms", ignore(ascii_case))]
    DomainAxioms,

    /// The `:subgoals-through-axioms` requirement (PDDL 1)
    #[token(":subgoals-through-axioms", ignore(ascii_case))]
    SubgoalsThroughAxioms,

    /// The `:safety-constraints` requirement (PDDL 1)
    #[token(":safety-constraints", ignore(ascii_case))]
    SafetyConstraints,

    /// The `:expression-evaluation` requirement (PDDL 1)
    #[token(":expression-evaluation", ignore(ascii_case))]
    ExpressionEvaluation,

    /// The `:fluents` requirement (PDDL 1)
    #[token(":fluents", ignore(ascii_case))]
    Fluents,

    /// The `:open-world` requirement (PDDL 1)
    #[token(":open-world", ignore(ascii_case))]
    OpenWorld,

    /// The `:true-negation` requirement (PDDL 1)
    #[token(":true-negation", ignore(ascii_case))]
    TrueNegation,

    /// The `:adl` requirement (PDDL 1)
    #[token(":adl", ignore(ascii_case))]
    Adl,

    /// The `:ucpop` requirement (PDDL 1)
    #[token(":ucpop", ignore(ascii_case))]
    Ucpop,

    // PDDL 2.1
    /// The `:numeric-fluents` requirement (PDDL 2.1)
    #[token(":numeric-fluents", ignore(ascii_case))]
    NumericFluents,

    /// The `:durative-actions` requirement (PDDL 2.1)
    #[token(":durative-actions", ignore(ascii_case))]
    DurativeActions,

    /// The `:durative-inequalities` (or, as a typo, `:duration-inequalities`) requirement (PDDL 2.1)
    #[regex(r":durative-inequalities", ignore(ascii_case))]
    #[regex(r":duration-inequalities", ignore(ascii_case))]
    DurativeInequalities,

    /// The `:continuous-effects` requirement (PDDL 2.1)
    #[token(":continuous-effects", ignore(ascii_case))]
    ContinuousEffects,

    /// The `:negative-preconditions` requirement (PDDL 2.1)
    #[token(":negative-preconditions", ignore(ascii_case))]
    NegativePreconditions,

    // PDDL 2.2
    /// The `:derived-predicates` requirement (PDDL 2.2)
    #[token(":derived-predicates", ignore(ascii_case))]
    DerivedPredicates,

    /// The `:timed-initial-literals` requirement (PDDL 2.2)
    #[token(":timed-initial-literals", ignore(ascii_case))]
    TimedInitialLiterals,

    // PDDL 3
    /// The `:preferences` requirement (PDDL 3)
    #[token(":preferences", ignore(ascii_case))]
    Preferences,

    /// The `:constraints` requirement (PDDL 3)
    #[token(":constraints", ignore(ascii_case))]
    Constraints,

    // PDDL 3.1
    /// The `:action-costs` requirement (PDDL 3.1)
    #[token(":action-costs", ignore(ascii_case))]
    ActionCosts,

    /// The `:goal-utilities` requirement (PDDL 3.1)
    #[token(":goal-utilities", ignore(ascii_case))]
    GoalUtilities,

    // PDDL+
    /// The `:time` requirement (PDDL+)
    #[token(":time", ignore(ascii_case))]
    Time,

    // PDDL Identifier
    /// A PDDL identifier (a sequence of letters, digits, underscores, and hyphens, starting with a letter)
    #[regex(r"[a-zA-Z][a-zA-Z0-9_\-]*", |lex| lex.slice().to_string())]
    Id(String),

    // PDDL Variable
    /// A PDDL variable (a sequence of letters, digits, underscores, and hyphens, starting with a question mark)
    #[regex(r"\?[a-zA-Z][a-zA-Z0-9_\-]*", |lex| lex.slice().to_string())]
    Var(String),

    // Dash
    /// A dash (`-`) character that can represent a minus sign or a hyphen
    #[token("-")]
    Dash,

    // Comments
    /// A comment (a semicolon followed by any characters). The comment is ignored.
    #[regex(r";.*", logos::skip)]
    Comment,

    // Packages
    /// A package declaration (a sequence of characters enclosed in parentheses, starting with `in-package`). The package name is ignored.
    #[regex(r#"\(\s*in-package\s+("[^"]*"|[^)\s]*)\)"#, logos::skip)]
    Package,
}

/// A stream of tokens. This is a wrapper around a [`logos::Lexer`]. It implements [`Clone`], so it can be cloned and used to peek ahead. It also implements [`Iterator`], so it can be used to iterate over the tokens.
#[derive(Debug)]
pub struct TokenStream<'a> {
    lexer: logos::Lexer<'a, Token>,
}

impl Clone for TokenStream<'_> {
    fn clone(&self) -> Self {
        Self {
            lexer: self.lexer.clone(),
        }
    }
}

impl<'a> TokenStream<'a> {
    /// Creates a new token stream from the given input string. The input string is not copied, so it must outlive the token stream.
    pub fn new(input: &'a str) -> Self {
        Self {
            lexer: Token::lexer(input),
        }
    }

    /// Returns the remaining input string.
    pub fn len(&self) -> usize {
        self.lexer.source().len() - self.lexer.span().end
    }

    /// Returns the number of remaining tokens in the stream.
    pub fn count(&self) -> usize {
        self.lexer.clone().spanned().count()
    }

    /// Returns `true` if the token stream is empty.
    pub fn is_empty(&self) -> bool {
        self.count() == 0
    }

    /// Returns the next token in the stream, or `None` if the stream is empty.
    pub fn peek(&self) -> Option<(Result<Token, ParserError>, &'a str)> {
        let mut iter = self.lexer.clone().spanned();
        iter.next().map(|(t, span)| (t, &self.lexer.source()[span]))
    }

    /// Returns the next `n` tokens in the stream. If there are fewer than `n` tokens left, returns the remaining tokens. If the stream is empty, returns `None`.
    pub fn peek_n(&self, n: usize) -> Option<Vec<(Result<Token, ParserError>, String)>> {
        let mut iter = self.lexer.clone().spanned();
        let mut tokens = Vec::new();
        for _ in 0..n {
            match iter.next() {
                Some((t, span)) => tokens.push((t, self.lexer.source()[span].to_string())),
                None => return if tokens.is_empty() { None } else { Some(tokens) },
            }
        }
        Some(tokens)
    }

    /// Skips the next token in the stream.
    pub fn advance(mut self) -> Self {
        self.lexer.next();
        self
    }

    /// Returns the span of the current token.
    pub fn span(&self) -> Range<usize> {
        self.lexer.span()
    }
}

impl<'a> nom::Parser<TokenStream<'a>, &'a str, ParserError> for Token {
    fn parse(&mut self, input: TokenStream<'a>) -> nom::IResult<TokenStream<'a>, &'a str, ParserError> {
        match input.peek() {
            Some((Ok(t), s)) if t == *self => Ok((input.advance(), s)),
            _ => Err(nom::Err::Error(ParserError::ExpectedToken(
                self.clone(),
                input.span(),
                input.peek_n(30),
            ))),
        }
    }
}

impl ToString for TokenStream<'_> {
    fn to_string(&self) -> String {
        self.lexer.source().to_string()
    }
}

impl<'a> From<&'a str> for TokenStream<'a> {
    fn from(s: &'a str) -> Self {
        Self::new(s)
    }
}

impl<'a> From<&'a String> for TokenStream<'a> {
    fn from(s: &'a String) -> Self {
        Self::new(s)
    }
}

impl InputLength for TokenStream<'_> {
    fn input_len(&self) -> usize {
        self.len()
    }
}