-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3faf939
commit 5af393b
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ParseFormulaReturnType } from './formulaNotation'; | ||
|
||
const CELL = /\(\s*(-?\d+\s*,\s*-?\d+\s*)\)/; | ||
const SIMPLE_CELL = new RegExp(`cell${CELL.source}`, 'g'); | ||
const MULTICURSOR_CELL = new RegExp(`cells\\(\\s*${CELL.source}\\s*,\\s*${CELL.source}\\s*\\)`, 'g'); | ||
|
||
export function parsePython(modelContent: string) { | ||
let matches: RegExpExecArray | null; | ||
|
||
let parsedEditorContent: ParseFormulaReturnType = { | ||
// could be improved to check for errors within the editor content | ||
parse_error_msg: undefined, | ||
parse_error_span: undefined, | ||
cell_refs: [], | ||
}; | ||
|
||
while ((matches = SIMPLE_CELL.exec(modelContent)) !== null) { | ||
const match = matches[0]; | ||
const group = matches[1]; | ||
const [x, y] = group.split(','); | ||
const startIndex = matches.index; | ||
const matchLength = match.length; | ||
|
||
parsedEditorContent.cell_refs.push({ | ||
cell_ref: { Cell: { x: { Relative: parseInt(x) }, y: { Relative: parseInt(y) } } }, | ||
span: { start: startIndex, end: startIndex + matchLength }, | ||
}); | ||
} | ||
|
||
while ((matches = MULTICURSOR_CELL.exec(modelContent)) !== null) { | ||
const match = matches[0]; | ||
const startCell = matches[1]; | ||
const endCell = matches[2]; | ||
const [startX, startY] = startCell.split(','); | ||
const [endX, endY] = endCell.split(','); | ||
const startIndex = matches.index; | ||
const matchLength = match.length; | ||
|
||
parsedEditorContent.cell_refs.push({ | ||
cell_ref: { | ||
CellRange: [ | ||
{ x: { Relative: parseInt(startX) }, y: { Relative: parseInt(startY) } }, | ||
{ x: { Relative: parseInt(endX) }, y: { Relative: parseInt(endY) } }, | ||
], | ||
}, | ||
span: { start: startIndex, end: startIndex + matchLength }, | ||
}); | ||
} | ||
return parsedEditorContent; | ||
} |