use of stanhebben.zenscript.parser.ParseException in project CraftTweaker by CraftTweaker.
the class CrTTweaker method loadScript.
@Override
public boolean loadScript(boolean isSyntaxCommand, String loaderName) {
CraftTweakerAPI.logInfo("Loading scripts");
CRT_LOADING_STARTED_EVENT_EVENT_LIST.publish(new CrTLoadingStartedEvent(loaderName, isSyntaxCommand, networkSide));
preprocessorManager.clean();
Set<String> executed = new HashSet<>();
boolean loadSuccessful = true;
List<ScriptFile> scriptFiles = collectScriptFiles(isSyntaxCommand);
// preprocessor magic
for (ScriptFile scriptFile : scriptFiles) {
scriptFile.addAll(preprocessorManager.checkFileForPreprocessors(scriptFile));
}
scriptFiles.sort(PreprocessorManager.SCRIPT_FILE_COMPARATOR);
Map<String, byte[]> classes = new HashMap<>();
IEnvironmentGlobal environmentGlobal = GlobalRegistry.makeGlobalEnvironment(classes);
// ZS magic
long totalTime = System.currentTimeMillis();
for (ScriptFile scriptFile : scriptFiles) {
if (!scriptFile.getLoaderName().equals(loaderName) && !isSyntaxCommand) {
CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Skipping file " + scriptFile + " as we are currently loading with a different loader");
continue;
}
if (!scriptFile.shouldBeLoadedOn(networkSide)) {
CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Skipping file " + scriptFile + " as we are on the wrong side of the Network");
continue;
}
if (!executed.contains(scriptFile.getEffectiveName())) {
// long time = System.currentTimeMillis();
executed.add(scriptFile.getEffectiveName());
CraftTweakerAPI.logInfo(getTweakerDescriptor(loaderName) + ": Loading Script: " + scriptFile);
ZenParsedFile zenParsedFile = null;
String filename = scriptFile.getEffectiveName();
String className = extractClassName(filename);
CRT_LOADING_SCRIPT_PRE_EVENT_LIST.publish(new CrTLoadingScriptEventPre(filename));
Reader reader = null;
try {
reader = new InputStreamReader(new BufferedInputStream(scriptFile.open()), "UTF-8");
CrTScriptLoadEvent loadEvent = new CrTScriptLoadEvent(scriptFile);
preprocessorManager.postLoadEvent(loadEvent);
// blocks the parsing of the script
if (scriptFile.isParsingBlocked())
continue;
ZenTokener parser = new ZenTokener(reader, environmentGlobal.getEnvironment(), filename, scriptFile.areBracketErrorsIgnored());
zenParsedFile = new ZenParsedFile(filename, className, parser, environmentGlobal);
} catch (IOException ex) {
CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Could not load script " + scriptFile + ": " + ex.getMessage());
loadSuccessful = false;
} catch (ParseException ex) {
CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Error parsing " + ex.getFile().getFileName() + ":" + ex.getLine() + " -- " + ex.getExplanation());
loadSuccessful = false;
} catch (Exception ex) {
CraftTweakerAPI.logError(getTweakerDescriptor(loaderName) + ": Error loading " + scriptFile + ": " + ex.toString(), ex);
loadSuccessful = false;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
try {
// Stops if the compile is disabled
if (zenParsedFile == null || scriptFile.isCompileBlocked())
continue;
compileScripts(className, Collections.singletonList(zenParsedFile), environmentGlobal, scriptFile.isDebugEnabled() || DEBUG);
// stops if the execution is disabled
if (scriptFile.isExecutionBlocked() || isSyntaxCommand)
continue;
ZenModule module = new ZenModule(classes, CraftTweakerAPI.class.getClassLoader());
Runnable runnable = module.getMain();
if (runnable != null)
runnable.run();
} catch (Throwable ex) {
CraftTweakerAPI.logError("[" + loaderName + "]: Error executing " + scriptFile + ": " + ex.getMessage(), ex);
}
CRT_LOADING_SCRIPT_POST_EVENT_LIST.publish(new CrTLoadingScriptEventPost(filename));
// CraftTweakerAPI.logInfo("Completed file: " + filename +" in: " + (System.currentTimeMillis() - time) + "ms");
}
}
CraftTweakerAPI.logInfo("Completed script loading in: " + (System.currentTimeMillis() - totalTime) + "ms");
return loadSuccessful;
}
use of stanhebben.zenscript.parser.ParseException in project ZenScript by CraftTweaker.
the class ParsedZenClassMethod method parse.
public static ParsedZenClassMethod parse(ZenTokener parser, EnvironmentScript classEnvironment, String className) {
Token tName = parser.required(ZenTokener.T_ID, "identifier expected");
// function (argname [as type], argname [as type], ...) [as type] {
// ...contents... }
parser.required(T_BROPEN, "( expected");
List<ParsedFunctionArgument> arguments = new ArrayList<>();
if (parser.optional(T_BRCLOSE) == null) {
Token argName = parser.required(T_ID, "identifier expected");
ZenType type = ZenTypeAny.INSTANCE;
ParsedExpression expression = null;
boolean hasDefaultArgument = false;
if (parser.optional(T_AS) != null) {
type = ZenType.read(parser, classEnvironment);
}
if (parser.optional(T_ASSIGN) != null) {
expression = ParsedExpression.read(parser, classEnvironment);
if (expression instanceof ParsedExpressionVariable) {
throw new ParseException(parser.getFile(), parser.getLine(), parser.getLineOffset(), "Variables are not allowed in default arguments");
}
hasDefaultArgument = true;
}
arguments.add(new ParsedFunctionArgument(argName.getValue(), type, expression));
while (parser.optional(T_COMMA) != null) {
Token argName2 = parser.required(T_ID, "identifier expected");
ZenType type2 = ZenTypeAny.INSTANCE;
ParsedExpression expression2 = null;
if (parser.optional(T_AS) != null) {
type2 = ZenType.read(parser, classEnvironment);
}
if (parser.optional(T_ASSIGN) != null) {
expression2 = ParsedExpression.read(parser, classEnvironment);
if (expression2 instanceof ParsedExpressionVariable) {
throw new ParseException(parser.getFile(), parser.getLine(), parser.getLineOffset(), "Variables are not allowed in default arguments");
}
hasDefaultArgument = true;
} else if (hasDefaultArgument) {
throw new ParseException(parser.getFile(), parser.getLine(), parser.getLineOffset(), "Parameter " + argName2.getValue() + " requires a default argument");
}
arguments.add(new ParsedFunctionArgument(argName2.getValue(), type2, expression2));
}
parser.required(T_BRCLOSE, ") expected");
}
ZenType type = ZenTypeAny.INSTANCE;
if (parser.optional(T_AS) != null) {
type = ZenType.read(parser, classEnvironment);
}
parser.required(T_AOPEN, "{ expected");
Statement[] statements;
if (parser.optional(T_ACLOSE) != null) {
statements = new Statement[0];
} else {
ArrayList<Statement> statementsAL = new ArrayList<>();
while (parser.optional(T_ACLOSE) == null) {
statementsAL.add(Statement.read(parser, classEnvironment, type));
}
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
}
return new ParsedZenClassMethod(new ParsedFunction(tName.getPosition(), tName.getValue(), arguments, type, statements), className);
}
use of stanhebben.zenscript.parser.ParseException in project ZenScript by CraftTweaker.
the class ParsedFunction method parse.
public static ParsedFunction parse(ZenTokener parser, IEnvironmentGlobal environment) {
parser.next();
Token tName = parser.required(ZenTokener.T_ID, "identifier expected");
// function (argname [as type], argname [as type], ...) [as type] {
// ...contents... }
parser.required(T_BROPEN, "( expected");
List<ParsedFunctionArgument> arguments = new ArrayList<>();
if (parser.optional(T_BRCLOSE) == null) {
Token argName = parser.required(T_ID, "identifier expected");
ZenType type = ZenTypeAny.INSTANCE;
ParsedExpression expression = null;
boolean hasDefaultArgument = false;
if (parser.optional(T_AS) != null) {
type = ZenType.read(parser, environment);
}
if (parser.optional(T_ASSIGN) != null) {
expression = ParsedExpression.read(parser, environment);
if (expression instanceof ParsedExpressionVariable) {
throw new ParseException(parser.getFile(), parser.getLine(), parser.getLineOffset(), "Variables are not allowed in default arguments");
}
hasDefaultArgument = true;
}
arguments.add(new ParsedFunctionArgument(argName.getValue(), type, expression));
while (parser.optional(T_COMMA) != null) {
Token argName2 = parser.required(T_ID, "identifier expected");
ZenType type2 = ZenTypeAny.INSTANCE;
ParsedExpression expression2 = null;
if (parser.optional(T_AS) != null) {
type2 = ZenType.read(parser, environment);
}
if (parser.optional(T_ASSIGN) != null) {
expression2 = ParsedExpression.read(parser, environment);
if (expression2 instanceof ParsedExpressionVariable) {
throw new ParseException(parser.getFile(), parser.getLine(), parser.getLineOffset(), "Variables are not allowed in default arguments");
}
hasDefaultArgument = true;
} else if (hasDefaultArgument) {
throw new ParseException(parser.getFile(), parser.getLine(), parser.getLineOffset(), "Parameter " + argName2.getValue() + " requires a default argument");
}
arguments.add(new ParsedFunctionArgument(argName2.getValue(), type2, expression2));
}
parser.required(T_BRCLOSE, ") expected");
}
ZenType type = ZenTypeAny.INSTANCE;
if (parser.optional(T_AS) != null) {
type = ZenType.read(parser, environment);
}
parser.required(T_AOPEN, "{ expected");
Statement[] statements;
if (parser.optional(T_ACLOSE) != null) {
statements = new Statement[0];
} else {
ArrayList<Statement> statementsAL = new ArrayList<>();
while (parser.optional(T_ACLOSE) == null) {
statementsAL.add(Statement.read(parser, environment, type));
}
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
}
return new ParsedFunction(tName.getPosition(), tName.getValue(), arguments, type, statements);
}
Aggregations