-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#212 Make the tag parser return all the sheet metadata it can
- Loading branch information
1 parent
e8be0b6
commit feccd0b
Showing
6 changed files
with
79 additions
and
62 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
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
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 |
---|---|---|
@@ -1,33 +1,40 @@ | ||
import {TagTree, TagTrees, WarnedTag} from "./types"; | ||
import {TagTree, TagNodes, WarnedTag, RawTagSheet, TagRoot} from "./types"; | ||
|
||
interface ParserOptions { | ||
rows: WarnedTag[][]; | ||
fromRow: number; | ||
depth: number; | ||
trees: TagTrees; | ||
parent?: TagTree; | ||
nodes: TagNodes; | ||
parent: TagTree | TagRoot; | ||
} | ||
|
||
const parseRows = ({rows, fromRow, depth, trees, parent}: ParserOptions): number => { | ||
const parseRows = ({rows, fromRow, depth, nodes, parent}: ParserOptions): number => { | ||
let row = fromRow; | ||
while (row < rows.length) { | ||
const {tag, warning} = rows[row][depth] ?? {}; | ||
if (tag) { | ||
const node = {tag, parent, warning}; | ||
// Keys in the tag trees map are lowercase for ez lookup later | ||
trees.set(tag.toLowerCase(), node); | ||
row = parseRows({rows, fromRow: row + 1, depth: depth + 1, trees, parent: node}); | ||
const node = {tag, parent, warning, children: []}; | ||
parent.children.push(node); | ||
// TODO if nodes already contains this tag, we need to emit a warning somehow!!! see #214 | ||
// Keys in the tag nodes map are lowercase for ez lookup later | ||
nodes.set(tag.toLowerCase(), node); | ||
row = parseRows({rows, fromRow: row + 1, depth: depth + 1, nodes, parent: node}); | ||
} else { | ||
break; | ||
} | ||
} | ||
return row; | ||
}; | ||
|
||
const parseTree = (rows: WarnedTag[][]) => { | ||
const trees: TagTrees = new Map(); | ||
for (let fromRow = 0; fromRow < rows.length; fromRow = parseRows({rows, fromRow, depth: 0, trees}) + 1); | ||
return trees; | ||
const parseTags = (sheets: RawTagSheet[]) => { | ||
const nodes: TagNodes = new Map(); | ||
const roots = sheets.map((sheet) => { | ||
const rows = sheet.values; | ||
const parent = {...sheet, children: []}; | ||
for (let fromRow = 0; fromRow < rows.length; fromRow = parseRows({rows, fromRow, depth: 0, nodes, parent}) + 1); | ||
return parent; | ||
}); | ||
return {nodes, roots}; | ||
}; | ||
|
||
export {parseTree}; | ||
export {parseTags}; |
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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import {Range} from "../sheets"; | ||
|
||
type TagMapper = `${string}$TAG${string}`; | ||
type TagSheetDescriptor = {range: Range; mapper: TagMapper; name: string; cwRange?: Range}; | ||
|
||
type RawTagSheet = TagSheetDescriptor & {values: WarnedTag[][]}; | ||
|
||
type TagRoot = RawTagSheet & {children: TagTree[]}; | ||
|
||
/** | ||
* A tag tree contains the properly cased tag at the root, | ||
* and a pointer to its parent in the tree | ||
*/ | ||
type TagTree = {tag: string; parent?: TagTree; warning: boolean}; | ||
type TagTree = {tag: string; parent: TagTree | TagRoot; warning: boolean; children: TagTree[]}; | ||
|
||
/** | ||
* TagTrees is a map of lowercase tag to the tag subtree at that tag | ||
* TagNodes is a map of lowercase tag to the tag subtree at that tag | ||
* It contains every subtree of the complete tag tree | ||
*/ | ||
type TagTrees = Map<string, TagTree>; | ||
type TagNodes = Map<string, TagTree>; | ||
|
||
type Tags = {nodes: TagNodes; roots: TagRoot[]}; | ||
|
||
interface TagSearchOptions { | ||
noCache: boolean; | ||
} | ||
|
||
type WarnedTag = {tag: string; warning: boolean}; | ||
|
||
export {WarnedTag, TagTree, TagTrees, TagSearchOptions}; | ||
export {WarnedTag, TagTree, TagRoot, TagNodes, TagSearchOptions, RawTagSheet, TagSheetDescriptor, TagMapper, Tags}; |
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
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