1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[derive(Debug, Clone, PartialEq)]
7pub struct PythonRoot {
8 pub program: Program,
10 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
12 pub span: Range<usize>,
13}
14
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[derive(Debug, Clone, PartialEq)]
18pub struct Program {
19 pub statements: Vec<Statement>,
21}
22
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[derive(Debug, Clone, PartialEq)]
26pub enum Statement {
27 FunctionDef {
29 decorators: Vec<Expression>,
31 name: String,
33 parameters: Vec<Parameter>,
35 return_type: Option<Type>,
37 body: Vec<Statement>,
39 },
40 AsyncFunctionDef {
42 decorators: Vec<Expression>,
44 name: String,
46 parameters: Vec<Parameter>,
48 return_type: Option<Type>,
50 body: Vec<Statement>,
52 },
53 ClassDef {
55 decorators: Vec<Expression>,
57 name: String,
59 bases: Vec<Expression>,
61 body: Vec<Statement>,
63 },
64 Assignment {
66 target: Expression,
68 value: Expression,
70 },
71 AugmentedAssignment {
73 target: Expression,
75 operator: AugmentedOperator,
77 value: Expression,
79 },
80 Expression(Expression),
82 Return(Option<Expression>),
84 If {
86 test: Expression,
88 body: Vec<Statement>,
90 orelse: Vec<Statement>,
92 },
93 For {
95 target: Expression,
97 iter: Expression,
99 body: Vec<Statement>,
101 orelse: Vec<Statement>,
103 },
104 AsyncFor {
106 target: Expression,
108 iter: Expression,
110 body: Vec<Statement>,
112 orelse: Vec<Statement>,
114 },
115 While {
117 test: Expression,
119 body: Vec<Statement>,
121 orelse: Vec<Statement>,
123 },
124 Break,
126 Continue,
128 Pass,
130 Import {
132 names: Vec<ImportName>,
134 },
135 ImportFrom {
137 module: Option<String>,
139 names: Vec<ImportName>,
141 },
142 Global {
144 names: Vec<String>,
146 },
147 Nonlocal {
149 names: Vec<String>,
151 },
152 Try {
154 body: Vec<Statement>,
156 handlers: Vec<ExceptHandler>,
158 orelse: Vec<Statement>,
160 finalbody: Vec<Statement>,
162 },
163 Raise {
165 exc: Option<Expression>,
167 cause: Option<Expression>,
169 },
170 With {
172 items: Vec<WithItem>,
174 body: Vec<Statement>,
176 },
177 AsyncWith {
179 items: Vec<WithItem>,
181 body: Vec<Statement>,
183 },
184 Assert {
186 test: Expression,
188 msg: Option<Expression>,
190 },
191 Match {
193 subject: Expression,
195 cases: Vec<MatchCase>,
197 },
198 Delete {
200 targets: Vec<Expression>,
202 },
203}
204
205#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
207#[derive(Debug, Clone, PartialEq)]
208pub struct MatchCase {
209 pub pattern: Pattern,
211 pub guard: Option<Expression>,
213 pub body: Vec<Statement>,
215}
216
217#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
219#[derive(Debug, Clone, PartialEq)]
220pub enum Pattern {
221 Value(Expression),
223 Wildcard,
225 As {
227 pattern: Option<Box<Pattern>>,
229 name: String,
231 },
232 Sequence(Vec<Pattern>),
234 Mapping {
236 keys: Vec<Expression>,
238 patterns: Vec<Pattern>,
240 },
241 Class {
243 cls: Expression,
245 patterns: Vec<Pattern>,
247 keywords: Vec<String>,
249 keyword_patterns: Vec<Pattern>,
251 },
252 Or(Vec<Pattern>),
254}
255
256#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
258#[derive(Debug, Clone, PartialEq)]
259pub enum Expression {
260 Literal(Literal),
262 Name(String),
264 BinaryOp {
266 left: Box<Expression>,
268 operator: BinaryOperator,
270 right: Box<Expression>,
272 },
273 UnaryOp {
275 operator: UnaryOperator,
277 operand: Box<Expression>,
279 },
280 BoolOp {
282 operator: BoolOperator,
284 values: Vec<Expression>,
286 },
287 Compare {
289 left: Box<Expression>,
291 ops: Vec<CompareOperator>,
293 comparators: Vec<Expression>,
295 },
296 Call {
298 func: Box<Expression>,
300 args: Vec<Expression>,
302 keywords: Vec<Keyword>,
304 },
305 Attribute {
307 value: Box<Expression>,
309 attr: String,
311 },
312 Subscript {
314 value: Box<Expression>,
316 slice: Box<Expression>,
318 },
319 List {
321 elts: Vec<Expression>,
323 },
324 Tuple {
326 elts: Vec<Expression>,
328 },
329 Slice {
331 lower: Option<Box<Expression>>,
333 upper: Option<Box<Expression>>,
335 step: Option<Box<Expression>>,
337 },
338 Dict {
340 keys: Vec<Option<Expression>>,
342 values: Vec<Expression>,
344 },
345 Set {
347 elts: Vec<Expression>,
349 },
350 ListComp {
352 elt: Box<Expression>,
354 generators: Vec<Comprehension>,
356 },
357 DictComp {
359 key: Box<Expression>,
361 value: Box<Expression>,
363 generators: Vec<Comprehension>,
365 },
366 SetComp {
368 elt: Box<Expression>,
370 generators: Vec<Comprehension>,
372 },
373 GeneratorExp {
375 elt: Box<Expression>,
377 generators: Vec<Comprehension>,
379 },
380 Lambda {
382 args: Vec<Parameter>,
384 body: Box<Expression>,
386 },
387 IfExp {
389 test: Box<Expression>,
391 body: Box<Expression>,
393 orelse: Box<Expression>,
395 },
396 JoinedStr {
398 values: Vec<Expression>,
400 },
401 FormattedValue {
403 value: Box<Expression>,
405 conversion: usize,
407 format_spec: Option<Box<Expression>>,
409 },
410 Yield(Option<Box<Expression>>),
412 YieldFrom(Box<Expression>),
414 Await(Box<Expression>),
416 Starred {
418 value: Box<Expression>,
420 is_double: bool,
422 },
423}
424
425#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
427#[derive(Debug, Clone, PartialEq)]
428pub enum Literal {
429 Integer(i64),
431 Float(f64),
433 String(String),
435 Bytes(Vec<u8>),
437 Boolean(bool),
439 None,
441}
442
443#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
445#[derive(Debug, Clone, PartialEq)]
446pub enum AugmentedOperator {
447 Add,
449 Sub,
451 Mult,
453 Div,
455 FloorDiv,
457 Mod,
459 Pow,
461 LShift,
463 RShift,
465 BitOr,
467 BitXor,
469 BitAnd,
471}
472
473#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
475#[derive(Debug, Clone, PartialEq)]
476pub enum BinaryOperator {
477 Add,
479 Sub,
481 Mult,
483 Div,
485 FloorDiv,
487 Mod,
489 Pow,
491 LShift,
493 RShift,
495 BitOr,
497 BitXor,
499 BitAnd,
501}
502
503#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
505#[derive(Debug, Clone, PartialEq)]
506pub enum UnaryOperator {
507 Invert,
509 Not,
511 UAdd,
513 USub,
515}
516
517#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
519#[derive(Debug, Clone, PartialEq)]
520pub enum BoolOperator {
521 And,
523 Or,
525}
526
527#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
529#[derive(Debug, Clone, PartialEq)]
530pub enum CompareOperator {
531 Eq,
533 NotEq,
535 Lt,
537 LtE,
539 Gt,
541 GtE,
543 Is,
545 IsNot,
547 In,
549 NotIn,
551}
552
553#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
555#[derive(Debug, Clone, PartialEq)]
556pub struct Parameter {
557 pub name: String,
559 pub annotation: Option<Type>,
561 pub default: Option<Expression>,
563 pub is_vararg: bool,
565 pub is_kwarg: bool,
567}
568
569#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
571#[derive(Debug, Clone, PartialEq)]
572pub enum Type {
573 Name(String),
575 Generic {
577 name: String,
579 args: Vec<Type>,
581 },
582 Union(Vec<Type>),
584 Optional(Box<Type>),
586}
587
588#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
590#[derive(Debug, Clone, PartialEq)]
591pub struct Keyword {
592 pub arg: Option<String>,
594 pub value: Expression,
596}
597
598#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
600#[derive(Debug, Clone, PartialEq)]
601pub struct Comprehension {
602 pub target: Expression,
604 pub iter: Expression,
606 pub ifs: Vec<Expression>,
608 pub is_async: bool,
610}
611
612#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
614#[derive(Debug, Clone, PartialEq)]
615pub struct ImportName {
616 pub name: String,
618 pub asname: Option<String>,
620}
621
622#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
624#[derive(Debug, Clone, PartialEq)]
625pub struct ExceptHandler {
626 pub type_: Option<Expression>,
628 pub name: Option<String>,
630 pub body: Vec<Statement>,
632}
633
634#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
636#[derive(Debug, Clone, PartialEq)]
637pub struct WithItem {
638 pub context_expr: Expression,
640 pub optional_vars: Option<Expression>,
642}
643
644impl Program {
645 pub fn new() -> Self {
647 Self { statements: Vec::new() }
648 }
649
650 pub fn add_statement(&mut self, statement: Statement) {
652 self.statements.push(statement)
653 }
654}
655
656impl Default for Program {
657 fn default() -> Self {
658 Self::new()
659 }
660}
661
662impl Expression {
663 pub fn name(name: impl Into<String>) -> Self {
665 Self::Name(name.into())
666 }
667
668 pub fn string(value: impl Into<String>) -> Self {
670 Self::Literal(Literal::String(value.into()))
671 }
672
673 pub fn integer(value: i64) -> Self {
675 Self::Literal(Literal::Integer(value))
676 }
677
678 pub fn float(value: f64) -> Self {
680 Self::Literal(Literal::Float(value))
681 }
682
683 pub fn boolean(value: bool) -> Self {
685 Self::Literal(Literal::Boolean(value))
686 }
687
688 pub fn none() -> Self {
690 Self::Literal(Literal::None)
691 }
692}
693
694impl Statement {
695 pub fn function_def(name: impl Into<String>, parameters: Vec<Parameter>, return_type: Option<Type>, body: Vec<Statement>) -> Self {
697 Self::FunctionDef { decorators: Vec::new(), name: name.into(), parameters, return_type, body }
698 }
699
700 pub fn assignment(target: Expression, value: Expression) -> Self {
702 Self::Assignment { target, value }
703 }
704
705 pub fn expression(expr: Expression) -> Self {
707 Self::Expression(expr)
708 }
709
710 pub fn return_stmt(value: Option<Expression>) -> Self {
712 Self::Return(value)
713 }
714}