@forgecharts/sdk 1.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +50 -0
- package/src/__tests__/backwardCompatibility.test.ts +191 -0
- package/src/__tests__/candleInvariant.test.ts +500 -0
- package/src/__tests__/public-api-surface.ts +76 -0
- package/src/__tests__/timeframeBoundary.test.ts +583 -0
- package/src/api/DrawingManager.ts +188 -0
- package/src/api/EventBus.ts +53 -0
- package/src/api/IndicatorDAG.ts +389 -0
- package/src/api/IndicatorRegistry.ts +47 -0
- package/src/api/LayoutManager.ts +72 -0
- package/src/api/PaneManager.ts +129 -0
- package/src/api/ReferenceAPI.ts +195 -0
- package/src/api/TChart.ts +881 -0
- package/src/api/createChart.ts +43 -0
- package/src/api/drawing tools/fib gann menu/fibRetracement.ts +27 -0
- package/src/api/drawing tools/lines menu/crossLine.ts +21 -0
- package/src/api/drawing tools/lines menu/disjointChannel.ts +74 -0
- package/src/api/drawing tools/lines menu/extendedLine.ts +22 -0
- package/src/api/drawing tools/lines menu/flatTopBottom.ts +45 -0
- package/src/api/drawing tools/lines menu/horizontal.ts +24 -0
- package/src/api/drawing tools/lines menu/horizontalRay.ts +25 -0
- package/src/api/drawing tools/lines menu/infoLine.ts +127 -0
- package/src/api/drawing tools/lines menu/insidePitchfork.ts +21 -0
- package/src/api/drawing tools/lines menu/modifiedSchiffPitchfork.ts +18 -0
- package/src/api/drawing tools/lines menu/parallelChannel.ts +47 -0
- package/src/api/drawing tools/lines menu/pitchfork.ts +15 -0
- package/src/api/drawing tools/lines menu/ray.ts +28 -0
- package/src/api/drawing tools/lines menu/regressionTrend.ts +157 -0
- package/src/api/drawing tools/lines menu/schiffPitchfork.ts +18 -0
- package/src/api/drawing tools/lines menu/trendAngle.ts +64 -0
- package/src/api/drawing tools/lines menu/trendline.ts +16 -0
- package/src/api/drawing tools/lines menu/vertical.ts +16 -0
- package/src/api/drawing tools/pointers menu/crosshair.ts +17 -0
- package/src/api/drawing tools/pointers menu/cursor.ts +16 -0
- package/src/api/drawing tools/pointers menu/demonstration.ts +35 -0
- package/src/api/drawing tools/pointers menu/dot.ts +26 -0
- package/src/api/drawing tools/shapes menu/rectangle.ts +24 -0
- package/src/api/drawing tools/shapes menu/text.ts +30 -0
- package/src/api/drawingUtils.ts +82 -0
- package/src/core/CanvasLayer.ts +77 -0
- package/src/core/Chart.ts +917 -0
- package/src/core/CoordTransform.ts +282 -0
- package/src/core/Crosshair.ts +207 -0
- package/src/core/IndicatorEngine.ts +216 -0
- package/src/core/InteractionManager.ts +899 -0
- package/src/core/PriceScale.ts +133 -0
- package/src/core/Series.ts +132 -0
- package/src/core/TimeScale.ts +175 -0
- package/src/datafeed/DatafeedConnector.ts +300 -0
- package/src/engine/CandleEngine.ts +458 -0
- package/src/engine/__tests__/CandleEngine.test.ts +402 -0
- package/src/engine/candleInvariants.ts +172 -0
- package/src/engine/mergeUtils.ts +93 -0
- package/src/engine/timeframeUtils.ts +118 -0
- package/src/index.ts +190 -0
- package/src/internal.ts +41 -0
- package/src/licensing/ChartRuntimeResolver.ts +380 -0
- package/src/licensing/LicenseManager.ts +131 -0
- package/src/licensing/__tests__/ChartRuntimeResolver.test.ts +207 -0
- package/src/licensing/__tests__/LicenseManager.test.ts +180 -0
- package/src/licensing/licenseTypes.ts +19 -0
- package/src/pine/PineCompiler.ts +68 -0
- package/src/pine/diagnostics.ts +30 -0
- package/src/pine/index.ts +7 -0
- package/src/pine/pine-ast.ts +163 -0
- package/src/pine/pine-lexer.ts +265 -0
- package/src/pine/pine-parser.ts +439 -0
- package/src/pine/pine-transpiler.ts +301 -0
- package/src/pixi/LayerName.ts +35 -0
- package/src/pixi/PixiCandlestickRenderer.ts +125 -0
- package/src/pixi/PixiChart.ts +425 -0
- package/src/pixi/PixiCrosshairRenderer.ts +134 -0
- package/src/pixi/PixiDrawingRenderer.ts +121 -0
- package/src/pixi/PixiGridRenderer.ts +136 -0
- package/src/pixi/PixiLayerManager.ts +102 -0
- package/src/renderers/CandlestickRenderer.ts +130 -0
- package/src/renderers/HistogramRenderer.ts +63 -0
- package/src/renderers/LineRenderer.ts +77 -0
- package/src/theme/colors.ts +21 -0
- package/src/tools/barDivergenceCheck.ts +305 -0
- package/src/trading/TradingOverlayStore.ts +161 -0
- package/src/trading/UnmanagedIngestion.ts +156 -0
- package/src/trading/__tests__/ManagedTradingController.test.ts +338 -0
- package/src/trading/__tests__/TradingOverlayStore.test.ts +323 -0
- package/src/trading/__tests__/UnmanagedIngestion.test.ts +205 -0
- package/src/trading/managed/ManagedTradingController.ts +292 -0
- package/src/trading/managed/managedCapabilities.ts +98 -0
- package/src/trading/managed/managedTypes.ts +151 -0
- package/src/trading/tradingTypes.ts +135 -0
- package/src/tscript/TScriptIndicator.ts +54 -0
- package/src/tscript/ast.ts +105 -0
- package/src/tscript/lexer.ts +190 -0
- package/src/tscript/parser.ts +334 -0
- package/src/tscript/runtime.ts +525 -0
- package/src/tscript/series.ts +84 -0
- package/src/types/IChart.ts +56 -0
- package/src/types/IRenderer.ts +16 -0
- package/src/types/ISeries.ts +30 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +15 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pine Script Lexer — tokenises a Pine v5 source string.
|
|
3
|
+
*
|
|
4
|
+
* Differences vs TScript lexer:
|
|
5
|
+
* - `//@version=N` comment is parsed for the version number
|
|
6
|
+
* - `color.red`, `ta.sma`, `math.abs` — namespace.member chains
|
|
7
|
+
* - `:=` reassignment operator
|
|
8
|
+
* - Indentation-based block structure (INDENT / DEDENT tokens)
|
|
9
|
+
* - `var` / `varip` keywords
|
|
10
|
+
* - `=>` (function body arrow, for single-line user functions — recognised
|
|
11
|
+
* but not fully transpiled; emits an diagnostic)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export type PineTok =
|
|
15
|
+
| 'NUMBER' | 'STRING' | 'BOOL' | 'COLOR'
|
|
16
|
+
| 'IDENT'
|
|
17
|
+
| 'PLUS' | 'MINUS' | 'STAR' | 'SLASH' | 'PERCENT'
|
|
18
|
+
| 'LT' | 'GT' | 'LTE' | 'GTE' | 'EQEQ' | 'NEQ'
|
|
19
|
+
| 'QMARK' | 'COLON' | 'ARROW' // ?: =>
|
|
20
|
+
| 'ASSIGN' | 'REASSIGN' // = :=
|
|
21
|
+
| 'DOT'
|
|
22
|
+
| 'LPAREN' | 'RPAREN' | 'LBRACKET' | 'RBRACKET'
|
|
23
|
+
| 'COMMA'
|
|
24
|
+
| 'INDENT' | 'DEDENT' | 'NEWLINE'
|
|
25
|
+
| 'EOF';
|
|
26
|
+
|
|
27
|
+
export type PineToken = {
|
|
28
|
+
kind: PineTok;
|
|
29
|
+
value: string;
|
|
30
|
+
line: number;
|
|
31
|
+
col: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export class PineLexer {
|
|
35
|
+
private _src: string;
|
|
36
|
+
private _pos: number = 0;
|
|
37
|
+
private _line: number = 1;
|
|
38
|
+
private _col: number = 1;
|
|
39
|
+
|
|
40
|
+
/** Version extracted from `//@version=N` */
|
|
41
|
+
version = 5;
|
|
42
|
+
|
|
43
|
+
constructor(src: string) {
|
|
44
|
+
this._src = src.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
tokenize(): PineToken[] {
|
|
48
|
+
// First pass: raw tokens (with raw NEWLINEs and spaces preserved)
|
|
49
|
+
const raw = this._rawTokenize();
|
|
50
|
+
// Second pass: inject INDENT / DEDENT for indented blocks
|
|
51
|
+
return this._injectIndents(raw);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ─── Raw tokenisation ────────────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
private _rawTokenize(): PineToken[] {
|
|
57
|
+
const tokens: PineToken[] = [];
|
|
58
|
+
|
|
59
|
+
while (this._pos < this._src.length) {
|
|
60
|
+
const ch = this._src[this._pos]!;
|
|
61
|
+
|
|
62
|
+
// Handle newline
|
|
63
|
+
if (ch === '\n') {
|
|
64
|
+
tokens.push(this._make('NEWLINE', '\n'));
|
|
65
|
+
this._pos++; this._line++; this._col = 1;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Skip horizontal whitespace (not newlines — needed for indent tracking)
|
|
70
|
+
if (ch === ' ' || ch === '\t') {
|
|
71
|
+
this._pos++; this._col++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Line comment
|
|
76
|
+
if (ch === '/' && this._peek(1) === '/') {
|
|
77
|
+
// Check for version annotation
|
|
78
|
+
const commentStart = this._pos;
|
|
79
|
+
while (this._pos < this._src.length && this._src[this._pos] !== '\n') {
|
|
80
|
+
this._pos++; this._col++;
|
|
81
|
+
}
|
|
82
|
+
const comment = this._src.slice(commentStart, this._pos);
|
|
83
|
+
const vMatch = comment.match(/\/\/@version\s*=\s*(\d+)/);
|
|
84
|
+
if (vMatch) this.version = parseInt(vMatch[1]!, 10);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Numbers
|
|
89
|
+
if ((ch >= '0' && ch <= '9') || (ch === '.' && this._isDigit(this._peek(1)))) {
|
|
90
|
+
tokens.push(this._readNumber());
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Strings
|
|
95
|
+
if (ch === '"' || ch === "'") {
|
|
96
|
+
tokens.push(this._readString(ch));
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Identifiers / keywords
|
|
101
|
+
if (this._isAlpha(ch)) {
|
|
102
|
+
const tok = this._readIdent();
|
|
103
|
+
tokens.push(tok);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Two-char operators
|
|
108
|
+
const two = this._src.slice(this._pos, this._pos + 2);
|
|
109
|
+
if (two === '<=') { tokens.push(this._make('LTE', two)); this._adv(2); continue; }
|
|
110
|
+
if (two === '>=') { tokens.push(this._make('GTE', two)); this._adv(2); continue; }
|
|
111
|
+
if (two === '==') { tokens.push(this._make('EQEQ', two)); this._adv(2); continue; }
|
|
112
|
+
if (two === '!=') { tokens.push(this._make('NEQ', two)); this._adv(2); continue; }
|
|
113
|
+
if (two === ':=') { tokens.push(this._make('REASSIGN', two)); this._adv(2); continue; }
|
|
114
|
+
if (two === '=>') { tokens.push(this._make('ARROW', two)); this._adv(2); continue; }
|
|
115
|
+
|
|
116
|
+
// Single-char operators
|
|
117
|
+
const singleOps: Record<string, PineTok> = {
|
|
118
|
+
'+': 'PLUS', '-': 'MINUS', '*': 'STAR', '/': 'SLASH', '%': 'PERCENT',
|
|
119
|
+
'<': 'LT', '>': 'GT',
|
|
120
|
+
'?': 'QMARK', ':': 'COLON',
|
|
121
|
+
'(': 'LPAREN', ')': 'RPAREN', '[': 'LBRACKET', ']': 'RBRACKET',
|
|
122
|
+
',': 'COMMA', '.': 'DOT',
|
|
123
|
+
'=': 'ASSIGN',
|
|
124
|
+
};
|
|
125
|
+
if (ch in singleOps) {
|
|
126
|
+
tokens.push(this._make(singleOps[ch]!, ch));
|
|
127
|
+
this._pos++; this._col++;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
throw new SyntaxError(`[Pine] Unexpected character '${ch}' at ${this._line}:${this._col}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
tokens.push(this._make('EOF', ''));
|
|
135
|
+
return tokens;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ─── Indent/Dedent injection ─────────────────────────────────────────────────
|
|
139
|
+
|
|
140
|
+
private _injectIndents(raw: PineToken[]): PineToken[] {
|
|
141
|
+
const out: PineToken[] = [];
|
|
142
|
+
const indentStack: number[] = [0];
|
|
143
|
+
let i = 0;
|
|
144
|
+
|
|
145
|
+
while (i < raw.length) {
|
|
146
|
+
const tok = raw[i]!;
|
|
147
|
+
|
|
148
|
+
if (tok.kind !== 'NEWLINE') {
|
|
149
|
+
out.push(tok);
|
|
150
|
+
i++;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// After a NEWLINE, measure the indentation of the next non-empty line
|
|
155
|
+
out.push(tok); // keep the NEWLINE token
|
|
156
|
+
i++;
|
|
157
|
+
|
|
158
|
+
// Count leading spaces of the next real line
|
|
159
|
+
let spaces = 0;
|
|
160
|
+
let j = i;
|
|
161
|
+
while (j < raw.length && (raw[j]!.kind === 'NEWLINE')) {
|
|
162
|
+
// blank line — skip
|
|
163
|
+
out.push(raw[j]!);
|
|
164
|
+
j++;
|
|
165
|
+
}
|
|
166
|
+
i = j;
|
|
167
|
+
|
|
168
|
+
if (i >= raw.length || raw[i]!.kind === 'EOF') break;
|
|
169
|
+
|
|
170
|
+
// Compute indent width from the column of the next token
|
|
171
|
+
const nextTok = raw[i]!;
|
|
172
|
+
spaces = nextTok.col - 1; // col is 1-based
|
|
173
|
+
|
|
174
|
+
const currentIndent = indentStack[indentStack.length - 1]!;
|
|
175
|
+
|
|
176
|
+
if (spaces > currentIndent) {
|
|
177
|
+
indentStack.push(spaces);
|
|
178
|
+
out.push({ kind: 'INDENT', value: '', line: nextTok.line, col: 1 });
|
|
179
|
+
} else {
|
|
180
|
+
while (spaces < indentStack[indentStack.length - 1]!) {
|
|
181
|
+
indentStack.pop();
|
|
182
|
+
out.push({ kind: 'DEDENT', value: '', line: nextTok.line, col: 1 });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Close any remaining open indents
|
|
188
|
+
while (indentStack.length > 1) {
|
|
189
|
+
indentStack.pop();
|
|
190
|
+
out.push({ kind: 'DEDENT', value: '', line: 0, col: 0 });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
out.push({ kind: 'EOF', value: '', line: this._line, col: this._col });
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
198
|
+
|
|
199
|
+
private _make(kind: PineTok, value: string): PineToken {
|
|
200
|
+
return { kind, value, line: this._line, col: this._col };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private _peek(offset: number): string {
|
|
204
|
+
return this._src[this._pos + offset] ?? '';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private _adv(n = 1): void {
|
|
208
|
+
this._pos += n;
|
|
209
|
+
this._col += n;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private _isDigit(ch: string): boolean {
|
|
213
|
+
return ch >= '0' && ch <= '9';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
private _isAlpha(ch: string): boolean {
|
|
217
|
+
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === '_';
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private _isAlphaNum(ch: string): boolean {
|
|
221
|
+
return this._isAlpha(ch) || this._isDigit(ch);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private _readNumber(): PineToken {
|
|
225
|
+
const start = this._pos;
|
|
226
|
+
const startCol = this._col;
|
|
227
|
+
while (this._pos < this._src.length) {
|
|
228
|
+
const c = this._src[this._pos]!;
|
|
229
|
+
if (this._isDigit(c) || c === '.') { this._pos++; this._col++; }
|
|
230
|
+
else break;
|
|
231
|
+
}
|
|
232
|
+
return { kind: 'NUMBER', value: this._src.slice(start, this._pos), line: this._line, col: startCol };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private _readString(quote: string): PineToken {
|
|
236
|
+
const startCol = this._col;
|
|
237
|
+
this._pos++; this._col++;
|
|
238
|
+
let result = '';
|
|
239
|
+
while (this._pos < this._src.length && this._src[this._pos] !== quote) {
|
|
240
|
+
if (this._src[this._pos] === '\\') {
|
|
241
|
+
this._pos++; this._col++;
|
|
242
|
+
const esc = this._src[this._pos] ?? '';
|
|
243
|
+
result += esc === 'n' ? '\n' : esc === 't' ? '\t' : esc;
|
|
244
|
+
} else {
|
|
245
|
+
result += this._src[this._pos];
|
|
246
|
+
}
|
|
247
|
+
this._pos++; this._col++;
|
|
248
|
+
}
|
|
249
|
+
this._pos++; this._col++;
|
|
250
|
+
return { kind: 'STRING', value: result, line: this._line, col: startCol };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private _readIdent(): PineToken {
|
|
254
|
+
const start = this._pos;
|
|
255
|
+
const startCol = this._col;
|
|
256
|
+
while (this._pos < this._src.length && this._isAlphaNum(this._src[this._pos]!)) {
|
|
257
|
+
this._pos++; this._col++;
|
|
258
|
+
}
|
|
259
|
+
const name = this._src.slice(start, this._pos);
|
|
260
|
+
if (name === 'true' || name === 'false') {
|
|
261
|
+
return { kind: 'BOOL', value: name, line: this._line, col: startCol };
|
|
262
|
+
}
|
|
263
|
+
return { kind: 'IDENT', value: name, line: this._line, col: startCol };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pine Script Parser — recursive-descent, produces a PineProgram AST.
|
|
3
|
+
*
|
|
4
|
+
* Handles the supported Pine v5 subset:
|
|
5
|
+
* indicator(), input.*(), ta.*(), math.*(), plot(), plotshape()
|
|
6
|
+
* var / varip declarations
|
|
7
|
+
* := reassignment
|
|
8
|
+
* if / else (indented blocks)
|
|
9
|
+
* ternary a ? b : c
|
|
10
|
+
* namespace.member and namespace.call(...)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { PineLexer } from './pine-lexer';
|
|
14
|
+
import type { PineToken, PineTok } from './pine-lexer';
|
|
15
|
+
import type {
|
|
16
|
+
PineProgram, PineStmt, PineExpr,
|
|
17
|
+
PineIndicatorDecl, PineVarDecl, PineAssign, PineExprStmt, PineIf,
|
|
18
|
+
PineNumberLit, PineStringLit, PineBoolLit, PineNa,
|
|
19
|
+
PineIdent, PineNsCall, PineCall, PineIndex,
|
|
20
|
+
PineBinary, PineUnary, PineTernary, PineMember,
|
|
21
|
+
Loc,
|
|
22
|
+
} from './pine-ast';
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export class PineParser {
|
|
26
|
+
private _tokens: PineToken[];
|
|
27
|
+
private _pos = 0;
|
|
28
|
+
readonly version: number;
|
|
29
|
+
|
|
30
|
+
constructor(src: string) {
|
|
31
|
+
const lexer = new PineLexer(src);
|
|
32
|
+
this._tokens = lexer.tokenize();
|
|
33
|
+
this.version = lexer.version;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
parse(): PineProgram {
|
|
37
|
+
const stmts: PineStmt[] = [];
|
|
38
|
+
this._skipNL();
|
|
39
|
+
while (!this._check('EOF')) {
|
|
40
|
+
const s = this._stmt();
|
|
41
|
+
if (s !== null) stmts.push(s);
|
|
42
|
+
this._skipNL();
|
|
43
|
+
}
|
|
44
|
+
return { kind: 'PineProgram', version: this.version, stmts };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ─── Statements ─────────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
private _stmt(): PineStmt | null {
|
|
50
|
+
const t = this._cur();
|
|
51
|
+
const loc: Loc = { line: t.line, col: t.col };
|
|
52
|
+
|
|
53
|
+
// var / varip declaration
|
|
54
|
+
if (t.kind === 'IDENT' && (t.value === 'var' || t.value === 'varip')) {
|
|
55
|
+
return this._varDecl();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// if statement
|
|
59
|
+
if (t.kind === 'IDENT' && t.value === 'if') {
|
|
60
|
+
return this._ifStmt();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Assignment: IDENT = expr or IDENT := expr
|
|
64
|
+
// Also handles "IDENT type_hint = expr" e.g. "float x = 1.0" — skip type hints
|
|
65
|
+
if (t.kind === 'IDENT') {
|
|
66
|
+
// Peek ahead skipping possible type-hint token
|
|
67
|
+
const p1 = this._peekKind(1);
|
|
68
|
+
const p2 = this._peekKind(2);
|
|
69
|
+
const isAssign = p1 === 'ASSIGN' || p1 === 'REASSIGN';
|
|
70
|
+
const isTyped = p1 === 'IDENT' && (p2 === 'ASSIGN' || p2 === 'REASSIGN');
|
|
71
|
+
if (isAssign || isTyped) {
|
|
72
|
+
return this._assignStmt();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// indicator() special top-level
|
|
77
|
+
if (t.kind === 'IDENT' && t.value === 'indicator') {
|
|
78
|
+
return this._indicatorDecl();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Expression statement (plot, function calls, etc.)
|
|
82
|
+
const expr = this._expr();
|
|
83
|
+
this._consumeNLorEOF();
|
|
84
|
+
return { kind: 'PineExprStmt', expr, loc };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private _varDecl(): PineVarDecl {
|
|
88
|
+
const tok = this._advance();
|
|
89
|
+
const modifier = tok.value as 'var' | 'varip';
|
|
90
|
+
const loc: Loc = { line: tok.line, col: tok.col };
|
|
91
|
+
|
|
92
|
+
// optional type hint (float, int, bool, string, color, ...)
|
|
93
|
+
let typeHint: string | undefined;
|
|
94
|
+
if (this._check('IDENT') && this._peekKind(1) === 'ASSIGN') {
|
|
95
|
+
// possible: "var float x = ..."
|
|
96
|
+
const maybeType = this._cur().value;
|
|
97
|
+
if (['float','int','bool','string','color','series','simple','const'].includes(maybeType)) {
|
|
98
|
+
typeHint = maybeType;
|
|
99
|
+
this._advance();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const name = this._consume('IDENT', "Expected variable name after 'var'").value;
|
|
104
|
+
this._consume('ASSIGN', "Expected '=' in var declaration");
|
|
105
|
+
const value = this._expr();
|
|
106
|
+
this._consumeNLorEOF();
|
|
107
|
+
return typeHint !== undefined
|
|
108
|
+
? { kind: 'PineVarDecl', modifier, name, typeHint, value, loc }
|
|
109
|
+
: { kind: 'PineVarDecl', modifier, name, value, loc } as PineVarDecl;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private _assignStmt(): PineAssign {
|
|
113
|
+
const nameTok = this._advance();
|
|
114
|
+
const loc: Loc = { line: nameTok.line, col: nameTok.col };
|
|
115
|
+
const name = nameTok.value;
|
|
116
|
+
|
|
117
|
+
// Skip type hint if present: "float x = ..."
|
|
118
|
+
let op: '=' | ':=' = '=';
|
|
119
|
+
if (this._check('IDENT')) {
|
|
120
|
+
// type hint token — consume silently
|
|
121
|
+
this._advance();
|
|
122
|
+
}
|
|
123
|
+
if (this._check('REASSIGN')) {
|
|
124
|
+
op = ':=';
|
|
125
|
+
this._advance();
|
|
126
|
+
} else {
|
|
127
|
+
this._consume('ASSIGN', "Expected '=' or ':='");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const value = this._expr();
|
|
131
|
+
this._consumeNLorEOF();
|
|
132
|
+
return { kind: 'PineAssign', name, op, value, loc };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private _indicatorDecl(): PineIndicatorDecl {
|
|
136
|
+
const tok = this._advance(); // consume 'indicator'
|
|
137
|
+
const loc: Loc = { line: tok.line, col: tok.col };
|
|
138
|
+
this._consume('LPAREN', "Expected '(' after 'indicator'");
|
|
139
|
+
const { args, namedArgs } = this._argList();
|
|
140
|
+
this._consume('RPAREN', "Expected ')' after indicator args");
|
|
141
|
+
this._consumeNLorEOF();
|
|
142
|
+
return { kind: 'PineIndicatorDecl', args, namedArgs, loc };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private _ifStmt(): PineIf {
|
|
146
|
+
const tok = this._advance(); // consume 'if'
|
|
147
|
+
const loc: Loc = { line: tok.line, col: tok.col };
|
|
148
|
+
|
|
149
|
+
const condition = this._expr();
|
|
150
|
+
this._consumeNLorEOF();
|
|
151
|
+
|
|
152
|
+
const then: PineStmt[] = this._indentedBlock();
|
|
153
|
+
const else_: PineStmt[] = [];
|
|
154
|
+
|
|
155
|
+
// optional 'else'
|
|
156
|
+
if (this._check('IDENT') && this._cur().value === 'else') {
|
|
157
|
+
this._advance();
|
|
158
|
+
this._consumeNLorEOF();
|
|
159
|
+
else_.push(...this._indentedBlock());
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return { kind: 'PineIf', condition, then, else_, loc };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private _indentedBlock(): PineStmt[] {
|
|
166
|
+
const stmts: PineStmt[] = [];
|
|
167
|
+
if (!this._check('INDENT')) return stmts;
|
|
168
|
+
this._advance(); // consume INDENT
|
|
169
|
+
this._skipNL();
|
|
170
|
+
while (!this._check('DEDENT') && !this._check('EOF')) {
|
|
171
|
+
const s = this._stmt();
|
|
172
|
+
if (s !== null) stmts.push(s);
|
|
173
|
+
this._skipNL();
|
|
174
|
+
}
|
|
175
|
+
if (this._check('DEDENT')) this._advance(); // consume DEDENT
|
|
176
|
+
return stmts;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ─── Expressions ────────────────────────────────────────────────────────────
|
|
180
|
+
|
|
181
|
+
private _expr(): PineExpr { return this._ternary(); }
|
|
182
|
+
|
|
183
|
+
private _ternary(): PineExpr {
|
|
184
|
+
let left = this._or();
|
|
185
|
+
if (this._match('QMARK')) {
|
|
186
|
+
const consequent = this._expr();
|
|
187
|
+
this._consume('COLON', "Expected ':' in ternary");
|
|
188
|
+
const alternate = this._expr();
|
|
189
|
+
const node: PineTernary = {
|
|
190
|
+
kind: 'PineTernary', condition: left, consequent, alternate,
|
|
191
|
+
loc: this._locOf(left),
|
|
192
|
+
};
|
|
193
|
+
return node;
|
|
194
|
+
}
|
|
195
|
+
return left;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private _or(): PineExpr {
|
|
199
|
+
let left = this._and();
|
|
200
|
+
while (this._checkIdent('or')) {
|
|
201
|
+
this._advance();
|
|
202
|
+
const right = this._and();
|
|
203
|
+
left = this._bin('or', left, right);
|
|
204
|
+
}
|
|
205
|
+
return left;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private _and(): PineExpr {
|
|
209
|
+
let left = this._not();
|
|
210
|
+
while (this._checkIdent('and')) {
|
|
211
|
+
this._advance();
|
|
212
|
+
const right = this._not();
|
|
213
|
+
left = this._bin('and', left, right);
|
|
214
|
+
}
|
|
215
|
+
return left;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private _not(): PineExpr {
|
|
219
|
+
if (this._checkIdent('not')) {
|
|
220
|
+
const loc = this._locOfCur();
|
|
221
|
+
this._advance();
|
|
222
|
+
const operand = this._comparison();
|
|
223
|
+
const node: PineUnary = { kind: 'PineUnary', op: 'not', operand, loc };
|
|
224
|
+
return node;
|
|
225
|
+
}
|
|
226
|
+
return this._comparison();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private _comparison(): PineExpr {
|
|
230
|
+
let left = this._addition();
|
|
231
|
+
while (true) {
|
|
232
|
+
if (this._match('LTE')) { left = this._bin('<=', left, this._addition()); continue; }
|
|
233
|
+
if (this._match('GTE')) { left = this._bin('>=', left, this._addition()); continue; }
|
|
234
|
+
if (this._match('EQEQ')) { left = this._bin('==', left, this._addition()); continue; }
|
|
235
|
+
if (this._match('NEQ')) { left = this._bin('!=', left, this._addition()); continue; }
|
|
236
|
+
if (this._match('LT')) { left = this._bin('<', left, this._addition()); continue; }
|
|
237
|
+
if (this._match('GT')) { left = this._bin('>', left, this._addition()); continue; }
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
return left;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private _addition(): PineExpr {
|
|
244
|
+
let left = this._multiply();
|
|
245
|
+
while (true) {
|
|
246
|
+
if (this._match('PLUS')) { left = this._bin('+', left, this._multiply()); continue; }
|
|
247
|
+
if (this._match('MINUS')) { left = this._bin('-', left, this._multiply()); continue; }
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
return left;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private _multiply(): PineExpr {
|
|
254
|
+
let left = this._unary();
|
|
255
|
+
while (true) {
|
|
256
|
+
if (this._match('STAR')) { left = this._bin('*', left, this._unary()); continue; }
|
|
257
|
+
if (this._match('SLASH')) { left = this._bin('/', left, this._unary()); continue; }
|
|
258
|
+
if (this._match('PERCENT')) { left = this._bin('%', left, this._unary()); continue; }
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
return left;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private _unary(): PineExpr {
|
|
265
|
+
if (this._match('MINUS')) {
|
|
266
|
+
const operand = this._unary();
|
|
267
|
+
const node: PineUnary = { kind: 'PineUnary', op: '-', operand, loc: this._locOfCur() };
|
|
268
|
+
return node;
|
|
269
|
+
}
|
|
270
|
+
return this._postfix();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private _postfix(): PineExpr {
|
|
274
|
+
let expr = this._primary();
|
|
275
|
+
// Series indexing: expr[n]
|
|
276
|
+
while (this._check('LBRACKET')) {
|
|
277
|
+
const loc = this._locOfCur();
|
|
278
|
+
this._advance();
|
|
279
|
+
const index = this._expr();
|
|
280
|
+
this._consume('RBRACKET', "Expected ']'");
|
|
281
|
+
const node: PineIndex = { kind: 'PineIndex', series: expr, index, loc };
|
|
282
|
+
expr = node;
|
|
283
|
+
}
|
|
284
|
+
return expr;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private _primary(): PineExpr {
|
|
288
|
+
const t = this._cur();
|
|
289
|
+
const loc: Loc = { line: t.line, col: t.col };
|
|
290
|
+
|
|
291
|
+
if (t.kind === 'NUMBER') {
|
|
292
|
+
this._advance();
|
|
293
|
+
const node: PineNumberLit = { kind: 'PineNumberLit', value: parseFloat(t.value), loc };
|
|
294
|
+
return node;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (t.kind === 'STRING') {
|
|
298
|
+
this._advance();
|
|
299
|
+
const node: PineStringLit = { kind: 'PineStringLit', value: t.value, loc };
|
|
300
|
+
return node;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (t.kind === 'BOOL') {
|
|
304
|
+
this._advance();
|
|
305
|
+
const node: PineBoolLit = { kind: 'PineBoolLit', value: t.value === 'true', loc };
|
|
306
|
+
return node;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// na constant
|
|
310
|
+
if (t.kind === 'IDENT' && t.value === 'na') {
|
|
311
|
+
this._advance();
|
|
312
|
+
const node: PineNa = { kind: 'PineNa', loc };
|
|
313
|
+
return node;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// IDENT '.' IDENT → namespace member or namespaced call
|
|
317
|
+
if (t.kind === 'IDENT' && this._peekKind(1) === 'DOT') {
|
|
318
|
+
const ns = t.value;
|
|
319
|
+
this._advance(); // consume ns
|
|
320
|
+
this._advance(); // consume .
|
|
321
|
+
const member = this._consume('IDENT', "Expected member name after '.'").value;
|
|
322
|
+
|
|
323
|
+
// Namespaced call: ns.fn(...)
|
|
324
|
+
if (this._check('LPAREN')) {
|
|
325
|
+
this._advance();
|
|
326
|
+
const { args, namedArgs } = this._argList();
|
|
327
|
+
this._consume('RPAREN', "Expected ')'");
|
|
328
|
+
const node: PineNsCall = { kind: 'PineNsCall', namespace: ns, fn: member, args, namedArgs, loc };
|
|
329
|
+
return node;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Namespaced member: color.red, bar_index, etc.
|
|
333
|
+
const node: PineMember = { kind: 'PineMember', object: ns, prop: member, loc };
|
|
334
|
+
return node;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Plain function call: IDENT '(' ... ')'
|
|
338
|
+
if (t.kind === 'IDENT' && this._peekKind(1) === 'LPAREN') {
|
|
339
|
+
const fn = t.value;
|
|
340
|
+
this._advance();
|
|
341
|
+
this._advance(); // consume (
|
|
342
|
+
const { args, namedArgs } = this._argList();
|
|
343
|
+
this._consume('RPAREN', "Expected ')'");
|
|
344
|
+
const node: PineCall = { kind: 'PineCall', fn, args, namedArgs, loc };
|
|
345
|
+
return node;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Plain identifier
|
|
349
|
+
if (t.kind === 'IDENT') {
|
|
350
|
+
this._advance();
|
|
351
|
+
const node: PineIdent = { kind: 'PineIdent', name: t.value, loc };
|
|
352
|
+
return node;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Grouped expression
|
|
356
|
+
if (t.kind === 'LPAREN') {
|
|
357
|
+
this._advance();
|
|
358
|
+
const inner = this._expr();
|
|
359
|
+
this._consume('RPAREN', "Expected ')'");
|
|
360
|
+
return inner;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
throw new SyntaxError(`[Pine] Unexpected token '${t.value}' (${t.kind}) at ${t.line}:${t.col}`);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ─── Arg list helper ────────────────────────────────────────────────────────
|
|
367
|
+
|
|
368
|
+
private _argList(): { args: PineExpr[]; namedArgs: Map<string, PineExpr> } {
|
|
369
|
+
const args: PineExpr[] = [];
|
|
370
|
+
const namedArgs = new Map<string, PineExpr>();
|
|
371
|
+
|
|
372
|
+
if (this._check('RPAREN')) return { args, namedArgs };
|
|
373
|
+
|
|
374
|
+
// First arg
|
|
375
|
+
this._parseOneArg(args, namedArgs);
|
|
376
|
+
|
|
377
|
+
while (this._match('COMMA')) {
|
|
378
|
+
if (this._check('RPAREN')) break; // trailing comma
|
|
379
|
+
this._parseOneArg(args, namedArgs);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return { args, namedArgs };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private _parseOneArg(
|
|
386
|
+
args: PineExpr[],
|
|
387
|
+
namedArgs: Map<string, PineExpr>,
|
|
388
|
+
): void {
|
|
389
|
+
// Named arg: name = value ? (peek: IDENT = non-'=')
|
|
390
|
+
if (
|
|
391
|
+
this._check('IDENT') &&
|
|
392
|
+
this._peekKind(1) === 'ASSIGN' &&
|
|
393
|
+
this._peekKind(2) !== 'ASSIGN' // not ==
|
|
394
|
+
) {
|
|
395
|
+
const key = this._advance().value;
|
|
396
|
+
this._advance(); // consume =
|
|
397
|
+
const val = this._expr();
|
|
398
|
+
namedArgs.set(key, val);
|
|
399
|
+
} else {
|
|
400
|
+
args.push(this._expr());
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// ─── Token helpers ───────────────────────────────────────────────────────────
|
|
405
|
+
|
|
406
|
+
private _cur(): PineToken { return this._tokens[this._pos]!; }
|
|
407
|
+
private _advance(): PineToken { return this._tokens[this._pos++]!; }
|
|
408
|
+
private _check(kind: PineTok): boolean { return this._cur().kind === kind; }
|
|
409
|
+
private _checkIdent(name: string): boolean {
|
|
410
|
+
const t = this._cur();
|
|
411
|
+
return t.kind === 'IDENT' && t.value === name;
|
|
412
|
+
}
|
|
413
|
+
private _peekKind(offset: number): PineTok {
|
|
414
|
+
return this._tokens[this._pos + offset]?.kind ?? 'EOF';
|
|
415
|
+
}
|
|
416
|
+
private _match(kind: PineTok): boolean {
|
|
417
|
+
if (this._check(kind)) { this._advance(); return true; }
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
private _consume(kind: PineTok, msg: string): PineToken {
|
|
421
|
+
if (this._check(kind)) return this._advance();
|
|
422
|
+
const t = this._cur();
|
|
423
|
+
throw new SyntaxError(`[Pine] ${msg} — got '${t.value}' (${t.kind}) at ${t.line}:${t.col}`);
|
|
424
|
+
}
|
|
425
|
+
private _skipNL(): void {
|
|
426
|
+
while (this._check('NEWLINE')) this._advance();
|
|
427
|
+
}
|
|
428
|
+
private _consumeNLorEOF(): void {
|
|
429
|
+
if (this._check('NEWLINE') || this._check('EOF')) {
|
|
430
|
+
if (this._check('NEWLINE')) this._advance();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
private _locOfCur(): Loc { return { line: this._cur().line, col: this._cur().col }; }
|
|
434
|
+
private _locOf(expr: PineExpr): Loc { return (expr as { loc: Loc }).loc; }
|
|
435
|
+
|
|
436
|
+
private _bin(op: PineBinary['op'], left: PineExpr, right: PineExpr): PineBinary {
|
|
437
|
+
return { kind: 'PineBinary', op, left, right, loc: this._locOf(left) };
|
|
438
|
+
}
|
|
439
|
+
}
|