Skip to content

Commit d8a1a7e

Browse files
committed
wip
1 parent aa942c8 commit d8a1a7e

5 files changed

Lines changed: 118 additions & 124 deletions

File tree

example/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// deno-fmt-ignore-file
22

3+
import * as Token from '../src/token/index.ts'
34

4-
import { Static } from '@sinclair/parsebox'
5-
6-
type A = Static.Parse<Static.Number, '.1'>
7-
5+
const A = Token.Ident('')
86

97

108

src/token/char.ts

Lines changed: 24 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,10 @@ THE SOFTWARE.
2929
import { Guard } from '../guard/index.ts'
3030

3131
// ------------------------------------------------------------------
32-
// StartsWith
33-
// ------------------------------------------------------------------
34-
type TStartsWith<Input extends string, Value extends string> = (
35-
Input extends `${Value}${string}`
36-
? true
37-
: false
38-
)
39-
function StartsWith<Input extends string, Value extends string>
40-
(input: Input, value: Value):
41-
TStartsWith<Input, Value> {
42-
return Guard.IsEqual(input.indexOf(value), 0) as never
43-
}
44-
// ------------------------------------------------------------------
45-
// IsWhitespace
46-
// ------------------------------------------------------------------
47-
export type TWhitespace = typeof Whitespace
48-
export const Whitespace = ' '
49-
50-
export type TIsWhitespace<Input extends string> = (
51-
TStartsWith<Input, TWhitespace>
52-
)
53-
export function IsWhitespace<Input extends string>(input: Input): TIsWhitespace<Input> {
54-
return StartsWith(input, Whitespace) as never
55-
}
56-
// ------------------------------------------------------------------
57-
// IsNewline
58-
// ------------------------------------------------------------------
59-
export type TNewline = typeof Newline
60-
export const Newline = '\n'
61-
62-
export type TIsNewline<Input extends string> = (
63-
TStartsWith<Input, TNewline>
64-
)
65-
export function IsNewline<Input extends string>(input: Input): TIsNewline<Input> {
66-
return StartsWith(input, Newline) as never
67-
}
68-
// ------------------------------------------------------------------
69-
// IsAlpha
32+
// Alphas
7033
// ------------------------------------------------------------------
7134
export type TAlpha = typeof Alpha
35+
7236
export const Alpha = [
7337
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
7438
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
@@ -78,85 +42,34 @@ export const Alpha = [
7842
'Y', 'Z'
7943
] as const
8044

81-
export type TIsAlpha<Input extends string> = (
82-
TStartsWith<Input, TAlpha[number]>
83-
)
84-
export function IsAlpha<Input extends string>(input: Input): TIsAlpha<Input> {
85-
const charCode = input.charCodeAt(0)
86-
return (
87-
(Guard.IsGreaterEqualThan(charCode, 65) && Guard.IsLessEqualThan(charCode, 90)) // A-Z
88-
|| (Guard.IsGreaterEqualThan(charCode, 97) && Guard.IsLessEqualThan(charCode, 122)) // a-z
89-
) as never
90-
}
9145
// ------------------------------------------------------------------
92-
// IsDigit
46+
// Digits
9347
// ------------------------------------------------------------------
48+
export type TNonZero = typeof NonZero
49+
export type TZero = typeof Zero
9450
export type TDigit = typeof Digit
95-
export const Digit = [
96-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
97-
] as const
98-
export type TIsDigit<Input extends string> = (
99-
TStartsWith<Input, TDigit[number]>
100-
)
101-
export function IsDigit<Input extends string>(input: Input): TIsDigit<Input> {
102-
return (
103-
IsNonZero(input) || IsZero(input)
104-
) as never
105-
}
106-
// ------------------------------------------------------------------
107-
// IsZero
108-
// ------------------------------------------------------------------
109-
export type TIsZero<Input extends string> = (
110-
TStartsWith<Input, '0'>
111-
)
112-
export function IsZero<Input extends string>(input: Input): boolean {
113-
const charCode = input.charCodeAt(0)
114-
return Guard.IsEqual(charCode, 48)
115-
}
116-
// ------------------------------------------------------------------
117-
// IsNonZero
118-
// ------------------------------------------------------------------
119-
export type TIsNonZero<Input extends string> = (
120-
TStartsWith<Input, Exclude<TDigit[number], '0'>>
121-
)
122-
export function IsNonZero<Input extends string>(input: Input): boolean {
123-
const charCode = input.charCodeAt(0)
124-
return Guard.IsGreaterEqualThan(charCode, 49)
125-
&& Guard.IsLessEqualThan(charCode, 57)
126-
}
127-
// ------------------------------------------------------------------
128-
// IsDot
129-
// ------------------------------------------------------------------
130-
export type TDot = typeof Dot
131-
export const Dot = '.'
13251

133-
export type TIsDot<Input extends string> = (
134-
TStartsWith<Input, TDot>
135-
)
136-
export function IsDot<Input extends string>(input: Input): TIsDot<Input> {
137-
return StartsWith(input, Dot)
138-
}
139-
// ------------------------------------------------------------------
140-
// IsUnderscore
141-
// ------------------------------------------------------------------
142-
export type TUnderscore = typeof Underscore
143-
export const Underscore = '_'
52+
export const NonZero = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] as const
53+
export const Zero = '0'
54+
export const Digit = [Zero, ...NonZero] as const
14455

