Skip to content

Commit

Permalink
GH-526 Support string method names (Resolve #526)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Jul 5, 2020
1 parent a45fcef commit 061201c
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 25 deletions.
7 changes: 6 additions & 1 deletion examples/tests/current_test.panda
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ main {
// hexagonal number
log 0x5A, 0x5F // expected: 90, 95
// int overflow
log Int.MAX_VALUE + 1
log Int.MAX_VALUE + 10_000_001

// assign integer in hex format to the i variable
Int i = 0x000001
Expand Down Expand Up @@ -251,6 +251,11 @@ internal class Test {
log message3
}

// method with string based name
shared Bool 'should return true' () {
return true
}

// get test instance
shared Test getTestField() {
if true {
Expand Down
13 changes: 13 additions & 0 deletions examples/tests/literal_methods.panda
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module tests

main {
log LiteralMethod.'should return true'()
}

type LiteralMethod {

shared static Bool 'should return true' () {
return true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

public interface Buildable<T> {

CustomPatternElement build();
CustomPatternElement<?> build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

public final class CustomPattern {

private final List<? extends CustomPatternElement> elements;
private final List<? extends CustomPatternElement<?>> elements;

private CustomPattern(List<? extends CustomPatternElement> elements) {
private CustomPattern(List<? extends CustomPatternElement<?>> elements) {
this.elements = elements;
}

Expand All @@ -50,7 +50,7 @@ public Result match(SourceStream source, @Nullable CustomPatternData data) {
return matcher.match(source);
}

protected List<? extends CustomPatternElement> getElements() {
protected List<? extends CustomPatternElement<?>> getElements() {
return elements;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected CustomPatternElementBuilder(String id) {
}

@Override
public CustomPatternElement build() {
return new CustomPatternElement(this);
public CustomPatternElement<T> build() {
return new CustomPatternElement<>(this);
}

public B optional() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@

public final class NextTokenTypeVerifier implements CustomVerify<Snippetable> {

private final TokenType type;
private final TokenType[] types;

public NextTokenTypeVerifier(TokenType type) {
this.type = type;
public NextTokenTypeVerifier(TokenType... types) {
this.types = types;
}

@Override
public boolean verify(Map<String, Object> results, SynchronizedSource source, Snippetable content) {
return source.hasNext() && source.getNext().getType() == type;
if (!source.hasNext()) {
return false;
}

TokenType next = source.getNext().getType();

for (TokenType type : types) {
if (type == next) {
return true;
}
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@

public final class TokenTypeVerifier implements CustomVerify<TokenInfo> {

private final TokenType type;
private final TokenType[] types;

public TokenTypeVerifier(TokenType type) {
this.type = type;
public TokenTypeVerifier(TokenType... types) {
this.types = types;
}

@Override
public boolean verify(Map<String, Object> results, SynchronizedSource source, TokenInfo content) {
return content.getType() == type;
for (TokenType type : types) {
if (content.getType() == type) {
return true;
}
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ void method() {
CustomPattern customPattern = CustomPattern.of(
VariantElement.create("visibility").content("public", "shared", "internal"),
UnitElement.create("isStatic").content("static").optional(),
TypeElement.create("type").optional().verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN)),
WildcardElement.create("name").verify(new TokenTypeVerifier(TokenTypes.UNKNOWN)),
TypeElement.create("type").optional().verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN, TokenTypes.SEQUENCE)),
WildcardElement.create("name").verify(new TokenTypeVerifier(TokenTypes.UNKNOWN, TokenTypes.SEQUENCE)),
SectionElement.create("parameters"),
SectionElement.create("body")
);

Snippet source = convert("shared static String[] of(String a, Int[] b) { /* content */ } another content");
Snippet source = convert("shared static String[] 'of'(String a, Int[] b) { /* content */ } another content");
Result result = customPattern.match(source);

Assertions.assertTrue(result.isMatched());
Assertions.assertEquals(convert("shared static String[] of(String a, Int[] b) { /* content */ }"), result.getSource());
Assertions.assertEquals(convert("shared static String[] 'of'(String a, Int[] b) { /* content */ }"), result.getSource());

Assertions.assertAll(
() -> Assertions.assertEquals("shared", result.get("visibility").toString()),
Expand All @@ -79,7 +79,7 @@ void field() {
KeywordElement.create(Keywords.STATIC).optional(),
KeywordElement.create(Keywords.MUT).optional(),
KeywordElement.create(Keywords.NIL).optional(),
TypeElement.create("type").optional().verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN)),
TypeElement.create("type").verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN)),
WildcardElement.create("name").verify(new TokenTypeVerifier(TokenTypes.UNKNOWN)),
SubPatternElement.create("assign").optional().of(
UnitElement.create("operator").content("="),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private static final class MethodWorker extends AbstractExpressionSubparserWorke
public @Nullable ExpressionResult next(ExpressionContext context, TokenInfo nameToken) {
SynchronizedSource source = context.getSynchronizedSource();

// name has to be declared by unknown type of token
if (nameToken.getType() != TokenTypes.UNKNOWN || !source.hasNext()) {
// name has to be declared by unknown or sequence token
if ((nameToken.getType() != TokenTypes.UNKNOWN) && (nameToken.getType() != TokenTypes.SEQUENCE) || !source.hasNext()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected BootstrapInitializer<Void> initialize(Context context, BootstrapInitia
KeywordElement.create(Keywords.STATIC).optional(),
KeywordElement.create(Keywords.MUT).optional(),
KeywordElement.create(Keywords.NIL).optional(),
TypeElement.create("type").optional().verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN)),
TypeElement.create("type").verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN)),
WildcardElement.create("name").verify(new TokenTypeVerifier(TokenTypes.UNKNOWN)),
SubPatternElement.create("assign").optional().of(
UnitElement.create("operator").content("="),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ protected BootstrapInitializer<Void> initialize(Context context, BootstrapInitia
KeywordElement.create(Keywords.OVERRIDE).optional(),
VariantElement.create("visibility").optional().content("public", "shared", "internal").map(value -> Visibility.valueOf(value.toString().toUpperCase())),
UnitElement.create("static").content("static").optional(),
TypeElement.create("type").optional().verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN)),
WildcardElement.create("name").verify(new TokenTypeVerifier(TokenTypes.UNKNOWN)),
TypeElement.create("type").optional().verify(new NextTokenTypeVerifier(TokenTypes.UNKNOWN, TokenTypes.SEQUENCE)),
WildcardElement.create("name").verify(new TokenTypeVerifier(TokenTypes.UNKNOWN, TokenTypes.SEQUENCE)),
SectionElement.create("parameters"),
SectionElement.create("body").optional()
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ void helloWorld() {
ExamplesLauncher.launch("", "hello_world.panda");
}

@Test
void literalMethod() {
ExamplesLauncher.launch("tests", "literal_methods.panda");
}

@Test
void testCurrentTest() {
for (int i = 0; i < 1; i++) {
Expand Down

0 comments on commit 061201c

Please sign in to comment.