|
1 | 1 | import * as vscode from "vscode";
|
| 2 | +import { tryParseRangeExpression } from "./positionExpression"; |
2 | 3 |
|
3 | 4 | const goToPositionCommandName = "io.orta.typescript-dev.go-to-position";
|
4 | 5 | const togglePositionCommandName = "io.orta.typescript-dev.toggle-position";
|
@@ -58,100 +59,3 @@ function updatePosition(statusBarItem: vscode.StatusBarItem) {
|
58 | 59 | statusBarItem.text = "Pos " + lower + (upper === lower ? "" : `, ${upper}`);
|
59 | 60 | }
|
60 | 61 | }
|
61 |
| - |
62 |
| -export function tryParseRangeExpression(expr: string): readonly [number, number | undefined] | undefined { |
63 |
| - const [startExpr, endExpr] = expr.split(","); |
64 |
| - const start = tryParsePositionExpression(startExpr); |
65 |
| - if (start === undefined) return undefined; |
66 |
| - return [start, endExpr === undefined ? endExpr : tryParsePositionExpression(endExpr)]; |
67 |
| -} |
68 |
| - |
69 |
| -function tryParsePositionExpression(expr: string): number | undefined { |
70 |
| - const enum State { |
71 |
| - ParseOperator, |
72 |
| - ParseOperand, |
73 |
| - } |
74 |
| - |
75 |
| - let state = State.ParseOperand as State; |
76 |
| - let value: number | undefined; |
77 |
| - let operator: "+" | "-" = "+"; |
78 |
| - let pos = 0; |
79 |
| - |
80 |
| - while (pos < expr.length) { |
81 |
| - skipSpace(); |
82 |
| - const current = expr[pos]; |
83 |
| - if (!current) break; |
84 |
| - |
85 |
| - switch (state) { |
86 |
| - case State.ParseOperand: |
87 |
| - if (isNumeric(current)) { |
88 |
| - value = operate(consumeInt()); |
89 |
| - state = State.ParseOperator; |
90 |
| - continue; |
91 |
| - } else if (current === "+") { |
92 |
| - pos++; |
93 |
| - continue; |
94 |
| - } else if (current === "-") { |
95 |
| - flipSign(); |
96 |
| - pos++; |
97 |
| - continue; |
98 |
| - } else { |
99 |
| - return undefined; |
100 |
| - } |
101 |
| - case State.ParseOperator: |
102 |
| - if (current === "+") { |
103 |
| - operator = "+"; |
104 |
| - pos++; |
105 |
| - state = State.ParseOperand; |
106 |
| - } else if (current === "-") { |
107 |
| - operator = "-"; |
108 |
| - pos++; |
109 |
| - state = State.ParseOperand; |
110 |
| - } else { |
111 |
| - return undefined; |
112 |
| - } |
113 |
| - } |
114 |
| - } |
115 |
| - return value !== undefined && value >= 0 ? value : undefined; |
116 |
| - |
117 |
| - function consumeInt(): number | undefined { |
118 |
| - let start = pos; |
119 |
| - while (pos < expr.length && isNumeric(expr[pos])) { |
120 |
| - pos++; |
121 |
| - } |
122 |
| - const int = +expr.slice(start, pos); |
123 |
| - if (Number.isSafeInteger(int)) { |
124 |
| - return int; |
125 |
| - } |
126 |
| - } |
127 |
| - |
128 |
| - function skipSpace() { |
129 |
| - while (pos < expr.length && /\s/.test(expr[pos])) { |
130 |
| - pos++; |
131 |
| - } |
132 |
| - } |
133 |
| - |
134 |
| - function operate(operand: number | undefined) { |
135 |
| - if (operand === undefined) return value; |
136 |
| - if (value === undefined) value = 0; |
137 |
| - switch (operator) { |
138 |
| - case "+": |
139 |
| - return value + operand; |
140 |
| - case "-": |
141 |
| - return value - operand; |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - function flipSign() { |
146 |
| - if (operator === "+") { |
147 |
| - operator = "-"; |
148 |
| - } else { |
149 |
| - operator = "+"; |
150 |
| - } |
151 |
| - } |
152 |
| -} |
153 |
| - |
154 |
| -function isNumeric(char: string) { |
155 |
| - const code = char.charCodeAt(0); |
156 |
| - return 48 <= code && code <= 57; |
157 |
| -} |
0 commit comments