Skip to content

Commit

Permalink
introduced text object to not perform substring
Browse files Browse the repository at this point in the history
  • Loading branch information
nesh committed Dec 8, 2019
1 parent b436529 commit f4d2df8
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 20 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/description.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/project-template.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Parser.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file added out/.DS_Store
Binary file not shown.
Binary file added out/production/.DS_Store
Binary file not shown.
Binary file added out/production/Parser/.DS_Store
Binary file not shown.
Binary file added out/production/Parser/com/.DS_Store
Binary file not shown.
Binary file added out/production/Parser/com/company/Cons.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Either.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Empty.class
Binary file not shown.
Binary file added out/production/Parser/com/company/FList.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Left.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Main.class
Binary file not shown.
Binary file added out/production/Parser/com/company/ParseError.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/Parser/com/company/Parser.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Right.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Text.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Tuple.class
Binary file not shown.
Binary file added out/production/Parser/com/company/Utils.class
Binary file not shown.
9 changes: 2 additions & 7 deletions src/com/company/Cons.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ public Cons(T head, FList<T> tail){

@Override
public String toString() {
if (tail instanceof Empty){
return head.toString();
} else{
return head.toString() + ":" +tail.toString();
}

return head.toString() + ":" +tail.toString();
}
}

Expand All @@ -40,6 +35,6 @@ static <A> Empty<A> empty(){

@Override
public String toString() {
return "";
return "[]";
}
}
3 changes: 2 additions & 1 deletion src/com/company/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static void main(String[] args) {
Parser<String> p0 = _str("aac");
Parser<FList<String>> p4 = many(p0);
String s = "aacaacaacaacaacaacaacaad";
Either<ParseError,ParseResults<FList<String>>> results = test(s,p4);
Text t = new Text(s);
Either<ParseError,ParseResults<FList<String>>> results = test(t,p4);
print(results.toString());
print("Finished...");
}
Expand Down
14 changes: 7 additions & 7 deletions src/com/company/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.function.Function;
import static com.company.Tuple.*;

class ParseResults<A> extends ArrayList<Tuple<A,String>> {
public static <A> ParseResults<A> result(Tuple<A,String> t){
class ParseResults<A> extends ArrayList<Tuple<A,Text>> {
public static <A> ParseResults<A> result(Tuple<A,Text> t){
ParseResults<A> pr = new ParseResults<A>();
pr.add(t);
return pr;
Expand All @@ -18,9 +18,9 @@ public static <A> ParseResults<A> empty(){
}

class Parser<A> {
private final Function<String,ParseResults<A>> f;
private final Function<Text,ParseResults<A>> f;

public Parser(Function<String,ParseResults<A>> f){
public Parser(Function<Text,ParseResults<A>> f){
this.f =f;
}

Expand All @@ -40,10 +40,10 @@ public <B> Parser<B> bind(Function<A,Parser<B>> f){
ParseResults<A> results = parse(this).apply(s);

ParseResults<B> prb = ParseResults.empty();
for (Tuple<A,String> r : results){
for (Tuple<A,Text> r : results){
Parser<B> pb = f.apply(r.getA());
ParseResults<B> bResults = parse(pb).apply(r.getB());
for (Tuple<B,String> br : bResults){
for (Tuple<B,Text> br : bResults){
prb.add(br);
}
}
Expand All @@ -65,7 +65,7 @@ public static <B> Parser<B> pure(B b){
});
}

public static <A> Function<String,ParseResults<A>> parse(Parser<A> p){
public static <A> Function<Text,ParseResults<A>> parse(Parser<A> p){
return p.f;
}

Expand Down
37 changes: 37 additions & 0 deletions src/com/company/Text.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.company;

public class Text {
private final Character[] arr;

public Text(String str){
this.arr = new Character[str.length()];
for (int i = 0; i < str.length(); i++) {
arr[i] = str.charAt(i);
}
this.pointer = 0;
}

int pointer;

public Character head(){
return arr[pointer];
}

public Text advance(){
pointer++;
return this;
}

public boolean canAdvance(){
return (pointer) < arr.length - 1;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = pointer; i < arr.length; i++) {
sb.append(arr[i]);
}
return sb.toString();
}
}
10 changes: 5 additions & 5 deletions src/com/company/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
public class Utils {
public static Parser<Character> item(){
return new Parser<Character>(s ->{
if (s.length() > 0){
Character c = s.charAt(0);
String rest = s.substring(1); //this is slow!
if (s.canAdvance()){
Character c = s.head();
Text rest = s.advance();
return result(t(c,rest));
} else {
return ParseResults.empty();
Expand All @@ -23,7 +23,7 @@ public static Parser<Character> satisfy(Function<Character,Boolean> predicate){
return new Parser<Character>(s -> {
ParseResults<Character> r = parse(item()).apply(s);
if (r.size() > 0){
Tuple<Character,String> t = r.get(0);
Tuple<Character,Text> t = r.get(0);
if (predicate.apply(t.getA())){
return r;
} else {
Expand Down Expand Up @@ -83,7 +83,7 @@ public static <A> Parser many1(Parser<A> p){



public static <A> Either<ParseError,ParseResults<A>> test(String s, Parser<A> p){
public static <A> Either<ParseError,ParseResults<A>> test(Text s, Parser<A> p){
ParseResults<A> results = parse(p).apply(s);
if (results.size() > 0){
return new Right(results);
Expand Down

0 comments on commit f4d2df8

Please sign in to comment.