Skip to content

Commit cb568ef

Browse files
committed
updates
1 parent 0cfaed9 commit cb568ef

File tree

8 files changed

+344
-294
lines changed

8 files changed

+344
-294
lines changed

example/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import { Token } from '@sinclair/parsebox'
44

5-
const R = Token.Const(' ', ' ')
5+
const R = Token.Span('(', ')', false, '()1')
66

7-
const A = Token.Number('123.00000000000')
7+
console.log(R)
88

99
console.log(R)
1010

src/runtime/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ THE SOFTWARE.
2929
// deno-fmt-ignore-file
3030
// deno-lint-ignore-file
3131

32-
import { Token } from '../token/index.ts'
32+
import * as Token from '../token/index.ts'
3333
import * as Types from './types.ts'
3434

3535
export function Throw(message: string): never {

src/token/span.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ import { type TUntil, Until } from './until.ts'
3838
// ------------------------------------------------------------------
3939
type TMultiLine<Start extends string, End extends string, Input extends string> = (
4040
Input extends `${Start}${infer Rest extends string}`
41-
? TUntil<[End], Rest> extends [infer Left extends string, infer Right extends string]
42-
? Right extends `${End}${infer Rest extends string}`
43-
? [`${Start}${Left}${End}`, Rest]
41+
? TUntil<[End], Rest> extends [infer Until extends string, infer UntilRest extends string]
42+
? UntilRest extends `${End}${infer Rest extends string}`
43+
? [`${Until}`, Rest]
4444
: [] // fail: did not match End
4545
: [] // fail: did not match Until
4646
: [] // fail: did not match Start
@@ -53,7 +53,7 @@ function MultiLine<Start extends string, End extends string, Input extends strin
5353
const until = Until([end], input.slice(start.length))
5454
return IsResult(until) ? (() => {
5555
return until[1].startsWith(end)
56-
? [`${start}${until[0]}${end}`, until[1].slice(end.length)]
56+
? [`${until[0]}`, until[1].slice(end.length)]
5757
: [] // fail: did not match End
5858
})() : [] // fail: did not match Until
5959
})() : [] // fail: did not match Start
@@ -91,16 +91,16 @@ function SingleLine<Start extends string, End extends string, Input extends stri
9191
/** Matches from Start and End capturing everything in-between. Start and End are consumed. */
9292
export type TSpan<Start extends string, End extends string, MultiLine extends boolean, Input extends string> = (
9393
MultiLine extends true
94-
? TMultiLine<TTrim<Input>, Start, End>
95-
: TSingleLine<TTrim<Input>, Start, End>
94+
? TMultiLine<Start, End, TTrim<Input>>
95+
: TSingleLine<Start, End, TTrim<Input>>
9696
)
9797
/** Matches from Start and End capturing everything in-between. Start and End are consumed. */
9898
export function Span<Start extends string, End extends string, MultiLine extends boolean, Input extends string>
9999
(start: Start, end: End, multiLine: MultiLine, input: Input):
100100
TSpan<Start, End, MultiLine, Input> {
101101
return (
102102
multiLine
103-
? MultiLine(Trim(input), start, end)
104-
: SingleLine(Trim(input), start, end)
103+
? MultiLine(start, end, Trim(input))
104+
: SingleLine(start, end, Trim(input))
105105
) as never
106106
}

