Skip to content

Commit 01ea33d

Browse files
authored
Revision 0.9.3 (#7)
1 parent 8ccf1a2 commit 01ea33d

File tree

25 files changed

+881
-284
lines changed

25 files changed

+881
-284
lines changed

deno.lock

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ console.log(Json)
142142
})
143143
console.log(project.parser) // parser file content
144144
console.log(project.mapping) // semantic mapping file content
145-
}
145+
}

example/typebox/compiled/module.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,40 @@ const GenericReference = Runtime.Tuple([
9999
// Reference
100100
// ------------------------------------------------------------------
101101
const Reference = Runtime.Ident()
102-
102+
// ------------------------------------------------------------------
103+
// TemplateText
104+
// ------------------------------------------------------------------
105+
const TemplateText = Runtime.Union([
106+
Runtime.Until('${'),
107+
Runtime.Until('`'),
108+
])
109+
// ------------------------------------------------------------------
110+
// TemplateInterpolate
111+
// ------------------------------------------------------------------
112+
const TemplateInterpolate = Runtime.Tuple([
113+
Runtime.Const('${'),
114+
Runtime.Ref('Type'),
115+
Runtime.Const('}')
116+
])
117+
// ------------------------------------------------------------------
118+
// TemplateBody
119+
// ------------------------------------------------------------------
120+
const TemplateBody = Runtime.Union([
121+
Runtime.Tuple([Runtime.Ref('TemplateText'), Runtime.Ref('TemplateInterpolate'), Runtime.Ref('TemplateBody')]),
122+
Runtime.Tuple([Runtime.Ref('TemplateText')]),
123+
])
124+
// ------------------------------------------------------------------
125+
// TemplateLiteral
126+
// ------------------------------------------------------------------
127+
const TemplateLiteral = Runtime.Tuple([
128+
Runtime.Const('`'),
129+
Runtime.Ref('TemplateBody'),
130+
Runtime.Const('`'),
131+
])
103132
// ------------------------------------------------------------------
104133
// Literal
105134
// ------------------------------------------------------------------
106-
const LiteralString = Runtime.String([SingleQuote, DoubleQuote, Tilde])
135+
const LiteralString = Runtime.String([SingleQuote, DoubleQuote])
107136
const LiteralNumber = Runtime.Number()
108137
const LiteralBoolean = Runtime.Union([Runtime.Const('true'), Runtime.Const('false')])
109138
const Literal = Runtime.Union([
@@ -177,6 +206,7 @@ const Base = Runtime.Union([
177206
Runtime.Ref('Object'),
178207
Runtime.Ref('Tuple'),
179208
Runtime.Ref('Literal'),
209+
Runtime.Ref('TemplateLiteral'),
180210
Runtime.Ref('Constructor'),
181211
Runtime.Ref('Function'),
182212
Runtime.Ref('Mapped'),
@@ -589,6 +619,10 @@ export const SyntaxModule = new Runtime.Module({
589619
KeywordSymbol,
590620
KeywordVoid,
591621
Keyword,
622+
TemplateInterpolate,
623+
TemplateBody,
624+
TemplateText,
625+
TemplateLiteral,
592626
LiteralString,
593627
LiteralNumber,
594628
LiteralBoolean,

example/typebox/interpreted/runtime.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,60 @@ function ReferenceMapping(result: string, context: T.TProperties) {
133133
return target
134134
}
135135
const Reference = Runtime.Ident((result, context) => ReferenceMapping(result, context))
136+
// ------------------------------------------------------------------
137+
// TemplateText
138+
// ------------------------------------------------------------------
139+
function TemplateTextMapping(input: string) {
140+
return T.Literal(input)
141+
}
142+
const TemplateText = Runtime.Union([
143+
Runtime.Until('${'),
144+
Runtime.Until('`'),
145+
], TemplateTextMapping)
146+
// ------------------------------------------------------------------
147+
// TemplateInterpolate
148+
// ------------------------------------------------------------------
149+
function TemplateInterpolateMapping(input: [unknown, unknown, unknown]) {
150+
return input[1]
151+
}
152+
const TemplateInterpolate = Runtime.Tuple([
153+
Runtime.Const('${'),
154+
Runtime.Ref('Type'),
155+
Runtime.Const('}')
156+
], TemplateInterpolateMapping)
157+
// ------------------------------------------------------------------
158+
// TemplateBody
159+
// ------------------------------------------------------------------
160+
function TemplateBodyMapping(input: [unknown] | [unknown, unknown, unknown]) {
161+
return (
162+
input.length === 3
163+
? [input[0], input[1], ...input[2] as unknown[]]
164+
: [input[0]]
165+
)
166+
}
167+
const TemplateBody = Runtime.Union([
168+
Runtime.Tuple([Runtime.Ref('TemplateText'), Runtime.Ref('TemplateInterpolate'), Runtime.Ref('TemplateBody')]),
169+
Runtime.Tuple([Runtime.Ref('TemplateText')]),
170+
], TemplateBodyMapping)
171+
// ------------------------------------------------------------------
172+
// TemplateLiteral
173+
// ------------------------------------------------------------------
174+
function TemplateLiteralMapping(input: [unknown, unknown, unknown]) {
175+
return T.TemplateLiteral(input[1] as T.TTemplateLiteralKind[])
176+
}
177+
const TemplateLiteral = Runtime.Tuple([
178+
Runtime.Const('`'),
179+
Runtime.Ref('TemplateBody'),
180+
Runtime.Const('`'),
181+
], TemplateLiteralMapping)
182+
136183
// ------------------------------------------------------------------
137184
// Literal
138185
// ------------------------------------------------------------------
139186
const Literal = Runtime.Union([
140187
Runtime.Union([Runtime.Const('true'), Runtime.Const('false')], value => T.Literal(value === 'true')),
141188
Runtime.Number(value => T.Literal(parseFloat(value))),
142-
Runtime.String([SingleQuote, DoubleQuote, Tilde], value => T.Literal(value))
189+
Runtime.String([SingleQuote, DoubleQuote], value => T.Literal(value))
143190
])
144191
// ------------------------------------------------------------------
145192
// Keyword
@@ -208,6 +255,7 @@ const Base = Runtime.Union([
208255
Runtime.Ref('Object'),
209256
Runtime.Ref('Tuple'),
210257
Runtime.Ref('Literal'),
258+
Runtime.Ref('TemplateLiteral'),
211259
Runtime.Ref('Constructor'),
212260
Runtime.Ref('Function'),
213261
Runtime.Ref('Mapped'),
@@ -630,12 +678,17 @@ const Date = Runtime.Const('Date', Runtime.As(T.Date()))
630678
// Uint8Array
631679
// ------------------------------------------------------------------
632680
const Uint8Array = Runtime.Const('Uint8Array', Runtime.As(T.Uint8Array()))
681+
633682
// ------------------------------------------------------------------
634683
// Module
635684
// ------------------------------------------------------------------
636685
export const Module = new Runtime.Module({
637686
GenericArgumentsList,
638687
GenericArguments,
688+
TemplateInterpolate,
689+
TemplateBody,
690+
TemplateText,
691+
TemplateLiteral,
639692
Literal,
640693
Keyword,
641694
KeyOf,

example/typebox/interpreted/static.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,56 @@ type Dereference<Context extends T.TProperties, Ref extends string> = (
9191
Ref extends keyof Context ? Context[Ref] : T.TRef<Ref>
9292
)
9393
// ------------------------------------------------------------------
94+
// TemplateText
95+
// ------------------------------------------------------------------
96+
interface TemplateTextMapping extends Static.IMapping {
97+
output: this['input'] extends string ? T.TLiteral<this['input']> : never
98+
}
99+
type TemplateText = Static.Union<[
100+
Static.Until<'${'>,
101+
Static.Until<'`'>,
102+
], TemplateTextMapping>
103+
// ------------------------------------------------------------------
104+
// TemplateInterpolate
105+
// ------------------------------------------------------------------
106+
interface TemplateInterpolateMapping extends Static.IMapping {
107+
output: this['input'] extends ['${', infer Type extends T.TSchema, '}']
108+
? Type
109+
: never
110+
}
111+
type TemplateInterpolate = Static.Tuple<[
112+
Static.Const<'${'>,
113+
Type,
114+
Static.Const<'}'>
115+
], TemplateInterpolateMapping>
116+
// ------------------------------------------------------------------
117+
// TemplateBody
118+
// ------------------------------------------------------------------
119+
interface TemplateBodyMapping extends Static.IMapping {
120+
output: (
121+
this['input'] extends [infer Text extends T.TSchema, infer Type extends T.TSchema, infer Rest extends T.TSchema[]] ? [Text, Type, ...Rest] :
122+
this['input'] extends [infer Text extends T.TSchema] ? [Text] :
123+
[]
124+
)
125+
}
126+
type TemplateBody = Static.Union<[
127+
Static.Tuple<[TemplateText, TemplateInterpolate, TemplateBody]>,
128+
Static.Tuple<[TemplateText]>,
129+
], TemplateBodyMapping>
130+
// ------------------------------------------------------------------
131+
// TemplateLiteral
132+
// ------------------------------------------------------------------
133+
interface TemplateLiteralMapping extends Static.IMapping {
134+
output: this['input'] extends ['`', infer Types extends T.TTemplateLiteralKind[], '`']
135+
? T.TTemplateLiteral<Types>
136+
: never
137+
}
138+
type TemplateLiteral = Static.Tuple<[
139+
Static.Const<'`'>,
140+
TemplateBody,
141+
Static.Const<'`'>,
142+
], TemplateLiteralMapping>
143+
// ------------------------------------------------------------------
94144
// GenericArguments
95145
// ------------------------------------------------------------------
96146
type GenericArgumentsContext<Args extends string[], Context extends T.TProperties, Result extends T.TProperties = {}> = (
@@ -154,7 +204,7 @@ interface LiteralStringMapping extends Static.IMapping {
154204
type Literal = Static.Union<[
155205
Static.Union<[Static.Const<'true'>, Static.Const<'false'>], LiteralBooleanMapping>,
156206
Static.Number<LiteralNumberMapping>,
157-
Static.String<[DoubleQuote, SingleQuote, Tilde], LiteralStringMapping>,
207+
Static.String<[DoubleQuote, SingleQuote], LiteralStringMapping>,
158208
]>
159209
// ------------------------------------------------------------------
160210
// Keyword
@@ -230,6 +280,7 @@ type Base = Static.Union<[
230280
Object,
231281
Tuple,
232282
Literal,
283+
TemplateLiteral,
233284
Function,
234285
Constructor,
235286
Mapped,

readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ License: MIT
6969
- [Tuple](#Tuple)
7070
- [Union](#Union)
7171
- [Array](#Array)
72+
- [Until](#Until)
7273
- [Optional](#Optional)
7374
- [Epsilon](#Epsilon)
7475
- [Terminals](#Terminals)
@@ -186,6 +187,27 @@ const R2 = Runtime.Parse(T, 'X X X Y Z') // const R2 = [['X', 'X', '
186187
const R3 = Runtime.Parse(T, 'Y Z') // const R3 = [[], 'Y Z']
187188
```
188189

190+
### Until
191+
192+
The Until combinator parses all characters up to (but not including) the specified string. The specified string remains unconsumed in the input. If the string is not found, parsing fails.
193+
194+
**BNF**
195+
196+
```bnf
197+
<T> ::= ? any character until 'Z' ?
198+
```
199+
200+
**TypeScript**
201+
202+
```typescript
203+
const T = Runtime.Until('Z') // const T = {
204+
// type: 'Until',
205+
// value: 'X'
206+
// }
207+
208+
const R = Runtime.Parse(T, 'X Y Z') // const R = ['X Y ', 'Z']
209+
```
210+
189211
### Optional
190212

191213
The Optional combinator parses zero or one occurrence of the interior parser, returning a tuple with one element or an empty tuple if there is no match.

src/compile/common/comment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function FromRef(parser: Runtime.IRef): string {
5353
function FromConst(parser: Runtime.IConst): string {
5454
return `'${Escape(parser.value)}'`
5555
}
56+
function FromUntil(parser: Runtime.IUntil): string {
57+
return `string`
58+
}
5659
function FromIdent(parser: Runtime.IIdent): string {
5760
return `<Ident>`
5861
}
@@ -71,6 +74,7 @@ function FromParser(parser: Runtime.IParser): string {
7174
Runtime.IsOptional(parser) ? FromOptional(parser) :
7275
Runtime.IsString(parser) ? FromString(parser) :
7376
Runtime.IsConst(parser) ? FromConst(parser) :
77+
Runtime.IsUntil(parser) ? FromUntil(parser) :
7478
Runtime.IsRef(parser) ? FromRef(parser) :
7579
Runtime.IsIdent(parser) ? FromIdent(parser) :
7680
Runtime.IsNumber(parser) ? FromNumber(parser) :

src/compile/common/infer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ function InferOptional(parser: Runtime.IParser) {
4949
function InferConst(parser: Runtime.IConst) {
5050
return `'${parser.value}'`
5151
}
52+
function InferUntil(parser: Runtime.IUntil) {
53+
return `string`
54+
}
5255
export function Infer(parser: Runtime.IParser): string {
5356
return (
5457
Runtime.IsContext(parser) ? InferContext(parser.right, parser.right) :
@@ -58,6 +61,7 @@ export function Infer(parser: Runtime.IParser): string {
5861
Runtime.IsOptional(parser) ? InferOptional(parser.parser) :
5962
Runtime.IsRef(parser) ? `unknown` :
6063
Runtime.IsConst(parser) ? InferConst(parser) :
64+
Runtime.IsUntil(parser) ? InferUntil(parser) :
6165
Runtime.IsString(parser) ? `string` :
6266
Runtime.IsIdent(parser) ? `string` :
6367
Runtime.IsNumber(parser) ? `string` :

src/compile/func/func.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ function FromConst(options: Options, name: string, value: string): string {
8383
return `Runtime.Token.Const('${Escape(value)}', input)`
8484
}
8585
// ------------------------------------------------------------------
86+
// Const
87+
// ------------------------------------------------------------------
88+
function FromUntil(options: Options, name: string, value: string): string {
89+
return `Runtime.Token.Until('${Escape(value)}', input)`
90+
}
91+
// ------------------------------------------------------------------
8692
// Ident
8793
// ------------------------------------------------------------------
8894
function FromIdent(options: Options, name: string): string {
@@ -119,6 +125,7 @@ function FromParser(options: Options, name: string, parser: Runtime.IParser): st
119125
Runtime.IsOptional(parser) ? FromOptional(options, name, parser) :
120126
Runtime.IsString(parser) ? FromString(options, name, parser.options) :
121127
Runtime.IsConst(parser) ? FromConst(options, name, parser.value) :
128+
Runtime.IsUntil(parser) ? FromUntil(options, name, parser.value) :
122129
Runtime.IsRef(parser) ? FromRef(options, name, parser.ref) :
123130
Runtime.IsIdent(parser) ? FromIdent(options, name) :
124131
Runtime.IsNumber(parser) ? FromNumber(options, name) :

0 commit comments

Comments
 (0)