public static IRFactory transformTree(ProgramTree tree, StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) { IRFactory irFactory = new IRFactory(sourceString, sourceFile, config, errorReporter, tree.sourceComments); // don't call transform as we don't want standard jsdoc handling. Node n = irFactory.transformDispatcher.process(tree); irFactory.setSourceInfo(n, tree); if (tree.sourceComments != null) { for (Comment comment : tree.sourceComments) { if ((comment.type == Comment.Type.JSDOC || comment.type == Comment.Type.IMPORTANT) && !irFactory.parsedComments.contains(comment)) { irFactory.handlePossibleFileOverviewJsDoc(comment); } } } irFactory.setFileOverviewJsDoc(n); irFactory.validateAll(n); irFactory.resultNode = n; return irFactory; }
Node processForInLoop(ForInStatementTree loopNode) { Node initializer = transform(loopNode.initializer); ImmutableSet<Integer> invalidInitializers = ImmutableSet.of(Token.ARRAYLIT, Token.OBJECTLIT); if (invalidInitializers.contains(initializer.getType())) { errorReporter.error("Invalid LHS for a for-in loop", sourceName, lineno(loopNode.initializer), charno(loopNode.initializer)); } return newNode( Token.FOR, initializer, transform(loopNode.collection), transformBlock(loopNode.body)); }
Node processParameterizedType(ParameterizedTypeTree tree) { ImmutableList.Builder<TypeDeclarationNode> arguments = ImmutableList.builder(); for (ParseTree arg : tree.typeArguments) { arguments.add((TypeDeclarationNode) transform(arg)); } TypeDeclarationNode typeName = (TypeDeclarationNode) transform(tree.typeName); return cloneProps(parameterizedType(typeName, arguments.build())); }
private JSDocInfo handleJsDoc(ParseTree node) { if (!shouldAttachJSDocHere(node)) { return null; } return handleJsDoc(getJsDoc(node)); }
JSDocInfo handleJsDoc(com.google.javascript.jscomp.parsing.parser.Token token) { return handleJsDoc(getJsDoc(token)); }
JSDocInfo handleInlineJsDoc(SourceRange location) { Comment comment = getJsDoc(location); if (comment != null && !comment.value.contains("@")) { return recordJsDoc(location, parseInlineTypeDoc(comment)); } else { return handleJsDoc(comment); } }
case 'b': case 'B': { maybeWarnForFeature(token, Feature.BINARY_LITERALS); double v = 0; int c = 1; while (++c < length) { v = (v * 2) + binarydigit(value.charAt(c)); maybeWarnForFeature(token, Feature.OCTAL_LITERALS); double v = 0; int c = 1; while (++c < length) { v = (v * 8) + octaldigit(value.charAt(c)); int c = 1; while (++c < length) { v = (v * 0x10) + hexdigit(value.charAt(c)); while (++c < length) { char digit = value.charAt(c); if (isOctalDigit(digit)) { v = (v * 8) + octaldigit(digit); } else { errorReporter.error(INVALID_OCTAL_DIGIT, sourceName, lineno(location.start), charno(location.start)); return 0; if (inStrictContext()) { errorReporter.error(INVALID_ES5_STRICT_OCTAL, sourceName,
maybeWarnForFeature(functionTree, Feature.GENERATORS); maybeWarnForFeature(functionTree, Feature.MEMBER_DECLARATIONS); maybeWarnForFeature(functionTree, Feature.ARROW_FUNCTIONS); maybeWarnForFeature(functionTree, Feature.ASYNC_FUNCTIONS); maybeWarnForFeature(functionTree, Feature.ASYNC_GENERATORS); "unnamed function statement", sourceName, lineno(functionTree), charno(functionTree)); newName = newStringNode(Token.NAME, ""); setSourceInfo(newName, functionTree); Node node = newNode(Token.FUNCTION); if (isMember) { newName.setString(""); node.addChildToBack(transform(functionTree.formalParameterList)); maybeProcessType(node, functionTree.returnType); Node bodyNode = transform(functionTree.functionBody); if (!isArrow && !isSignature && !bodyNode.isBlock()) { setSourceInfo(node, functionTree);
case 'B': { features = features.require(Feature.BINARY_LITERALS); if (!isEs6Mode()) { errorReporter.warning(BINARY_NUMBER_LITERAL_WARNING, sourceName, lineno(token.location.start), charno(token.location.start)); v = (v * 2) + binarydigit(value.charAt(c)); case 'O': { features = features.require(Feature.OCTAL_LITERALS); if (!isEs6Mode()) { errorReporter.warning(OCTAL_NUMBER_LITERAL_WARNING, sourceName, lineno(token.location.start), charno(token.location.start)); v = (v * 8) + octaldigit(value.charAt(c)); int c = 1; while (++c < length) { v = (v * 0x10) + hexdigit(value.charAt(c)); if (!inStrictContext()) { double v = 0; int c = 0; while (++c < length) { char digit = value.charAt(c); if (isOctalDigit(digit)) { v = (v * 8) + octaldigit(digit);
case '\n': features = features.require(Feature.STRING_CONTINUATION); if (isEs5OrBetterMode()) { errorReporter.warning(STRING_CONTINUATION_WARNING, sourceName, lineno(token.location.start), charno(token.location.start)); } else { errorReporter.error(STRING_CONTINUATION_ERROR, sourceName, lineno(token.location.start), charno(token.location.start)); if (inStrictContext()) { if (c == '0' && !isOctalDigit(next1)) { errorReporter.warning(OCTAL_STRING_LITERAL_WARNING, sourceName, lineno(token.location.start), charno(token.location.start)); if (!isOctalDigit(next1)) { result.append((char) octaldigit(c)); } else { char next2 = value.charAt(cur + 2); if (!isOctalDigit(next2)) { result.append((char) (8 * octaldigit(c) + octaldigit(next1))); cur += 1; } else { result.append((char) (8 * 8 * octaldigit(c) + 8 * octaldigit(next1) + octaldigit(next2))); cur += 2;
maybeWarnForFeature(token, Feature.STRING_CONTINUATION); errorReporter.warning( STRING_CONTINUATION_WARNING, sourceName, lineno(token.location.start), charno(token.location.start)); break; case '0': if (cur + 1 < value.length() && isOctalDigit(value.charAt(cur + 1))) { if (cur + 2 < value.length() && isOctalDigit(value.charAt(cur + 2))) { numDigits = 3; } else { if (inStrictContext() || templateLiteral) { if (c == '0' && numDigits == 1) { errorReporter.warning(OCTAL_STRING_LITERAL_WARNING, sourceName, lineno(token.location.start), charno(token.location.start)); case 'x': result.append((char) ( hexdigit(value.charAt(cur + 1)) * 0x10 + hexdigit(value.charAt(cur + 2)))); cur += 2; break; "Undefined Unicode code-point",
private JSDocInfo handleJsDoc(Comment comment) { if (comment != null) { JsDocInfoParser jsDocParser = createJsDocInfoParser(comment); parsedComments.add(comment); if (!handlePossibleFileOverviewJsDoc(jsDocParser)) { return recordJsDoc(comment.location, jsDocParser.retrieveAndResetParsedJSDocInfo()); } } return null; }
void maybeWarnForFeature(ParseTree node, Feature feature) { features = features.with(feature); if (!isSupportedForInputLanguageMode(feature)) { errorReporter.warning( languageFeatureWarningMessage(feature), sourceName, lineno(node), charno(node)); } }
void maybeWarnTypeSyntax(ParseTree node, Feature feature) { if (config.languageMode != LanguageMode.ECMASCRIPT6_TYPED) { errorReporter.warning( "type syntax is only supported in ES6 typed mode: " + feature, sourceName, lineno(node), charno(node)); } features = features.require(feature); recordTypeSyntax(node.location); }
public static ParseResult parse( StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) { // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config" SourceFile file = new SourceFile(sourceFile.getName(), sourceString); Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, config.isIdeMode); com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = new com.google.javascript.jscomp.parsing.parser.Parser.Config(mode( config.languageMode)); Parser p = new Parser(es6config, es6ErrorReporter, file); ProgramTree tree = p.parseProgram(); Node root = null; List<Comment> comments = ImmutableList.of(); FeatureSet features = p.getFeatures(); if (tree != null && (!es6ErrorReporter.hadError() || config.isIdeMode)) { IRFactory factory = IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter); root = factory.getResultNode(); features = features.require(factory.getFeatures()); root.setIsSyntheticBlock(true); root.putProp(Node.FEATURE_SET, features); if (config.isIdeMode) { comments = p.getComments(); } } return new ParseResult(root, comments, features); }
if (param.type == ParseTreeType.TYPED_PARAMETER) { TypedParameterTree typedParam = param.asTypedParameter(); type = transform(typedParam.typeAnnotation); param = typedParam.param; "A required parameter cannot follow an optional parameter.", sourceName, lineno(param), charno(param)); good = false; "A rest parameter must be last in a parameter list.", sourceName, lineno(param), charno(param)); good = false; "A rest parameter must be of an array type.", sourceName, lineno(param), charno(param)); good = false;
private void validateRegExpFlags(LiteralExpressionTree tree, String flags) { for (char flag : Lists.charactersOf(flags)) { switch (flag) { case 'g': case 'i': case 'm': break; case 'u': case 'y': Feature feature = flag == 'u' ? Feature.REGEXP_FLAG_U : Feature.REGEXP_FLAG_Y; maybeWarnForFeature(tree, feature); break; case 's': maybeWarnForFeature(tree, Feature.REGEXP_FLAG_S); break; default: errorReporter.error( "Invalid RegExp flag '" + flag + "'", sourceName, lineno(tree), charno(tree)); } } }
void setSourceInfo( Node node, SourcePosition start, SourcePosition end) { if (node.getLineno() == -1) { // If we didn't already set the line, then set it now. This avoids // cases like ParenthesizedExpression where we just return a previous // node, but don't want the new node to get its parent's line number. int lineno = lineno(start); node.setLineno(lineno); int charno = charno(start); node.setCharno(charno); maybeSetLength(node, start, end); } }