Skip to content

Commit 4f9a3b3

Browse files
committed
Unsigned | Match
1 parent 39f5c56 commit 4f9a3b3

23 files changed

+746
-302
lines changed

src/runtime/parse.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import { ParseRest, IsRest } from './rest.ts'
4646
import { ParseString, IsString } from './string.ts'
4747
import { ParseTuple, IsTuple } from './tuple.ts'
4848
import { ParseUnion, IsUnion } from './union.ts'
49+
import { ParseUnsignedInteger, IsUnsignedInteger } from './unsigned_integer.ts'
50+
import { ParseUnsignedNumber, IsUnsignedNumber} from './unsigned_number.ts'
4951
import { ParseUntil_1, IsUntil_1 } from './until_1.ts'
5052
import { ParseUntil, IsUntil } from './until.ts'
5153

@@ -66,6 +68,8 @@ export function ParseParser<Parser extends IParser>(context: IProperties, parser
6668
IsString(parser) ? ParseString(parser.quotes, input) :
6769
IsTuple(parser) ? ParseTuple(context, parser.parsers, input) :
6870
IsUnion(parser) ? ParseUnion(context, parser.parsers, input) :
71+
IsUnsignedInteger(parser) ? ParseUnsignedInteger(input) :
72+
IsUnsignedNumber(parser) ? ParseUnsignedNumber(input) :
6973
IsUntil(parser) ? ParseUntil(parser.end, input) :
7074
IsUntil_1(parser) ? ParseUntil_1(parser.end, input) :
7175
[]

src/runtime/unsigned_integer.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2026 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+
// deno-fmt-ignore-file
30+
31+
import * as Token from '../token/index.ts'
32+
33+
import { Arguments } from '../system/arguments/index.ts'
34+
import { Guard } from '../guard/index.ts'
35+
import { type IParser, type IMapping, Identity } from './parser.ts'
36+
37+
// ------------------------------------------------------------------
38+
// Type
39+
// ------------------------------------------------------------------
40+
export interface IUnsignedInteger<Output extends unknown = unknown> extends IParser<Output> {
41+
type: 'UnsignedInteger'
42+
}
43+
// ------------------------------------------------------------------
44+
// Factory
45+
// ------------------------------------------------------------------
46+
export function UnsignedInteger<Mapping extends IMapping<string>>(mapping: Mapping): IUnsignedInteger<ReturnType<Mapping>>
47+
export function UnsignedInteger(): IUnsignedInteger<string>
48+
export function UnsignedInteger(...args: unknown[]): never {
49+
const [mapping] = Arguments.Match<[IParser, IMapping]>(args, {
50+
1: (mapping) => [mapping],
51+
0: () => [Identity]
52+
})
53+
return { type: 'UnsignedInteger', mapping } as never
54+
}
55+
// ------------------------------------------------------------------
56+
// Guard
57+
// ------------------------------------------------------------------
58+
export function IsUnsignedInteger(value: unknown): value is IUnsignedInteger {
59+
return Guard.IsObject(value)
60+
&& Guard.HasPropertyKey(value, 'type')
61+
&& Guard.IsEqual(value.type, 'UnsignedInteger')
62+
}
63+
// ------------------------------------------------------------------
64+
// Parse
65+
// ------------------------------------------------------------------
66+
export function ParseUnsignedInteger(input: string): [] | [string, string] {
67+
return Token.UnsignedInteger(input)
68+
}

src/runtime/unsigned_number.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2026 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+
// deno-fmt-ignore-file
30+
31+
import * as Token from '../token/index.ts'
32+
33+
import { Arguments } from '../system/arguments/index.ts'
34+
import { Guard } from '../guard/index.ts'
35+
import { type IParser, type IMapping, Identity } from './parser.ts'
36+
37+
// ------------------------------------------------------------------
38+
// Type
39+
// ------------------------------------------------------------------
40+
export interface IUnsignedNumber<Output extends unknown = unknown> extends IParser<Output> {
41+
type: 'UnsignedNumber'
42+
}
43+
// ------------------------------------------------------------------
44+
// Factory
45+
// ------------------------------------------------------------------
46+
export function UnsignedNumber<Mapping extends IMapping<string>>(mapping: Mapping): IUnsignedNumber<ReturnType<Mapping>>
47+
export function UnsignedNumber(): IUnsignedNumber<string>
48+
export function UnsignedNumber(...args: unknown[]): never {
49+
const [mapping] = Arguments.Match<[IParser, IMapping]>(args, {
50+
1: (mapping) => [mapping],
51+
0: () => [Identity]
52+
})
53+
return { type: 'UnsignedNumber', mapping } as never
54+
}
55+
// ------------------------------------------------------------------
56+
// Guard
57+
// ------------------------------------------------------------------
58+
export function IsUnsignedNumber(value: unknown): value is IUnsignedNumber {
59+
return Guard.IsObject(value)
60+
&& Guard.HasPropertyKey(value, 'type')
61+
&& Guard.IsEqual(value.type, 'UnsignedNumber')
62+
}
63+
// ------------------------------------------------------------------
64+
// Parse
65+
// ------------------------------------------------------------------
66+
export function ParseUnsignedNumber(input: string): [] | [string, string] {
67+
return Token.UnsignedNumber(input)
68+
}

src/static/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ export type { Rest } from './rest.ts'
3939
export type { String } from './string.ts'
4040
export type { Tuple } from './tuple.ts'
4141
export type { Union } from './union.ts'
42+
export type { UnsignedInteger } from './unsigned_integer.ts'
43+
export type { UnsignedNumber } from './unsigned_number.ts'
4244
export type { Until_1 } from './until_1.ts'
4345
export type { Until } from './until.ts'

src/static/parse.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import type { ParseRest, Rest } from './rest.ts'
3939
import type { ParseString, String } from './string.ts'
4040
import type { ParseTuple, Tuple } from './tuple.ts'
4141
import type { ParseUnion, Union } from './union.ts'
42+
import type { ParseUnsignedInteger, UnsignedInteger } from './unsigned_integer.ts'
43+
import type { ParseUnsignedNumber, UnsignedNumber } from './unsigned_number.ts'
4244
import type { ParseUntil_1, Until_1 } from './until_1.ts'
4345
import type { ParseUntil, Until } from './until.ts'
4446

@@ -58,10 +60,12 @@ type ParseInput<Input extends string, Parser extends IParser> = (
5860
Parser extends Rest ? ParseRest<Input> :
5961
Parser extends String<infer Quotes extends string[]> ? ParseString<Quotes, Input> :
6062
Parser extends Tuple<infer Parsers extends IParser[]> ? ParseTuple<Parsers, Input> :
61-
Parser extends Union<infer Parsers extends IParser[]> ? ParseUnion<Parsers, Input> :
63+
Parser extends Union<infer Parsers extends IParser[]> ? ParseUnion<Parsers, Input> :
64+
Parser extends UnsignedInteger ? ParseUnsignedInteger<Input> :
65+
Parser extends UnsignedNumber ? ParseUnsignedNumber<Input> :
6266
Parser extends Until<infer End extends string[]> ? ParseUntil<End, Input> :
63-
Parser extends Until_1<infer End extends string[]> ? ParseUntil_1<End, Input>
64-
: []
67+
Parser extends Until_1<infer End extends string[]> ? ParseUntil_1<End, Input> :
68+
[]
6569
)
6670
// ------------------------------------------------------------------
6771
// ParseMapping

src/static/unsigned_integer.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2026 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+
// deno-fmt-ignore-file
30+
31+
import type { Identity, IMapping, IParser } from './parser.ts'
32+
import * as Token from '../token/index.ts'
33+
34+
// ------------------------------------------------------------------
35+
// Type
36+
// ------------------------------------------------------------------
37+
export interface UnsignedInteger<Mapping extends IMapping = Identity> extends IParser<Mapping> {
38+
type: 'UnsignedInteger'
39+
}
40+
// ------------------------------------------------------------------
41+
// Parse
42+
// ------------------------------------------------------------------
43+
export type ParseUnsignedInteger<Input extends string> = (
44+
Token.TUnsignedInteger<Input>
45+
)

src/static/unsigned_number.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*--------------------------------------------------------------------------
2+
3+
ParseBox
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2024-2026 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+
// deno-fmt-ignore-file
30+
31+
import type { Identity, IMapping, IParser } from './parser.ts'
32+
import * as Token from '../token/index.ts'
33+
34+
// ------------------------------------------------------------------
35+
// Type
36+
// ------------------------------------------------------------------
37+
export interface UnsignedNumber<Mapping extends IMapping = Identity> extends IParser<Mapping> {
38+
type: 'UnsignedNumber'
39+
}
40+
// ------------------------------------------------------------------
41+
// Parse
42+
// ------------------------------------------------------------------
43+
export type ParseUnsignedNumber<Input extends string> = (
44+
Token.TUnsignedNumber<Input>
45+
)

src/token/bigint.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { IsResult } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTake, Take } from './internal/take.ts'
3333
import { type TInteger, Integer } from './integer.ts'
3434

