-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
1f8a0cf
commit 646db90
Showing
42 changed files
with
1,311 additions
and
581 deletions.
There are no files selected for viewing
This file contains 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 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 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
35 changes: 35 additions & 0 deletions
35
src/application/Parser/Script/Compiler/Expressions/Expression/Expression.ts
This file contains 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,35 @@ | ||
import { ExpressionPosition } from './ExpressionPosition'; | ||
import { ExpressionArguments, IExpression } from './IExpression'; | ||
|
||
export type ExpressionEvaluator = (args?: ExpressionArguments) => string; | ||
export class Expression implements IExpression { | ||
constructor( | ||
public readonly position: ExpressionPosition, | ||
public readonly evaluator: ExpressionEvaluator, | ||
public readonly parameters: readonly string[] = new Array<string>()) { | ||
if (!position) { | ||
throw new Error('undefined position'); | ||
} | ||
if (!evaluator) { | ||
throw new Error('undefined evaluator'); | ||
} | ||
} | ||
public evaluate(args?: ExpressionArguments): string { | ||
args = filterUnusedArguments(this.parameters, args); | ||
return this.evaluator(args); | ||
} | ||
} | ||
|
||
function filterUnusedArguments( | ||
parameters: readonly string[], args: ExpressionArguments): ExpressionArguments { | ||
let result: ExpressionArguments = {}; | ||
for (const parameter of Object.keys(args)) { | ||
if (parameters.includes(parameter)) { | ||
result = { | ||
...result, | ||
[parameter]: args[parameter], | ||
}; | ||
} | ||
} | ||
return result; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition.ts
This file contains 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,15 @@ | ||
export class ExpressionPosition { | ||
constructor( | ||
public readonly start: number, | ||
public readonly end: number) { | ||
if (start === end) { | ||
throw new Error(`no length (start = end = ${start})`); | ||
} | ||
if (start > end) { | ||
throw Error(`start (${start}) after end (${end})`); | ||
} | ||
if (start < 0) { | ||
throw Error(`negative start position: ${start}`); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/application/Parser/Script/Compiler/Expressions/Expression/IExpression.ts
This file contains 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,12 @@ | ||
import { ExpressionPosition } from './ExpressionPosition'; | ||
|
||
export interface IExpression { | ||
readonly position: ExpressionPosition; | ||
readonly parameters?: readonly string[]; | ||
evaluate(args?: ExpressionArguments): string; | ||
} | ||
|
||
export interface ExpressionArguments { | ||
readonly [parameter: string]: string; | ||
} | ||
|
54 changes: 36 additions & 18 deletions
54
src/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.ts
This file contains 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
73 changes: 0 additions & 73 deletions
73
src/application/Parser/Script/Compiler/Expressions/ILCode.ts
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
src/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser.ts
This file contains 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,23 @@ | ||
import { IExpression } from '../Expression/IExpression'; | ||
import { IExpressionParser } from './IExpressionParser'; | ||
import { ParameterSubstitutionParser } from '../SyntaxParsers/ParameterSubstitutionParser'; | ||
|
||
const parsers = [ | ||
new ParameterSubstitutionParser(), | ||
]; | ||
|
||
export class CompositeExpressionParser implements IExpressionParser { | ||
public constructor(private readonly leafs: readonly IExpressionParser[] = parsers) { | ||
if (leafs.some((leaf) => !leaf)) { throw new Error('undefined leaf'); } | ||
} | ||
public findExpressions(code: string): IExpression[] { | ||
const expressions = new Array<IExpression>(); | ||
for (const parser of this.leafs) { | ||
const newExpressions = parser.findExpressions(code); | ||
if (newExpressions && newExpressions.length) { | ||
expressions.push(...newExpressions); | ||
} | ||
} | ||
return expressions; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/application/Parser/Script/Compiler/Expressions/Parser/IExpressionParser.ts
This file contains 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,5 @@ | ||
import { IExpression } from '../Expression/IExpression'; | ||
|
||
export interface IExpressionParser { | ||
findExpressions(code: string): IExpression[]; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.ts
This file contains 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,35 @@ | ||
import { IExpressionParser } from './IExpressionParser'; | ||
import { ExpressionPosition } from '../Expression/ExpressionPosition'; | ||
import { IExpression } from '../Expression/IExpression'; | ||
import { Expression, ExpressionEvaluator } from '../Expression/Expression'; | ||
|
||
export abstract class RegexParser implements IExpressionParser { | ||
protected abstract readonly regex: RegExp; | ||
public findExpressions(code: string): IExpression[] { | ||
return Array.from(this.findRegexExpressions(code)); | ||
} | ||
|
||
protected abstract buildExpression(match: RegExpMatchArray): IPrimitiveExpression; | ||
|
||
private* findRegexExpressions(code: string): Iterable<IExpression> { | ||
const matches = Array.from(code.matchAll(this.regex)); | ||
for (const match of matches) { | ||
const startPos = match.index; | ||
const endPos = startPos + match[0].length; | ||
let position: ExpressionPosition; | ||
try { | ||
position = new ExpressionPosition(startPos, endPos); | ||
} catch (error) { | ||
throw new Error(`[${this.constructor.name}] invalid script position: ${error.message}\nRegex ${this.regex}\nCode: ${code}`); | ||
} | ||
const primitiveExpression = this.buildExpression(match); | ||
const expression = new Expression(position, primitiveExpression.evaluator, primitiveExpression.parameters); | ||
yield expression; | ||
} | ||
} | ||
} | ||
|
||
export interface IPrimitiveExpression { | ||
evaluator: ExpressionEvaluator; | ||
parameters?: readonly string[]; | ||
} |
12 changes: 12 additions & 0 deletions
12
...plication/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.ts
This file contains 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,12 @@ | ||
import { RegexParser, IPrimitiveExpression } from '../Parser/RegexParser'; | ||
|
||
export class ParameterSubstitutionParser extends RegexParser { | ||
protected readonly regex = /{{\s*\$\s*([^}| ]+)\s*}}/g; | ||
protected buildExpression(match: RegExpMatchArray): IPrimitiveExpression { | ||
const parameterName = match[1]; | ||
return { | ||
parameters: [ parameterName ], | ||
evaluator: (args) => args[parameterName], | ||
}; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.