test/parsebox/token/const.ts

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,104 @@ const Test = Assert.Context('Token.Const')
77
// Const: Empty
88
// ------------------------------------------------------------------
99
Test('Should Const 1', () => Assert.IsExact(Token.Const('', ''), ['', '']))
10-
Test('Should Const 2', () => Assert.IsExact(Token.Const('A', ''), ['', 'A']))
11-
Test('Should Const 3', () => Assert.IsExact(Token.Const(' A', ''), ['', ' A']))
10+
Test('Should Const 2', () => Assert.IsExact(Token.Const('', 'A'), ['', 'A']))
11+
Test('Should Const 3', () => Assert.IsExact(Token.Const('', ' A'), ['', ' A']))
12+
13+
// ------------------------------------------------------------------
14+
// Const: Single-Char
15+
// ------------------------------------------------------------------
16+
Test('Should Const 4', () => Assert.IsExact(Token.Const('A', 'A'), ['A', '']))
17+
Test('Should Const 5', () => Assert.IsExact(Token.Const('A', 'A '), ['A', ' ']))
18+
Test('Should Const 6', () => Assert.IsExact(Token.Const('A', 'AA'), ['A', 'A']))
19+
Test('Should Const 7', () => Assert.IsExact(Token.Const('A', 'AA '), ['A', 'A ']))
20+
21+
// ------------------------------------------------------------------
22+
// Const: Multi-Char
23+
// ------------------------------------------------------------------
24+
Test('Should Const 8', () => Assert.IsExact(Token.Const('AB', 'AB'), ['AB', '']))
25+
Test('Should Const 9', () => Assert.IsExact(Token.Const('AB', 'AB '), ['AB', ' ']))
26+
Test('Should Const 10', () => Assert.IsExact(Token.Const('AB', 'ABA'), ['AB', 'A']))
27+
Test('Should Const 11', () => Assert.IsExact(Token.Const('AB', 'ABA '), ['AB', 'A ']))
28+
29+
// ------------------------------------------------------------------
30+
// Const: Single-Char -> Ignore-Whitespace
31+
// ------------------------------------------------------------------
32+
Test('Should Const 12', () => Assert.IsExact(Token.Const('A', ' A'), ['A', '']))
33+
Test('Should Const 13', () => Assert.IsExact(Token.Const('A', ' A '), ['A', ' ']))
34+
Test('Should Const 14', () => Assert.IsExact(Token.Const('A', ' AA'), ['A', 'A']))
35+
Test('Should Const 15', () => Assert.IsExact(Token.Const('A', ' AA '), ['A', 'A ']))
36+
Test('Should Const 16', () => Assert.IsExact(Token.Const('A', '\nAA '), ['A', 'A ']))
37+
Test('Should Const 17', () => Assert.IsExact(Token.Const('A', ' \nAA '), ['A', 'A ']))
38+
Test('Should Const 18', () => Assert.IsExact(Token.Const('A', '\n AA '), ['A', 'A ']))
39+
Test('Should Const 19', () => Assert.IsExact(Token.Const('A', ' \n AA '), ['A', 'A ']))
40+
41+
// ------------------------------------------------------------------
42+
// Const: Multi-Char -> Ignore-Whitespace
43+
// ------------------------------------------------------------------
44+
Test('Should Const 20', () => Assert.IsExact(Token.Const('AB', ' AB'), ['AB', '']))
45+
Test('Should Const 21', () => Assert.IsExact(Token.Const('AB', ' AB '), ['AB', ' ']))
46+
Test('Should Const 22', () => Assert.IsExact(Token.Const('AB', ' ABA'), ['AB', 'A']))
47+
Test('Should Const 23', () => Assert.IsExact(Token.Const('AB', ' ABA '), ['AB', 'A ']))
48+
Test('Should Const 24', () => Assert.IsExact(Token.Const('AB', '\nABA '), ['AB', 'A ']))
49+
Test('Should Const 25', () => Assert.IsExact(Token.Const('AB', ' \nABA '), ['AB', 'A ']))
50+
Test('Should Const 26', () => Assert.IsExact(Token.Const('AB', '\n ABA '), ['AB', 'A ']))
51+
Test('Should Const 27', () => Assert.IsExact(Token.Const('AB', ' \n ABA '), ['AB', 'A ']))
1252

1353
// ------------------------------------------------------------------
1454
// Const: Single-Whitespace
1555
// ------------------------------------------------------------------
16-
Test('Should Const Single-Whitespace 1', () => Assert.IsExact(Token.Const('', ' '), []))
17-
Test('Should Const Single-Whitespace 2', () => Assert.IsExact(Token.Const(' ', ' '), [' ', '']))
18-
Test('Should Const Single-Whitespace 3', () => Assert.IsExact(Token.Const(' A', ' '), [' ', 'A']))
19-
Test('Should Const Single-Whitespace 4', () => Assert.IsExact(Token.Const(' A ', ' '), [' ', 'A ']))
20-
Test('Should Const Single-Whitespace 5', () => Assert.IsExact(Token.Const(' AA', ' '), [' ', 'AA']))
21-
Test('Should Const Single-Whitespace 6', () => Assert.IsExact(Token.Const(' AA ', ' '), [' ', 'AA ']))
56+
Test('Should Const 28', () => Assert.IsExact(Token.Const(' ', ''), []))
57+
Test('Should Const 29', () => Assert.IsExact(Token.Const(' ', ' '), [' ', '']))
58+
Test('Should Const 30', () => Assert.IsExact(Token.Const(' ', ' A'), [' ', 'A']))
59+
Test('Should Const 31', () => Assert.IsExact(Token.Const(' ', ' A '), [' ', 'A ']))
60+
Test('Should Const 32', () => Assert.IsExact(Token.Const(' ', ' AA'), [' ', 'AA']))
61+
Test('Should Const 33', () => Assert.IsExact(Token.Const(' ', ' AA '), [' ', 'AA ']))
2262

