|
import { |
|
isHexDigit, |
|
Ident, |
|
Number, |
|
Dimension |
|
} from '../../tokenizer/index.js'; |
|
|
|
const PLUSSIGN = 0x002B; |
|
const HYPHENMINUS = 0x002D; |
|
const QUESTIONMARK = 0x003F; |
|
|
|
function eatHexSequence(offset, allowDash) { |
|
let len = 0; |
|
|
|
for (let pos = this.tokenStart + offset; pos < this.tokenEnd; pos++) { |
|
const code = this.charCodeAt(pos); |
|
|
|
if (code === HYPHENMINUS && allowDash && len !== 0) { |
|
eatHexSequence.call(this, offset + len + 1, false); |
|
return -1; |
|
} |
|
|
|
if (!isHexDigit(code)) { |
|
this.error( |
|
allowDash && len !== 0 |
|
? 'Hyphen minus' + (len < 6 ? ' or hex digit' : '') + ' is expected' |
|
: (len < 6 ? 'Hex digit is expected' : 'Unexpected input'), |
|
pos |
|
); |
|
} |
|
|
|
if (++len > 6) { |
|
this.error('Too many hex digits', pos); |
|
}; |
|
} |
|
|
|
this.next(); |
|
return len; |
|
} |
|
|
|
function eatQuestionMarkSequence(max) { |
|
let count = 0; |
|
|
|
while (this.isDelim(QUESTIONMARK)) { |
|
if (++count > max) { |
|
this.error('Too many question marks'); |
|
} |
|
|
|
this.next(); |
|
} |
|
} |
|
|
|
function startsWith(code) { |
|
if (this.charCodeAt(this.tokenStart) !== code) { |
|
this.error((code === PLUSSIGN ? 'Plus sign' : 'Hyphen minus') + ' is expected'); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function scanUnicodeRange() { |
|
let hexLength = 0; |
|
|
|
switch (this.tokenType) { |
|
case Number: |
|
|
|
|
|
|
|
hexLength = eatHexSequence.call(this, 1, true); |
|
|
|
if (this.isDelim(QUESTIONMARK)) { |
|
eatQuestionMarkSequence.call(this, 6 - hexLength); |
|
break; |
|
} |
|
|
|
if (this.tokenType === Dimension || |
|
this.tokenType === Number) { |
|
startsWith.call(this, HYPHENMINUS); |
|
eatHexSequence.call(this, 1, false); |
|
break; |
|
} |
|
|
|
break; |
|
|
|
case Dimension: |
|
|
|
hexLength = eatHexSequence.call(this, 1, true); |
|
|
|
if (hexLength > 0) { |
|
eatQuestionMarkSequence.call(this, 6 - hexLength); |
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
|
|
|
this.eatDelim(PLUSSIGN); |
|
|
|
if (this.tokenType === Ident) { |
|
hexLength = eatHexSequence.call(this, 0, true); |
|
if (hexLength > 0) { |
|
eatQuestionMarkSequence.call(this, 6 - hexLength); |
|
} |
|
break; |
|
} |
|
|
|
if (this.isDelim(QUESTIONMARK)) { |
|
this.next(); |
|
eatQuestionMarkSequence.call(this, 5); |
|
break; |
|
} |
|
|
|
this.error('Hex digit or question mark is expected'); |
|
} |
|
} |
|
|
|
export const name = 'UnicodeRange'; |
|
export const structure = { |
|
value: String |
|
}; |
|
|
|
export function parse() { |
|
const start = this.tokenStart; |
|
|
|
|
|
this.eatIdent('u'); |
|
scanUnicodeRange.call(this); |
|
|
|
return { |
|
type: 'UnicodeRange', |
|
loc: this.getLocation(start, this.tokenStart), |
|
value: this.substrToCursor(start) |
|
}; |
|
} |
|
|
|
export function generate(node) { |
|
this.tokenize(node.value); |
|
} |
|
|