Skip to content

Commit aa942c8

Browse files
committed
wip
1 parent 5da4bd8 commit aa942c8

8 files changed

Lines changed: 166 additions & 72 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"node_modules": true,
55
"package-lock.json": true
66
},
7-
"deno.enable": false,
7+
"deno.enable": true,
88
"deno.lint": false,
99
}

src/token/binary.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
129
// ----- Helpers -----
230
type D = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9';
331
type C = '0'|'1';

src/token/char.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ParseBox
44
55
The MIT License (MIT)
66
7-
Copyright (c) 2024-2025 Haydn Paterson
7+
Copyright (c) 2024-2025 Haydn Paterson
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal

src/token/const.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ParseBox
44
55
The MIT License (MIT)
66
7-
Copyright (c) 2017-2025 Haydn Paterson
7+
Copyright (c) 2024-2025 Haydn Paterson
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -34,16 +34,10 @@ import { type TTake, Take } from './take.ts'
3434
// ------------------------------------------------------------------
3535
// Const
3636
// ------------------------------------------------------------------
37-
export type TConst<Input extends string, Value extends string,
38-
Trimmed extends string = TTrim<Input>,
39-
> = TTake<Trimmed, [Value]>
40-
41-
export function Const<Input extends string, Value extends string>
42-
(input: Input, value: Value):
43-
TConst<Input, Value> {
44-
const trimmed = Trim(input)
45-
return Take(trimmed, [value]) as never
37+
export type TConst<Input extends string, Value extends string> = (
38+
TTake<TTrim<Input>, [Value]>
39+
)
40+
export function Const<Input extends string, Value extends string>(input: Input, value: Value): TConst<Input, Value> {
41+
return Take(Trim(input), [value]) as never
4642
}
4743

48-
const X = Const('hello workd', 'hello')
49-

src/token/ident.ts

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ParseBox
44
55
The MIT License (MIT)
66
7-
Copyright (c) 2017-2025 Haydn Paterson
7+
Copyright (c) 2024-2025 Haydn Paterson
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -27,55 +27,80 @@ THE SOFTWARE.
2727
---------------------------------------------------------------------------*/
2828

2929
// deno-fmt-ignore-file
30+
3031
import { type TTrim, Trim } from './trim.ts'
31-
import { type TTake, type TTakeResult, Take, IsTakeTrue } from './take.ts'
32+
import { type TTake, Take, IsTakeTrue } from './take.ts'
3233

3334
import * as Char from './char.ts'
3435

3536
// ------------------------------------------------------------------
36-
// TFirst
37+
// TakeFirst
3738
// ------------------------------------------------------------------
38-
type TIdentFirst = [...Char.TAlpha, Char.TUnderscore, Char.TDollarSign]
39-
type TIdentRest = [...TIdentFirst, ...Char.TDigit]
40-
41-
const IdentFirst = [...Char.Alpha, Char.Underscore, Char.DollarSign]
42-
const IdentRest = [...IdentFirst, ...Char.Digit]
43-
44-
39+
type TFirst = [...Char.TAlpha, Char.TUnderscore, Char.TDollarSign]
40+
const First = [...Char.Alpha, Char.Underscore, Char.DollarSign]
4541

42+
type TTakeFirst<Input extends string> = (
43+
TTake<Input, TFirst> extends [infer First extends string, infer Right extends string]
44+
? [First, Right]
45+
: []
46+
)
47+
function TakeFirst<Input extends string>(input: Input): TTakeFirst<Input> {
48+
const firstResult = Take(input, First) as string[]
49+
return (
50+
IsTakeTrue(firstResult)
51+
? firstResult
52+
: []
53+
) as never
54+
}
55+
// ------------------------------------------------------------------
56+
// TakeRemaining
57+
// ------------------------------------------------------------------
58+
type TRemaining = [...TFirst, ...Char.TDigit]
59+
const Remaining = [...First, ...Char.Digit]
4660

61+
type TTakeRemaining<Input extends string, Result extends string = ''> = (
62+
TTake<Input, TRemaining> extends [infer Next extends string, infer Right extends string]
63+
? TTakeRemaining<Right, `${Result}${Next}`>
64+
: [Result, Input]
65+
)
66+
function TakeRemaining<Input extends string>(input: Input, result: string = ''): TTakeRemaining<Input> {
67+
const remainingResult = Take(input, Remaining) as string[]
68+
return (
69+
IsTakeTrue(remainingResult)
70+
? TakeRemaining(remainingResult[1], `${result}${remainingResult[1]}`)
71+
: [input, result]
72+
) as never
73+
}
4774
// ------------------------------------------------------------------
48-
// TakeFirst
75+
// TakeIdent
4976
// ------------------------------------------------------------------
50-
type TTaskFirst<Input extends string> = (
51-
TTake<Input, TIdentFirst> extends [infer First extends string, infer Rest extends string]
52-
? TTake<Rest, TIdentRest> extends [infer Remaining extends string, infer Rest extends string]
77+
type TTakeIdent<Input extends string> = (
78+
TTakeFirst<Input> extends [infer First extends string, infer Remaining extends string]
79+
? TTakeRemaining<Remaining> extends [infer Remaining extends string, infer Rest extends string]
5380
? [`${First}${Remaining}`, Rest]
54-
: []
81+
: never
5582
: []
5683
)
57-
function TakeFirst<Input extends string>(input: Input): TTaskFirst<Input> {
58-
const firstResult = Take(input, IdentFirst) as TTakeResult
84+
function TakeIdent<Input extends string>(input: Input): TTakeIdent<Input> {
85+
const firstResult = TakeFirst(input)
5986
return (
60-
IsTakeTrue(firstResult) ? (() => {
61-
const restResult = Take(firstResult[1], IdentRest as never) as TTakeResult
62-
return IsTakeTrue(restResult)
63-
? [`${firstResult[0]}${restResult[0]}`, restResult[1]]
64-
: []
87+
IsTakeTrue(firstResult) ? (() => {
88+
const remainingResult = TakeRemaining(firstResult[1])
89+
return IsTakeTrue(remainingResult)
90+
? [`${firstResult[0]}${remainingResult[0]}`, remainingResult[1]]
91+
: (() => { throw 'Unreachable' })()
6592
})() : []
6693
) as never
6794
}
68-
69-
type A = TTaskFirst<'hello world'>
70-
7195
// ------------------------------------------------------------------
7296
// Tdent
7397
// ------------------------------------------------------------------
74-
export type TIdent<Input extends string,
75-
Trimmed extends string = TTrim<Input>,
76-
> = TTaskFirst<Trimmed>
98+
export type TIdent<Input extends string> = (
99+
TTakeIdent<TTrim<Input>>
100+
)
77101
export function Ident<Input extends string>(input: Input): TIdent<Input> {
78-
const trimmed = Trim(input)
79-
return TakeFirst(trimmed) as never
102+
return TakeIdent(Trim(input)) as never
80103
}
81104

105+
type B = TIdent<' hello world'>
106+

src/token/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
export { type TConst, Const } from './const.ts'
30+
export { type TIdent, Ident } from './ident.ts'

src/token/take.ts

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2025 Haydn Paterson
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
129
import { Guard } from '../guard/index.ts'
230

31+
// ------------------------------------------------------------------
32+
// Guard
33+
// ------------------------------------------------------------------
34+
export function IsTakeTrue(result: unknown[]): result is [string, string] {
35+
return Guard.IsEqual(result.length, 2)
36+
}
337
// ------------------------------------------------------------------
438
// FromString
539
// ------------------------------------------------------------------
6-
type TTakeString<Input extends string, Value extends string,
7-
Result extends [Value, string] | [] = Input extends `${Value}${infer Right extends string}`
8-
? [Value, Right]
40+
type TTakeString<Input extends string, Value extends string> = (
41+
Input extends `${Value}${infer Rest extends string}`
42+
? [Value, Rest]
943
: []
10-
> = Result
11-
function TakeString<Input extends string, Value extends string>
12-
(input: Input, value: Value):
13-
TTakeString<Input, Value> {
44+
)
45+
function TakeString<Input extends string, Value extends string>(input: Input, value: Value): TTakeString<Input, Value> {
1446
return (
1547
Guard.IsEqual(input.indexOf(value), 0)
1648
? [value, input.slice(value.length)]
1749
: []
1850
) as never
1951
}
2052
// ------------------------------------------------------------------
21-
// Guard
22-
// ------------------------------------------------------------------
23-
export type TTakeFalse = []
24-
export type TTakeTrue<Value extends string = string, Rest extends string = string> = [Value, Rest]
25-
export type TTakeResult<Value extends string = string, Rest extends string = string> = TTakeTrue<Value, Rest> | TTakeFalse
26-
27-
export function IsTakeTrue(result: TTakeResult): result is TTakeTrue {
28-
return Guard.IsEqual(result.length, 2)
29-
}
30-
export function IsTakeFalse(result: TTakeResult): result is TTakeFalse {
31-
return !IsTakeTrue(result)
32-
}
33-
// ------------------------------------------------------------------
3453
// Take
3554
// ------------------------------------------------------------------
36-
/** Takes one of the given values from the input string, or [] if not found */
3755
export type TTake<Input extends string, Values extends string[]> = (
38-
Values extends [infer Left extends string, ...infer Right extends string[]]
39-
? TTakeString<Input, Left> extends [infer Left extends string, infer Right extends string]
40-
? TTakeTrue<Left, Right>
41-
: TTake<Input, Right>
42-
: TTakeFalse
56+
Values extends [infer ValueLeft extends string, ...infer ValueRight extends string[]]
57+
? TTakeString<Input, ValueLeft> extends [infer Take extends string, infer Rest extends string]
58+
? [Take, Rest]
59+
: TTake<Input, ValueRight>
60+
: []
4361
)
44-
/** Takes one of the given values from the input string, or [] if not found */
4562
export function Take<Input extends string, Values extends string[]>
4663
(input: Input, values: [...Values]):
4764
TTake<Input, Values> {

src/token/trim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ParseBox
44
55
The MIT License (MIT)
66
7-
Copyright (c) 2017-2025 Haydn Paterson
7+
Copyright (c) 2024-2025 Haydn Paterson
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)