Skip to content

Linker-Parser 0.9: #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.gradle/
/build/
97 changes: 82 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

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

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

compileJava {
sourceCompatibility = '1.8'
Expand All @@ -32,22 +34,42 @@ repositories {

// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:+'
compile 'org.slf4j:slf4j-log4j12:+'
// https://mvnrepository.com/artifact/org.reflections/reflections
compile group: 'org.reflections', name: 'reflections', version: '0.9.11'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'
compile "com.onkiup:linker-parser-api:${project.version}"

// The production code uses the SLF4J logging API at compile time
compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.28'
compileOnly group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.28'
// https://mvnrepository.com/artifact/org.reflections/reflections
compileOnly group: 'org.reflections', name: 'reflections', version: '0.9.11'
compileOnly group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.0.0'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.2'

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.0.0'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.2'
}

task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}

task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}

artifacts {
archives javadocJar, sourcesJar
}

signing {
sign configurations.archives
}

publishing {
Expand All @@ -57,3 +79,48 @@ publishing {
}
}
}

uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
//authentication(userName: nexusUsername, password: nexusPassword)
}

snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
//authentication(userName: nexusUsername, password: nexusPassword)
}

pom.project {
name 'Linker-Parser'
packaging 'jar'
// optionally artifactId can be defined here
description 'Text parser that uses Java classes as grammar definitions'
url 'https://github.com/chedim/linker-parser'

scm {
connection 'scm:git:https://github.com/chedim/linker-parser.git'
developerConnection 'scm:git:[email protected]:chedim/linker-parser.git'
url 'https://github.com/chedim/linker-parser'
}

licenses {
license {
name 'MIT License'
url 'https://raw.githubusercontent.com/chedim/linker-parser/master/LICENSE'
}
}

developers {
developer {
id 'chedim'
name 'Dmitrii Chechetkin'
email '[email protected]'
}
}
}
}
}
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip
#Thu Oct 17 16:33:41 EDT 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DEFAULT_JVM_OPTS="-Xmx64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ include 'services:webservice'
*/

rootProject.name = 'linker-parser'
include 'linker-parser-api'

5 changes: 0 additions & 5 deletions src/main/java/com/onkiup/linker/parser/EnumRule.java

This file was deleted.

5 changes: 0 additions & 5 deletions src/main/java/com/onkiup/linker/parser/LinkerParser.java

This file was deleted.

20 changes: 10 additions & 10 deletions src/main/java/com/onkiup/linker/parser/NumberMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@Deprecated
public class NumberMatcher implements TokenMatcher {
private Constructor<? extends Number> pattern;
private Class<? extends Number> type;
Expand All @@ -20,24 +19,25 @@ public NumberMatcher(Class<? extends Number> type) {
@Override
public TokenTestResult apply(CharSequence buffer) {
try {
pattern.newInstance(buffer.toString());
return TestResult.matchContinue(buffer.length(), buffer.toString());
if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == ' ') {
// a fix for Number constructors that eat trailing characters
throw new InvocationTargetException(new NumberFormatException("--"));
}

return TestResult.matchContinue(buffer.length(), pattern.newInstance(buffer.toString()));
} catch (InvocationTargetException nfe) {
Throwable cause = nfe.getCause();
if (!(cause instanceof NumberFormatException)) {
return TestResult.fail();
}
if (cause.getMessage().indexOf("out of range") > -1){
if (cause.getMessage() != null && cause.getMessage().indexOf("out of range") > -1){
return TestResult.fail();
}
if (buffer.length() > 1) {
// rolling back one character
// rolling back one character (under the assumption that buffer accumulation performed on a char-by-char basis)
try {
char drop = buffer.charAt(buffer.length() - 1);
if (drop != '.') {
Number token = pattern.newInstance(buffer.subSequence(0, buffer.length()));
return TestResult.match(buffer.length() - 1, token);
}
Number token = pattern.newInstance(buffer.subSequence(0, buffer.length() - 1));
return TestResult.match(buffer.length() - 1, token);
} catch (InvocationTargetException nfe2) {
if (nfe2.getCause() instanceof NumberFormatException) {
// this is fine
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/onkiup/linker/parser/ParserContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.onkiup.linker.parser;

import java.io.Reader;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.stream.Stream;

import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

public class ParserContext<X extends Rule> implements LinkerParser<X> {

private static InheritableThreadLocal<ParserContext> INSTANCE = new InheritableThreadLocal<>();

private Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forClassLoader(TokenGrammar.class.getClassLoader()))
.setScanners(new SubTypesScanner(true))
);

private Class<? extends Extension<X>> extension;

private Map<Class, Class> extensions = new WeakHashMap<>();

private Class<X> target;

private TokenGrammar<X> grammar;

public static ParserContext<?> get() {
ParserContext instance = INSTANCE.get();
if (instance == null) {
instance = new ParserContext();
INSTANCE.set(instance);
}
return instance;
}

public <X extends Rule> Stream<Class<? extends X>> implementations(Class<X> junction) {
return subClasses(junction)
.filter(TokenGrammar::isConcrete)
.filter(TokenGrammar::isRule);
}

public <X> Stream<Class<? extends X>> subClasses(Class<X> parent) {
return reflections.getSubTypesOf(parent).stream();
}

/**
* This method can be used to limit the scope in which grammar tokens are looked up for
* @param classLoader a classloader to take classpath from
*/
public void classLoader(ClassLoader classLoader) {
reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forClassLoader(classLoader))
.setScanners(new SubTypesScanner(true))
);
}

@Override
public LinkerParser<X> target(Class<X> target) {
this.target = target;
this.grammar = TokenGrammar.forClass(target);
return this;
}

@Override
public X parse(String sourceName, Reader from) {
if (grammar == null) {
grammar = TokenGrammar.forClass(target);
}
return grammar.tokenize(sourceName, from);
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/onkiup/linker/parser/PatternMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public PatternMatcher(String pattern) {
}

public PatternMatcher(CapturePattern pattern) {
this(pattern, false);
}

public PatternMatcher(CapturePattern pattern, boolean ignoreCase) {
String matcherPattern = pattern.pattern();
if (matcherPattern.length() == 0) {
String value = pattern.value();
Expand All @@ -34,7 +38,7 @@ public PatternMatcher(CapturePattern pattern) {
}
this.replacement = pattern.replacement();
this.until = pattern.until();
this.pattern = Pattern.compile(matcherPattern);
this.pattern = Pattern.compile(matcherPattern, ignoreCase ? Pattern.CASE_INSENSITIVE : 0);
matcher = this.pattern.matcher("");
}

Expand Down
Loading