145-
export type TIsUnderscore<Input extends string> = (
146-
TStartsWith<Input, TUnderscore>
147-
)
148-
export function IsUnderscore<Input extends string>(input: Input): TIsUnderscore<Input> {
149-
return StartsWith(input, Underscore)
150-
}
15156
// ------------------------------------------------------------------
152-
// IsDollarSign
57+
// Characters
15358
// ------------------------------------------------------------------
154-
export type TDollarSign = typeof DollarSign
59+
export const Whitespace = ' '
60+
export const Newline = '\n'
61+
export const Underscore = '_'
62+
export const Dot = '.'
15563
export const DollarSign = '$'
64+
export const SingleQuote = "'"
65+
export const DoubleQuote = '"'
66+
export const Backtick = "`"
15667

157-
export type TIsDollarSign<Input extends string> = (
158-
TStartsWith<Input, TDollarSign>
159-
)
160-
export function IsDollarSign<Input extends string>(input: Input): TIsDollarSign<Input> {
161-
return StartsWith(input, DollarSign)
162-
}
68+
export type TWhitespace = typeof Whitespace
69+
export type TNewline = typeof Newline
70+
export type TUnderscore = typeof Underscore
71+
export type TDot = typeof Dot
72+
export type TDollarSign = typeof DollarSign
73+
export type TSingleQuote = typeof SingleQuote
74+
export type TDoubleQuote = typeof DoubleQuote
75+
export type TBacktick = typeof Backtick

src/token/ident.ts

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

2929
// deno-fmt-ignore-file
3030

31+
import { type TUnreachable, Unreachable } from './unreachable.ts'
3132
import { type TTrim, Trim } from './trim.ts'
3233
import { type TTake, Take, IsTakeTrue } from './take.ts'
3334

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

3637
// ------------------------------------------------------------------
37-
// TakeFirst
38+
// First
3839
// ------------------------------------------------------------------
3940
type TFirst = [...Char.TAlpha, Char.TUnderscore, Char.TDollarSign]
4041
const First = [...Char.Alpha, Char.Underscore, Char.DollarSign]
@@ -53,7 +54,7 @@ function TakeFirst<Input extends string>(input: Input): TTakeFirst<Input> {
5354
) as never
5455
}
5556
// ------------------------------------------------------------------
56-
// TakeRemaining
57+
// Remaining
5758
// ------------------------------------------------------------------
5859
type TRemaining = [...TFirst, ...Char.TDigit]
5960
const Remaining = [...First, ...Char.Digit]
@@ -72,13 +73,13 @@ function TakeRemaining<Input extends string>(input: Input, result: string = ''):
7273
) as never
7374
}
7475
// ------------------------------------------------------------------
75-
// TakeIdent
76+
// Take
7677
// ------------------------------------------------------------------
7778
type TTakeIdent<Input extends string> = (
7879
TTakeFirst<Input> extends [infer First extends string, infer Remaining extends string]
7980
? TTakeRemaining<Remaining> extends [infer Remaining extends string, infer Rest extends string]
8081
? [`${First}${Remaining}`, Rest]
81-
: never
82+
: TUnreachable
8283
: []
8384
)
8485
function TakeIdent<Input extends string>(input: Input): TTakeIdent<Input> {
@@ -88,7 +89,7 @@ function TakeIdent<Input extends string>(input: Input): TTakeIdent<Input> {
8889
const remainingResult = TakeRemaining(firstResult[1])
8990
return IsTakeTrue(remainingResult)
9091
? [`${firstResult[0]}${remainingResult[0]}`, remainingResult[1]]
91-
: (() => { throw 'Unreachable' })()
92+
: Unreachable()
9293
})() : []
9394
) as never
9495
}
@@ -100,7 +101,4 @@ export type TIdent<Input extends string> = (
100101
)
101102
export function Ident<Input extends string>(input: Input): TIdent<Input> {
102103
return TakeIdent(Trim(input)) as never
103-
}
104-
105-
type B = TIdent<' hello world'>
106-
104+
}

src/token/string.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
// deno-fmt-ignore-file
30+
31+
import { type TTrim, Trim } from './trim.ts'
32+
import { type TTake, Take } from './take.ts'
33+
34+
import * as Char from './char.ts'
35+
36+
// ------------------------------------------------------------------
37+
// TakeFirst
38+
// ------------------------------------------------------------------
39+
type TSingleLineQuote = [Char.TSingleQuote, Char.TDoubleQuote]
40+
const SingleLineQuote = [Char.SingleQuote, Char.DoubleQuote]
41+
42+
43+
// // ------------------------------------------------------------------
44+
// // Const
45+
// // ------------------------------------------------------------------
46+
// export type TString<Input extends string> = (
47+
// TTake<TTrim<Input>, [Value]>
48+
// )
49+
// export function String<Input extends string>(input: Input): TConst<Input, Value> {
50+
// return Take(Trim(input), [value]) as never
51+
// }
52+

src/token/unreachable.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 TUnreachable = never
30+
31+
export function Unreachable(): TUnreachable {
32+
throw new Error('Unreachable')
33+
}

0 commit comments

Comments
 (0)