Merge pull request #8289 from valtech-nyc/implement-smart-pipeline-in-parser

Implement Smart Pipeline proposal in @babel/parser
This commit is contained in:
Sven Sauleau
2018-12-03 19:28:45 +01:00
committed by GitHub
158 changed files with 8496 additions and 86 deletions

View File

@@ -374,7 +374,36 @@ export default class Tokenizer extends LocationParser {
// into it.
//
// All in the name of speed.
//
// number sign is "#"
readToken_numberSign(): void {
if (this.state.pos === 0 && this.readToken_interpreter()) {
return;
}
const nextPos = this.state.pos + 1;
const next = this.input.charCodeAt(nextPos);
if (next >= charCodes.digit0 && next <= charCodes.digit9) {
this.raise(this.state.pos, "Unexpected digit after hash token");
}
if (
(this.hasPlugin("classPrivateProperties") ||
this.hasPlugin("classPrivateMethods")) &&
this.state.classLevel > 0
) {
++this.state.pos;
this.finishToken(tt.hash);
return;
} else if (
this.getPluginOption("pipelineOperator", "proposal") === "smart"
) {
this.finishOp(tt.hash, 1);
} else {
this.raise(this.state.pos, "Unexpected character '#'");
}
}
readToken_dot(): void {
const next = this.input.charCodeAt(this.state.pos + 1);
if (next >= charCodes.digit0 && next <= charCodes.digit9) {
@@ -623,24 +652,8 @@ export default class Tokenizer extends LocationParser {
getTokenFromCode(code: number): void {
switch (code) {
case charCodes.numberSign:
if (this.state.pos === 0 && this.readToken_interpreter()) {
return;
}
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 '${String.fromCodePoint(code)}'`,
);
}
this.readToken_numberSign();
return;
// The interpretation of a dot depends on whether it is followed
// by a digit or another two dots.

View File

@@ -6,7 +6,7 @@ import { Position } from "../util/location";
import { types as ct, type TokContext } from "./context";
import type { Token } from "./index";
import { types as tt, type TokenType } from "./types";
import { types as tt, type TokenType, type TopicContextState } from "./types";
export default class State {
init(options: Options, input: string): void {
@@ -26,6 +26,7 @@ export default class State {
this.maybeInArrowParameters = false;
this.inGenerator = false;
this.inAsync = false;
this.inPipeline = false;
this.inPropertyName = false;
this.inType = false;
this.inClassProperty = false;
@@ -33,6 +34,12 @@ export default class State {
this.hasFlowComment = false;
this.isIterator = false;
// Used by smartPipelines.
this.topicContext = {
maxNumOfResolvableTopics: 0,
maxTopicIndex: null,
};
this.classLevel = 0;
this.labels = [];
@@ -104,6 +111,7 @@ export default class State {
inGenerator: boolean;
inMethod: boolean | N.MethodKind;
inAsync: boolean;
inPipeline: boolean;
inType: boolean;
noAnonFunctionType: boolean;
inPropertyName: boolean;
@@ -111,6 +119,9 @@ export default class State {
hasFlowComment: boolean;
isIterator: boolean;
// For the smartPipelines plugin:
topicContext: TopicContextState;
// Check whether we are in a (nested) class or not.
classLevel: number;

View File

@@ -193,3 +193,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,
};