forked from handsontable/hyperformula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildEngineFactory.ts
145 lines (128 loc) · 6.86 KB
/
BuildEngineFactory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @license
* Copyright (c) 2024 Handsoncode. All rights reserved.
*/
import {ArraySizePredictor} from './ArraySize'
import {CellContentParser} from './CellContentParser'
import {ClipboardOperations} from './ClipboardOperations'
import {Config} from './Config'
import {CrudOperations} from './CrudOperations'
import {DateTimeHelper} from './DateTimeHelper'
import {DependencyGraph} from './DependencyGraph'
import {SheetSizeLimitExceededError} from './errors'
import {Evaluator} from './Evaluator'
import {Exporter} from './Exporter'
import {GraphBuilder} from './GraphBuilder'
import {UIElement} from './i18n'
import {ArithmeticHelper} from './interpreter/ArithmeticHelper'
import {FunctionRegistry} from './interpreter/FunctionRegistry'
import {Interpreter} from './interpreter/Interpreter'
import {LazilyTransformingAstService} from './LazilyTransformingAstService'
import {buildColumnSearchStrategy, ColumnSearchStrategy} from './Lookup/SearchStrategy'
import {NamedExpressions} from './NamedExpressions'
import {NumberLiteralHelper} from './NumberLiteralHelper'
import {Operations} from './Operations'
import {buildLexerConfig, ParserWithCaching, Unparser} from './parser'
import {Serialization, SerializedNamedExpression} from './Serialization'
import {findBoundaries, Sheet, Sheets, validateAsSheet} from './Sheet'
import {EmptyStatistics, Statistics, StatType} from './statistics'
import {UndoRedo} from './UndoRedo'
import {ConfigParams} from './ConfigParams'
export type EngineState = {
config: Config,
stats: Statistics,
dependencyGraph: DependencyGraph,
columnSearch: ColumnSearchStrategy,
parser: ParserWithCaching,
unparser: Unparser,
cellContentParser: CellContentParser,
evaluator: Evaluator,
lazilyTransformingAstService: LazilyTransformingAstService,
crudOperations: CrudOperations,
exporter: Exporter,
namedExpressions: NamedExpressions,
serialization: Serialization,
functionRegistry: FunctionRegistry,
}
export class BuildEngineFactory {
public static buildFromSheets(sheets: Sheets, configInput: Partial<ConfigParams> = {}, namedExpressions: SerializedNamedExpression[] = []): EngineState {
const config = new Config(configInput)
return this.buildEngine(config, sheets, namedExpressions)
}
public static buildFromSheet(sheet: Sheet, configInput: Partial<ConfigParams> = {}, namedExpressions: SerializedNamedExpression[] = []): EngineState {
const config = new Config(configInput)
const newsheetprefix = config.translationPackage.getUITranslation(UIElement.NEW_SHEET_PREFIX) + '1'
return this.buildEngine(config, {[newsheetprefix]: sheet}, namedExpressions)
}
public static buildEmpty(configInput: Partial<ConfigParams> = {}, namedExpressions: SerializedNamedExpression[] = []): EngineState {
return this.buildEngine(new Config(configInput), {}, namedExpressions)
}
public static rebuildWithConfig(config: Config, sheets: Sheets, namedExpressions: SerializedNamedExpression[], stats: Statistics): EngineState {
return this.buildEngine(config, sheets, namedExpressions, stats)
}
private static buildEngine(config: Config, sheets: Sheets = {}, inputNamedExpressions: SerializedNamedExpression[] = [], stats: Statistics = config.useStats ? new Statistics() : new EmptyStatistics()): EngineState {
stats.start(StatType.BUILD_ENGINE_TOTAL)
const namedExpressions = new NamedExpressions()
const functionRegistry = new FunctionRegistry(config)
const lazilyTransformingAstService = new LazilyTransformingAstService(stats)
const dependencyGraph = DependencyGraph.buildEmpty(lazilyTransformingAstService, config, functionRegistry, namedExpressions, stats)
const columnSearch = buildColumnSearchStrategy(dependencyGraph, config, stats)
const sheetMapping = dependencyGraph.sheetMapping
const addressMapping = dependencyGraph.addressMapping
for (const sheetName in sheets) {
if (Object.prototype.hasOwnProperty.call(sheets, sheetName)) {
const sheet = sheets[sheetName]
validateAsSheet(sheet)
const boundaries = findBoundaries(sheet)
if (boundaries.height > config.maxRows || boundaries.width > config.maxColumns) {
throw new SheetSizeLimitExceededError()
}
const sheetId = sheetMapping.addSheet(sheetName)
addressMapping.autoAddSheet(sheetId, boundaries)
}
}
const parser = new ParserWithCaching(config, functionRegistry, sheetMapping.get)
lazilyTransformingAstService.parser = parser
const unparser = new Unparser(config, buildLexerConfig(config), sheetMapping.fetchDisplayName, namedExpressions)
const dateTimeHelper = new DateTimeHelper(config)
const numberLiteralHelper = new NumberLiteralHelper(config)
const arithmeticHelper = new ArithmeticHelper(config, dateTimeHelper, numberLiteralHelper)
const cellContentParser = new CellContentParser(config, dateTimeHelper, numberLiteralHelper)
const arraySizePredictor = new ArraySizePredictor(config, functionRegistry)
const operations = new Operations(config, dependencyGraph, columnSearch, cellContentParser, parser, stats, lazilyTransformingAstService, namedExpressions, arraySizePredictor)
const undoRedo = new UndoRedo(config, operations)
lazilyTransformingAstService.undoRedo = undoRedo
const clipboardOperations = new ClipboardOperations(config, dependencyGraph, operations)
const crudOperations = new CrudOperations(config, operations, undoRedo, clipboardOperations, dependencyGraph, columnSearch, parser, cellContentParser, lazilyTransformingAstService, namedExpressions)
inputNamedExpressions.forEach((entry: SerializedNamedExpression) => {
crudOperations.ensureItIsPossibleToAddNamedExpression(entry.name, entry.expression, entry.scope)
crudOperations.operations.addNamedExpression(entry.name, entry.expression, entry.scope, entry.options)
})
const exporter = new Exporter(config, namedExpressions, sheetMapping.fetchDisplayName, lazilyTransformingAstService)
const serialization = new Serialization(dependencyGraph, unparser, exporter)
const interpreter = new Interpreter(config, dependencyGraph, columnSearch, stats, arithmeticHelper, functionRegistry, namedExpressions, serialization, arraySizePredictor, dateTimeHelper)
stats.measure(StatType.GRAPH_BUILD, () => {
const graphBuilder = new GraphBuilder(dependencyGraph, columnSearch, parser, cellContentParser, stats, arraySizePredictor)
graphBuilder.buildGraph(sheets, stats)
})
const evaluator = new Evaluator(config, stats, interpreter, lazilyTransformingAstService, dependencyGraph, columnSearch)
evaluator.run()
stats.end(StatType.BUILD_ENGINE_TOTAL)
return {
config,
stats,
dependencyGraph,
columnSearch,
parser,
unparser,
cellContentParser,
evaluator,
lazilyTransformingAstService,
crudOperations,
exporter,
namedExpressions,
serialization,
functionRegistry,
}
}
}