Skip to content

Commit bc8bc1f

Browse files
string iterator into new file
1 parent 417f783 commit bc8bc1f

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

assembly/parser/parser.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isDigit, Char } from "../char";
2+
import { StringIterator } from "./string-iterator";
23
import {
34
AST,
45
RangeRepetitionNode,
@@ -58,46 +59,7 @@ class Range {
5859
to: i32 = -1;
5960
}
6061

61-
class StringIterator {
62-
current: u32;
63-
cursor: u32 = 0;
64-
65-
constructor(private sourceString: string) {
66-
this.current = this.sourceString.charCodeAt(0);
67-
}
68-
69-
lookahead(distance: u32): u32 {
70-
return this.sourceString.charCodeAt(this.cursor + distance);
71-
}
72-
73-
next(): bool {
74-
this.cursor++;
75-
if (this.cursor >= u32(this.sourceString.length)) {
76-
return false;
77-
}
78-
this.current = this.sourceString.charCodeAt(this.cursor);
79-
return true;
80-
}
81-
82-
currentAsString(): string {
83-
return String.fromCharCode(this.current);
84-
}
85-
86-
more(): bool {
87-
return this.cursor < u32(this.sourceString.length);
88-
}
89-
90-
copy(): StringIterator {
91-
const iterator = new StringIterator(this.sourceString);
92-
iterator.cursor = this.cursor;
93-
iterator.current = this.current;
94-
return iterator;
95-
}
96-
}
97-
9862
export class Parser {
99-
// currentToken: string = "";
100-
// cursor: u32 = 0;
10163
iterator: StringIterator;
10264

10365
private constructor(input: string) {

assembly/parser/string-iterator.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export class StringIterator {
2+
current: u32;
3+
cursor: u32 = 0;
4+
5+
constructor(private sourceString: string) {
6+
this.current = this.sourceString.charCodeAt(0);
7+
}
8+
9+
lookahead(distance: u32): u32 {
10+
return this.sourceString.charCodeAt(this.cursor + distance);
11+
}
12+
13+
next(): bool {
14+
this.cursor++;
15+
if (this.cursor >= u32(this.sourceString.length)) {
16+
return false;
17+
}
18+
this.current = this.sourceString.charCodeAt(this.cursor);
19+
return true;
20+
}
21+
22+
currentAsString(): string {
23+
return String.fromCharCode(this.current);
24+
}
25+
26+
more(): bool {
27+
return this.cursor < u32(this.sourceString.length);
28+
}
29+
30+
copy(): StringIterator {
31+
const iterator = new StringIterator(this.sourceString);
32+
iterator.cursor = this.cursor;
33+
iterator.current = this.current;
34+
return iterator;
35+
}
36+
}

0 commit comments

Comments
 (0)