Skip to content

Commit

Permalink
Merge pull request #4 from chedim/release/0.8
Browse files Browse the repository at this point in the history
Release/0.8
  • Loading branch information
dmitriic authored Oct 3, 2019
2 parents 4833beb + e7f0c14 commit 896e6f3
Show file tree
Hide file tree
Showing 45 changed files with 2,651 additions and 1,800 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'chedim' at '7/5/19 1:04 PM' with Gradle 3.2.1
*
Expand All @@ -9,13 +9,13 @@

// Apply the java plugin to add support for Java
plugins {
id "java"
id "maven-publish"
id "eclipse"
id "java"
}

project.group = 'com.onkiup'
project.version = '0.7.1'
project.version = '0.8'

compileJava {
sourceCompatibility = '1.8'
Expand All @@ -27,6 +27,7 @@ repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenLocal()
}

// In this section you declare the dependencies for your production and test code
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/onkiup/linker/parser/EnumRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.onkiup.linker.parser;

public interface EnumRule {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class EvaluationError extends RuntimeException {

public EvaluationError(PartialToken token, Object context, Exception cause) {
super("Failed to evaluate token " + token.getTokenType(), cause);
super("Failed to evaluate token " + token.tokenType(), cause);
}
}

19 changes: 19 additions & 0 deletions src/main/java/com/onkiup/linker/parser/NullMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.onkiup.linker.parser;

public class NullMatcher implements TokenMatcher {

public NullMatcher() {

}

@Override
public TokenTestResult apply(CharSequence buffer) {
return TestResult.match(0, null);
}

@Override
public String toString() {
return "NullMatcher";
}
}

4 changes: 2 additions & 2 deletions src/main/java/com/onkiup/linker/parser/NumberMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public NumberMatcher(Class<? extends Number> type) {
}

@Override
public TokenTestResult apply(StringBuilder buffer) {
public TokenTestResult apply(CharSequence buffer) {
try {
pattern.newInstance(buffer.toString());
return TestResult.matchContinue(buffer.length(), buffer.toString());
Expand All @@ -35,7 +35,7 @@ public TokenTestResult apply(StringBuilder buffer) {
try {
char drop = buffer.charAt(buffer.length() - 1);
if (drop != '.') {
Number token = pattern.newInstance(buffer.substring(0, buffer.length() - 1));
Number token = pattern.newInstance(buffer.subSequence(0, buffer.length()));
return TestResult.match(buffer.length() - 1, token);
}
} catch (InvocationTargetException nfe2) {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/onkiup/linker/parser/ParserLocation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.onkiup.linker.parser;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -11,6 +13,21 @@ public class ParserLocation {
private final int line, column, position;
private final String name;

public static ParserLocation endOf(CharSequence text) {
int lines = 0;
int column = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '\n') {
lines++;
column = 0;
} else {
column++;
}
}

return new ParserLocation(null, text.length(), lines, column);
}

public ParserLocation(String name, int position, int line, int column) {
if (position < 0) {
throw new IllegalArgumentException("Position cannot be negative");
Expand Down Expand Up @@ -75,5 +92,28 @@ public ParserLocation advance(CharSequence source) {
logger.debug("Advanced from {} to {} using chars: '{}'", this, result, source);
return result;
}

public ParserLocation advance(char character) {
if (character < 0) {
return this;
}
int column = this.column + 1;
int line = this.line;
if (character == '\n') {
line++;
column = 0;
}
return new ParserLocation(name, position + 1, line, column);
}

public ParserLocation add(ParserLocation another) {
if (another.name() != null && !Objects.equals(name(), another.name())) {
throw new IllegalArgumentException("Unable to add parser location with a different name");
}
int anotherLines = another.line();
int resultLine = line + anotherLines;
int resultColumn = anotherLines == 0 ? column + another.column() : another.column();
return new ParserLocation(name, position + another.position(), resultLine, resultColumn);
}
}

6 changes: 3 additions & 3 deletions src/main/java/com/onkiup/linker/parser/PatternMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PatternMatcher(CapturePattern pattern) {
}

@Override
public TokenTestResult apply(StringBuilder buffer) {
public TokenTestResult apply(CharSequence buffer) {
matcher.reset(buffer);
boolean matches = matcher.matches(),
lookingAt = matcher.lookingAt(),
Expand All @@ -54,7 +54,7 @@ public TokenTestResult apply(StringBuilder buffer) {
matcher.appendReplacement(result, replacement);
return TestResult.match(matcher.end(), result.toString());
} else {
String token = buffer.substring(0, matcher.end());
String token = buffer.subSequence(0, matcher.end()).toString();
return TestResult.match(matcher.end(), token);
}
} else {
Expand All @@ -68,7 +68,7 @@ public TokenTestResult apply(StringBuilder buffer) {
} else if (lookingAt) {
return TestResult.fail();
} else {
String token = buffer.substring(0, matcher.start());
String token = buffer.subSequence(0, matcher.start()).toString();
return TestResult.match(matcher.start(), token);
}
} else {
Expand Down
35 changes: 21 additions & 14 deletions src/main/java/com/onkiup/linker/parser/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,38 @@ static void remove(Rule rule) {
}

/**
* @returns parent token or null if this token is root token
* @return parent token or null if this token is root token
*/
default <R extends Rule> Optional<R> parent() {
PartialToken meta = Metadata.metadata(this).get();
do {
meta = (PartialToken) meta.getParent().orElse(null);
} while (meta != null && !(meta instanceof RuleToken));

if (meta != null) {
return Optional.of((R) meta.getToken());
}
return Optional.empty();
return Metadata.metadata(this)
.map(meta -> {
do {
meta = (PartialToken) meta.parent().orElse(null);
} while (!(meta instanceof RuleToken));
return meta;
})
.flatMap(PartialToken::token);
}

/**
* @returns true if this token was successfully populated; false if parser is still working on some of the token's fields
* @return true if this token was successfully populated; false if parser is still working on some of the token's fields
*/
default boolean populated() {
return Metadata.metadata(this)
.map(PartialToken::isPopulated)
.orElse(false);
}

default PartialToken metadata() {
return Metadata.metadata(this).orElseThrow(() -> new RuntimeException("Failed to obtain metadata for " + Rule.this));
default void onPopulated() {

}

default Optional<PartialToken> metadata() {
return Metadata.metadata(this);
}

default ParserLocation location() {
return metadata().location();
return metadata().map(PartialToken::location).orElse(null);
}

/**
Expand All @@ -81,5 +84,9 @@ default void reevaluate() {
default void invalidate() {

}

default CharSequence source() {
return metadata().map(PartialToken::source).orElse(null);
}
}

25 changes: 14 additions & 11 deletions src/main/java/com/onkiup/linker/parser/SyntaxError.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,36 @@

public class SyntaxError extends RuntimeException {

private PartialToken lastToken;
private StringBuilder source;
private PartialToken<?> expected;
private CharSequence source;
private String message;

public SyntaxError(String message, PartialToken lastToken, StringBuilder source) {
public SyntaxError(String message, PartialToken expected, CharSequence source) {
this.message = message;
this.lastToken = lastToken;
this.expected = expected;
this.source = source;
}

@Override
public String toString() {
PartialToken expected = lastToken.expected();
StringBuilder result = new StringBuilder("Parser error:")
.append(message)
.append("\n")
.append("\tExpected ")
.append(expected)
.append(" but got: '")
.append(expected != null && source != null && expected.position() < source.length() ? source.substring(expected.position()) : source)
.append(expected != null && source != null && expected.position() < source.length() ? source.subSequence(expected.position(), source.length()) : source)
.append("'\n\tSource:\n\t\t")
.append(source)
.append("\n\n\tTraceback:\n\t\t");

PartialToken parent = expected;
while (null != (parent = (PartialToken) parent.getParent().orElse(null))) {
result.append(parent.toString().replace("\n", "\n\t\t"));
.append("\n\n\tTraceback:\n");

if (expected != null) {
expected.path().stream()
.map(PartialToken::toString)
.map(text -> text.replaceAll("\n", "\n\t\t") + '\n')
.forEach(result::append);
} else {
result.append("No traceback provided");
}

return result.toString();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/onkiup/linker/parser/TerminalMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public TerminalMatcher(String pattern) {
}

@Override
public TokenTestResult apply(StringBuilder buffer) {
public TokenTestResult apply(CharSequence buffer) {
int bufferLen = buffer.length();
int charsToCompare = Math.min(patternLen, bufferLen);
for (int i = 0; i < charsToCompare; i++) {
Expand All @@ -21,9 +21,9 @@ public TokenTestResult apply(StringBuilder buffer) {
}

if (patternLen <= bufferLen) {
return TestResult.MATCH.token(patternLen, pattern);
return TestResult.match(patternLen, pattern);
}
return TestResult.matchContinue(bufferLen, buffer.toString());
return TestResult.continueNoMatch();
}

@Override
Expand Down
Loading

0 comments on commit 896e6f3

Please sign in to comment.