Skip to content

Commit

Permalink
Fix import errors. Split up SimpleScript.MutableLineList#nextToken a …
Browse files Browse the repository at this point in the history
…bit so that it's not quite so huge.
  • Loading branch information
AlexIIL committed Dec 7, 2018
1 parent 1892fc0 commit 3b61696
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import buildcraft.lib.client.model.ModelHolderRegistry;
import buildcraft.lib.client.model.ModelUtil;
import buildcraft.lib.client.model.ModelUtil.TexturedFace;
import buildcraft.lib.client.model.MutableQuad;
import buildcraft.lib.client.model.ResourceLoaderContext;
import buildcraft.lib.client.reload.ReloadManager;
import buildcraft.lib.client.reload.ReloadSource;
import buildcraft.lib.client.reload.SourceType;
import buildcraft.lib.client.model.MutableQuad;
import buildcraft.lib.client.model.ResourceLoaderContext;
import buildcraft.lib.expression.FunctionContext;
import buildcraft.lib.json.JsonVariableObject;
import buildcraft.lib.misc.JsonUtil;
Expand Down
27 changes: 12 additions & 15 deletions common/buildcraft/lib/script/ScriptableRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.IOUtils;

import net.minecraft.util.JsonUtils;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -158,25 +158,22 @@ private void visitFile(List<FileSystem> openFileSystems, Map<File, Path> loadedF
private Path getRoot(List<FileSystem> openFileSystems, File file) {
final PackType sourceType = manager.getType();
Path scriptDirRoot = file.toPath();
final Path root;
if (file.isDirectory()) {
root = scriptDirRoot.resolve(sourceType.prefix);
} else {
FileSystem fileSystem = null;
try {
fileSystem = FileSystems.newFileSystem(scriptDirRoot, null);
openFileSystems.add(fileSystem);
root = fileSystem.getPath("/" + sourceType.prefix);
} catch (IOException e) {
IOUtils.closeQuietly(fileSystem);
BCLog.logger.error("Unable to load " + file + " as a separate file system!", e);
Path root = scriptDirRoot.resolve(sourceType.prefix);
return Files.exists(root) ? root : null;
}
try {
FileSystem fileSystem = FileSystems.newFileSystem(scriptDirRoot, null);
Path root = fileSystem.getPath("/" + sourceType.prefix);
if (!Files.exists(root)) {
return null;
}
}
if (!Files.exists(root)) {
openFileSystems.add(fileSystem);
return root;
} catch (IOException e) {
BCLog.logger.error("Unable to load " + file + " as a separate file system!", e);
return null;
}
return root;
}

private void loadScripts(List<FileSystem> openFileSystems, List<ScriptAction> actions, File file, Path root,
Expand Down
211 changes: 112 additions & 99 deletions common/buildcraft/lib/script/SimpleScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ public SimpleScript(ScriptableRegistry<?> registry, Path scriptDirRoot, String s
if (!token.isValid) {
continue;
}
int tokenStart = token.datas[0].original.line;
switch (token.type) {
case COMMENT:
continue;
Expand Down Expand Up @@ -287,7 +286,6 @@ public SimpleScript(ScriptableRegistry<?> registry, Path scriptDirRoot, String s
for (int i = 0; i < replacements.size(); i++) {
rdata[i] = new LineData(replacements.get(i), file, i);
}
int current = lines.lineIterator.previous().original.line;
lines.lineIterator.next();
if (!lines.replace(token.datas[0], rdata, s -> s)) {
log("Recursive import!");
Expand Down Expand Up @@ -798,124 +796,98 @@ public LineToken nextToken(boolean jumpToNextLine) {
char end = ' ';
start_search: for (int i = Math.max(0, currentIndexInLine); i < line.length(); i++) {
char c = line.charAt(i);
switch (c) {
case ' ': {
continue;
}
case '/': {
if (i + 1 == line.length()) {
return new LineToken(line.substring(i), data, TokenType.COMMENT, false, i,
line.length());
}
isComment = true;
if (!line.startsWith("/**", i)) {
// The comment extends for the rest of the line
currentIndexInLine = -1;
// Don't go to the previous() element
return new LineToken(line.substring(i), data, TokenType.COMMENT,
line.startsWith("//", i), i, line.length());
known_char: {
switch (c) {
case ' ': {
continue;
}
start = i + 3;
break start_search;
}
case '"': {
end = '"';
start = i + 1;
break start_search;
}
case '`': {
end = '`';
isMultiLine = true;
start = i + 1;
break start_search;
}
case '¬': {
boolean isLast = true;
// Check to ensure that this is the last char
for (int j = i; j < line.length(); j++) {
if (!Character.isWhitespace(line.charAt(j))) {
if (!line.startsWith("//", j)) {
isLast = false;
}
break;
case '/': {
if (i + 1 == line.length()) {
return new LineToken(line.substring(i), data, TokenType.COMMENT, false, i,
line.length());
}
isComment = true;
if (!line.startsWith("/**", i)) {
// The comment extends for the rest of the line
currentIndexInLine = -1;
// Don't go to the previous() element
return new LineToken(line.substring(i), data, TokenType.COMMENT,
line.startsWith("//", i), i, line.length());
}
start = i + 3;
break start_search;
}
if (isLast) {
foundNextLineSymbol = true;
currentIndexInLine = -1;
continue start_search_line;
case '"': {
end = '"';
start = i + 1;
break start_search;
}
// Intentionally treat it as part of a separate token
}
//$FALL-THROUGH$ (Intentional)
default: {
if (Character.isWhitespace(c)) {
continue;
case '`': {
end = '`';
isMultiLine = true;
start = i + 1;
break start_search;
}
// Must be the start of a single-word element
for (int j = i; j < line.length(); j++) {
char d = line.charAt(j);
if (Character.isWhitespace(d)) {
currentIndexInLine = j + 1;
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(i, j), data, TokenType.SEPARATE, true, i, j);
case '¬': {
boolean isLast = true;
// Check to ensure that this is the last char
for (int j = i; j < line.length(); j++) {
if (!Character.isWhitespace(line.charAt(j))) {
if (!line.startsWith("//", j)) {
isLast = false;
}
break;
}
}
if (isLast) {
foundNextLineSymbol = true;
currentIndexInLine = -1;
continue start_search_line;
}
// Intentionally treat it as part of a separate token
break known_char;
}
default: {
break known_char;
}
currentIndexInLine = line.length();
}
}
if (Character.isWhitespace(c)) {
continue;
}
// Must be the start of a single-word element
for (int j = i; j < line.length(); j++) {
char d = line.charAt(j);
if (Character.isWhitespace(d)) {
currentIndexInLine = j + 1;
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(i), data, TokenType.SEPARATE, true, i, line.length());
return new LineToken(line.substring(i, j), data, TokenType.SEPARATE, true, i, j);
}
}
currentIndexInLine = line.length();
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(i), data, TokenType.SEPARATE, true, i, line.length());
}
if (start < 0) {
currentIndexInLine = -1;
continue;
}
if (isComment) {
for (int i = start; i < line.length(); i++) {
if (line.startsWith("*/", i)) {
currentIndexInLine = i + 3;
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(start, i + 3), data, TokenType.FUNC_DOCS, true, start,
i + 3);
}
}

break start_search_line;
} else {
for (int i = start; i < line.length(); i++) {
char c = line.charAt(i);
if (c == '\\') {
i++;
continue;
}
if (c == end) {
currentIndexInLine = i + 1;
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(start, i), data, TokenType.QUOTED_STRING, true, start,
i);
}
}
if (!isMultiLine) {
currentIndexInLine = line.length();
// Ensure that the next iteration will take the line
lineIterator.previous();

// Invalid token - we found the start but not the end
// so we'll return the invalid part
return new LineToken(line.substring(start + 1), data, TokenType.BACKTICK_STRING, false,
start + 1, line.length());
}
break start_search_line;
LineToken stringToken = checkForString(isComment, start, data, line, isMultiLine, end);
if (stringToken != null) {
return stringToken;
}
break start_search_line;
} while (jumpToNextLine || foundNextLineSymbol);
if (start < 0) {
return null;
}
return handleMultiLineToken(isComment, start, data, line);
}

@Nullable
private LineToken handleMultiLineToken(boolean isComment, int start, LineData data, String line) {
// Now we have:
// - a multiline thing
// - that is either a /* \n* \n* \n*/
Expand Down Expand Up @@ -968,6 +940,47 @@ public LineToken nextToken(boolean jumpToNextLine) {
isComment ? TokenType.FUNC_DOCS : TokenType.BACKTICK_STRING, true, start, currentIndexInLine);
}

@Nullable
private LineToken checkForString(boolean isComment, int start, LineData data, String line, boolean isMultiLine,
char end) {
if (isComment) {
for (int i = start; i < line.length(); i++) {
if (line.startsWith("*/", i)) {
currentIndexInLine = i + 3;
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(start, i + 3), data, TokenType.FUNC_DOCS, true, start,
i + 3);
}
}
} else {
for (int i = start; i < line.length(); i++) {
char c = line.charAt(i);
if (c == '\\') {
i++;
continue;
}
if (c == end) {
currentIndexInLine = i + 1;
// Ensure that the next iteration will take the line
lineIterator.previous();
return new LineToken(line.substring(start, i), data, TokenType.QUOTED_STRING, true, start, i);
}
}
if (!isMultiLine) {
currentIndexInLine = line.length();
// Ensure that the next iteration will take the line
lineIterator.previous();

// Invalid token - we found the start but not the end
// so we'll return the invalid part
return new LineToken(line.substring(start + 1), data, TokenType.BACKTICK_STRING, false, start + 1,
line.length());
}
}
return null;
}

public boolean replace(LineData start, LineData[] with, Function<String, String> transform) {
SourceLine srcLine = with[0].original;
List<LineData> removed = new ArrayList<>();
Expand Down
5 changes: 5 additions & 0 deletions guidelines/buildcraft.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@
<property name="ignoreComments" value="true"/>
<property name="message" value="Standard output/error stream should not be used."/>
</module>
<module name="RegexpSinglelineJava">
<property name="format" value="import org.apache.commons.compress"/>
<property name="ignoreComments" value="true"/>
<property name="message" value="Apache commons-compress isn't included in the 1.12.2 server!"/>
</module>
</module>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package buildcraft.lib.expression.node.cast;

import buildcraft.lib.expression.NodeInliningHelper;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IDependancyVisitor;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IExpressionNode.INodeDouble;
import buildcraft.lib.expression.node.value.NodeConstantDouble;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package buildcraft.lib.expression.node.cast;

import buildcraft.lib.expression.NodeInliningHelper;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IDependancyVisitor;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IExpressionNode;
import buildcraft.lib.expression.api.IExpressionNode.INodeObject;
import buildcraft.lib.expression.node.value.NodeConstantObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

package buildcraft.lib.expression.node.condition;

import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IDependancyVisitor;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IExpressionNode.INodeBoolean;
import buildcraft.lib.expression.node.value.NodeConstantBoolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

package buildcraft.lib.expression.node.condition;

import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IDependancyVisitor;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IExpressionNode.INodeDouble;
import buildcraft.lib.expression.node.value.NodeConstantBoolean;
import buildcraft.lib.expression.node.value.NodeConstantDouble;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

package buildcraft.lib.expression.node.condition;

import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IDependancyVisitor;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IExpressionNode.INodeLong;
import buildcraft.lib.expression.node.value.NodeConstantBoolean;
import buildcraft.lib.expression.node.value.NodeConstantLong;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import buildcraft.lib.expression.ExpressionDebugManager;
import buildcraft.lib.expression.api.IConstantNode;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IDependancyVisitor;
import buildcraft.lib.expression.api.IDependantNode;
import buildcraft.lib.expression.api.IExpressionNode.INodeObject;

public class NodeConditionalObject<T> implements INodeObject<T>, IDependantNode {
Expand Down
Loading

0 comments on commit 3b61696

Please sign in to comment.