Skip to content

Commit

Permalink
Merge branch 'main' into ast-query-test
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce authored Oct 8, 2024
2 parents c42c77f + f43dfd8 commit e01d547
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/cli/repl/commands/repl-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export function asciiSummaryOfQueryResult(formatter: OutputFormatter, totalInMs:
result.push(`Query: ${bold(query, formatter)} (${out['.meta'].timing.toFixed(0)}ms)`);
result.push(` ╰ [Dataflow Graph](${graphToMermaidUrl(out.graph)})`);
continue;
} else if(query === 'id-map') {
const out = queryResults as QueryResults<'id-map'>['id-map'];
result.push(`Query: ${bold(query, formatter)} (${out['.meta'].timing.toFixed(0)}ms)`);
result.push(` ╰ Id List: ${[...out.idMap.keys()].join(', ')}`);
} else if(query === 'normalized-ast') {
const out = queryResults as QueryResults<'normalized-ast'>['normalized-ast'];
result.push(`Query: ${bold(query, formatter)} (${out['.meta'].timing.toFixed(0)}ms)`);
Expand Down
21 changes: 21 additions & 0 deletions src/documentation/print-query-wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { exampleQueryCode } from './data/query/example-query-code';
import { details } from './doc-util/doc-structure';
import { codeBlock } from './doc-util/doc-code';
import { executeDataflowQuery } from '../queries/catalog/dataflow-query/dataflow-query-executor';
import { executeIdMapQuery } from '../queries/catalog/id-map-query/id-map-query-executor';
import { executeNormalizedAstQuery } from '../queries/catalog/normalized-ast-query/normalized-ast-query-executor';


Expand Down Expand Up @@ -127,6 +128,26 @@ ${
}
});

registerQueryDocumentation('id-map', {
name: 'Id-Map Query',
type: 'active',
shortDescription: 'Returns the id-map of the normalized AST of the given code.',
functionName: executeIdMapQuery.name,
functionFile: '../queries/catalog/id-map-query/id-map-query-executor.ts',
buildExplanation: async(shell: RShell) => {
return `
This query provides access to all nodes in the [normalized AST](${FlowrWikiBaseRef}/Normalized%20AST) as a mapping from their id to the node itself.
Using the example code from above, the following query returns all nodes from the code:
${
await showQuery(shell, exampleQueryCode, [{
type: 'id-map'
}], { showCode: true })
}
`;
}
});

registerQueryDocumentation('compound', {
name: 'Compound Query',
type: 'virtual',
Expand Down
18 changes: 18 additions & 0 deletions src/queries/catalog/id-map-query/id-map-query-executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { BasicQueryData } from '../../query';
import { log } from '../../../util/log';
import type { IdMapQuery, IdMapQueryResult } from './id-map-query-format';


export function executeIdMapQuery({ ast }: BasicQueryData, queries: readonly IdMapQuery[]): IdMapQueryResult {
if(queries.length !== 1) {
log.warn('Id-Map query expects only up to one query, but got', queries.length);
}

return {
'.meta': {
/* there is no sense in measuring a get */
timing: 0
},
idMap: ast.idMap
};
}
10 changes: 10 additions & 0 deletions src/queries/catalog/id-map-query/id-map-query-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { BaseQueryFormat, BaseQueryResult } from '../../base-query-format';
import type { AstIdMap } from '../../../r-bridge/lang-4.x/ast/model/processing/decorate';

export interface IdMapQuery extends BaseQueryFormat {
readonly type: 'id-map';
}

export interface IdMapQueryResult extends BaseQueryResult {
readonly idMap: AstIdMap;
}
5 changes: 5 additions & 0 deletions src/queries/query-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ export const DataflowQuerySchema = Joi.object({
type: Joi.string().valid('dataflow').required().description('The type of the query.'),
}).description('The dataflow query simply returns the dataflow graph, there is no need to pass it multiple times!');

export const IdMapQuerySchema = Joi.object({
type: Joi.string().valid('id-map').required().description('The type of the query.'),
}).description('The id map query retrieves the id map from the normalized AST.');

export const NormalizedAstQuerySchema = Joi.object({
type: Joi.string().valid('normalized-ast').required().description('The type of the query.'),
}).description('The normalized AST query simply returns the normalized AST, there is no need to pass it multiple times!');

export const SupportedQueriesSchema = Joi.alternatives(
CallContextQuerySchema,
DataflowQuerySchema,
IdMapQuerySchema,
NormalizedAstQuerySchema
).description('Supported queries');

Expand Down
13 changes: 6 additions & 7 deletions src/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import type { VirtualCompoundConstraint } from './virtual-query/compound-query';
import type { NormalizedAst } from '../r-bridge/lang-4.x/ast/model/processing/decorate';
import { executeDataflowQuery } from './catalog/dataflow-query/dataflow-query-executor';
import type { DataflowQuery } from './catalog/dataflow-query/dataflow-query-format';
import {
executeNormalizedAstQuery
} from './catalog/normalized-ast-query/normalized-ast-query-executor';
import type {
NormalizedAstQuery
} from './catalog/normalized-ast-query/normalized-ast-query-format';
import { executeIdMapQuery } from './catalog/id-map-query/id-map-query-executor';
import type { IdMapQuery } from './catalog/id-map-query/id-map-query-format';
import { executeNormalizedAstQuery } from './catalog/normalized-ast-query/normalized-ast-query-executor';
import type { NormalizedAstQuery } from './catalog/normalized-ast-query/normalized-ast-query-format';

export type Query = CallContextQuery | DataflowQuery | NormalizedAstQuery;
export type Query = CallContextQuery | DataflowQuery | NormalizedAstQuery | IdMapQuery;

export type QueryArgumentsWithType<QueryType extends BaseQueryFormat['type']> = Query & { type: QueryType };

Expand All @@ -36,6 +34,7 @@ type SupportedQueries = {
export const SupportedQueries = {
'call-context': executeCallContextQueries,
'dataflow': executeDataflowQuery,
'id-map': executeIdMapQuery,
'normalized-ast': executeNormalizedAstQuery
} as const satisfies SupportedQueries;

Expand Down
13 changes: 13 additions & 0 deletions test/functionality/dataflow/query/id-map-query-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { assertQuery } from '../../_helper/query';
import { label } from '../../_helper/label';
import { withShell } from '../../_helper/shell';
import type { IdMapQuery } from '../../../../src/queries/catalog/id-map-query/id-map-query-format';

describe('Normalized AST Query', withShell(shell => {
function testQuery(name: string, code: string, query: readonly IdMapQuery[]) {
assertQuery(label(name), shell, code, query, (({ normalize }) => ({ 'id-map': { idMap: normalize.idMap } })));
}

testQuery('Single normalized AST', 'x + 1', [{ type: 'id-map' }]);
testQuery('Multiple Queries', 'x + 1', [{ type: 'id-map' }, { type: 'id-map' }, { type: 'id-map' }]);
}));

0 comments on commit e01d547

Please sign in to comment.