Skip to content
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

Parser.withSource() parser #16

Merged
merged 1 commit into from
Mar 20, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/org/codehaus/jparsec/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ public final Parser<String> source() {
return new ReturnSourceParser(this);
}

/**
* A {@link Parser} that returns both parsed object and matched string.
*/
public final Parser<WithSource<T>> withSource() {
return new WithSourceParser<T>(this);
}

/**
* A {@link Parser} that takes as input the {@link Token} collection returned by {@code lexer}, and runs {@code
* this} to parse the tokens.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/codehaus/jparsec/ReturnSourceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Returns the source string of the part that matches the target parser.
*
* @author Ben Yu
*
* @see org.codehaus.jparsec.WithSourceParser
*/
final class ReturnSourceParser extends Parser<String> {
private final Parser<?> parser;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/codehaus/jparsec/ToTokenParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Converts the current return value as a {@link Token} with starting index and length.
*
* @author Ben Yu
*
* @see org.codehaus.jparsec.WithSourceParser
*/
final class ToTokenParser extends Parser<Token> {
private final Parser<?> parser;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/codehaus/jparsec/WithSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.codehaus.jparsec;

/**
* @author Stepan Koltsov
*/
public class WithSource<T> {
private final T value;
private final String source;

public WithSource(T value, String source) {
this.value = value;
this.source = source;
}

public T getValue() {
return value;
}

public String getSource() {
return source;
}

/** Returns the string representation of the token value. */
@Override public String toString() {
return String.valueOf(value);
}

@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

WithSource that = (WithSource) o;

if (value != null ? !value.equals(that.value) : that.value != null) return false;
if (source != null ? !source.equals(that.source) : that.source != null) return false;

return true;
}

@Override public int hashCode() {
int result = value != null ? value.hashCode() : 0;
result = 31 * result + (source != null ? source.hashCode() : 0);
return result;
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/codehaus/jparsec/WithSourceParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.codehaus.jparsec;

/**
* Converts the current return value as a {@link org.codehaus.jparsec.WithSource}
* with string that matches it.
*
* @author Stepan Koltsov
*
* @see org.codehaus.jparsec.ReturnSourceParser
* @see org.codehaus.jparsec.ToTokenParser
*/
final class WithSourceParser<T> extends Parser<WithSource<T>> {
private final Parser<?> parser;

WithSourceParser(Parser<?> parser) {
this.parser = parser;
}

@Override boolean apply(ParseContext ctxt) {
int begin = ctxt.getIndex();
if (!parser.apply(ctxt)) {
return false;
}
String source = ctxt.source.subSequence(begin, ctxt.getIndex()).toString();
WithSource<T> withSource = new WithSource<T>((T) ctxt.result, source);
ctxt.result = withSource;
return true;
}

@Override public String toString() {
return parser.toString();
}
}
7 changes: 7 additions & 0 deletions src/test/java/org/codehaus/jparsec/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public void testToken() {
assertParser(INTEGER.token(), "123", new Token(0, 3, 123));
assertFailure(INTEGER.token(), "a", 1, 1);
}

public void testWithSource() {
assertEquals("foo", FOO.withSource().toString());
assertParser(FOO.withSource(), "", new WithSource<String>("foo", ""));
assertParser(INTEGER.withSource(), "123", new WithSource<Integer>(123, "123"));
assertFailure(INTEGER.withSource(), "a", 1, 1);
}

@Mock Map<Object, Parser<String>> next;
public void testNext_withMap() {
Expand Down