Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 8 additions & 43 deletions src/main/java/com/virtuslab/using_directives/custom/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,6 @@ public <T> T inBracesOrIndented(Supplier<T> callback) {
}
}

/**
* oneBlock: - changes COLON to COLONEOL nested nested
*
* <p>secondBlock { - stops at LBRACE nested nested }
*
* <p>thirdBlock { - skips the NEWLINE and stops at LBRACE nested nested }
*/
public void possibleTemplateStart() {
in.observeColonEOL();
if (in.td.token == Tokens.COLONEOL) {
if (in.lookahead().token == Tokens.END) {
in.td.token = Tokens.NEWLINE;
} else {
in.nextToken();
if (in.td.token != Tokens.INDENT && in.td.token != Tokens.LBRACE) {
error(String.format("Expected indent or braces but found %s", in.td.token.str));
}
}
} else {
newLineOptWhenFollowedBy(Tokens.LBRACE);
}
}

public void newLineOptWhenFollowedBy(Tokens token) {
if (in.td.token == Tokens.NEWLINE && in.next.token == token) {
in.nextToken();
Expand Down Expand Up @@ -147,7 +124,6 @@ UsingDef usingDirective() {
else if (in.td.token == Tokens.REQUIRE) syntax = UsingDirectiveSyntax.Require;

in.nextToken();
possibleTemplateStart();
return new UsingDef(settings(), syntax, source.getPositionFromOffset(offset));
}
return null;
Expand Down Expand Up @@ -214,28 +190,17 @@ String key() {
return null;
}

/**
* We enter this place after parsing a key. Now we need to decide whether we want to parse
* settings block or value. We know that settings block needs to be put inside braces or in
* indentation block. For having a settings block we should have either the COLON or LBRACE token.
* In other cases we want to accept value.
*/
SettingDefOrUsingValue valueOrSetting(int keyEnd) {
possibleTemplateStart();
if (in.td.token == Tokens.LBRACE || in.td.token == Tokens.INDENT) {
return settings();
} else {
UsingValue v = value(keyEnd);
String scope = scope();
if (scope != null) {
if (v instanceof UsingPrimitive) {
((UsingPrimitive) v).setScope(scope);
} else {
((UsingValues) v).getValues().forEach(p -> p.setScope(scope));
}
UsingValue v = value(keyEnd);
String scope = scope();
if (scope != null) {
if (v instanceof UsingPrimitive) {
((UsingPrimitive) v).setScope(scope);
} else {
((UsingValues) v).getValues().forEach(p -> p.setScope(scope));
}
return v;
}
return v;
}

UsingValue value(int keyEnd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,6 @@ public void postProcessToken() {
case END:
td.token = Tokens.IDENTIFIER;
break;
case COLON:
// observeColonEOL();
break;
case RBRACE:
case RPAREN:
case RBRACKET:
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ private void compareAST(String pathToInput, String pathToExpectedResult, String
pathToInput, expectedAST.toString(), AST.toString()));
}

// Ignored case 2, 3, 6, 15, 16, 18 since they use removed multiline syntax
@ParameterizedTest(name = "Run parser testcase no. {0}")
@ValueSource(
ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22})
@ValueSource(ints = {1, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 17, 19, 20, 21, 22})
public void testParser(int no) {
compareAST("testcase" + no + ".txt", "ast" + no + ".json", "config" + no + ".json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void testSpecialComments() {
testCode(UsingDirectiveKind.SpecialComment, 1, specialComment, plainComment);
testCode(UsingDirectiveKind.SpecialComment, 2, specialComment, specialComment2);
testCode(UsingDirectiveKind.SpecialComment, 1, multiLine1);
testCode(UsingDirectiveKind.SpecialComment, 3, multiLine2);
testCode(UsingDirectiveKind.SpecialComment, 4, multiLine1, multiLine2);
testCode(UsingDirectiveKind.SpecialComment, 1, multiLine2);
testCode(UsingDirectiveKind.SpecialComment, 2, multiLine1, multiLine2);
testCode(UsingDirectiveKind.SpecialComment, 1, keywordDirective, specialComment, plainComment);
testCode(UsingDirectiveKind.SpecialComment, 1, binaryScalaVersionNumericComment);
testCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class ParserUnitTest {

@Test
public void testNestedAndValuesOnOneLevel() {
String input = joinLines("using", " keyA \"valueA\"", " keyB: ", " keyC \"valueC\"");
String input =
joinLines("using", " keyA \"valueA\"", "using keyB.keyC ", " \"valueC\"");
UsingDirectives parsedDirective = testCode(2, input);
assertSamePaths(parsedDirective, "keyA", "keyB.keyC");
}
Expand All @@ -33,7 +34,8 @@ public void testBooleanLiteral() {
@Test
public void testFailMissingBrace() {
PersistentReporter reporter = reporterAfterParsing("using keyA {", " keyB 18", "x");
assertDiagnostic(reporter, 2, 0, "Expected closing region token but found identifier");
assertDiagnostic(
reporter, 0, 11, "Expected new line after the using directive, in the line; but found '{'");
}

@Test
Expand All @@ -45,7 +47,8 @@ public void testFailExpectingIndentOrBrace() {
@Test
public void testFailInvalidTokenOnTemplateStart() {
PersistentReporter reporter = reporterAfterParsing("using keyA:", "using keyB 42");
assertDiagnostic(reporter, 1, 0, "Expected indent or braces but found", "using");
assertDiagnostic(
reporter, 0, 10, "Expected new line after the using directive, in the line; but found :");
}

@Test
Expand All @@ -59,8 +62,8 @@ public void testStatementSeparators() {
@Test
public void testStatementSeparatorsNoIdentifier() {
String input = joinLines("using {", " keyA \"valueA\";", "}");
UsingDirectives parsedDirective = testCode(1, input);
assertValueAtPath(parsedDirective, "keyA", "valueA");
PersistentReporter reporter = reporterAfterParsing(input);
assertDiagnostic(reporter, 1, 16, "Expected token '}' but found ';'");
}

@Test
Expand All @@ -72,8 +75,9 @@ public void testFailInvalidKey() {
@Test
public void testDropBracesInIndentRegion() {
String input = joinLines("using keyA:", " keyB { ", " keyC: ", " keyD 2 }");
UsingDirectives parsedDirective = testCode(1, input);
assertValueAtPath(parsedDirective, "keyA.keyB.keyC.keyD", "2");
PersistentReporter reporter = reporterAfterParsing(input);
assertDiagnostic(
reporter, 0, 10, "Expected new line after the using directive, in the line; but found :");
}

@Test
Expand All @@ -87,13 +91,14 @@ public void testNewlineInfixOperator() {
public void testFailInvalidIndentation() {
PersistentReporter reporter = reporterAfterParsing("using keyA:", " keyB", " keyC");
assertDiagnostic(
reporter, 2, 2, "this line does not match any of the previous indentation widths");
reporter, 0, 10, "Expected new line after the using directive, in the line; but found :");
}

@Test
public void testFailIncompatibileMixedIndentation() {
PersistentReporter reporter = reporterAfterParsing("using keyA:", "\t\t keyB", " \t\tkeyC");
assertDiagnostic(reporter, 2, 3, "Incompatible combinations of tabs and spaces");
assertDiagnostic(
reporter, 0, 10, "Expected new line after the using directive, in the line; but found :");
}

@Test
Expand Down Expand Up @@ -157,8 +162,8 @@ public void testFailEmptyQuotedIdentifier() {
@Test
public void testSkipMultilineComment() {
String input = joinLines("using keyA:", " keyB 42", "\\* ", "using keyC 2137", "*\\");
UsingDirectives parsedDirective = testCode(1, input);
assertValueAtPath(parsedDirective, "keyA.keyB", "42");
assertNoValueAtPath(parsedDirective, "keyC");
PersistentReporter reporter = reporterAfterParsing(input);
assertDiagnostic(
reporter, 0, 10, "Expected new line after the using directive, in the line; but found :");
}
}