Skip to content
Snippets Groups Projects
Unverified Commit bb1acb8a authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(bazel-module): Reorganize parser into the own directory (#30945)

parent 65b42128
No related branches found
No related tags found
No related merge requests found
import { codeBlock } from 'common-tags'; import { codeBlock } from 'common-tags';
import * as fragments from './fragments'; import * as fragments from '../fragments';
import { parse } from './parser'; import { parse } from '.';
describe('modules/manager/bazel-module/parser', () => { describe('modules/manager/bazel-module/parser/index', () => {
describe('parse', () => { describe('parse', () => {
it('returns empty string if invalid content', () => { it('returns empty string if invalid content', () => {
const input = codeBlock` const input = codeBlock`
......
import { lang, query as q } from 'good-enough-parser';
import { Ctx } from '../context';
import type { RecordFragment } from '../fragments';
import { moduleRules } from './module';
const rule = q.alt<Ctx>(moduleRules);
const query = q.tree<Ctx>({
type: 'root-tree',
maxDepth: 16,
search: rule,
});
const starlarkLang = lang.createLang('starlark');
export function parse(input: string): RecordFragment[] {
const parsedResult = starlarkLang.query(input, query, new Ctx());
return parsedResult?.results ?? [];
}
import { lang, query as q } from 'good-enough-parser'; import { query as q } from 'good-enough-parser';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../../util/regex';
import { Ctx } from './context'; import type { Ctx } from '../context';
import type { RecordFragment } from './fragments'; import * as starlark from '../starlark';
import * as starlark from './starlark';
const booleanValuesRegex = regEx(`^${starlark.booleanStringValues.join('|')}$`); const booleanValuesRegex = regEx(`^${starlark.booleanStringValues.join('|')}$`);
const supportedRules = [ const supportedRules = [
...@@ -26,7 +25,7 @@ const kvParams = q ...@@ -26,7 +25,7 @@ const kvParams = q
q.sym<Ctx>(booleanValuesRegex, (ctx, token) => ctx.addBoolean(token.value)), q.sym<Ctx>(booleanValuesRegex, (ctx, token) => ctx.addBoolean(token.value)),
); );
const moduleRules = q export const moduleRules = q
.sym<Ctx>(supportedRulesRegex, (ctx, token) => ctx.startRule(token.value)) .sym<Ctx>(supportedRulesRegex, (ctx, token) => ctx.startRule(token.value))
.join( .join(
q.tree({ q.tree({
...@@ -36,18 +35,3 @@ const moduleRules = q ...@@ -36,18 +35,3 @@ const moduleRules = q
postHandler: (ctx, tree) => ctx.endRule(), postHandler: (ctx, tree) => ctx.endRule(),
}), }),
); );
const rule = q.alt<Ctx>(moduleRules);
const query = q.tree<Ctx>({
type: 'root-tree',
maxDepth: 16,
search: rule,
});
const starlarkLang = lang.createLang('starlark');
export function parse(input: string): RecordFragment[] {
const parsedResult = starlarkLang.query(input, query, new Ctx());
return parsedResult?.results ?? [];
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment