Skip to content

Commit

Permalink
Add explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kota Mizushima committed Dec 9, 2016
1 parent ddab754 commit 859b371
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions README.md
Expand Up @@ -9,7 +9,7 @@ to reduce boilerplate code using lambda expression.

To use jcombinator, Add the following lines to your pom.xml

```
```xml
<dependencies>
<dependency>
<groupId>com.github.kmizu</groupId>
Expand All @@ -20,6 +20,66 @@ To use jcombinator, Add the following lines to your pom.xml
</dependencies>
```

## Usage
### Usage

See [JavaDoc](http://javadoc-badge.appsp ot.com/com.github.kmizu/jcombinator/index.html#com.github.kmizu.jcombinator.package) and [Tests](https://github.com/kmizu/jcombinator/tree/releases%2F0.0.1/src/test/java/com/github/kmizu/jcombinator)

### Example

The following code is a parser of arithmetic expression:

```java

import com.github.kmizu.jcombinator.datatype.Function2;

import static com.github.kmizu.jcombinator.Parser.*;
import static com.github.kmizu.jcombinator.Functions.*;

public class ArithmeticExpression {
private Rule<Integer> expression() {
return rule(() ->
additive().cat(eof()).map(t -> t.extract((result, __) -> result))
);
}
private Rule<Integer> additive() {
return rule(() -> {
final Parser<Function2<Integer, Integer, Integer>> Q = string("+").map(op -> (Integer lhs, Integer rhs) -> lhs + rhs);
final Parser<Function2<Integer, Integer, Integer>> R = string("-").map(op -> (Integer lhs, Integer rhs) -> lhs - rhs);
return multitive().chain(Q.or(R));
});
}
private Rule<Integer> multitive() {
return rule(() -> {
final Parser<Function2<Integer, Integer, Integer>> Q = string("*").map(op -> (Integer lhs, Integer rhs) -> lhs * rhs);
final Parser<Function2<Integer, Integer, Integer>> R = string("/").map(op -> (Integer lhs, Integer rhs) -> lhs / rhs);
return primary().chain(Q.or(R));
});
}
private final Rule<Integer> primary() {
return rule(() ->
number().or((string("(").cat(expression())).cat(string(")")).map(t -> t.item1().item2()))
);
}
private final Rule<Integer> number() {
return rule(() ->
digit().many1().map(digits -> Integer.parseInt(join(digits, "")))
);
}

See [JavaDoc](http://javadoc-badge.appspot.com/com.github.kmizu/jcombinator.svg?label=javadoc)](http://javadoc-badge.appsp ot.com/com.github.kmizu/jcombinator/index.html#com.github.kmizu.jcombinator.package) and [Tests](https://github.com/kmizu/jcombinator/tree/releases%2F0.0.1/src/test/java/com/github/kmizu/jcombinator)
public void testExpression() {
Parser<Integer> arithmetic = expression();
arithmetic.invoke("100").onSuccess(s -> {
assert ((Integer)100) == s.value();
});
arithmetic.invoke("100+200").onSuccess(s -> {
assert ((Integer)300) == s.value();
});
arithmetic.invoke("(1+2)*(3+4)").onSuccess(s -> {
assert ((Integer)21) == s.value();
});
arithmetic.invoke("1+2*3+4").onSuccess(s -> {
assert ((Integer)11) == s.value();
});
}
}
```

0 comments on commit 859b371

Please sign in to comment.