Support new JS inline function format in multi-module projects

This commit is contained in:
Alexey Andreev
2017-07-14 14:25:15 +03:00
parent 7c421b0b83
commit 445a80c755
6 changed files with 99 additions and 50 deletions
@@ -1108,6 +1108,8 @@ public class JsAstMapper {
}
private <T extends JsNode> T withLocation(T astNode, Node node) {
if (astNode == null) return null;
CodePosition location = node.getPosition();
if (location != null) {
JsLocation jsLocation = new JsLocation(fileName, location.getLine(), location.getOffset());
@@ -37,10 +37,9 @@
package com.google.gwt.dev.js.rhino;
import org.jetbrains.kotlin.js.parser.ParserEvents;
import java.io.IOException;
import java.util.Observable;
import java.util.ArrayList;
import java.util.List;
/**
* This class implements the JavaScript parser.
@@ -51,12 +50,21 @@ import java.util.Observable;
* @see TokenStream
*/
public class Parser extends Observable {
public class Parser {
private List<ParserListener> listeners;
public Parser(IRFactory nf, boolean insideFunction) {
this.nf = nf;
this.insideFunction = insideFunction;
}
public void addListener(ParserListener listener) {
if (listeners == null) {
listeners = new ArrayList<>();
}
listeners.add(listener);
}
private void mustMatchToken(TokenStream ts, int toMatch, String messageId) throws IOException, JavaScriptException {
int tt;
if ((tt = ts.getToken()) != toMatch) {
@@ -167,7 +175,11 @@ public class Parser extends Observable {
}
private Node function(TokenStream ts, boolean isExpr) throws IOException, JavaScriptException {
notifyObservers(new ParserEvents.OnFunctionParsingStart());
if (listeners != null) {
for (ParserListener listener : listeners) {
listener.functionStarted();
}
}
CodePosition basePosition = ts.tokenPosition;
Node nameNode;
@@ -247,7 +259,11 @@ public class Parser extends Observable {
wellTerminated(ts, TokenStream.FUNCTION);
}
notifyObservers(new ParserEvents.OnFunctionParsingEnd(ts));
if (listeners != null) {
for (ParserListener listener : listeners) {
listener.functionEnded(ts);
}
}
return pn;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,10 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.js.parser
package com.google.gwt.dev.js.rhino
import com.google.gwt.dev.js.rhino.*
interface ParserListener {
fun functionStarted()
object ParserEvents {
class OnFunctionParsingStart
class OnFunctionParsingEnd(val tokenStream: TokenStream)
}
fun functionEnded(tokenStream: TokenStream)
}
@@ -18,13 +18,9 @@ package org.jetbrains.kotlin.js.parser
import com.google.gwt.dev.js.JsAstMapper
import com.google.gwt.dev.js.rhino.*
import org.jetbrains.kotlin.js.backend.ast.JsFunction
import org.jetbrains.kotlin.js.backend.ast.JsFunctionScope
import org.jetbrains.kotlin.js.backend.ast.JsScope
import org.jetbrains.kotlin.js.backend.ast.JsStatement
import org.jetbrains.kotlin.js.backend.ast.*
import java.io.Reader
import java.io.StringReader
import java.util.*
fun parse(code: String, reporter: ErrorReporter, scope: JsScope, fileName: String): List<JsStatement> {
val insideFunction = scope is JsFunctionScope
@@ -34,27 +30,24 @@ fun parse(code: String, reporter: ErrorReporter, scope: JsScope, fileName: Strin
}
}
fun parseFunction(code: String, fileName: String, position: CodePosition, offset: Int, reporter: ErrorReporter, scope: JsScope): JsFunction =
parse(code, position, offset, reporter, insideFunction = false) {
addObserver(FunctionParsingObserver())
primaryExpr(it)
}.toJsAst(scope, fileName, JsAstMapper::mapFunction)
fun parseFunction(code: String, fileName: String, position: CodePosition, offset: Int, reporter: ErrorReporter, scope: JsScope): JsFunction {
val rootNode = parse(code, position, offset, reporter, insideFunction = false) {
addListener(FunctionParsingObserver())
primaryExpr(it)
}
return rootNode.toJsAst(scope, fileName, JsAstMapper::mapFunction)
}
private class FunctionParsingObserver : Observer {
private class FunctionParsingObserver : ParserListener {
var functionsStarted = 0
override fun update(o: Observable?, arg: Any?) {
when (arg) {
is ParserEvents.OnFunctionParsingStart -> {
functionsStarted++
}
is ParserEvents.OnFunctionParsingEnd -> {
functionsStarted--
override fun functionStarted() {
functionsStarted++
}
if (functionsStarted == 0) {
arg.tokenStream.ungetToken(TokenStream.EOF)
}
}
override fun functionEnded(tokenStream: TokenStream) {
if (--functionsStarted == 0) {
tokenStream.ungetToken(TokenStream.EOF)
}
}
}