Skip to content

Commit 88f0a79

Browse files
committed
Make shorter skip
1 parent 1773443 commit 88f0a79

5 files changed

Lines changed: 13 additions & 12 deletions

File tree

feature/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { token, skip, next, cur, idx, expr } from "../src/parse.js"
33

44
// /**/, //
55
// FIXME: try replacing with group
6-
token('/*', PREC_TOKEN, (a, prec) => (next(c => c !== STAR && cur.charCodeAt(idx + 1) !== 47), skip(2), a || expr(prec) || []))
6+
token('/*', PREC_TOKEN, (a, prec) => (next(c => c !== STAR && cur.charCodeAt(idx + 1) !== 47), skip(),skip(), a || expr(prec) || []))
77
token('//', PREC_TOKEN, (a, prec) => (next(c => c >= SPACE), a || expr(prec) || []))

feature/number.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lookup, next, skip, err, cur, idx } from "../src/parse.js"
1+
import { lookup, next, err } from "../src/parse.js"
22
import { PERIOD, _0, _E, _e, _9 } from "../src/const.js"
33

44
// parse number

feature/string.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { skip, err, cur, idx, lookup } from '../src/parse.js'
1+
import { skip, err, next, cur, idx, lookup } from '../src/parse.js'
22
import { DQUOTE, QUOTE, BSLASH } from '../src/const.js'
33

44
const escape = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', v: '\v' },
5-
string = q => (qc, c, str = '') => {
5+
string = q => (qc, prec, str = '') => {
66
qc && err('Unexpected string') // must not follow another token
7-
skip() // first quote
8-
while (c = cur.charCodeAt(idx), c - q) {
9-
if (c === BSLASH) skip(), c = skip(), str += escape[c] || c
10-
else str += skip()
11-
}
7+
skip()
8+
// while (c = cur.charCodeAt(idx), c - q) {
9+
// if (c === BSLASH) skip(), c = cur[idx], skip(), str += escape[c] || c
10+
// else str += cur[idx], skip()
11+
// }
12+
next(c => c - q && (c === BSLASH ? (str += escape[cur[idx+1]] || cur[idx+1], 2 ) : (str += cur[idx], 1)))
1213
skip() || err('Bad string')
1314
return [, str]
1415
}

src/parse.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export namespace parse {
66
function id(n: any): any;
77
}
88
export function err(msg?: string, frag?: string): never;
9-
export function skip(n: number): string;
9+
export function skip(): string;
1010
export function next(is: ((c: number) => number)): string;
1111
export const lookup: ((a: any, b: any) => any)[];
1212
export function token(op: string, prec: number, map: (a: any, curPrec: number, from: number) => any): (a: any, curPrec: number, from?: any) => any;

src/parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export let idx, cur,
2222
return cur.slice(from, idx)
2323
},
2424

25-
// consume n characters
26-
skip = (n = 1, from = idx) => (idx += n, cur.slice(from, idx)),
25+
// advance n characters
26+
skip = () => cur[idx++],
2727

2828
// a + b - c
2929
expr = (prec = 0, end) => {

0 commit comments

Comments
 (0)