Skip to content

Handle the closing brace for parsing at rule. Make enums as consts. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/shady-css/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const matcher = {
/**
* An enumeration of Node types.
*/
export enum nodeType {
export const enum nodeType {
stylesheet = 'stylesheet',
comment = 'comment',
atRule = 'atRule',
Expand Down
89 changes: 42 additions & 47 deletions src/shady-css/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import {AtRule, Comment, Declaration, Discarded, Rule, Rulelist, Ruleset, Stylesheet} from './common';
import {NodeFactory} from './node-factory';
import {Token} from './token';
import {TokenType} from './token';
import {Tokenizer} from './tokenizer';

/**
Expand Down Expand Up @@ -81,20 +81,21 @@ class Parser {
if (token === null) {
return null;
}
if (token.is(Token.type.whitespace)) {
if (token.is(TokenType.whitespace)) {
tokenizer.advance();
return null;

} else if (token.is(Token.type.comment)) {
} else if (token.is(TokenType.comment)) {
return this.parseComment(tokenizer);

} else if (token.is(Token.type.word)) {
} else if (token.is(TokenType.word) ||
token.is(TokenType.openBrace)) {
return this.parseDeclarationOrRuleset(tokenizer);

} else if (token.is(Token.type.propertyBoundary)) {
} else if (token.is(TokenType.propertyBoundary)) {
return this.parseUnknown(tokenizer);

} else if (token.is(Token.type.at)) {
} else if (token.is(TokenType.at)) {
return this.parseAtRule(tokenizer);

} else {
Expand Down Expand Up @@ -130,7 +131,7 @@ class Parser {
}

while (tokenizer.currentToken &&
tokenizer.currentToken.is(Token.type.boundary)) {
tokenizer.currentToken.is(TokenType.semicolon)) {
end = tokenizer.advance();
}

Expand All @@ -155,26 +156,28 @@ class Parser {
const start = tokenizer.currentToken.start;

while (tokenizer.currentToken) {
if (tokenizer.currentToken.is(Token.type.whitespace)) {
if (tokenizer.currentToken.is(TokenType.whitespace)) {
tokenizer.advance();
} else if (!name && tokenizer.currentToken.is(Token.type.at)) {
} else if (!name && tokenizer.currentToken.is(TokenType.at)) {
// Discard the @:
tokenizer.advance();
const start = tokenizer.currentToken;
let end;

while (tokenizer.currentToken &&
tokenizer.currentToken.is(Token.type.word)) {
tokenizer.currentToken.is(TokenType.word)) {
end = tokenizer.advance();
}
nameRange = tokenizer.getRange(start, end);
name = tokenizer.cssText.slice(nameRange.start, nameRange.end);
} else if (tokenizer.currentToken.is(Token.type.openBrace)) {
} else if (tokenizer.currentToken.is(TokenType.openBrace)) {
rulelist = this.parseRulelist(tokenizer);
break;
} else if (tokenizer.currentToken.is(Token.type.propertyBoundary)) {
} else if (tokenizer.currentToken.is(TokenType.semicolon)) {
tokenizer.advance();
break;
} else if (tokenizer.currentToken.is(TokenType.closeBrace)) {
break;
} else {
if (parametersStart == null) {
parametersStart = tokenizer.advance();
Expand Down Expand Up @@ -215,7 +218,7 @@ class Parser {
tokenizer.advance();

while (tokenizer.currentToken) {
if (tokenizer.currentToken.is(Token.type.closeBrace)) {
if (tokenizer.currentToken.is(TokenType.closeBrace)) {
endToken = tokenizer.currentToken;
tokenizer.advance();
break;
Expand All @@ -239,50 +242,40 @@ class Parser {
* @param tokenizer A Tokenizer node.
*/
parseDeclarationOrRuleset(tokenizer: Tokenizer): Declaration|Ruleset|null {
let ruleStart = null;
let ruleEnd = null;
let colon = null;
if (!tokenizer.currentToken) {
return null;
}

// This code is not obviously correct. e.g. there's what looks to be a
// null-dereference if the declaration starts with an open brace or
// property boundary.. though that may be impossible.
let ruleStart = tokenizer.currentToken;
let ruleEnd = ruleStart.previous;
let colon = null;

while (tokenizer.currentToken) {
if (tokenizer.currentToken.is(Token.type.whitespace)) {
if (tokenizer.currentToken.is(TokenType.whitespace)) {
tokenizer.advance();
} else if (tokenizer.currentToken.is(Token.type.openParenthesis)) {
} else if (tokenizer.currentToken.is(TokenType.openParenthesis)) {
// skip until close paren
while (tokenizer.currentToken &&
!tokenizer.currentToken.is(Token.type.closeParenthesis)) {
!tokenizer.currentToken.is(TokenType.closeParenthesis)) {
tokenizer.advance();
}
} else if (
tokenizer.currentToken.is(Token.type.openBrace) ||
tokenizer.currentToken.is(Token.type.propertyBoundary)) {
tokenizer.currentToken.is(TokenType.openBrace) ||
tokenizer.currentToken.is(TokenType.propertyBoundary)) {
break;
} else {
if (tokenizer.currentToken.is(Token.type.colon)) {
if (tokenizer.currentToken.is(TokenType.colon)) {
colon = tokenizer.currentToken;
}

if (ruleStart === null) {
ruleStart = tokenizer.advance();
ruleEnd = ruleStart;
} else {
ruleEnd = tokenizer.advance();
}
ruleEnd = tokenizer.advance();
}
}

if (tokenizer.currentToken === null) {
// terminated early
return null;
}

// A ruleset never contains or ends with a semi-colon.
if (tokenizer.currentToken.is(Token.type.propertyBoundary)) {
if (!tokenizer.currentToken ||
tokenizer.currentToken.is(TokenType.propertyBoundary)) {
const nameRange =
tokenizer.getRange(ruleStart!, colon ? colon.previous : ruleEnd);
tokenizer.getRange(ruleStart, colon ? colon.previous : ruleEnd);
const declarationName =
tokenizer.cssText.slice(nameRange.start, nameRange.end);

Expand All @@ -296,12 +289,13 @@ class Parser {
this.nodeFactory.expression(expressionValue, expressionRange);
}

if (tokenizer.currentToken.is(Token.type.semicolon)) {
if (tokenizer.currentToken &&
tokenizer.currentToken.is(TokenType.semicolon)) {
tokenizer.advance();
}

const range = tokenizer.trimRange(tokenizer.getRange(
ruleStart!,
ruleStart,
tokenizer.currentToken && tokenizer.currentToken.previous ||
ruleEnd));

Expand All @@ -311,33 +305,34 @@ class Parser {
} else if (colon && colon === ruleEnd) {
const rulelist = this.parseRulelist(tokenizer);

if (tokenizer.currentToken.is(Token.type.semicolon)) {
if (tokenizer.currentToken &&
tokenizer.currentToken.is(TokenType.semicolon)) {
tokenizer.advance();
}

const nameRange = tokenizer.getRange(ruleStart!, ruleEnd.previous);
const nameRange = tokenizer.getRange(ruleStart, ruleEnd.previous);
const declarationName =
tokenizer.cssText.slice(nameRange.start, nameRange.end);

const range = tokenizer.trimRange(tokenizer.getRange(
ruleStart!,
ruleStart,
tokenizer.currentToken && tokenizer.currentToken.previous ||
ruleEnd));

return this.nodeFactory.declaration(
declarationName, rulelist, nameRange, range);
// Otherwise, this is a ruleset:
} else {
const selectorRange = tokenizer.getRange(ruleStart!, ruleEnd);
const selectorRange = tokenizer.getRange(ruleStart, ruleEnd);
const selector =
tokenizer.cssText.slice(selectorRange.start, selectorRange.end);
const rulelist = this.parseRulelist(tokenizer);
const start = ruleStart!.start;
const start = ruleStart.start;
let end;
if (tokenizer.currentToken) {
end = tokenizer.currentToken.previous ?
tokenizer.currentToken.previous.end :
ruleStart!.end;
ruleStart.end;
} else {
// no current token? must have reached the end of input, so go up
// until there
Expand Down
52 changes: 25 additions & 27 deletions src/shady-css/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,38 @@
/**
* An enumeration of Token types.
*/
export enum TokenType {
export const enum TokenType {
none = 0,
whitespace = (2 ** 0),
string = (2 ** 1),
comment = (2 ** 2),
word = (2 ** 3),
boundary = (2 ** 4),
propertyBoundary = (2 ** 5),
whitespace = (1 << 0),
string = (1 << 1),
comment = (1 << 2),
word = (1 << 3),
boundary = (1 << 4),
propertyBoundary = (1 << 5),
// Special cases for boundary:
openParenthesis = (2 ** 6) | TokenType.boundary,
closeParenthesis = (2 ** 7) | TokenType.boundary,
at = (2 ** 8) | TokenType.boundary,
openBrace = (2 ** 9) | TokenType.boundary,
openParenthesis = (1 << 6) | TokenType.boundary,
closeParenthesis = (1 << 7) | TokenType.boundary,
at = (1 << 8) | TokenType.boundary,
openBrace = (1 << 9) | TokenType.boundary,
// [};] are property boundaries:
closeBrace = (2 ** 10) | TokenType.propertyBoundary | TokenType.boundary,
semicolon = (2 ** 11) | TokenType.propertyBoundary | TokenType.boundary,
closeBrace = (1 << 10) | TokenType.propertyBoundary | TokenType.boundary,
semicolon = (1 << 11) | TokenType.propertyBoundary | TokenType.boundary,
// : is a chimaeric abomination:
// foo:bar{}
// foo:bar;
colon = (2 ** 12) | TokenType.boundary | TokenType.word,
colon = (1 << 12) | TokenType.boundary | TokenType.word,

// TODO: are these two boundaries? I mean, sometimes they are I guess? Or
// maybe they shouldn't exist in the boundaryTokenTypes map.
hyphen = (2 ** 13),
underscore = (2 ** 14)
hyphen = (1 << 13),
underscore = (1 << 14)
}


/**
* Class that describes individual tokens as produced by the Tokenizer.
*/
class Token {
static type = TokenType;

readonly type: TokenType;
readonly start: number;
readonly end: number;
Expand Down Expand Up @@ -84,15 +82,15 @@ class Token {
* A mapping of boundary token text to their corresponding types.
*/
const boundaryTokenTypes: {[boundaryText: string]: TokenType | undefined} = {
'(': Token.type.openParenthesis,
')': Token.type.closeParenthesis,
':': Token.type.colon,
'@': Token.type.at,
'{': Token.type.openBrace,
'}': Token.type.closeBrace,
';': Token.type.semicolon,
'-': Token.type.hyphen,
'_': Token.type.underscore
'(': TokenType.openParenthesis,
')': TokenType.closeParenthesis,
':': TokenType.colon,
'@': TokenType.at,
'{': TokenType.openBrace,
'}': TokenType.closeBrace,
';': TokenType.semicolon,
'-': TokenType.hyphen,
'_': TokenType.underscore
};

export {Token, boundaryTokenTypes};
14 changes: 7 additions & 7 deletions src/shady-css/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

import {matcher, Range} from './common';
import {boundaryTokenTypes, Token} from './token';
import {boundaryTokenTypes, Token, TokenType} from './token';

/**
* Class that implements tokenization of significant lexical features of the
Expand All @@ -23,7 +23,7 @@ class Tokenizer {
* Tracks the position of the tokenizer in the source string.
* Also the default head of the Token linked list.
*/
private cursorToken_ = new Token(Token.type.none, 0, 0);
private cursorToken_ = new Token(TokenType.none, 0, 0);

/**
* Holds a reference to a Token that is "next" in the source string, often
Expand Down Expand Up @@ -181,7 +181,7 @@ class Tokenizer {
}
}

return new Token(Token.type.string, start, offset);
return new Token(TokenType.string, start, offset);
}

/**
Expand All @@ -200,7 +200,7 @@ class Tokenizer {
offset++;
}

return new Token(Token.type.word, start, offset);
return new Token(TokenType.word, start, offset);
}

/**
Expand All @@ -220,7 +220,7 @@ class Tokenizer {
offset = matcher.whitespaceGreedy.lastIndex;
}

return new Token(Token.type.whitespace, start, offset);
return new Token(TokenType.whitespace, start, offset);
}

/**
Expand All @@ -242,7 +242,7 @@ class Tokenizer {
offset = matcher.commentGreedy.lastIndex;
}

return new Token(Token.type.comment, start, offset);
return new Token(TokenType.comment, start, offset);
}

/**
Expand All @@ -255,7 +255,7 @@ class Tokenizer {
tokenizeBoundary(offset: number): Token {
// TODO(cdata): Evaluate if this is faster than a switch statement:
const type =
boundaryTokenTypes[this.cssText[offset]] || Token.type.boundary;
boundaryTokenTypes[this.cssText[offset]] || TokenType.boundary;

return new Token(type, offset, offset + 1);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ div {

export const minifiedRuleset = '.foo{bar:baz}div .qux{vim:fet;}';

export const minifiedRulesetWithExtraSemicolons = '.foo{bar:baz;;}div .qux{vim:fet;}';

export const psuedoRuleset = '.foo:bar:not(#rif){baz:qux}';

export const dataUriRuleset = '.foo{bar:url(qux;gib)}';
Expand Down
2 changes: 1 addition & 1 deletion src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function linkedTokens(tokens: Token[]) {
}

return r;
}, new Token(Token.type.none, 0, 0));
}, new Token(TokenType.none, 0, 0));

return tokens;
}
Expand Down
Loading