Skip to content

Commit

Permalink
feat, wip: i think i scrap the idea of per-step printers. it is not n…
Browse files Browse the repository at this point in the history
…eeded.
  • Loading branch information
EagleoutIce committed Aug 28, 2023
1 parent 5eb9c5b commit 5aa51a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/core/print/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@
* @module
*/

import { ISubStepPrinter, StepOutputFormat } from './print'
import { SubStepProcessor } from '../steps'
import { DEFAULT_XML_PARSER_CONFIG, TokenMap, XmlParserConfig } from '../../r-bridge'
import { deepMergeObject } from '../../util/objects'
import { xlm2jsonObject } from '../../r-bridge/lang-4.x/ast/parser/xml/internal'

/**
* Receives the xml of the parsed R code AST and returns my attempt at an ASCII Art representation.
*/
export const parseResultToText: ISubStepPrinter<SubStepProcessor<'parse'>, StepOutputFormat.text, [TokenMap]> = async(input: string, tokenMap: TokenMap): Promise<string> => {
const config = deepMergeObject<XmlParserConfig>(DEFAULT_XML_PARSER_CONFIG, { tokenMap })
const object = await xlm2jsonObject(config, input)

return JSON.stringify(object)
}
6 changes: 5 additions & 1 deletion src/core/print/print.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StepFunction } from '../steps'

/**
* Defines the output format of a step that you are interested in.
*/
Expand Down Expand Up @@ -42,4 +44,6 @@ export function internalPrinter<Input>(input: Input): Input {
* A mapping function that maps the result of a step (i.e., the dataflow graph)
* to another representation (linked by {@link StepOutputFormat} in an {@link ISubStep}).
*/
export type ISubStepPrinter<Input, Format extends StepOutputFormat> = (input: Input) => Format extends StepOutputFormat.internal ? Input : string
export type ISubStepPrinter<StepInput extends StepFunction, Format extends StepOutputFormat, AdditionalInput extends unknown[]> =
Format extends StepOutputFormat.internal ? (input: Awaited<ReturnType<StepInput>>) => Awaited<ReturnType<StepInput>> :
(input: Awaited<ReturnType<StepInput>>, ...additional: AdditionalInput) => Promise<string>
10 changes: 5 additions & 5 deletions src/core/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*
* Please note that the combination of `satisfies` and `as` seems to be required.
* With `satisfies` we make sure that the respective element has all the keys it requires, and the `as` force the type to be exactly the given one
* TODO: add a visualizer for each step for the given format
* TODO: add a differ for each step?
*
* @module
*/
Expand All @@ -24,13 +22,14 @@ import {
import { produceDataFlowGraph } from '../dataflow'
import { convertAllSlicingCriteriaToIds, reconstructToCode, staticSlicing } from '../slicing'
import { internalPrinter, ISubStepPrinter, StepOutputFormat } from './print/print'
import { parseResultToText } from './print/parse'

/**
* The names of all main steps of the slicing process.
*/
export const STEP_NAMES = ['parse', 'normalize', 'decorate', 'dataflow', 'slice', 'reconstruct'] as const

type StepFunction = (...args: never[]) => unknown
export type StepFunction = (...args: never[]) => unknown

export type StepRequired = 'once-per-file' | 'once-per-slice'

Expand All @@ -50,7 +49,7 @@ export interface ISubStep<Fn extends StepFunction> extends MergeableRecord {
/* does this step has to be repeated for each new slice or can it be performed only once in the initialization */
required: StepRequired
printer: {
[K in StepOutputFormat]?: ISubStepPrinter<Awaited<ReturnType<Fn>>, K>
[K in StepOutputFormat]?: ISubStepPrinter<Fn, K, unknown[]>
}
}

Expand All @@ -62,7 +61,8 @@ export const STEPS_PER_FILE = {
processor: retrieveXmlFromRCode,
required: 'once-per-file',
printer: {
[StepOutputFormat.internal]: internalPrinter
[StepOutputFormat.internal]: internalPrinter,
[StepOutputFormat.text]: parseResultToText
}
} satisfies ISubStep<typeof retrieveXmlFromRCode> as ISubStep<typeof retrieveXmlFromRCode>,
'normalize ast': {
Expand Down

0 comments on commit 5aa51a6

Please sign in to comment.