2363
// ------------------------------------------------------------------
2464
// Const: Multi-Whitespace
2565
// ------------------------------------------------------------------
26-
Test('Should Const Multi-Whitespace 1', () => Assert.IsExact(Token.Const('', ' '), []))
27-
Test('Should Const Multi-Whitespace 2', () => Assert.IsExact(Token.Const(' ', ' '), []))
28-
Test('Should Const Multi-Whitespace 3', () => Assert.IsExact(Token.Const(' A', ' '), [' ', 'A']))
29-
Test('Should Const Multi-Whitespace 4', () => Assert.IsExact(Token.Const(' A ', ' '), [' ', 'A ']))
30-
Test('Should Const Multi-Whitespace 5', () => Assert.IsExact(Token.Const(' AA', ' '), [' ', 'AA']))
31-
Test('Should Const Multi-Whitespace 6', () => Assert.IsExact(Token.Const(' AA ', ' '), [' ', 'AA ']))
66+
Test('Should Const 34', () => Assert.IsExact(Token.Const(' ', ''), []))
67+
Test('Should Const 35', () => Assert.IsExact(Token.Const(' ', ' '), []))
68+
Test('Should Const 36', () => Assert.IsExact(Token.Const(' ', ' A'), [' ', 'A']))
69+
Test('Should Const 37', () => Assert.IsExact(Token.Const(' ', ' A '), [' ', 'A ']))
70+
Test('Should Const 38', () => Assert.IsExact(Token.Const(' ', ' AA'), [' ', 'AA']))
71+
Test('Should Const 39', () => Assert.IsExact(Token.Const(' ', ' AA '), [' ', 'AA ']))
3272

3373
// ------------------------------------------------------------------
3474
// Const: Newline
3575
// ------------------------------------------------------------------
36-
Test('Should Const Newline 1', () => Assert.IsExact(Token.Const('', '\n'), []))
37-
Test('Should Const Newline 2', () => Assert.IsExact(Token.Const(' ', '\n'), []))
38-
Test('Should Const Newline 3', () => Assert.IsExact(Token.Const('\nA', '\n'), ['\n', 'A']))
39-
Test('Should Const Newline 4', () => Assert.IsExact(Token.Const(' \nA ', '\n'), ['\n', 'A ']))
40-
Test('Should Const Newline 5', () => Assert.IsExact(Token.Const(' \nAA', '\n'), ['\n', 'AA']))
41-
Test('Should Const Newline 6', () => Assert.IsExact(Token.Const(' \nAA ', '\n'), ['\n', 'AA ']))
76+
Test('Should Const 40', () => Assert.IsExact(Token.Const('\n', ''), []))
77+
Test('Should Const 41', () => Assert.IsExact(Token.Const('\n', ' '), []))
78+
Test('Should Const 42', () => Assert.IsExact(Token.Const('\n', '\nA'), ['\n', 'A']))
79+
Test('Should Const 43', () => Assert.IsExact(Token.Const('\n', ' \nA '), ['\n', 'A ']))
80+
Test('Should Const 44', () => Assert.IsExact(Token.Const('\n', ' \nAA'), ['\n', 'AA']))
81+
Test('Should Const 45', () => Assert.IsExact(Token.Const('\n', ' \nAA '), ['\n', 'AA ']))
4282

