Skip to content

Commit 9c971b5

Browse files
committed
Improve early detection of errant symbols
Checks binary operators for unexpected adjacent symbols during auto concat rewrite. Does adjacent symbol token detection in aliases too. This was limited to when inPureMScript, skipping the right side of aliases.
1 parent b0cf01a commit 9c971b5

File tree

2 files changed

+58
-109
lines changed

2 files changed

+58
-109
lines changed

src/main/java/com/laytonsmith/core/MethodScriptCompiler.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -946,25 +946,22 @@ public static TokenStream lex(String script, Environment env, File file,
946946
}
947947
}
948948

949-
// Skip this check if we're not in pure mscript.
950-
if(inPureMScript) {
951-
if(it.hasNext()) {
952-
Token next = it.next(); // Select 'next' -->.
953-
it.previous(); // Select 'next' <--.
954-
it.previous(); // Select 't' <--.
955-
if(t.type.isSymbol() && !t.type.isUnary() && !next.type.isUnary()) {
956-
if(it.hasPrevious()) {
957-
Token prev1 = it.previous(); // Select 'prev1' <--.
958-
if(prev1.type.equals(TType.FUNC_START) || prev1.type.equals(TType.COMMA)
959-
|| next.type.equals(TType.FUNC_END) || next.type.equals(TType.COMMA)
960-
|| prev1.type.isSymbol() || next.type.isSymbol()) {
961-
throw new ConfigCompileException("Unexpected symbol (" + t.val() + ")", t.getTarget());
962-
}
963-
it.next(); // Select 'prev1' -->.
949+
if(it.hasNext()) {
950+
Token next = it.next(); // Select 'next' -->.
951+
it.previous(); // Select 'next' <--.
952+
it.previous(); // Select 't' <--.
953+
if(t.type.isSymbol() && !t.type.isUnary() && !next.type.isUnary()) {
954+
if(it.hasPrevious()) {
955+
Token prev1 = it.previous(); // Select 'prev1' <--.
956+
if(prev1.type.equals(TType.FUNC_START) || prev1.type.equals(TType.COMMA)
957+
|| next.type.equals(TType.FUNC_END) || next.type.equals(TType.COMMA)
958+
|| prev1.type.isSymbol() || next.type.isSymbol()) {
959+
throw new ConfigCompileException("Unexpected symbol token (" + t.val() + ")", t.getTarget());
964960
}
961+
it.next(); // Select 'prev1' -->.
965962
}
966-
it.next(); // Select 't' -->.
967963
}
964+
it.next(); // Select 't' -->.
968965
}
969966
}
970967

src/main/java/com/laytonsmith/core/functions/Compiler.java

Lines changed: 45 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -330,136 +330,69 @@ public static ParseTree rewrite(List<ParseTree> list, boolean returnSConcat,
330330
//Exponential
331331
for(int i = 0; i < list.size() - 1; i++) {
332332
ParseTree next = list.get(i + 1);
333-
if(next.getData() instanceof CSymbol) {
334-
if(((CSymbol) next.getData()).isExponential()) {
335-
ParseTree conversion = new ParseTree(new CFunction(((CSymbol) next.getData()).convert(), next.getTarget()), next.getFileOptions());
336-
conversion.addChild(list.get(i));
337-
conversion.addChild(list.get(i + 2));
338-
list.set(i, conversion);
339-
list.remove(i + 1);
340-
list.remove(i + 1);
341-
i--;
342-
}
333+
if(next.getData() instanceof CSymbol sy && sy.isExponential()) {
334+
rewriteBinaryOperator(list, sy, i--);
343335
}
344336
}
345-
346337
//Multiplicative
347338
for(int i = 0; i < list.size() - 1; i++) {
348339
ParseTree next = list.get(i + 1);
349-
if(next.getData() instanceof CSymbol) {
350-
CSymbol nextData = (CSymbol) next.getData();
351-
if(nextData.isMultaplicative() && !nextData.isAssignment()) {
352-
ParseTree conversion = new ParseTree(new CFunction(((CSymbol) next.getData()).convert(), next.getTarget()), next.getFileOptions());
353-
conversion.addChild(list.get(i));
354-
conversion.addChild(list.get(i + 2));
355-
list.set(i, conversion);
356-
list.remove(i + 1);
357-
list.remove(i + 1);
358-
i--;
359-
}
340+
if(next.getData() instanceof CSymbol sy && sy.isMultaplicative() && !sy.isAssignment()) {
341+
rewriteBinaryOperator(list, sy, i--);
360342
}
361343
}
362344
//Additive
363345
for(int i = 0; i < list.size() - 1; i++) {
364346
ParseTree next = list.get(i + 1);
365-
if(next.getData() instanceof CSymbol && ((CSymbol) next.getData()).isAdditive() && !((CSymbol) next.getData()).isAssignment()) {
366-
ParseTree conversion = new ParseTree(new CFunction(((CSymbol) next.getData()).convert(), next.getTarget()), next.getFileOptions());
367-
conversion.addChild(list.get(i));
368-
conversion.addChild(list.get(i + 2));
369-
list.set(i, conversion);
370-
list.remove(i + 1);
371-
list.remove(i + 1);
372-
i--;
347+
if(next.getData() instanceof CSymbol sy && sy.isAdditive() && !sy.isAssignment()) {
348+
rewriteBinaryOperator(list, sy, i--);
373349
}
374350
}
375351
//relational
376352
for(int i = 0; i < list.size() - 1; i++) {
377-
ParseTree node = list.get(i + 1);
378-
if(node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isRelational()) {
379-
CSymbol sy = (CSymbol) node.getData();
380-
ParseTree conversion = new ParseTree(new CFunction(sy.convert(), node.getTarget()), node.getFileOptions());
381-
conversion.addChild(list.get(i));
382-
conversion.addChild(list.get(i + 2));
383-
list.set(i, conversion);
384-
list.remove(i + 1);
385-
list.remove(i + 1);
386-
i--;
353+
ParseTree next = list.get(i + 1);
354+
if(next.getData() instanceof CSymbol sy && sy.isRelational()) {
355+
rewriteBinaryOperator(list, sy, i--);
387356
}
388357
}
389358
//equality
390359
for(int i = 0; i < list.size() - 1; i++) {
391-
ParseTree node = list.get(i + 1);
392-
if(node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isEquality()) {
393-
CSymbol sy = (CSymbol) node.getData();
394-
ParseTree conversion = new ParseTree(new CFunction(sy.convert(), node.getTarget()), node.getFileOptions());
395-
conversion.addChild(list.get(i));
396-
conversion.addChild(list.get(i + 2));
397-
list.set(i, conversion);
398-
list.remove(i + 1);
399-
list.remove(i + 1);
400-
i--;
360+
ParseTree next = list.get(i + 1);
361+
if(next.getData() instanceof CSymbol sy && sy.isEquality()) {
362+
rewriteBinaryOperator(list, sy, i--);
401363
}
402364
}
403365
// default and
404366
for(int i = 0; i < list.size() - 1; i++) {
405-
ParseTree node = list.get(i + 1);
406-
if(node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isDefaultAnd()) {
407-
CSymbol sy = (CSymbol) node.getData();
408-
ParseTree conversion = new ParseTree(new CFunction(sy.convert(), node.getTarget()), node.getFileOptions());
409-
conversion.addChild(list.get(i));
410-
conversion.addChild(list.get(i + 2));
411-
list.set(i, conversion);
412-
list.remove(i + 1);
413-
list.remove(i + 1);
414-
i--;
367+
ParseTree next = list.get(i + 1);
368+
if(next.getData() instanceof CSymbol sy && sy.isDefaultAnd()) {
369+
rewriteBinaryOperator(list, sy, i--);
415370
}
416371
}
417-
418372
// default or
419373
for(int i = 0; i < list.size() - 1; i++) {
420-
ParseTree node = list.get(i + 1);
421-
if(node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isDefaultOr()) {
422-
CSymbol sy = (CSymbol) node.getData();
423-
ParseTree conversion = new ParseTree(new CFunction(sy.convert(), node.getTarget()), node.getFileOptions());
424-
conversion.addChild(list.get(i));
425-
conversion.addChild(list.get(i + 2));
426-
list.set(i, conversion);
427-
list.remove(i + 1);
428-
list.remove(i + 1);
429-
i--;
374+
ParseTree next = list.get(i + 1);
375+
if(next.getData() instanceof CSymbol sy && sy.isDefaultOr()) {
376+
rewriteBinaryOperator(list, sy, i--);
430377
}
431378
}
432-
433379
//logical and
434380
for(int i = 0; i < list.size() - 1; i++) {
435-
ParseTree node = list.get(i + 1);
436-
if(node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isLogicalAnd()) {
437-
CSymbol sy = (CSymbol) node.getData();
438-
ParseTree conversion = new ParseTree(new CFunction(sy.convert(), node.getTarget()), node.getFileOptions());
439-
conversion.addChild(list.get(i));
440-
conversion.addChild(list.get(i + 2));
441-
list.set(i, conversion);
442-
list.remove(i + 1);
443-
list.remove(i + 1);
444-
i--;
381+
ParseTree next = list.get(i + 1);
382+
if(next.getData() instanceof CSymbol sy && sy.isLogicalAnd()) {
383+
rewriteBinaryOperator(list, sy, i--);
445384
}
446385
}
447386
//logical or
448387
for(int i = 0; i < list.size() - 1; i++) {
449-
ParseTree node = list.get(i + 1);
450-
if(node.getData() instanceof CSymbol && ((CSymbol) node.getData()).isLogicalOr()) {
451-
CSymbol sy = (CSymbol) node.getData();
452-
ParseTree conversion = new ParseTree(new CFunction(sy.convert(), node.getTarget()), node.getFileOptions());
453-
conversion.addChild(list.get(i));
454-
conversion.addChild(list.get(i + 2));
455-
list.set(i, conversion);
456-
list.remove(i + 1);
457-
list.remove(i + 1);
458-
i--;
388+
ParseTree next = list.get(i + 1);
389+
if(next.getData() instanceof CSymbol sy && sy.isLogicalOr()) {
390+
rewriteBinaryOperator(list, sy, i--);
459391
}
460392
}
461393
} catch (IndexOutOfBoundsException e) {
462-
throw new ConfigCompileException("Unexpected symbol (" + list.get(list.size() - 1).getData().val() + "). Did you forget to quote your symbols?", list.get(list.size() - 1).getTarget());
394+
throw new ConfigCompileException("Unexpected symbol (" + list.get(list.size() - 1).getData().val() + ")",
395+
list.get(list.size() - 1).getTarget());
463396
}
464397
}
465398

@@ -630,6 +563,25 @@ public static ParseTree rewrite(List<ParseTree> list, boolean returnSConcat,
630563
}
631564
}
632565

566+
private static void rewriteBinaryOperator(List<ParseTree> list, CSymbol symbol, int leftIndex) throws ConfigCompileException {
567+
ParseTree left = list.get(leftIndex);
568+
if(left.getData() instanceof CSymbol) {
569+
throw new ConfigCompileException("Unexpected symbol (" + left.getData().val() + ") before binary operator ("
570+
+ list.get(leftIndex + 1).getData().val() + ")", left.getTarget());
571+
}
572+
ParseTree right = list.get(leftIndex + 2);
573+
if(right.getData() instanceof CSymbol) {
574+
throw new ConfigCompileException("Unexpected symbol (" + right.getData().val() + ") after binary operator ("
575+
+ list.get(leftIndex + 1).getData().val() + ")", right.getTarget());
576+
}
577+
ParseTree conversion = new ParseTree(new CFunction(symbol.convert(), symbol.getTarget()), left.getFileOptions());
578+
conversion.addChild(left);
579+
conversion.addChild(right);
580+
list.set(leftIndex, conversion);
581+
list.remove(leftIndex + 1);
582+
list.remove(leftIndex + 1);
583+
}
584+
633585
private static void rewriteParenthesis(List<ParseTree> list) throws ConfigCompileException {
634586
for(int listInd = list.size() - 1; listInd >= 1; listInd--) {
635587
Stack<ParseTree> executes = new Stack<>();

0 commit comments

Comments
 (0)