Last active
August 8, 2017 07:57
-
-
Save kmizu/682db6bda0b41bd81982cd641e2fd2f5 to your computer and use it in GitHub Desktop.
Javaで始めるパーザコンビネータの作り方(1) ~ Hello, World! ref: http://qiita.com/kmizu/items/ecf3f345a10c6daab495
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.kmizu.scomb.SCombinator | |
object IntegerParser extends SCombinator[Int] { | |
override def root: P[Int] = (digit.+).map{ case digits => digits.mkString.toInt } | |
lazy val digit: P[String] = set('0'to'9') | |
def main(args: Array[String]): Unit = { | |
assert(parse("100") == Success(100)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Parser<T> { | |
ParseResult<T> invoke(String input); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.*; | |
public interface ParseResult<T> { | |
public static class Success<T> implements ParseResult<T> { | |
public final T value; | |
public final String next; | |
Success(T value, String next) { | |
this.value = value; | |
this.next = next; | |
} | |
@Override | |
public <U> ParseResult<U> map(Function<T, U> fn) { | |
return new Success<U>(fn.apply(value), next); | |
} | |
} | |
public static class Failure<T> implements ParseResult<T> { | |
public final String message; | |
public final String next; | |
public Failure(String message, String next) { | |
this.message = message; | |
this.next = next; | |
} | |
@Override | |
public <U> ParseResult<U> map(Function1<T, U> fn) { | |
return (ParseResult<U>)this; | |
} | |
} | |
<U> ParseResult<U> map(Function<T, U> fn); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringParser implements Parser<String> { | |
public final String literal; | |
public StringParser(String literal) { | |
this.literal = literal; | |
} | |
@Override | |
public ParseResult<String> invoke(String input) { | |
if(input.startsWith(literal)) { | |
return new ParseResult.Success<>(literal, input.substring(literal.length())); | |
}else { | |
return new ParseResult.Failure<>("expect: " + literal, input); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Parser<T> { | |
ParseResult<T> invoke(String input); | |
static Parser<String> string(String literal) { | |
return new StringParser(literal); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
ParseResult<String> result = Parser.string("Hello, World!").invoke("Hello, World!"); | |
System.out.println((ParseResult.Success<String>)result).value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment