Skip to content

Commit

Permalink
add python cell parser
Browse files Browse the repository at this point in the history
  • Loading branch information
juanbercoff committed Jun 20, 2023
1 parent 3faf939 commit 5af393b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/helpers/parseEditorPythonCell.ts
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;
}

0 comments on commit 5af393b

Please sign in to comment.