@@ -37,21 +37,17 @@ import { type TInteger, Integer } from './integer.ts'
3737
// ------------------------------------------------------------------
3838
type TTakeBigInt<Input extends string> = (
3939
TInteger<Input> extends [infer Integer extends string, infer IntegerRest extends string]
40-
? TTake<['n'], IntegerRest> extends [infer N extends string, infer NRest extends string]
40+
? TTake<['n'], IntegerRest> extends [infer _N extends string, infer NRest extends string]
4141
? [`${Integer}`, NRest]
4242
: [] // fail: did not match 'n'
4343
: [] // fail: did not match Integer
4444
)
4545
function TakeBigInt<Input extends string>(input: Input): TTakeBigInt<Input> {
46-
const integer = Integer(input)
47-
return (
48-
IsResult(integer) ? (() => {
49-
const n = Take(['n'], integer[1])
50-
return IsResult(n)
51-
? [`${integer[0]}`, n[1]]
52-
: [] // fail: did not match 'n'
53-
})() : [] // fail: did not match Integer
54-
) as never
46+
return Match(Integer(input), (Integer, IntegerRest) =>
47+
Match(Take(['n'], IntegerRest), (_N, NRest) =>
48+
[`${Integer}`, NRest],
49+
() => []), // fail: did not match 'n'
50+
() => []) as never // fail: did not match Integer
5551
}
5652
// ------------------------------------------------------------------
5753
// BigInt

