-
-
Notifications
You must be signed in to change notification settings - Fork 56
Extract compiler #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Extract compiler #108
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Node, NodeType } from './Ast.js' | ||
import CucumberExpressionError from './CucumberExpressionError.js' | ||
import { | ||
createAlternativeMayNotBeEmpty, | ||
createAlternativeMayNotExclusivelyContainOptionals, | ||
createOptionalIsNotAllowedInOptional, | ||
createOptionalMayNotBeEmpty, | ||
createParameterIsNotAllowedInOptional, | ||
createUndefinedParameterType, | ||
} from './Errors.js' | ||
import ParameterType from './ParameterType.js' | ||
import ParameterTypeRegistry from './ParameterTypeRegistry.js' | ||
|
||
export default abstract class AbstractCompiler<T> { | ||
constructor( | ||
private readonly expression: string, | ||
private readonly parameterTypeRegistry: ParameterTypeRegistry | ||
) {} | ||
|
||
protected abstract produceText(expression: string): T | ||
protected abstract produceOptional(segments: T[]): T | ||
protected abstract produceAlternation(segments: T[]): T | ||
protected abstract produceAlternative(segments: T[]): T | ||
protected abstract produceParameter(parameterType: ParameterType<unknown>): T | ||
protected abstract produceExpression(segments: T[]): T | ||
|
||
public compile(node: Node): T { | ||
switch (node.type) { | ||
case NodeType.text: | ||
return this.produceText(node.text()) | ||
case NodeType.optional: | ||
return this.compileOptional(node) | ||
case NodeType.alternation: | ||
return this.compileAlternation(node) | ||
case NodeType.alternative: | ||
return this.compileAlternative(node) | ||
case NodeType.parameter: | ||
return this.compileParameter(node) | ||
case NodeType.expression: | ||
return this.compileExpression(node) | ||
default: | ||
// Can't happen as long as the switch case is exhaustive | ||
throw new Error(node.type) | ||
} | ||
} | ||
|
||
private compileOptional(node: Node): T { | ||
this.assertNoParameters(node, (astNode) => | ||
createParameterIsNotAllowedInOptional(astNode, this.expression) | ||
) | ||
this.assertNoOptionals(node, (astNode) => | ||
createOptionalIsNotAllowedInOptional(astNode, this.expression) | ||
) | ||
this.assertNotEmpty(node, (astNode) => createOptionalMayNotBeEmpty(astNode, this.expression)) | ||
|
||
const segments = (node.nodes || []).map((node) => this.compile(node)) | ||
return this.produceOptional(segments) | ||
} | ||
|
||
private compileAlternation(node: Node) { | ||
// Make sure the alternative parts aren't empty and don't contain parameter types | ||
for (const alternative of node.nodes || []) { | ||
if (!alternative.nodes || alternative.nodes.length == 0) { | ||
throw createAlternativeMayNotBeEmpty(alternative, this.expression) | ||
} | ||
this.assertNotEmpty(alternative, (astNode) => | ||
createAlternativeMayNotExclusivelyContainOptionals(astNode, this.expression) | ||
) | ||
} | ||
const segments = (node.nodes || []).map((node) => this.compile(node)) | ||
return this.produceAlternation(segments) | ||
} | ||
|
||
private compileAlternative(node: Node) { | ||
const segments = (node.nodes || []).map((lastNode) => this.compile(lastNode)) | ||
return this.produceAlternative(segments) | ||
} | ||
|
||
private compileParameter(node: Node) { | ||
const name = node.text() | ||
const parameterType = this.parameterTypeRegistry.lookupByTypeName(name) | ||
if (!parameterType) { | ||
throw createUndefinedParameterType(node, this.expression, name) | ||
} | ||
return this.produceParameter(parameterType) | ||
} | ||
|
||
private compileExpression(node: Node) { | ||
const segments = (node.nodes || []).map((node) => this.compile(node)) | ||
return this.produceExpression(segments) | ||
} | ||
|
||
private assertNotEmpty( | ||
node: Node, | ||
createNodeWasNotEmptyException: (astNode: Node) => CucumberExpressionError | ||
) { | ||
const textNodes = (node.nodes || []).filter((astNode) => NodeType.text == astNode.type) | ||
|
||
if (textNodes.length == 0) { | ||
throw createNodeWasNotEmptyException(node) | ||
} | ||
} | ||
|
||
private assertNoParameters( | ||
node: Node, | ||
createNodeContainedAParameterError: (astNode: Node) => CucumberExpressionError | ||
) { | ||
const parameterNodes = (node.nodes || []).filter( | ||
(astNode) => NodeType.parameter == astNode.type | ||
) | ||
if (parameterNodes.length > 0) { | ||
throw createNodeContainedAParameterError(parameterNodes[0]) | ||
} | ||
} | ||
|
||
private assertNoOptionals( | ||
node: Node, | ||
createNodeContainedAnOptionalError: (astNode: Node) => CucumberExpressionError | ||
) { | ||
const parameterNodes = (node.nodes || []).filter((astNode) => NodeType.optional == astNode.type) | ||
if (parameterNodes.length > 0) { | ||
throw createNodeContainedAnOptionalError(parameterNodes[0]) | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import AbstractCompiler from './AbstractCompiler.js' | ||
import ParameterType from './ParameterType.js' | ||
import ParameterTypeRegistry from './ParameterTypeRegistry' | ||
|
||
const ESCAPE_PATTERN = () => /([\\^[({$.|?*+})\]])/g | ||
|
||
export default class RegExpCompiler extends AbstractCompiler<string> { | ||
constructor( | ||
expression: string, | ||
parameterTypeRegistry: ParameterTypeRegistry, | ||
private readonly parameterTypes: Array<ParameterType<unknown>> | ||
) { | ||
super(expression, parameterTypeRegistry) | ||
} | ||
|
||
protected produceText(expression: string) { | ||
return expression.replace(ESCAPE_PATTERN(), '\\$1') | ||
} | ||
|
||
protected produceOptional(segments: string[]): string { | ||
const regex = segments.join('') | ||
return `(?:${regex})?` | ||
} | ||
|
||
protected produceAlternation(segments: string[]): string { | ||
const regex = segments.join('|') | ||
return `(?:${regex})` | ||
} | ||
|
||
protected produceAlternative(segments: string[]): string { | ||
return segments.join('') | ||
} | ||
|
||
protected produceParameter(parameterType: ParameterType<unknown>): string { | ||
this.parameterTypes.push(parameterType) | ||
const regexps = parameterType.regexpStrings | ||
if (regexps.length == 1) { | ||
return `(${regexps[0]})` | ||
} | ||
return `((?:${regexps.join(')|(?:')}))` | ||
} | ||
|
||
protected produceExpression(segments: string[]): string { | ||
const regex = segments.join('') | ||
return `^${regex}$` | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could also export
RegExpCompiler
? That may be useful sometime?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only used internally, so I'd rather not. By exposing as little as possible we minimise the API surface to maintain.