4383
// ------------------------------------------------------------------
4484
// Const: Newline-Single-Whitespace
4585
// ------------------------------------------------------------------
46-
Test('Should Const Newline-Single-WS 1', () => Assert.IsExact(Token.Const('', '\n '), []))
47-
Test('Should Const Newline-Single-WS 2', () => Assert.IsExact(Token.Const(' ', '\n '), []))
48-
Test('Should Const Newline-Single-WS 3', () => Assert.IsExact(Token.Const('\nA', '\n '), []))
49-
Test('Should Const Newline-Single-WS 4', () => Assert.IsExact(Token.Const(' \nA ', '\n '), []))
50-
Test('Should Const Newline-Single-WS 5', () => Assert.IsExact(Token.Const(' \nAA', '\n '), []))
51-
Test('Should Const Newline-Single-WS 6', () => Assert.IsExact(Token.Const(' \nAA ', '\n '), []))
52-
Test('Should Const Newline-Single-WS 7', () => Assert.IsExact(Token.Const('\n A', '\n '), ['\n ', 'A']))
53-
Test('Should Const Newline-Single-WS 8', () => Assert.IsExact(Token.Const(' \n A ', '\n '), ['\n ', 'A ']))
54-
Test('Should Const Newline-Single-WS 9', () => Assert.IsExact(Token.Const(' \n AA', '\n '), ['\n ', 'AA']))
55-
Test('Should Const Newline-Single-WS 10', () => Assert.IsExact(Token.Const(' \n AA ', '\n '), ['\n ', 'AA ']))
86+
Test('Should Const 46', () => Assert.IsExact(Token.Const('\n ', ''), []))
87+
Test('Should Const 47', () => Assert.IsExact(Token.Const('\n ', ' '), []))
88+
Test('Should Const 48', () => Assert.IsExact(Token.Const('\n ', '\nA'), []))
89+
Test('Should Const 49', () => Assert.IsExact(Token.Const('\n ', ' \nA '), []))
90+
Test('Should Const 50', () => Assert.IsExact(Token.Const('\n ', ' \nAA'), []))
91+
Test('Should Const 51', () => Assert.IsExact(Token.Const('\n ', ' \nAA '), []))
92+
Test('Should Const 52', () => Assert.IsExact(Token.Const('\n ', '\n A'), ['\n ', 'A']))
93+
Test('Should Const 53', () => Assert.IsExact(Token.Const('\n ', ' \n A '), ['\n ', 'A ']))
94+
Test('Should Const 54', () => Assert.IsExact(Token.Const('\n ', ' \n AA'), ['\n ', 'AA']))
95+
Test('Should Const 55', () => Assert.IsExact(Token.Const('\n ', ' \n AA '), ['\n ', 'AA ']))
5696

5797
// ------------------------------------------------------------------
5898
// Const: Newline-Multi-Whitespace
5999
// ------------------------------------------------------------------
60-
Test('Should Const Newline-Multi-WS 1', () => Assert.IsExact(Token.Const('', '\n '), []))
61-
Test('Should Const Newline-Multi-WS 2', () => Assert.IsExact(Token.Const(' ', '\n '), []))
62-
Test('Should Const Newline-Multi-WS 3', () => Assert.IsExact(Token.Const('\n A', '\n '), []))
63-
Test('Should Const Newline-Multi-WS 4', () => Assert.IsExact(Token.Const(' \n A ', '\n '), []))
64-
Test('Should Const Newline-Multi-WS 5', () => Assert.IsExact(Token.Const(' \n AA', '\n '), []))
65-
Test('Should Const Newline-Multi-WS 6', () => Assert.IsExact(Token.Const(' \n AA ', '\n '), []))
66-
Test('Should Const Newline-Multi-WS 7', () => Assert.IsExact(Token.Const('\n A', '\n '), ['\n ', 'A']))
67-
Test('Should Const Newline-Multi-WS 8', () => Assert.IsExact(Token.Const(' \n A ', '\n '), ['\n ', 'A ']))
68-
Test('Should Const Newline-Multi-WS 9', () => Assert.IsExact(Token.Const(' \n AA', '\n '), ['\n ', 'AA']))
69-
Test('Should Const Newline-Multi-WS 10', () => Assert.IsExact(Token.Const(' \n AA ', '\n '), ['\n ', 'AA ']))
100+
Test('Should Const 56', () => Assert.IsExact(Token.Const('\n ', ''), []))
101+
Test('Should Const 57', () => Assert.IsExact(Token.Const('\n ', ' '), []))
102+
Test('Should Const 58', () => Assert.IsExact(Token.Const('\n ', '\n A'), []))
103+
Test('Should Const 59', () => Assert.IsExact(Token.Const('\n ', ' \n A '), []))
104+
Test('Should Const 60', () => Assert.IsExact(Token.Const('\n ', ' \n AA'), []))
105+
Test('Should Const 61', () => Assert.IsExact(Token.Const('\n ', ' \n AA '), []))
106+
Test('Should Const 62', () => Assert.IsExact(Token.Const('\n ', '\n A'), ['\n ', 'A']))
107+
Test('Should Const 63', () => Assert.IsExact(Token.Const('\n ', ' \n A '), ['\n ', 'A ']))
108+
Test('Should Const 64', () => Assert.IsExact(Token.Const('\n ', ' \n AA'), ['\n ', 'AA']))
109+
Test('Should Const 65', () => Assert.IsExact(Token.Const('\n ', ' \n AA '), ['\n ', 'AA ']))
110+

0 commit comments

Comments
 (0)