Skip to content

Commit 87d0dd9

Browse files
committed
Refactor scanner for clarity
1 parent 5a31aa2 commit 87d0dd9

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

scanner.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ function lex (argument) {
4141
}
4242
// Iterate through the argument string, buffering
4343
// non-space characters and building tokens.
44+
var characterBuffer
45+
var startedBuffering
46+
resetBuffer()
4447
var tokens = []
45-
var characterBuffer = ''
46-
var startedBuffering = null
4748
var length = argument.length
4849
for (var offset = 0; offset < length; offset++) {
4950
var character = argument[offset]
@@ -74,6 +75,11 @@ function lex (argument) {
7475
})
7576
return tokens
7677

78+
function resetBuffer () {
79+
characterBuffer = ''
80+
startedBuffering = null
81+
}
82+
7783
function pushBuffered () {
7884
if (characterBuffer) {
7985
// Create a token for the buffered characters.
@@ -83,9 +89,7 @@ function lex (argument) {
8389
start: startedBuffering,
8490
end: startedBuffering + characterBuffer.length
8591
})
86-
// Reset the buffer.
87-
characterBuffer = ''
88-
startedBuffering = null
92+
resetBuffer()
8993
}
9094
}
9195
}
@@ -94,11 +98,11 @@ function tokenTypeForString (string, start) {
9498
if (ids.indexOf(string) !== -1) {
9599
return 'LICENSE'
96100
} else if (string === 'AND') {
97-
return string
101+
return 'AND'
98102
} else if (string === 'OR') {
99-
return string
103+
return 'OR'
100104
} else if (string === 'WITH') {
101-
return string
105+
return 'WITH'
102106
} else if (exceptions.indexOf(string) !== -1) {
103107
return 'EXCEPTION'
104108
} else if (LICENSEREF.test(string)) {
@@ -109,10 +113,10 @@ function tokenTypeForString (string, start) {
109113
return 'OPEN'
110114
} else if (string === ')') {
111115
return 'CLOSE'
112-
} else if (string === ':') {
113-
return 'COLON'
114116
} else if (string === '+') {
115117
return 'PLUS'
118+
} else if (string === ':') {
119+
return 'COLON'
116120
} else {
117121
throw new Error('Invalid input at offset ' + start)
118122
}

0 commit comments

Comments
 (0)