Skip to content

Commit

Permalink
Update OH parser version, add translation and specific exception support
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Mar 12, 2020
1 parent 1039966 commit 8c3e79a
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/bin/
/.classpath
/.externalToolBuilders/
/.project
8 changes: 8 additions & 0 deletions .tx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[main]
host = https://www.transifex.com

[conditionalresrtictionparser.messagesproperties]
file_filter = src/main/resources/ch/poole/conditionalresrtictionparser/Messages_<lang>.properties
source_file = src/main/resources/ch/poole/conditionalresrtictionparser/Messages.properties
source_lang = en
minimum_perc = 100
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ You can either download the jar from github or add the following to your build.g

dependencies {
...
compile 'ch.poole:ConditionalRestrictionParser:0.2.3'
compile 'ch.poole:ConditionalRestrictionParser:0.3.0'
...
}
47 changes: 30 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'simon' at '02.07.16 22:45' with Gradle 2.14
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/2.14/userguide/tutorial_java_projects.html
*/

plugins {
Expand All @@ -17,16 +11,28 @@ apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: "jacoco"


version = '0.2.3'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '0.3.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
javaccOutput = file(project.buildDir.absolutePath + '/generated/javacc/ch/poole/conditionalrestrictionparser')
mainSource = file('src/main/java/ch/poole/conditionalrestrictionparser')
testSource = file('src/test/java/ch/poole/conditionalrestrictionparser')
javaccSourcePath = project.buildDir.absolutePath + '/generated/javacc/'
javaccOutput = file(javaccSourcePath)
mainSource = file('src/main/java/')
testSource = file('src/test/java/')
}

eclipse {
classpath {
file {
beforeMerged { classpath ->
classpath.entries.add(new org.gradle.plugins.ide.eclipse.model.SourceFolder(javaccSourcePath, "bin"))
}
}
}
}

compileJavacc {
Expand All @@ -39,7 +45,7 @@ compileJava {
}

compileTestJava {
source javaccOutput,mainSource, testSource
source javaccOutput,mainSource,testSource
}

javadoc {
Expand Down Expand Up @@ -67,9 +73,14 @@ task replaceVersion {
}
build.dependsOn replaceVersion

// test input may change and require rerunning the tests
test.inputs.files("test-data/cr.txt")

test {
// test input may change and require rerunning the tests
inputs.files("test-data/cr.txt")
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
}
}

publishing {
publications {
Expand Down Expand Up @@ -111,7 +122,9 @@ repositories {

// In this section you declare the dependencies for your production and test code
dependencies {
compile 'ch.poole:OpeningHoursParser:0.16.1'
compile 'ch.poole:OpeningHoursParser:0.21.1'
compileOnly 'org.jetbrains:annotations:19.0.0'
testCompileOnly 'org.jetbrains:annotations:19.0.0'
testCompile 'junit:junit:4.12'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ch.poole.conditionalrestrictionparser;

import org.jetbrains.annotations.Nullable;

import static ch.poole.conditionalrestrictionparser.I18n.tr;

/**
* Represents an exception when parsing the conditional restriction string
* @author JOSM team, Simon Legner
*/
public class ConditionalRestrictionParseException extends ParseException {

private final int line;
private final int column;
private String encountered = null;
private String expected = null;

ConditionalRestrictionParseException(String message) {
this(message, -1, -1);
}

ConditionalRestrictionParseException(String message, @Nullable Token token) {
this(message, token != null ? token.beginLine : -1, token != null ? token.beginColumn : -1);
}

private ConditionalRestrictionParseException(String message, int line, int column) {
super(message);
this.line = line;
this.column = column;
}

ConditionalRestrictionParseException(ParseException ex) {
this(null, ex.currentToken);
this.currentToken = ex.currentToken;
this.expectedTokenSequences = ex.expectedTokenSequences;
this.tokenImage = ex.tokenImage;
setEncounteredExpected();
}

/**
* Returns the line number
* @return the line number
*/
public int getLine() {
return line;
}

/**
* Returns the column number
* @return the column number
*/
public int getColumn() {
return column;
}

@Override
public String getMessage() {
final String message = encountered == null || encountered.isEmpty()
? super.getMessage()
: tr("exception_encountered", encountered);
final String string = line >= 0 && column >= 0
? tr("exception_line_column", message, line, column)
: message;
final String appendix = expected == null || expected.isEmpty()
? ""
: EOL + tr("exception_expecting", expected);
return string + appendix;
}

private void setEncounteredExpected() {
// localized ch.poole.openinghoursparser.ParseException.initialise
StringBuilder expected = new StringBuilder();
int maxSize = 0;
for (int[] expectedTokenSequence : expectedTokenSequences) {
if (maxSize < expectedTokenSequence.length) {
maxSize = expectedTokenSequence.length;
}
for (int i : expectedTokenSequence) {
expected.append(tokenImage[i]).append(' ');
}
if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) {
expected.append("...");
}
expected.append(EOL).append(" ");
}
this.expected = expected.toString().trim();

StringBuilder encountered = new StringBuilder();
Token tok = currentToken.next;
for (int i = 0; i < maxSize; i++) {
if (i != 0) encountered.append(" ");
if (tok.kind == 0) {
encountered.append(tokenImage[0]);
break;
}
encountered.append(" ").append(tokenImage[tok.kind]);
encountered.append(" \"");
encountered.append(add_escapes(tok.image));
encountered.append(" \"");
tok = tok.next;
}
this.encountered = encountered.toString();
}
}
Loading

0 comments on commit 8c3e79a

Please sign in to comment.