Skip to content

Commit

Permalink
string not closed error
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Sep 27, 2023
1 parent 891ace9 commit 86dfcf9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/main/java/leekscript/common/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,5 @@ public enum Error {
UNARY_OPERATOR_INCOMPATIBLE_TYPE, // 141
INTERVAL_INFINITE_CLOSED, // 142
DOT_DOT_EXPECTED, // 143
STRING_NOT_CLOSED, // 144
}
15 changes: 11 additions & 4 deletions src/main/java/leekscript/compiler/LexicalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public LexicalParserTokenStream parse(ErrorReporter error) throws LeekCompilerEx
for (stream = new CharStream(aiFile.getCode()); stream.hasMore();) {

if (tryParseWhiteSpaces()) continue;
if (tryParseString()) continue;
if (tryParseString(error)) continue;
if (tryParseComments()) continue;
if (tryParseNumber(error)) continue;
if (tryParseSpecialIdentifier()) continue;
Expand Down Expand Up @@ -207,7 +207,7 @@ private boolean tryParseNumber(ErrorReporter error) throws LeekCompilerException
return true;
}

private boolean tryParseString() {
private boolean tryParseString(ErrorReporter error) throws LeekCompilerException {

var openQuote = stream.peek();
if (openQuote != '"' && openQuote != '\'') {
Expand All @@ -219,20 +219,27 @@ private boolean tryParseString() {
stream.next();

var escaped = false;
var closed = false;
for (char c = stream.peek(); stream.hasMore(); c = stream.next()) {
if (c == '\\') {
escaped = !escaped;
continue;
}
if (c == openQuote && !escaped) {
closed = true;
stream.next();
break;
}
escaped = false;
}

addToken(stream.getSubStringSince(startingPoint), TokenType.VAR_STRING);
return true;
if (closed) {
addToken(stream.getSubStringSince(startingPoint), TokenType.VAR_STRING);
return true;
} else {
error.report(new AnalyzeError(new Location(aiFile, stream.getLineCounter(), stream.getCharCounter()), AnalyzeErrorLevel.ERROR, Error.STRING_NOT_CLOSED));
return false;
}
}

private boolean tryParseWhiteSpaces() {
Expand Down
64 changes: 32 additions & 32 deletions src/main/java/leekscript/compiler/WordCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,46 +224,46 @@ public void secondPass() throws LeekCompilerException {
mTokens = this.mAI.getTokenStream();
assert mTokens != null : "tokens are null";
mTokens.reset();
try {
// Vraie compilation
while (mTokens.hasMoreTokens()) {

if (isInterrupted()) throw new LeekCompilerException(mTokens.get(), Error.AI_TIMEOUT);
// Vraie compilation
while (mTokens.hasMoreTokens()) {

// On vérifie les instructions en cours
if (mCurentBlock instanceof DoWhileBlock && !((DoWhileBlock) mCurentBlock).hasAccolade() && mCurentBlock.isFull()) {
DoWhileBlock do_block = (DoWhileBlock) mCurentBlock;
mCurentBlock = mCurentBlock.endInstruction();
dowhileendBlock(do_block);
mTokens.skip();
} else mCurentBlock = mCurentBlock.endInstruction();
if (!mTokens.hasMoreTokens()) break;
if (isInterrupted()) throw new LeekCompilerException(mTokens.get(), Error.AI_TIMEOUT);

// Puis on lit l'instruction
compileWord();
}
while (mCurentBlock.getParent() != null && !mCurentBlock.hasAccolade()) {
// On vérifie les instructions en cours
if (mCurentBlock instanceof DoWhileBlock && !((DoWhileBlock) mCurentBlock).hasAccolade() && mCurentBlock.isFull()) {
DoWhileBlock do_block = (DoWhileBlock) mCurentBlock;
mCurentBlock = mCurentBlock.endInstruction();
dowhileendBlock(do_block);
mTokens.skip();
} else mCurentBlock = mCurentBlock.endInstruction();
if (!mTokens.hasMoreTokens()) break;

if (isInterrupted()) throw new LeekCompilerException(mTokens.get(), Error.AI_TIMEOUT);
// Puis on lit l'instruction
compileWord();
}
while (mCurentBlock.getParent() != null && !mCurentBlock.hasAccolade()) {

if (mCurentBlock instanceof DoWhileBlock) {
DoWhileBlock do_block = (DoWhileBlock) mCurentBlock;
mCurentBlock = mCurentBlock.endInstruction();
dowhileendBlock(do_block);
mTokens.skip();
} else {
if (mCurentBlock.endInstruction() == mCurentBlock) {
throw new LeekCompilerException(mTokens.get(), Error.NO_BLOC_TO_CLOSE);
}
mCurentBlock = mCurentBlock.endInstruction();
if (isInterrupted()) throw new LeekCompilerException(mTokens.get(), Error.AI_TIMEOUT);

if (mCurentBlock instanceof DoWhileBlock) {
DoWhileBlock do_block = (DoWhileBlock) mCurentBlock;
mCurentBlock = mCurentBlock.endInstruction();
dowhileendBlock(do_block);
mTokens.skip();
} else {
if (mCurentBlock.endInstruction() == mCurentBlock) {
throw new LeekCompilerException(mTokens.get(), Error.NO_BLOC_TO_CLOSE);
}
mCurentBlock = mCurentBlock.endInstruction();
}
if (!mMain.equals(mCurentBlock)) throw new LeekCompilerException(mTokens.get(), Error.OPEN_BLOC_REMAINING);

} catch (IndexOutOfBoundsException e) {
e.printStackTrace(System.out);
addError(new AnalyzeError(mTokens.get(), AnalyzeErrorLevel.ERROR, Error.END_OF_SCRIPT_UNEXPECTED));
}
if (!mMain.equals(mCurentBlock)) throw new LeekCompilerException(mTokens.get(), Error.OPEN_BLOC_REMAINING);

// } catch (IndexOutOfBoundsException e) {
// e.printStackTrace(System.out);
// addError(new AnalyzeError(mTokens.get(), AnalyzeErrorLevel.ERROR, Error.END_OF_SCRIPT_UNEXPECTED));
// }
}

public void analyze() throws LeekCompilerException {
Expand Down

0 comments on commit 86dfcf9

Please sign in to comment.