Implement Smart Pipeline proposal in @babel/parser
This commit is contained in:
committed by
James DiGioia
parent
19a1705293
commit
fbf62b4830
@@ -404,6 +404,43 @@ export default class Tokenizer extends LocationParser {
|
||||
//
|
||||
// All in the name of speed.
|
||||
//
|
||||
readToken_numberSign(code: number): void {
|
||||
if (this.state.pos === 0 && this.readToken_interpreter()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextPos = this.state.pos + 1;
|
||||
const next = this.input.charCodeAt(nextPos);
|
||||
// if (isIdentifierStart(next)) {
|
||||
if (
|
||||
(this.hasPlugin("classPrivateProperties") ||
|
||||
this.hasPlugin("classPrivateMethods")) &&
|
||||
this.state.classLevel > 0
|
||||
) {
|
||||
++this.state.pos;
|
||||
this.finishToken(tt.hash);
|
||||
return;
|
||||
} else if (
|
||||
"smart" === this.getPluginOption("pipelineOperator", "proposal")
|
||||
) {
|
||||
if (next >= charCodes.digit0 && next <= charCodes.digit9) {
|
||||
this.raise(
|
||||
this.state.pos,
|
||||
`Unexpected digit after topic reference: '#${String.fromCodePoint(
|
||||
next,
|
||||
)}'`,
|
||||
);
|
||||
} else {
|
||||
this.finishOp(tt.primaryTopicReference, 1);
|
||||
}
|
||||
} else {
|
||||
this.raise(
|
||||
this.state.pos,
|
||||
`Unexpected character '${codePointToString(code)}'`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
readToken_dot(): void {
|
||||
const next = this.input.charCodeAt(this.state.pos + 1);
|
||||
if (next >= charCodes.digit0 && next <= charCodes.digit9) {
|
||||
@@ -651,25 +688,12 @@ export default class Tokenizer extends LocationParser {
|
||||
|
||||
getTokenFromCode(code: number): void {
|
||||
switch (code) {
|
||||
case charCodes.numberSign:
|
||||
if (this.state.pos === 0 && this.readToken_interpreter()) {
|
||||
return;
|
||||
}
|
||||
// The interpretation of a number sign "#" depends on whether it is
|
||||
// followed by an identifier or not.
|
||||
|
||||
if (
|
||||
(this.hasPlugin("classPrivateProperties") ||
|
||||
this.hasPlugin("classPrivateMethods")) &&
|
||||
this.state.classLevel > 0
|
||||
) {
|
||||
++this.state.pos;
|
||||
this.finishToken(tt.hash);
|
||||
return;
|
||||
} else {
|
||||
this.raise(
|
||||
this.state.pos,
|
||||
`Unexpected character '${codePointToString(code)}'`,
|
||||
);
|
||||
}
|
||||
case charCodes.numberSign:
|
||||
this.readToken_numberSign(code);
|
||||
return;
|
||||
|
||||
// The interpretation of a dot depends on whether it is followed
|
||||
// by a digit or another two dots.
|
||||
|
||||
@@ -33,6 +33,12 @@ export default class State {
|
||||
this.hasFlowComment = false;
|
||||
this.isIterator = false;
|
||||
|
||||
// Used by smartPipelines.
|
||||
this.topicContextState = {
|
||||
maxNumOfResolvableTopics: 0,
|
||||
maxTopicIndex: undefined,
|
||||
};
|
||||
|
||||
this.classLevel = 0;
|
||||
|
||||
this.labels = [];
|
||||
@@ -111,6 +117,9 @@ export default class State {
|
||||
hasFlowComment: boolean;
|
||||
isIterator: boolean;
|
||||
|
||||
// For the smartPipelines plugin:
|
||||
topicContextState: tt.TopicContextState;
|
||||
|
||||
// Check whether we are in a (nested) class or not.
|
||||
classLevel: number;
|
||||
|
||||
|
||||
@@ -86,6 +86,7 @@ export const types: { [name: string]: TokenType } = {
|
||||
regexp: new TokenType("regexp", { startsExpr }),
|
||||
string: new TokenType("string", { startsExpr }),
|
||||
name: new TokenType("name", { startsExpr }),
|
||||
primaryTopicReference: new TokenType("#", { startsExpr }),
|
||||
eof: new TokenType("eof"),
|
||||
|
||||
// Punctuation token types.
|
||||
@@ -199,3 +200,17 @@ export const keywords = {
|
||||
Object.keys(keywords).forEach(name => {
|
||||
types["_" + name] = keywords[name];
|
||||
});
|
||||
|
||||
// A type for the smartPipelines plugin.
|
||||
export type TopicContextState = {
|
||||
// When a topic binding has been currently established,
|
||||
// then this is 1. Otherwise, it is 0. This is forwards compatible
|
||||
// with a future plugin for multiple lexical topics.
|
||||
maxNumOfResolvableTopics: number,
|
||||
|
||||
// When a topic binding has been currently established, and if that binding
|
||||
// has been used as a topic reference `#`, then this is 0. Otherwise, it is
|
||||
// `null`. This is forwards compatible with a future plugin for multiple
|
||||
// lexical topics.
|
||||
maxTopicIndex: null | 0,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user