src/token/ident.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ THE SOFTWARE.
2828

2929
// deno-fmt-ignore-file
3030

31-
import { IsResult } from './internal/result.ts'
31+
import { Match } from './internal/match.ts'
3232
import { type TTrim, Trim } from './internal/trim.ts'
3333
import { type TTake, Take } from './internal/take.ts'
3434

@@ -61,12 +61,9 @@ type TTakeRemaining<Input extends string, Result extends string = ''> = (
6161
: [Result, Input]
6262
)
6363
function TakeRemaining<Input extends string>(input: Input, result: string = ''): TTakeRemaining<Input> {
64-
const remaining = Take(Remaining, input) as string[]
65-
return (
66-
IsResult(remaining)
67-
? TakeRemaining(remaining[1], `${result}${remaining[0]}`)
68-
: [result, input]
69-
) as never
64+
return Match(Take(Remaining, input), (Remaining, RemainingRest) =>
65+
TakeRemaining(RemainingRest, `${result}${Remaining}`),
66+
() => [result, input]) as never
7067
}
7168
// ------------------------------------------------------------------
7269
// TakeIdent
@@ -79,15 +76,11 @@ type TTakeIdent<Input extends string> = (
7976
: [] // fail: did not match Initial
8077
)
8178
function TakeIdent<Input extends string>(input: Input): TTakeIdent<Input> {
82-
const initial = TakeInitial(input) as string[]
83-
return (
84-
IsResult(initial) ? (() => {
85-
const remaining = TakeRemaining(initial[1])
86-
return IsResult(remaining)
87-
? [`${initial[0]}${remaining[0]}`, remaining[1]]
88-
: [] // fail: did not match Remaining
89-
})() : [] // fail: did not match Initial
90-
) as never
79+
return Match(TakeInitial(input), (Initial, InitialRest) =>
80+
Match(TakeRemaining(InitialRest), (Remaining, RemainingRest) =>
81+
[`${Initial}${Remaining}`, RemainingRest],
82+
() => []), // fail: did not match Remaining
83+
() => []) as never // fail: did not match Initial
9184
}
9285
// ------------------------------------------------------------------
9386
// Ident

src/token/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ export * from './number.ts'
3434
export * from './rest.ts'
3535
export * from './span.ts'
3636
export * from './string.ts'
37+
export * from './unsigned_integer.ts'
38+
export * from './unsigned_number.ts'
3739
export * from './until_1.ts'
3840
export * from './until.ts'

0 commit comments

Comments
 (0)