Skip to content

Commit

Permalink
simplify identifier consuming
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 6, 2017
1 parent 9f09898 commit 633141b
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 68 deletions.
16 changes: 3 additions & 13 deletions lib/parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,15 @@ var getUniversal = require('./type/Universal');
var getUrl = require('./type/Url');
var getValue = require('./type/Value');

function scanIdent(varAllowed) {
function readIdent() {
var start = this.scanner.tokenStart;

// optional first -
if (this.scanner.tokenType === HYPHENMINUS) {
this.scanner.next();

// variable --
if (varAllowed && this.scanner.tokenType === HYPHENMINUS) {
this.scanner.next();
}
}

this.scanner.eat(IDENTIFIER);
}

function readIdent(varAllowed) {
var start = this.scanner.tokenStart;

this.scanIdent(varAllowed);

return this.scanner.substrToCursor(start);
}
Expand Down Expand Up @@ -173,7 +164,6 @@ Parser.prototype = {
Url: getUrl,
Value: getValue,

scanIdent: scanIdent,
readIdent: readIdent,
readSC: readSC,
readSequence: sequence.default,
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/atrule/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var IDENTIFIER = TYPE.Identifier;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var HYPHENMINUS = TYPE.HyphenMinus;
var COLON = TYPE.Colon;
var DISALLOW_VAR = false;
var BALANCED = true;

function readRaw() {
Expand Down Expand Up @@ -64,7 +63,7 @@ function readSequence() {
if (this.scanner.lookupType(1) === LEFTPARENTHESIS) {
child = this.Function(this.scopeAtruleExpression, readRaw);
} else {
child = this.Identifier(DISALLOW_VAR);
child = this.Identifier();
}

break;
Expand Down
19 changes: 11 additions & 8 deletions lib/parser/function/var.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var List = require('../../utils/list');
var TYPE = require('../../tokenizer').TYPE;

var IDENTIFIER = TYPE.Identifier;
var COMMA = TYPE.Comma;
var HYPHENMINUS = TYPE.HyphenMinus;
var EXCLAMATIONMARK = TYPE.ExclamationMark;
var ALLOWED_VAR = true;
var BALANCED = true;

// var '(' ident (',' <value>? )? ')'
Expand All @@ -13,14 +13,17 @@ module.exports = function getVarFunction() {

this.readSC();

if (this.scanner.tokenType !== HYPHENMINUS) {
this.scanner.error('Hyphen minus is expected');
}
if (this.scanner.lookupType(1) !== HYPHENMINUS) {
this.scanner.error('Hyphen minus is expected', this.scanner.tokenStart + 1);
}
var identStart = this.scanner.tokenStart;

this.scanner.eat(HYPHENMINUS);
this.scanner.eat(HYPHENMINUS);
this.scanner.eat(IDENTIFIER);

children.appendData(this.Identifier(ALLOWED_VAR));
children.appendData({
type: 'Identifier',
loc: this.getLocation(identStart, this.scanner.tokenStart),
name: this.scanner.substrToCursor(identStart)
});

this.readSC();

Expand Down
7 changes: 3 additions & 4 deletions lib/parser/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ var U = 117; // 'u'.charCodeAt(0)

var ALLOW_OF_CLAUSE = true;
var DISALLOW_OF_CLAUSE = false;
var DISALLOW_VAR = false;

function singleIdentifier() {
return new List().appendData(
this.Identifier(DISALLOW_VAR)
this.Identifier()
);
}

Expand Down Expand Up @@ -99,7 +98,7 @@ function defaultSequence(scope) {
if (this.scanner.lookupType(2) === LEFTPARENTHESIS) {
child = this.Function(scope, defaultSequence);
} else {
child = this.Identifier(DISALLOW_VAR);
child = this.Identifier();
}
} else {
child = this.Operator();
Expand Down Expand Up @@ -147,7 +146,7 @@ function defaultSequence(scope) {
child = this.Function(scope, defaultSequence);
}
} else {
child = this.Identifier(DISALLOW_VAR);
child = this.Identifier();
}

break;
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/type/Atrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var SEMICOLON = TYPE.Semicolon;
var COMMERCIALAT = TYPE.CommercialAt;
var LEFTCURLYBRACKET = TYPE.LeftCurlyBracket;
var RIGHTCURLYBRACKET = TYPE.RightCurlyBracket;
var DISALLOW_VAR = false;
var BALANCED = true;

function isBlockAtrule() {
Expand Down Expand Up @@ -32,7 +31,7 @@ module.exports = function Atrule() {

this.scanner.eat(COMMERCIALAT);

name = this.readIdent(DISALLOW_VAR);
name = this.readIdent();
nameLowerCase = name.toLowerCase();
this.readSC();

Expand Down
21 changes: 12 additions & 9 deletions lib/parser/type/Attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var RIGHTSQUAREBRACKET = TYPE.RightSquareBracket;
var CIRCUMFLEXACCENT = TYPE.CircumflexAccent;
var VERTICALLINE = TYPE.VerticalLine;
var TILDE = TYPE.Tilde;
var DISALLOW_VAR = false;

function getAttributeName() {
if (this.scanner.eof) {
Expand All @@ -28,18 +27,19 @@ function getAttributeName() {
checkColon = false;
this.scanner.next();
} else if (this.scanner.tokenType !== VERTICALLINE) {
this.scanIdent(false);
if (this.scanner.tokenType === HYPHENMINUS) {
this.scanner.next();
}
this.scanner.eat(IDENTIFIER);
}

if (this.scanner.tokenType === VERTICALLINE) {
if (this.scanner.lookupType(1) !== EQUALSSIGN) {
this.scanner.next();

if (this.scanner.tokenType === HYPHENMINUS || this.scanner.tokenType === IDENTIFIER) {
this.scanIdent(false);
} else {
this.scanner.error('Identifier is expected');
if (this.scanner.tokenType === HYPHENMINUS) {
this.scanner.next();
}
this.scanner.eat(IDENTIFIER);
} else if (expectIdentifier) {
this.scanner.error('Identifier is expected', this.scanner.tokenEnd);
}
Expand All @@ -49,7 +49,10 @@ function getAttributeName() {

if (checkColon && this.scanner.tokenType === COLON) {
this.scanner.next();
this.scanIdent(false);
if (this.scanner.tokenType === HYPHENMINUS) {
this.scanner.next();
}
this.scanner.eat(IDENTIFIER);
}

return {
Expand Down Expand Up @@ -107,7 +110,7 @@ module.exports = function Attribute() {

value = this.scanner.tokenType === STRING
? this.String()
: this.Identifier(DISALLOW_VAR);
: this.Identifier();

this.readSC();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/type/Class.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function Class() {
var name;

this.scanner.eat(FULLSTOP);
name = this.readIdent(false);
name = this.readIdent();

return {
type: 'Class',
Expand Down
29 changes: 23 additions & 6 deletions lib/parser/type/Declaration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var TYPE = require('../../tokenizer').TYPE;

var IDENTIFIER = TYPE.Identifier;
var COLON = TYPE.Colon;
var EXCLAMATIONMARK = TYPE.ExclamationMark;
var SOLIDUS = TYPE.Solidus;
Expand All @@ -13,17 +14,33 @@ var BALANCED = true;

function readProperty() {
var start = this.scanner.tokenStart;
var type;
var prefix = 0;

for (; type = this.scanner.tokenType; this.scanner.next()) {
if (type !== SOLIDUS &&
type !== ASTERISK &&
type !== DOLLARSIGN) {
// hacks
switch (this.scanner.tokenType) {
case ASTERISK:
case DOLLARSIGN:
prefix = 1;
break;

// TODO: not sure we should support this hack
case SOLIDUS:
prefix = this.scanner.lookupType(1) === SOLIDUS ? 2 : 1;
break;
}

if (this.scanner.lookupType(prefix) === HYPHENMINUS) {
prefix++;
if (this.scanner.lookupType(prefix) === HYPHENMINUS) {
prefix++;
}
}

this.scanIdent(true);
if (prefix) {
this.scanner.skip(prefix);
}

this.scanner.eat(IDENTIFIER);

return this.scanner.substrToCursor(start);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/type/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ var TYPE = require('../../tokenizer').TYPE;

var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var DISALLOW_VAR = false;

// ident '(' <sequence> ')'
module.exports = function Function(scope, readSequence) {
var start = this.scanner.tokenStart;
var name = this.readIdent(DISALLOW_VAR);
var name = this.readIdent();
var nameLowerCase = name.toLowerCase();
var children;

Expand Down
2 changes: 2 additions & 0 deletions lib/parser/type/Hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ module.exports = function Hash() {
this.scanner.next();

// if next token is identifier add it to result
// TODO: consume hex only
if (this.scanner.tokenType === IDENTIFIER) {
this.scanner.next();
}

break;

case IDENTIFIER:
// TODO: consume hex only
this.scanner.next(); // add token to result
break;

Expand Down
2 changes: 1 addition & 1 deletion lib/parser/type/Id.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function Id() {
var name;

this.scanner.eat(NUMBERSIGN);
name = this.readIdent(false);
name = this.readIdent();

return {
type: 'Id',
Expand Down
5 changes: 3 additions & 2 deletions lib/parser/type/Identifier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = function Identifier(varAllowed) {

module.exports = function Identifier() {
var start = this.scanner.tokenStart;
var name = this.readIdent(varAllowed);
var name = this.readIdent();

return {
type: 'Identifier',
Expand Down
5 changes: 2 additions & 3 deletions lib/parser/type/MediaFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var COLON = TYPE.Colon;
var SOLIDUS = TYPE.Solidus;
var DISALLOW_VAR = false;

module.exports = function MediaFeature() {
var start = this.scanner.tokenStart;
Expand All @@ -16,7 +15,7 @@ module.exports = function MediaFeature() {
this.scanner.eat(LEFTPARENTHESIS);
this.readSC();

name = this.readIdent(DISALLOW_VAR);
name = this.readIdent();
this.readSC();

if (this.scanner.tokenType !== RIGHTPARENTHESIS) {
Expand All @@ -36,7 +35,7 @@ module.exports = function MediaFeature() {
break;

case IDENTIFIER:
value = this.Identifier(DISALLOW_VAR);
value = this.Identifier();

break;

Expand Down
3 changes: 1 addition & 2 deletions lib/parser/type/MediaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var WHITESPACE = TYPE.Whitespace;
var COMMENT = TYPE.Comment;
var IDENTIFIER = TYPE.Identifier;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var DISALLOW_VAR = false;

module.exports = function MediaQuery() {
this.readSC();
Expand All @@ -31,7 +30,7 @@ module.exports = function MediaQuery() {
this.scanner.error('Space is expected');
}

child = this.Identifier(DISALLOW_VAR);
child = this.Identifier();
break;

case LEFTPARENTHESIS:
Expand Down
4 changes: 1 addition & 3 deletions lib/parser/type/Nth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var DISALLOW_VAR = false;

// https://drafts.csswg.org/css-syntax-3/#the-anb-type
module.exports = function Nth(allowOfClause) {
this.readSC();
Expand All @@ -10,7 +8,7 @@ module.exports = function Nth(allowOfClause) {
var query;

if (this.scanner.lookupValue(0, 'odd') || this.scanner.lookupValue(0, 'even')) {
query = this.Identifier(DISALLOW_VAR);
query = this.Identifier();
} else {
query = this.AnPlusB();
}
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/type/PseudoClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var TYPE = require('../../tokenizer').TYPE;
var COLON = TYPE.Colon;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var DISALLOW_VAR = false;
var BALANCED = true;

// : ident [ '(' .. ')' ]?
Expand All @@ -15,7 +14,7 @@ module.exports = function PseudoClass() {

this.scanner.eat(COLON);

name = this.readIdent(DISALLOW_VAR);
name = this.readIdent();

if (this.scanner.tokenType === LEFTPARENTHESIS) {
var nameLowerCase = name.toLowerCase();
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/type/PseudoElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var TYPE = require('../../tokenizer').TYPE;
var COLON = TYPE.Colon;
var LEFTPARENTHESIS = TYPE.LeftParenthesis;
var RIGHTPARENTHESIS = TYPE.RightParenthesis;
var DISALLOW_VAR = false;
var BALANCED = true;

// :: ident [ '(' .. ')' ]?
Expand All @@ -16,7 +15,7 @@ module.exports = function PseudoElement() {
this.scanner.eat(COLON);
this.scanner.eat(COLON);

name = this.readIdent(DISALLOW_VAR);
name = this.readIdent();

if (this.scanner.tokenType === LEFTPARENTHESIS) {
var nameLowerCase = name.toLowerCase();
Expand Down

0 comments on commit 633141b

Please sign in to comment.