FormatCompiler.ts
101 lines
| 2.9 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r79 | import { FormatScanner, TokeType } from "./FormatScanner"; | ||
|
|
r81 | import { isNullOrEmptyString } from "../safe"; | ||
| import { TextWriter } from "../interfaces"; | ||||
|
|
r79 | |||
| export class FormatCompiler { | ||||
|
|
r81 | _scanner: FormatScanner; | ||
|
|
r79 | |||
|
|
r81 | _parts: []; | ||
| compile() { | ||||
| return (writer: TextWriter, args: any) => { | ||||
| this._parts.forEach(x => writer.WriteValue(x)) | ||||
| }; | ||||
| } | ||||
| visitText() { | ||||
| while (this._scanner.next()) { | ||||
| switch (this._scanner.getTokenType()) { | ||||
| case TokeType.CurlOpen: | ||||
| this.visitCurlOpen(); | ||||
| break; | ||||
| case TokeType.CurlClose: | ||||
| this.visitCurlClose(); | ||||
| break; | ||||
| default: | ||||
| this.pushText(this._scanner.getTokenValue()); | ||||
| } | ||||
| if (this._scanner.getTokenType() === TokeType.CurlOpen) | ||||
| this.visitCurlOpen(); | ||||
|
|
r79 | } | ||
| } | ||||
|
|
r81 | visitCurlClose() { | ||
| if (!this._scanner.next()) | ||||
| this.dieUnexpectedEnd("}"); | ||||
| if (this._scanner.getTokenType() !== TokeType.CurlClose) | ||||
| this.dieUnexpectedToken("}"); | ||||
| this.pushText("}"); | ||||
| } | ||||
| visitCurlOpen() { | ||||
| if (this._scanner.next()) { | ||||
| if (this._scanner.getTokenType() === TokeType.CurlOpen) | ||||
|
|
r79 | this.pushText("{"); | ||
| else | ||||
|
|
r81 | this.visitTemplateSubst(); | ||
|
|
r79 | } | ||
| } | ||||
|
|
r81 | visitTemplateSubst() { | ||
| if (this._scanner.getTokenType() !== TokeType.Text) | ||||
| this.dieUnexpectedToken("TEXT"); | ||||
|
|
r79 | |||
|
|
r81 | const fieldName = this._scanner.getTokenValue(); | ||
| const filedFormat = this.readColon() && this.readFieldFormat(); | ||||
|
|
r79 | |||
| this.pushSubst(fieldName, filedFormat); | ||||
| } | ||||
|
|
r80 | |||
|
|
r81 | readFieldFormat() { | ||
| const parts = new Array<string>(); | ||||
| while (this._scanner.next()) { | ||||
| if (this._scanner.getTokenType() === TokeType.CurlClose) { | ||||
| return parts.join(""); | ||||
| } else { | ||||
| parts.push(this._scanner.getTokenValue()); | ||||
| } | ||||
| } | ||||
| this.dieUnexpectedEnd("}"); | ||||
|
|
r79 | } | ||
|
|
r80 | |||
|
|
r81 | readColon() { | ||
| if (!this._scanner.next()) | ||||
| this.dieUnexpectedEnd(); | ||||
| if (this._scanner.getTokenType() !== TokeType.Colon) | ||||
| return false; | ||||
| if (!this._scanner.next()) | ||||
| this.dieUnexpectedEnd(); | ||||
| return true; | ||||
|
|
r79 | } | ||
|
|
r80 | |||
|
|
r81 | pushSubst(fieldName: string, filedFormat: string) { | ||
|
|
r80 | |||
|
|
r79 | } | ||
| pushText(text: string) { | ||||
| } | ||||
|
|
r81 | dieUnexpectedToken(expected?: string) { | ||
| throw new Error(isNullOrEmptyString(expected) ? | ||||
| `Unexpected token ${this._scanner.getTokenValue()}` : | ||||
| `Unexpected token ${this._scanner.getTokenValue()}, expected ${expected}` | ||||
| ); | ||||
|
|
r79 | } | ||
|
|
r81 | dieUnexpectedEnd(expected?: string) { | ||
| throw new Error(isNullOrEmptyString(expected) ? "Unexpected end of the string" : `Unexpected end of the string, expected ${expected}`); | ||||
|
|
r79 | } | ||
| } | ||||
