Skip to content

Commit

Permalink
parser fixes, parse error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 26, 2016
1 parent b90d061 commit d54e539
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 137 deletions.
8 changes: 4 additions & 4 deletions lib/parser/const.js
Expand Up @@ -40,10 +40,10 @@ exports.TokenType = {
DecimalNumber: 'DecimalNumber'
};

var i = 1;
for (var key in exports.TokenType) {
exports.TokenType[key] = i++;
}
// var i = 1;
// for (var key in exports.TokenType) {
// exports.TokenType[key] = i++;
// }

exports.NodeType = {
AtkeywordType: 'atkeyword',
Expand Down
157 changes: 64 additions & 93 deletions lib/parser/index.js
Expand Up @@ -116,14 +116,6 @@ function expectAny(what) {
parseError(what + ' is expected');
}

function checkEOF() {
if (pos < tokens.length) {
return;
}

parseError('Unexpected end of input');
}

function getInfo(idx) {
if (!needPositions) {
return null;
Expand Down Expand Up @@ -242,12 +234,11 @@ function getAtrule() {
node.push(getOperator());
break;

case TokenType.NumberSign:
node.push(getVhash());
case TokenType.Colon:
node.push(getPseudo());
break;

case TokenType.LeftParenthesis:
case TokenType.LeftSquareBracket:
node.push(getBraces());
break;

Expand Down Expand Up @@ -340,11 +331,19 @@ function getSimpleSelector(nested) {
node.push(getPseudo());
break;

default:
case TokenType.HyphenMinus:
case TokenType.LowLine:
case TokenType.Identifier:
case TokenType.Asterisk:
case TokenType.DecimalNumber:
node.push(
tryGetPercentage() ||
getNamespacedIdentifier(false)
);
break;

default:
parseError('Unexpected input');
}
}

Expand Down Expand Up @@ -529,51 +528,47 @@ function getAny() {
}

return number;
} else {
if (tokens[pos].type === TokenType.HyphenMinus ||
tokens[pos].type === TokenType.PlusSign) {
return getUnary();
}
}

if (tokens[pos].type === TokenType.HyphenMinus &&
pos < tokens.length &&
(tokens[pos + 1].type === TokenType.Identifier || tokens[pos + 1].type === TokenType.HyphenMinus)) {
break;
}

if (tokens[pos].type === TokenType.HyphenMinus ||
tokens[pos].type === TokenType.PlusSign) {
return getUnary();
}

parseError('Unexpected input');
break;

default:
var ident = getIdentifier();

if (pos < tokens.length && tokens[pos].type === TokenType.LeftParenthesis) {
switch (ident[2]) {
case 'url':
return getUri(startPos, ident);
case TokenType.HyphenMinus:
case TokenType.LowLine:
case TokenType.Identifier:
break;

case 'expression':
return getFunctionExpression(startPos, ident);
default:
parseError('Unexpected input');
}

default:
return getFunction(startPos, ident);
}
}
var ident = getIdentifier();

return ident;
}
}
if (pos < tokens.length && tokens[pos].type === TokenType.LeftParenthesis) {
switch (ident[2]) {
case 'url':
return getUri(startPos, ident);

function checkAttrHasOperator() {
if (pos < tokens.length) {
var type = tokens[pos].type;
case 'expression':
return getFunctionExpression(startPos, ident);

if (type === TokenType.EqualsSign || // =
type === TokenType.Tilde || // ~=
type === TokenType.CircumflexAccent || // ^=
type === TokenType.DollarSign || // $=
type === TokenType.Asterisk || // *=
type === TokenType.VerticalLine) { // |=
return true;
default:
return getFunction(startPos, ident);
}
}

return false;
return ident;
}

function getAttrib() {
Expand All @@ -587,7 +582,7 @@ function getAttrib() {

readSC(node);

if (checkAttrHasOperator()) {
if (pos < tokens.length && tokens[pos].type !== TokenType.RightSquareBracket) {
node.push(getAttrselector());
readSC(node);

Expand All @@ -606,45 +601,42 @@ function getAttrib() {
}

function getAttrselector() {
checkEOF();
expectAny('Attribute selector (=, ~=, ^=, $=, *=, |=)',
TokenType.EqualsSign, // =
TokenType.Tilde, // ~=
TokenType.CircumflexAccent, // ^=
TokenType.DollarSign, // $=
TokenType.Asterisk, // *=
TokenType.VerticalLine // |=
);

var startPos = pos;
var type = tokens[pos].type;
var name;

if (type === TokenType.EqualsSign) {
if (tokens[pos].type === TokenType.EqualsSign) {
name = '=';
pos++;
} else {
if (type === TokenType.Tilde || // ~=
type === TokenType.CircumflexAccent || // ^=
type === TokenType.DollarSign || // $=
type === TokenType.Asterisk || // *=
type === TokenType.VerticalLine) { // |=
name = tokens[pos++].value + '=';

eat(TokenType.EqualsSign);
}
}

if (!name) {
parseError('Attribute selector (=, ~=, ^=, $=, *=,|=) is expected');
name = tokens[pos].value + '=';
pos++;
eat(TokenType.EqualsSign);
}

return [getInfo(startPos), NodeType.AttrselectorType, name];
}

function getBraces() {
checkEOF();
expectAny('Parenthesis or square bracket',
TokenType.LeftParenthesis,
TokenType.LeftSquareBracket
);

var close;

if (tokens[pos].type === TokenType.LeftParenthesis) {
close = TokenType.RightParenthesis;
} else if (tokens[pos].type === TokenType.LeftSquareBracket) {
close = TokenType.RightSquareBracket;
} else {
parseError('Unexpected input');
close = TokenType.RightSquareBracket;
}

var node = [
Expand Down Expand Up @@ -700,48 +692,29 @@ function getBraces() {
}

// '.' ident
// FIXME: class names are ident and can't starts with number
function getClass() {
var startPos = pos;
var name = '';

eat(TokenType.FullStop);

if (pos < tokens.length && tokens[pos].type === TokenType.DecimalNumber) {
name = tokens[pos].value;
pos++;
}

return [
getInfo(startPos),
NodeType.ClassType,
[
getInfo(startPos + 1),
NodeType.IdentType,
name + readIdent()
]
getIdentifier()
];
}

// '#' ident
// FIXME: ids are ident and can't starts with number
// FIXME: shash node should has structure like other ident's (['shash', ['ident', ident]])
function getShash() {
var startPos = pos;

eat(TokenType.NumberSign);

var name = tokens[pos].value;

if (tokens[pos++].type === TokenType.DecimalNumber) {
if (pos < tokens.length && tokens[pos].type === TokenType.Identifier) {
name += tokens[pos++].value;
}
}

return [
getInfo(startPos),
NodeType.ShashType,
name
readIdent()
];
}

Expand Down Expand Up @@ -776,10 +749,6 @@ function getCombinator() {

// '/*' .* '*/'
function getComment() {
if (pos >= tokens.length || tokens[pos].type !== TokenType.Comment) {
parseError('Comment is expected');
}

var value = tokens[pos].value;
var len = value.length;

Expand Down Expand Up @@ -1001,7 +970,9 @@ function readIdent() {
}

function getNamespacedIdentifier(checkColon) {
checkEOF();
if (pos >= tokens.length) {
parseError('Unexpected end of input');
}

var info = getInfo(pos);
var name;
Expand Down Expand Up @@ -1195,7 +1166,7 @@ function getOperator() {
// node: Percentage
function tryGetPercentage() {
var startPos = pos;
var number = tryGetNumber(pos);
var number = tryGetNumber();

if (!number) {
return null;
Expand Down
6 changes: 0 additions & 6 deletions test/fixture/compress/issue/99.css

This file was deleted.

1 change: 0 additions & 1 deletion test/fixture/compress/issue/99.min.css

This file was deleted.

12 changes: 6 additions & 6 deletions test/fixture/internal/shash.json
@@ -1,18 +1,18 @@
{
"shash.0": {
"source": "#100",
"translate": "#100",
"source": "#id",
"translate": "#id",
"ast": {
"type": "Id",
"name": "100"
"name": "id"
}
},
"shash.1": {
"source": "#id",
"translate": "#id",
"source": "#-id",
"translate": "#-id",
"ast": {
"type": "Id",
"name": "id"
"name": "-id"
}
}
}

0 comments on commit d54e539

Please sign in to comment.