fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 168 lines 3.9 kB view raw
1import type { AnalysisContext } from '@hey-api/codegen-core'; 2 3import { py } from '../../ts-python'; 4import type { MaybePyDsl } from '../base'; 5import { PyDsl } from '../base'; 6 7export type PyBinaryOperator = 8 | '+' 9 | '-' 10 | '*' 11 | '/' 12 | '//' 13 | '%' 14 | '**' 15 | '==' 16 | '!=' 17 | '>' 18 | '>=' 19 | '<' 20 | '<=' 21 | 'is' 22 | 'is not' 23 | 'in' 24 | 'not in' 25 | 'and' 26 | 'or'; 27 28const Mixed = PyDsl<py.BinaryExpression>; 29 30export class BinaryPyDsl extends Mixed { 31 readonly '~dsl' = 'BinaryPyDsl'; 32 33 protected _left?: MaybePyDsl<py.Expression>; 34 protected _op?: PyBinaryOperator; 35 protected _right?: MaybePyDsl<py.Expression>; 36 37 constructor( 38 left: MaybePyDsl<py.Expression>, 39 op: PyBinaryOperator, 40 right: MaybePyDsl<py.Expression>, 41 ) { 42 super(); 43 this._left = left; 44 this._op = op; 45 this._right = right; 46 } 47 48 override analyze(ctx: AnalysisContext): void { 49 super.analyze(ctx); 50 ctx.analyze(this._left); 51 ctx.analyze(this._right); 52 } 53 54 /** Returns true when all required builder calls are present. */ 55 get isValid(): boolean { 56 return this.missingRequiredCalls().length === 0; 57 } 58 59 and(right: MaybePyDsl<py.Expression>): this { 60 return this.opAndExpr('and', right); 61 } 62 63 div(right: MaybePyDsl<py.Expression>): this { 64 return this.opAndExpr('/', right); 65 } 66 67 eq(right: MaybePyDsl<py.Expression>): this { 68 return this.opAndExpr('==', right); 69 } 70 71 floordiv(right: MaybePyDsl<py.Expression>): this { 72 return this.opAndExpr('//', right); 73 } 74 75 gt(right: MaybePyDsl<py.Expression>): this { 76 return this.opAndExpr('>', right); 77 } 78 79 gte(right: MaybePyDsl<py.Expression>): this { 80 return this.opAndExpr('>=', right); 81 } 82 83 in_(right: MaybePyDsl<py.Expression>): this { 84 return this.opAndExpr('in', right); 85 } 86 87 is(right: MaybePyDsl<py.Expression>): this { 88 return this.opAndExpr('is', right); 89 } 90 91 isNot(right: MaybePyDsl<py.Expression>): this { 92 return this.opAndExpr('is not', right); 93 } 94 95 lt(right: MaybePyDsl<py.Expression>): this { 96 return this.opAndExpr('<', right); 97 } 98 99 lte(right: MaybePyDsl<py.Expression>): this { 100 return this.opAndExpr('<=', right); 101 } 102 103 minus(right: MaybePyDsl<py.Expression>): this { 104 return this.opAndExpr('-', right); 105 } 106 107 mod(right: MaybePyDsl<py.Expression>): this { 108 return this.opAndExpr('%', right); 109 } 110 111 neq(right: MaybePyDsl<py.Expression>): this { 112 return this.opAndExpr('!=', right); 113 } 114 115 notIn(right: MaybePyDsl<py.Expression>): this { 116 return this.opAndExpr('not in', right); 117 } 118 119 or(right: MaybePyDsl<py.Expression>): this { 120 return this.opAndExpr('or', right); 121 } 122 123 plus(right: MaybePyDsl<py.Expression>): this { 124 return this.opAndExpr('+', right); 125 } 126 127 pow(right: MaybePyDsl<py.Expression>): this { 128 return this.opAndExpr('**', right); 129 } 130 131 times(right: MaybePyDsl<py.Expression>): this { 132 return this.opAndExpr('*', right); 133 } 134 135 override toAst(): py.BinaryExpression { 136 this.$validate(); 137 138 return py.factory.createBinaryExpression( 139 this.$node(this._left!) as py.Expression, 140 this._op!, 141 this.$node(this._right!) as py.Expression, 142 ); 143 } 144 145 $validate(): asserts this is this & { 146 _left: MaybePyDsl<py.Expression>; 147 _op: PyBinaryOperator; 148 _right: MaybePyDsl<py.Expression>; 149 } { 150 const missing = this.missingRequiredCalls(); 151 if (missing.length === 0) return; 152 throw new Error(`Binary expression missing ${missing.join(' and ')}`); 153 } 154 155 private missingRequiredCalls(): ReadonlyArray<string> { 156 const missing: Array<string> = []; 157 if (!this._left) missing.push('left operand'); 158 if (!this._op) missing.push('operator'); 159 if (!this._right) missing.push('right operand'); 160 return missing; 161 } 162 163 private opAndExpr(op: PyBinaryOperator, right: MaybePyDsl<py.Expression>): this { 164 this._right = right; 165 this._op = op; 166 return this; 167 } 168}